1 /* 2 BlueZ - Bluetooth protocol stack for Linux 3 Copyright (c) 2000-2001, 2010, Code Aurora Forum. All rights reserved. 4 5 Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com> 6 7 This program is free software; you can redistribute it and/or modify 8 it under the terms of the GNU General Public License version 2 as 9 published by the Free Software Foundation; 10 11 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 12 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 13 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. 14 IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY 15 CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES 16 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 17 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 18 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 19 20 ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, 21 COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS 22 SOFTWARE IS DISCLAIMED. 23 */ 24 25 /* Bluetooth HCI connection handling. */ 26 27 #include <linux/export.h> 28 #include <linux/debugfs.h> 29 30 #include <net/bluetooth/bluetooth.h> 31 #include <net/bluetooth/hci_core.h> 32 #include <net/bluetooth/l2cap.h> 33 #include <net/bluetooth/iso.h> 34 #include <net/bluetooth/mgmt.h> 35 36 #include "hci_request.h" 37 #include "smp.h" 38 #include "a2mp.h" 39 #include "eir.h" 40 41 struct sco_param { 42 u16 pkt_type; 43 u16 max_latency; 44 u8 retrans_effort; 45 }; 46 47 static const struct sco_param esco_param_cvsd[] = { 48 { EDR_ESCO_MASK & ~ESCO_2EV3, 0x000a, 0x01 }, /* S3 */ 49 { EDR_ESCO_MASK & ~ESCO_2EV3, 0x0007, 0x01 }, /* S2 */ 50 { EDR_ESCO_MASK | ESCO_EV3, 0x0007, 0x01 }, /* S1 */ 51 { EDR_ESCO_MASK | ESCO_HV3, 0xffff, 0x01 }, /* D1 */ 52 { EDR_ESCO_MASK | ESCO_HV1, 0xffff, 0x01 }, /* D0 */ 53 }; 54 55 static const struct sco_param sco_param_cvsd[] = { 56 { EDR_ESCO_MASK | ESCO_HV3, 0xffff, 0xff }, /* D1 */ 57 { EDR_ESCO_MASK | ESCO_HV1, 0xffff, 0xff }, /* D0 */ 58 }; 59 60 static const struct sco_param esco_param_msbc[] = { 61 { EDR_ESCO_MASK & ~ESCO_2EV3, 0x000d, 0x02 }, /* T2 */ 62 { EDR_ESCO_MASK | ESCO_EV3, 0x0008, 0x02 }, /* T1 */ 63 }; 64 65 /* This function requires the caller holds hdev->lock */ 66 static void hci_connect_le_scan_cleanup(struct hci_conn *conn) 67 { 68 struct hci_conn_params *params; 69 struct hci_dev *hdev = conn->hdev; 70 struct smp_irk *irk; 71 bdaddr_t *bdaddr; 72 u8 bdaddr_type; 73 74 bdaddr = &conn->dst; 75 bdaddr_type = conn->dst_type; 76 77 /* Check if we need to convert to identity address */ 78 irk = hci_get_irk(hdev, bdaddr, bdaddr_type); 79 if (irk) { 80 bdaddr = &irk->bdaddr; 81 bdaddr_type = irk->addr_type; 82 } 83 84 params = hci_pend_le_action_lookup(&hdev->pend_le_conns, bdaddr, 85 bdaddr_type); 86 if (!params || !params->explicit_connect) 87 return; 88 89 /* The connection attempt was doing scan for new RPA, and is 90 * in scan phase. If params are not associated with any other 91 * autoconnect action, remove them completely. If they are, just unmark 92 * them as waiting for connection, by clearing explicit_connect field. 93 */ 94 params->explicit_connect = false; 95 96 list_del_init(¶ms->action); 97 98 switch (params->auto_connect) { 99 case HCI_AUTO_CONN_EXPLICIT: 100 hci_conn_params_del(hdev, bdaddr, bdaddr_type); 101 /* return instead of break to avoid duplicate scan update */ 102 return; 103 case HCI_AUTO_CONN_DIRECT: 104 case HCI_AUTO_CONN_ALWAYS: 105 list_add(¶ms->action, &hdev->pend_le_conns); 106 break; 107 case HCI_AUTO_CONN_REPORT: 108 list_add(¶ms->action, &hdev->pend_le_reports); 109 break; 110 default: 111 break; 112 } 113 114 hci_update_passive_scan(hdev); 115 } 116 117 static void hci_conn_cleanup(struct hci_conn *conn) 118 { 119 struct hci_dev *hdev = conn->hdev; 120 121 if (test_bit(HCI_CONN_PARAM_REMOVAL_PEND, &conn->flags)) 122 hci_conn_params_del(conn->hdev, &conn->dst, conn->dst_type); 123 124 if (test_and_clear_bit(HCI_CONN_FLUSH_KEY, &conn->flags)) 125 hci_remove_link_key(hdev, &conn->dst); 126 127 hci_chan_list_flush(conn); 128 129 hci_conn_hash_del(hdev, conn); 130 131 if (conn->cleanup) 132 conn->cleanup(conn); 133 134 if (conn->type == SCO_LINK || conn->type == ESCO_LINK) { 135 switch (conn->setting & SCO_AIRMODE_MASK) { 136 case SCO_AIRMODE_CVSD: 137 case SCO_AIRMODE_TRANSP: 138 if (hdev->notify) 139 hdev->notify(hdev, HCI_NOTIFY_DISABLE_SCO); 140 break; 141 } 142 } else { 143 if (hdev->notify) 144 hdev->notify(hdev, HCI_NOTIFY_CONN_DEL); 145 } 146 147 hci_conn_del_sysfs(conn); 148 149 debugfs_remove_recursive(conn->debugfs); 150 151 hci_dev_put(hdev); 152 153 hci_conn_put(conn); 154 } 155 156 static void le_scan_cleanup(struct work_struct *work) 157 { 158 struct hci_conn *conn = container_of(work, struct hci_conn, 159 le_scan_cleanup); 160 struct hci_dev *hdev = conn->hdev; 161 struct hci_conn *c = NULL; 162 163 BT_DBG("%s hcon %p", hdev->name, conn); 164 165 hci_dev_lock(hdev); 166 167 /* Check that the hci_conn is still around */ 168 rcu_read_lock(); 169 list_for_each_entry_rcu(c, &hdev->conn_hash.list, list) { 170 if (c == conn) 171 break; 172 } 173 rcu_read_unlock(); 174 175 if (c == conn) { 176 hci_connect_le_scan_cleanup(conn); 177 hci_conn_cleanup(conn); 178 } 179 180 hci_dev_unlock(hdev); 181 hci_dev_put(hdev); 182 hci_conn_put(conn); 183 } 184 185 static void hci_connect_le_scan_remove(struct hci_conn *conn) 186 { 187 BT_DBG("%s hcon %p", conn->hdev->name, conn); 188 189 /* We can't call hci_conn_del/hci_conn_cleanup here since that 190 * could deadlock with another hci_conn_del() call that's holding 191 * hci_dev_lock and doing cancel_delayed_work_sync(&conn->disc_work). 192 * Instead, grab temporary extra references to the hci_dev and 193 * hci_conn and perform the necessary cleanup in a separate work 194 * callback. 195 */ 196 197 hci_dev_hold(conn->hdev); 198 hci_conn_get(conn); 199 200 /* Even though we hold a reference to the hdev, many other 201 * things might get cleaned up meanwhile, including the hdev's 202 * own workqueue, so we can't use that for scheduling. 203 */ 204 schedule_work(&conn->le_scan_cleanup); 205 } 206 207 static void hci_acl_create_connection(struct hci_conn *conn) 208 { 209 struct hci_dev *hdev = conn->hdev; 210 struct inquiry_entry *ie; 211 struct hci_cp_create_conn cp; 212 213 BT_DBG("hcon %p", conn); 214 215 /* Many controllers disallow HCI Create Connection while it is doing 216 * HCI Inquiry. So we cancel the Inquiry first before issuing HCI Create 217 * Connection. This may cause the MGMT discovering state to become false 218 * without user space's request but it is okay since the MGMT Discovery 219 * APIs do not promise that discovery should be done forever. Instead, 220 * the user space monitors the status of MGMT discovering and it may 221 * request for discovery again when this flag becomes false. 222 */ 223 if (test_bit(HCI_INQUIRY, &hdev->flags)) { 224 /* Put this connection to "pending" state so that it will be 225 * executed after the inquiry cancel command complete event. 226 */ 227 conn->state = BT_CONNECT2; 228 hci_send_cmd(hdev, HCI_OP_INQUIRY_CANCEL, 0, NULL); 229 return; 230 } 231 232 conn->state = BT_CONNECT; 233 conn->out = true; 234 conn->role = HCI_ROLE_MASTER; 235 236 conn->attempt++; 237 238 conn->link_policy = hdev->link_policy; 239 240 memset(&cp, 0, sizeof(cp)); 241 bacpy(&cp.bdaddr, &conn->dst); 242 cp.pscan_rep_mode = 0x02; 243 244 ie = hci_inquiry_cache_lookup(hdev, &conn->dst); 245 if (ie) { 246 if (inquiry_entry_age(ie) <= INQUIRY_ENTRY_AGE_MAX) { 247 cp.pscan_rep_mode = ie->data.pscan_rep_mode; 248 cp.pscan_mode = ie->data.pscan_mode; 249 cp.clock_offset = ie->data.clock_offset | 250 cpu_to_le16(0x8000); 251 } 252 253 memcpy(conn->dev_class, ie->data.dev_class, 3); 254 } 255 256 cp.pkt_type = cpu_to_le16(conn->pkt_type); 257 if (lmp_rswitch_capable(hdev) && !(hdev->link_mode & HCI_LM_MASTER)) 258 cp.role_switch = 0x01; 259 else 260 cp.role_switch = 0x00; 261 262 hci_send_cmd(hdev, HCI_OP_CREATE_CONN, sizeof(cp), &cp); 263 } 264 265 int hci_disconnect(struct hci_conn *conn, __u8 reason) 266 { 267 BT_DBG("hcon %p", conn); 268 269 /* When we are central of an established connection and it enters 270 * the disconnect timeout, then go ahead and try to read the 271 * current clock offset. Processing of the result is done 272 * within the event handling and hci_clock_offset_evt function. 273 */ 274 if (conn->type == ACL_LINK && conn->role == HCI_ROLE_MASTER && 275 (conn->state == BT_CONNECTED || conn->state == BT_CONFIG)) { 276 struct hci_dev *hdev = conn->hdev; 277 struct hci_cp_read_clock_offset clkoff_cp; 278 279 clkoff_cp.handle = cpu_to_le16(conn->handle); 280 hci_send_cmd(hdev, HCI_OP_READ_CLOCK_OFFSET, sizeof(clkoff_cp), 281 &clkoff_cp); 282 } 283 284 return hci_abort_conn(conn, reason); 285 } 286 287 static void hci_add_sco(struct hci_conn *conn, __u16 handle) 288 { 289 struct hci_dev *hdev = conn->hdev; 290 struct hci_cp_add_sco cp; 291 292 BT_DBG("hcon %p", conn); 293 294 conn->state = BT_CONNECT; 295 conn->out = true; 296 297 conn->attempt++; 298 299 cp.handle = cpu_to_le16(handle); 300 cp.pkt_type = cpu_to_le16(conn->pkt_type); 301 302 hci_send_cmd(hdev, HCI_OP_ADD_SCO, sizeof(cp), &cp); 303 } 304 305 static bool find_next_esco_param(struct hci_conn *conn, 306 const struct sco_param *esco_param, int size) 307 { 308 for (; conn->attempt <= size; conn->attempt++) { 309 if (lmp_esco_2m_capable(conn->link) || 310 (esco_param[conn->attempt - 1].pkt_type & ESCO_2EV3)) 311 break; 312 BT_DBG("hcon %p skipped attempt %d, eSCO 2M not supported", 313 conn, conn->attempt); 314 } 315 316 return conn->attempt <= size; 317 } 318 319 static bool hci_enhanced_setup_sync_conn(struct hci_conn *conn, __u16 handle) 320 { 321 struct hci_dev *hdev = conn->hdev; 322 struct hci_cp_enhanced_setup_sync_conn cp; 323 const struct sco_param *param; 324 325 bt_dev_dbg(hdev, "hcon %p", conn); 326 327 /* for offload use case, codec needs to configured before opening SCO */ 328 if (conn->codec.data_path) 329 hci_req_configure_datapath(hdev, &conn->codec); 330 331 conn->state = BT_CONNECT; 332 conn->out = true; 333 334 conn->attempt++; 335 336 memset(&cp, 0x00, sizeof(cp)); 337 338 cp.handle = cpu_to_le16(handle); 339 340 cp.tx_bandwidth = cpu_to_le32(0x00001f40); 341 cp.rx_bandwidth = cpu_to_le32(0x00001f40); 342 343 switch (conn->codec.id) { 344 case BT_CODEC_MSBC: 345 if (!find_next_esco_param(conn, esco_param_msbc, 346 ARRAY_SIZE(esco_param_msbc))) 347 return false; 348 349 param = &esco_param_msbc[conn->attempt - 1]; 350 cp.tx_coding_format.id = 0x05; 351 cp.rx_coding_format.id = 0x05; 352 cp.tx_codec_frame_size = __cpu_to_le16(60); 353 cp.rx_codec_frame_size = __cpu_to_le16(60); 354 cp.in_bandwidth = __cpu_to_le32(32000); 355 cp.out_bandwidth = __cpu_to_le32(32000); 356 cp.in_coding_format.id = 0x04; 357 cp.out_coding_format.id = 0x04; 358 cp.in_coded_data_size = __cpu_to_le16(16); 359 cp.out_coded_data_size = __cpu_to_le16(16); 360 cp.in_pcm_data_format = 2; 361 cp.out_pcm_data_format = 2; 362 cp.in_pcm_sample_payload_msb_pos = 0; 363 cp.out_pcm_sample_payload_msb_pos = 0; 364 cp.in_data_path = conn->codec.data_path; 365 cp.out_data_path = conn->codec.data_path; 366 cp.in_transport_unit_size = 1; 367 cp.out_transport_unit_size = 1; 368 break; 369 370 case BT_CODEC_TRANSPARENT: 371 if (!find_next_esco_param(conn, esco_param_msbc, 372 ARRAY_SIZE(esco_param_msbc))) 373 return false; 374 param = &esco_param_msbc[conn->attempt - 1]; 375 cp.tx_coding_format.id = 0x03; 376 cp.rx_coding_format.id = 0x03; 377 cp.tx_codec_frame_size = __cpu_to_le16(60); 378 cp.rx_codec_frame_size = __cpu_to_le16(60); 379 cp.in_bandwidth = __cpu_to_le32(0x1f40); 380 cp.out_bandwidth = __cpu_to_le32(0x1f40); 381 cp.in_coding_format.id = 0x03; 382 cp.out_coding_format.id = 0x03; 383 cp.in_coded_data_size = __cpu_to_le16(16); 384 cp.out_coded_data_size = __cpu_to_le16(16); 385 cp.in_pcm_data_format = 2; 386 cp.out_pcm_data_format = 2; 387 cp.in_pcm_sample_payload_msb_pos = 0; 388 cp.out_pcm_sample_payload_msb_pos = 0; 389 cp.in_data_path = conn->codec.data_path; 390 cp.out_data_path = conn->codec.data_path; 391 cp.in_transport_unit_size = 1; 392 cp.out_transport_unit_size = 1; 393 break; 394 395 case BT_CODEC_CVSD: 396 if (lmp_esco_capable(conn->link)) { 397 if (!find_next_esco_param(conn, esco_param_cvsd, 398 ARRAY_SIZE(esco_param_cvsd))) 399 return false; 400 param = &esco_param_cvsd[conn->attempt - 1]; 401 } else { 402 if (conn->attempt > ARRAY_SIZE(sco_param_cvsd)) 403 return false; 404 param = &sco_param_cvsd[conn->attempt - 1]; 405 } 406 cp.tx_coding_format.id = 2; 407 cp.rx_coding_format.id = 2; 408 cp.tx_codec_frame_size = __cpu_to_le16(60); 409 cp.rx_codec_frame_size = __cpu_to_le16(60); 410 cp.in_bandwidth = __cpu_to_le32(16000); 411 cp.out_bandwidth = __cpu_to_le32(16000); 412 cp.in_coding_format.id = 4; 413 cp.out_coding_format.id = 4; 414 cp.in_coded_data_size = __cpu_to_le16(16); 415 cp.out_coded_data_size = __cpu_to_le16(16); 416 cp.in_pcm_data_format = 2; 417 cp.out_pcm_data_format = 2; 418 cp.in_pcm_sample_payload_msb_pos = 0; 419 cp.out_pcm_sample_payload_msb_pos = 0; 420 cp.in_data_path = conn->codec.data_path; 421 cp.out_data_path = conn->codec.data_path; 422 cp.in_transport_unit_size = 16; 423 cp.out_transport_unit_size = 16; 424 break; 425 default: 426 return false; 427 } 428 429 cp.retrans_effort = param->retrans_effort; 430 cp.pkt_type = __cpu_to_le16(param->pkt_type); 431 cp.max_latency = __cpu_to_le16(param->max_latency); 432 433 if (hci_send_cmd(hdev, HCI_OP_ENHANCED_SETUP_SYNC_CONN, sizeof(cp), &cp) < 0) 434 return false; 435 436 return true; 437 } 438 439 static bool hci_setup_sync_conn(struct hci_conn *conn, __u16 handle) 440 { 441 struct hci_dev *hdev = conn->hdev; 442 struct hci_cp_setup_sync_conn cp; 443 const struct sco_param *param; 444 445 bt_dev_dbg(hdev, "hcon %p", conn); 446 447 conn->state = BT_CONNECT; 448 conn->out = true; 449 450 conn->attempt++; 451 452 cp.handle = cpu_to_le16(handle); 453 454 cp.tx_bandwidth = cpu_to_le32(0x00001f40); 455 cp.rx_bandwidth = cpu_to_le32(0x00001f40); 456 cp.voice_setting = cpu_to_le16(conn->setting); 457 458 switch (conn->setting & SCO_AIRMODE_MASK) { 459 case SCO_AIRMODE_TRANSP: 460 if (!find_next_esco_param(conn, esco_param_msbc, 461 ARRAY_SIZE(esco_param_msbc))) 462 return false; 463 param = &esco_param_msbc[conn->attempt - 1]; 464 break; 465 case SCO_AIRMODE_CVSD: 466 if (lmp_esco_capable(conn->link)) { 467 if (!find_next_esco_param(conn, esco_param_cvsd, 468 ARRAY_SIZE(esco_param_cvsd))) 469 return false; 470 param = &esco_param_cvsd[conn->attempt - 1]; 471 } else { 472 if (conn->attempt > ARRAY_SIZE(sco_param_cvsd)) 473 return false; 474 param = &sco_param_cvsd[conn->attempt - 1]; 475 } 476 break; 477 default: 478 return false; 479 } 480 481 cp.retrans_effort = param->retrans_effort; 482 cp.pkt_type = __cpu_to_le16(param->pkt_type); 483 cp.max_latency = __cpu_to_le16(param->max_latency); 484 485 if (hci_send_cmd(hdev, HCI_OP_SETUP_SYNC_CONN, sizeof(cp), &cp) < 0) 486 return false; 487 488 return true; 489 } 490 491 bool hci_setup_sync(struct hci_conn *conn, __u16 handle) 492 { 493 if (enhanced_sync_conn_capable(conn->hdev)) 494 return hci_enhanced_setup_sync_conn(conn, handle); 495 496 return hci_setup_sync_conn(conn, handle); 497 } 498 499 u8 hci_le_conn_update(struct hci_conn *conn, u16 min, u16 max, u16 latency, 500 u16 to_multiplier) 501 { 502 struct hci_dev *hdev = conn->hdev; 503 struct hci_conn_params *params; 504 struct hci_cp_le_conn_update cp; 505 506 hci_dev_lock(hdev); 507 508 params = hci_conn_params_lookup(hdev, &conn->dst, conn->dst_type); 509 if (params) { 510 params->conn_min_interval = min; 511 params->conn_max_interval = max; 512 params->conn_latency = latency; 513 params->supervision_timeout = to_multiplier; 514 } 515 516 hci_dev_unlock(hdev); 517 518 memset(&cp, 0, sizeof(cp)); 519 cp.handle = cpu_to_le16(conn->handle); 520 cp.conn_interval_min = cpu_to_le16(min); 521 cp.conn_interval_max = cpu_to_le16(max); 522 cp.conn_latency = cpu_to_le16(latency); 523 cp.supervision_timeout = cpu_to_le16(to_multiplier); 524 cp.min_ce_len = cpu_to_le16(0x0000); 525 cp.max_ce_len = cpu_to_le16(0x0000); 526 527 hci_send_cmd(hdev, HCI_OP_LE_CONN_UPDATE, sizeof(cp), &cp); 528 529 if (params) 530 return 0x01; 531 532 return 0x00; 533 } 534 535 void hci_le_start_enc(struct hci_conn *conn, __le16 ediv, __le64 rand, 536 __u8 ltk[16], __u8 key_size) 537 { 538 struct hci_dev *hdev = conn->hdev; 539 struct hci_cp_le_start_enc cp; 540 541 BT_DBG("hcon %p", conn); 542 543 memset(&cp, 0, sizeof(cp)); 544 545 cp.handle = cpu_to_le16(conn->handle); 546 cp.rand = rand; 547 cp.ediv = ediv; 548 memcpy(cp.ltk, ltk, key_size); 549 550 hci_send_cmd(hdev, HCI_OP_LE_START_ENC, sizeof(cp), &cp); 551 } 552 553 /* Device _must_ be locked */ 554 void hci_sco_setup(struct hci_conn *conn, __u8 status) 555 { 556 struct hci_conn *sco = conn->link; 557 558 if (!sco) 559 return; 560 561 BT_DBG("hcon %p", conn); 562 563 if (!status) { 564 if (lmp_esco_capable(conn->hdev)) 565 hci_setup_sync(sco, conn->handle); 566 else 567 hci_add_sco(sco, conn->handle); 568 } else { 569 hci_connect_cfm(sco, status); 570 hci_conn_del(sco); 571 } 572 } 573 574 static void hci_conn_timeout(struct work_struct *work) 575 { 576 struct hci_conn *conn = container_of(work, struct hci_conn, 577 disc_work.work); 578 int refcnt = atomic_read(&conn->refcnt); 579 580 BT_DBG("hcon %p state %s", conn, state_to_string(conn->state)); 581 582 WARN_ON(refcnt < 0); 583 584 /* FIXME: It was observed that in pairing failed scenario, refcnt 585 * drops below 0. Probably this is because l2cap_conn_del calls 586 * l2cap_chan_del for each channel, and inside l2cap_chan_del conn is 587 * dropped. After that loop hci_chan_del is called which also drops 588 * conn. For now make sure that ACL is alive if refcnt is higher then 0, 589 * otherwise drop it. 590 */ 591 if (refcnt > 0) 592 return; 593 594 /* LE connections in scanning state need special handling */ 595 if (conn->state == BT_CONNECT && conn->type == LE_LINK && 596 test_bit(HCI_CONN_SCANNING, &conn->flags)) { 597 hci_connect_le_scan_remove(conn); 598 return; 599 } 600 601 hci_abort_conn(conn, hci_proto_disconn_ind(conn)); 602 } 603 604 /* Enter sniff mode */ 605 static void hci_conn_idle(struct work_struct *work) 606 { 607 struct hci_conn *conn = container_of(work, struct hci_conn, 608 idle_work.work); 609 struct hci_dev *hdev = conn->hdev; 610 611 BT_DBG("hcon %p mode %d", conn, conn->mode); 612 613 if (!lmp_sniff_capable(hdev) || !lmp_sniff_capable(conn)) 614 return; 615 616 if (conn->mode != HCI_CM_ACTIVE || !(conn->link_policy & HCI_LP_SNIFF)) 617 return; 618 619 if (lmp_sniffsubr_capable(hdev) && lmp_sniffsubr_capable(conn)) { 620 struct hci_cp_sniff_subrate cp; 621 cp.handle = cpu_to_le16(conn->handle); 622 cp.max_latency = cpu_to_le16(0); 623 cp.min_remote_timeout = cpu_to_le16(0); 624 cp.min_local_timeout = cpu_to_le16(0); 625 hci_send_cmd(hdev, HCI_OP_SNIFF_SUBRATE, sizeof(cp), &cp); 626 } 627 628 if (!test_and_set_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->flags)) { 629 struct hci_cp_sniff_mode cp; 630 cp.handle = cpu_to_le16(conn->handle); 631 cp.max_interval = cpu_to_le16(hdev->sniff_max_interval); 632 cp.min_interval = cpu_to_le16(hdev->sniff_min_interval); 633 cp.attempt = cpu_to_le16(4); 634 cp.timeout = cpu_to_le16(1); 635 hci_send_cmd(hdev, HCI_OP_SNIFF_MODE, sizeof(cp), &cp); 636 } 637 } 638 639 static void hci_conn_auto_accept(struct work_struct *work) 640 { 641 struct hci_conn *conn = container_of(work, struct hci_conn, 642 auto_accept_work.work); 643 644 hci_send_cmd(conn->hdev, HCI_OP_USER_CONFIRM_REPLY, sizeof(conn->dst), 645 &conn->dst); 646 } 647 648 static void le_disable_advertising(struct hci_dev *hdev) 649 { 650 if (ext_adv_capable(hdev)) { 651 struct hci_cp_le_set_ext_adv_enable cp; 652 653 cp.enable = 0x00; 654 cp.num_of_sets = 0x00; 655 656 hci_send_cmd(hdev, HCI_OP_LE_SET_EXT_ADV_ENABLE, sizeof(cp), 657 &cp); 658 } else { 659 u8 enable = 0x00; 660 hci_send_cmd(hdev, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable), 661 &enable); 662 } 663 } 664 665 static void le_conn_timeout(struct work_struct *work) 666 { 667 struct hci_conn *conn = container_of(work, struct hci_conn, 668 le_conn_timeout.work); 669 struct hci_dev *hdev = conn->hdev; 670 671 BT_DBG(""); 672 673 /* We could end up here due to having done directed advertising, 674 * so clean up the state if necessary. This should however only 675 * happen with broken hardware or if low duty cycle was used 676 * (which doesn't have a timeout of its own). 677 */ 678 if (conn->role == HCI_ROLE_SLAVE) { 679 /* Disable LE Advertising */ 680 le_disable_advertising(hdev); 681 hci_dev_lock(hdev); 682 hci_conn_failed(conn, HCI_ERROR_ADVERTISING_TIMEOUT); 683 hci_dev_unlock(hdev); 684 return; 685 } 686 687 hci_abort_conn(conn, HCI_ERROR_REMOTE_USER_TERM); 688 } 689 690 struct iso_list_data { 691 union { 692 u8 cig; 693 u8 big; 694 }; 695 union { 696 u8 cis; 697 u8 bis; 698 u16 sync_handle; 699 }; 700 int count; 701 struct { 702 struct hci_cp_le_set_cig_params cp; 703 struct hci_cis_params cis[0x11]; 704 } pdu; 705 }; 706 707 static void bis_list(struct hci_conn *conn, void *data) 708 { 709 struct iso_list_data *d = data; 710 711 /* Skip if not broadcast/ANY address */ 712 if (bacmp(&conn->dst, BDADDR_ANY)) 713 return; 714 715 if (d->big != conn->iso_qos.big || d->bis == BT_ISO_QOS_BIS_UNSET || 716 d->bis != conn->iso_qos.bis) 717 return; 718 719 d->count++; 720 } 721 722 static void find_bis(struct hci_conn *conn, void *data) 723 { 724 struct iso_list_data *d = data; 725 726 /* Ignore unicast */ 727 if (bacmp(&conn->dst, BDADDR_ANY)) 728 return; 729 730 d->count++; 731 } 732 733 static int terminate_big_sync(struct hci_dev *hdev, void *data) 734 { 735 struct iso_list_data *d = data; 736 737 bt_dev_dbg(hdev, "big 0x%2.2x bis 0x%2.2x", d->big, d->bis); 738 739 hci_remove_ext_adv_instance_sync(hdev, d->bis, NULL); 740 741 /* Check if ISO connection is a BIS and terminate BIG if there are 742 * no other connections using it. 743 */ 744 hci_conn_hash_list_state(hdev, find_bis, ISO_LINK, BT_CONNECTED, d); 745 if (d->count) 746 return 0; 747 748 return hci_le_terminate_big_sync(hdev, d->big, 749 HCI_ERROR_LOCAL_HOST_TERM); 750 } 751 752 static void terminate_big_destroy(struct hci_dev *hdev, void *data, int err) 753 { 754 kfree(data); 755 } 756 757 static int hci_le_terminate_big(struct hci_dev *hdev, u8 big, u8 bis) 758 { 759 struct iso_list_data *d; 760 761 bt_dev_dbg(hdev, "big 0x%2.2x bis 0x%2.2x", big, bis); 762 763 d = kmalloc(sizeof(*d), GFP_KERNEL); 764 if (!d) 765 return -ENOMEM; 766 767 memset(d, 0, sizeof(*d)); 768 d->big = big; 769 d->bis = bis; 770 771 return hci_cmd_sync_queue(hdev, terminate_big_sync, d, 772 terminate_big_destroy); 773 } 774 775 static int big_terminate_sync(struct hci_dev *hdev, void *data) 776 { 777 struct iso_list_data *d = data; 778 779 bt_dev_dbg(hdev, "big 0x%2.2x sync_handle 0x%4.4x", d->big, 780 d->sync_handle); 781 782 /* Check if ISO connection is a BIS and terminate BIG if there are 783 * no other connections using it. 784 */ 785 hci_conn_hash_list_state(hdev, find_bis, ISO_LINK, BT_CONNECTED, d); 786 if (d->count) 787 return 0; 788 789 hci_le_big_terminate_sync(hdev, d->big); 790 791 return hci_le_pa_terminate_sync(hdev, d->sync_handle); 792 } 793 794 static int hci_le_big_terminate(struct hci_dev *hdev, u8 big, u16 sync_handle) 795 { 796 struct iso_list_data *d; 797 798 bt_dev_dbg(hdev, "big 0x%2.2x sync_handle 0x%4.4x", big, sync_handle); 799 800 d = kmalloc(sizeof(*d), GFP_KERNEL); 801 if (!d) 802 return -ENOMEM; 803 804 memset(d, 0, sizeof(*d)); 805 d->big = big; 806 d->sync_handle = sync_handle; 807 808 return hci_cmd_sync_queue(hdev, big_terminate_sync, d, 809 terminate_big_destroy); 810 } 811 812 /* Cleanup BIS connection 813 * 814 * Detects if there any BIS left connected in a BIG 815 * broadcaster: Remove advertising instance and terminate BIG. 816 * broadcaster receiver: Teminate BIG sync and terminate PA sync. 817 */ 818 static void bis_cleanup(struct hci_conn *conn) 819 { 820 struct hci_dev *hdev = conn->hdev; 821 822 bt_dev_dbg(hdev, "conn %p", conn); 823 824 if (conn->role == HCI_ROLE_MASTER) { 825 if (!test_and_clear_bit(HCI_CONN_PER_ADV, &conn->flags)) 826 return; 827 828 hci_le_terminate_big(hdev, conn->iso_qos.big, 829 conn->iso_qos.bis); 830 } else { 831 hci_le_big_terminate(hdev, conn->iso_qos.big, 832 conn->sync_handle); 833 } 834 } 835 836 static int remove_cig_sync(struct hci_dev *hdev, void *data) 837 { 838 u8 handle = PTR_ERR(data); 839 840 return hci_le_remove_cig_sync(hdev, handle); 841 } 842 843 static int hci_le_remove_cig(struct hci_dev *hdev, u8 handle) 844 { 845 bt_dev_dbg(hdev, "handle 0x%2.2x", handle); 846 847 return hci_cmd_sync_queue(hdev, remove_cig_sync, ERR_PTR(handle), NULL); 848 } 849 850 static void find_cis(struct hci_conn *conn, void *data) 851 { 852 struct iso_list_data *d = data; 853 854 /* Ignore broadcast */ 855 if (!bacmp(&conn->dst, BDADDR_ANY)) 856 return; 857 858 d->count++; 859 } 860 861 /* Cleanup CIS connection: 862 * 863 * Detects if there any CIS left connected in a CIG and remove it. 864 */ 865 static void cis_cleanup(struct hci_conn *conn) 866 { 867 struct hci_dev *hdev = conn->hdev; 868 struct iso_list_data d; 869 870 memset(&d, 0, sizeof(d)); 871 d.cig = conn->iso_qos.cig; 872 873 /* Check if ISO connection is a CIS and remove CIG if there are 874 * no other connections using it. 875 */ 876 hci_conn_hash_list_state(hdev, find_cis, ISO_LINK, BT_CONNECTED, &d); 877 if (d.count) 878 return; 879 880 hci_le_remove_cig(hdev, conn->iso_qos.cig); 881 } 882 883 struct hci_conn *hci_conn_add(struct hci_dev *hdev, int type, bdaddr_t *dst, 884 u8 role) 885 { 886 struct hci_conn *conn; 887 888 BT_DBG("%s dst %pMR", hdev->name, dst); 889 890 conn = kzalloc(sizeof(*conn), GFP_KERNEL); 891 if (!conn) 892 return NULL; 893 894 bacpy(&conn->dst, dst); 895 bacpy(&conn->src, &hdev->bdaddr); 896 conn->handle = HCI_CONN_HANDLE_UNSET; 897 conn->hdev = hdev; 898 conn->type = type; 899 conn->role = role; 900 conn->mode = HCI_CM_ACTIVE; 901 conn->state = BT_OPEN; 902 conn->auth_type = HCI_AT_GENERAL_BONDING; 903 conn->io_capability = hdev->io_capability; 904 conn->remote_auth = 0xff; 905 conn->key_type = 0xff; 906 conn->rssi = HCI_RSSI_INVALID; 907 conn->tx_power = HCI_TX_POWER_INVALID; 908 conn->max_tx_power = HCI_TX_POWER_INVALID; 909 910 set_bit(HCI_CONN_POWER_SAVE, &conn->flags); 911 conn->disc_timeout = HCI_DISCONN_TIMEOUT; 912 913 /* Set Default Authenticated payload timeout to 30s */ 914 conn->auth_payload_timeout = DEFAULT_AUTH_PAYLOAD_TIMEOUT; 915 916 if (conn->role == HCI_ROLE_MASTER) 917 conn->out = true; 918 919 switch (type) { 920 case ACL_LINK: 921 conn->pkt_type = hdev->pkt_type & ACL_PTYPE_MASK; 922 break; 923 case LE_LINK: 924 /* conn->src should reflect the local identity address */ 925 hci_copy_identity_address(hdev, &conn->src, &conn->src_type); 926 break; 927 case ISO_LINK: 928 /* conn->src should reflect the local identity address */ 929 hci_copy_identity_address(hdev, &conn->src, &conn->src_type); 930 931 /* set proper cleanup function */ 932 if (!bacmp(dst, BDADDR_ANY)) 933 conn->cleanup = bis_cleanup; 934 else if (conn->role == HCI_ROLE_MASTER) 935 conn->cleanup = cis_cleanup; 936 937 break; 938 case SCO_LINK: 939 if (lmp_esco_capable(hdev)) 940 conn->pkt_type = (hdev->esco_type & SCO_ESCO_MASK) | 941 (hdev->esco_type & EDR_ESCO_MASK); 942 else 943 conn->pkt_type = hdev->pkt_type & SCO_PTYPE_MASK; 944 break; 945 case ESCO_LINK: 946 conn->pkt_type = hdev->esco_type & ~EDR_ESCO_MASK; 947 break; 948 } 949 950 skb_queue_head_init(&conn->data_q); 951 952 INIT_LIST_HEAD(&conn->chan_list); 953 954 INIT_DELAYED_WORK(&conn->disc_work, hci_conn_timeout); 955 INIT_DELAYED_WORK(&conn->auto_accept_work, hci_conn_auto_accept); 956 INIT_DELAYED_WORK(&conn->idle_work, hci_conn_idle); 957 INIT_DELAYED_WORK(&conn->le_conn_timeout, le_conn_timeout); 958 INIT_WORK(&conn->le_scan_cleanup, le_scan_cleanup); 959 960 atomic_set(&conn->refcnt, 0); 961 962 hci_dev_hold(hdev); 963 964 hci_conn_hash_add(hdev, conn); 965 966 /* The SCO and eSCO connections will only be notified when their 967 * setup has been completed. This is different to ACL links which 968 * can be notified right away. 969 */ 970 if (conn->type != SCO_LINK && conn->type != ESCO_LINK) { 971 if (hdev->notify) 972 hdev->notify(hdev, HCI_NOTIFY_CONN_ADD); 973 } 974 975 hci_conn_init_sysfs(conn); 976 977 return conn; 978 } 979 980 int hci_conn_del(struct hci_conn *conn) 981 { 982 struct hci_dev *hdev = conn->hdev; 983 984 BT_DBG("%s hcon %p handle %d", hdev->name, conn, conn->handle); 985 986 cancel_delayed_work_sync(&conn->disc_work); 987 cancel_delayed_work_sync(&conn->auto_accept_work); 988 cancel_delayed_work_sync(&conn->idle_work); 989 990 if (conn->type == ACL_LINK) { 991 struct hci_conn *sco = conn->link; 992 if (sco) 993 sco->link = NULL; 994 995 /* Unacked frames */ 996 hdev->acl_cnt += conn->sent; 997 } else if (conn->type == LE_LINK) { 998 cancel_delayed_work(&conn->le_conn_timeout); 999 1000 if (hdev->le_pkts) 1001 hdev->le_cnt += conn->sent; 1002 else 1003 hdev->acl_cnt += conn->sent; 1004 } else { 1005 struct hci_conn *acl = conn->link; 1006 if (acl) { 1007 acl->link = NULL; 1008 hci_conn_drop(acl); 1009 } 1010 } 1011 1012 if (conn->amp_mgr) 1013 amp_mgr_put(conn->amp_mgr); 1014 1015 skb_queue_purge(&conn->data_q); 1016 1017 /* Remove the connection from the list and cleanup its remaining 1018 * state. This is a separate function since for some cases like 1019 * BT_CONNECT_SCAN we *only* want the cleanup part without the 1020 * rest of hci_conn_del. 1021 */ 1022 hci_conn_cleanup(conn); 1023 1024 return 0; 1025 } 1026 1027 struct hci_dev *hci_get_route(bdaddr_t *dst, bdaddr_t *src, uint8_t src_type) 1028 { 1029 int use_src = bacmp(src, BDADDR_ANY); 1030 struct hci_dev *hdev = NULL, *d; 1031 1032 BT_DBG("%pMR -> %pMR", src, dst); 1033 1034 read_lock(&hci_dev_list_lock); 1035 1036 list_for_each_entry(d, &hci_dev_list, list) { 1037 if (!test_bit(HCI_UP, &d->flags) || 1038 hci_dev_test_flag(d, HCI_USER_CHANNEL) || 1039 d->dev_type != HCI_PRIMARY) 1040 continue; 1041 1042 /* Simple routing: 1043 * No source address - find interface with bdaddr != dst 1044 * Source address - find interface with bdaddr == src 1045 */ 1046 1047 if (use_src) { 1048 bdaddr_t id_addr; 1049 u8 id_addr_type; 1050 1051 if (src_type == BDADDR_BREDR) { 1052 if (!lmp_bredr_capable(d)) 1053 continue; 1054 bacpy(&id_addr, &d->bdaddr); 1055 id_addr_type = BDADDR_BREDR; 1056 } else { 1057 if (!lmp_le_capable(d)) 1058 continue; 1059 1060 hci_copy_identity_address(d, &id_addr, 1061 &id_addr_type); 1062 1063 /* Convert from HCI to three-value type */ 1064 if (id_addr_type == ADDR_LE_DEV_PUBLIC) 1065 id_addr_type = BDADDR_LE_PUBLIC; 1066 else 1067 id_addr_type = BDADDR_LE_RANDOM; 1068 } 1069 1070 if (!bacmp(&id_addr, src) && id_addr_type == src_type) { 1071 hdev = d; break; 1072 } 1073 } else { 1074 if (bacmp(&d->bdaddr, dst)) { 1075 hdev = d; break; 1076 } 1077 } 1078 } 1079 1080 if (hdev) 1081 hdev = hci_dev_hold(hdev); 1082 1083 read_unlock(&hci_dev_list_lock); 1084 return hdev; 1085 } 1086 EXPORT_SYMBOL(hci_get_route); 1087 1088 /* This function requires the caller holds hdev->lock */ 1089 static void hci_le_conn_failed(struct hci_conn *conn, u8 status) 1090 { 1091 struct hci_dev *hdev = conn->hdev; 1092 struct hci_conn_params *params; 1093 1094 params = hci_pend_le_action_lookup(&hdev->pend_le_conns, &conn->dst, 1095 conn->dst_type); 1096 if (params && params->conn) { 1097 hci_conn_drop(params->conn); 1098 hci_conn_put(params->conn); 1099 params->conn = NULL; 1100 } 1101 1102 /* If the status indicates successful cancellation of 1103 * the attempt (i.e. Unknown Connection Id) there's no point of 1104 * notifying failure since we'll go back to keep trying to 1105 * connect. The only exception is explicit connect requests 1106 * where a timeout + cancel does indicate an actual failure. 1107 */ 1108 if (status != HCI_ERROR_UNKNOWN_CONN_ID || 1109 (params && params->explicit_connect)) 1110 mgmt_connect_failed(hdev, &conn->dst, conn->type, 1111 conn->dst_type, status); 1112 1113 /* Since we may have temporarily stopped the background scanning in 1114 * favor of connection establishment, we should restart it. 1115 */ 1116 hci_update_passive_scan(hdev); 1117 1118 /* Enable advertising in case this was a failed connection 1119 * attempt as a peripheral. 1120 */ 1121 hci_enable_advertising(hdev); 1122 } 1123 1124 /* This function requires the caller holds hdev->lock */ 1125 void hci_conn_failed(struct hci_conn *conn, u8 status) 1126 { 1127 struct hci_dev *hdev = conn->hdev; 1128 1129 bt_dev_dbg(hdev, "status 0x%2.2x", status); 1130 1131 switch (conn->type) { 1132 case LE_LINK: 1133 hci_le_conn_failed(conn, status); 1134 break; 1135 case ACL_LINK: 1136 mgmt_connect_failed(hdev, &conn->dst, conn->type, 1137 conn->dst_type, status); 1138 break; 1139 } 1140 1141 conn->state = BT_CLOSED; 1142 hci_connect_cfm(conn, status); 1143 hci_conn_del(conn); 1144 } 1145 1146 static void create_le_conn_complete(struct hci_dev *hdev, void *data, int err) 1147 { 1148 struct hci_conn *conn = data; 1149 1150 hci_dev_lock(hdev); 1151 1152 if (!err) { 1153 hci_connect_le_scan_cleanup(conn); 1154 goto done; 1155 } 1156 1157 bt_dev_err(hdev, "request failed to create LE connection: err %d", err); 1158 1159 /* Check if connection is still pending */ 1160 if (conn != hci_lookup_le_connect(hdev)) 1161 goto done; 1162 1163 hci_conn_failed(conn, bt_status(err)); 1164 1165 done: 1166 hci_dev_unlock(hdev); 1167 } 1168 1169 static int hci_connect_le_sync(struct hci_dev *hdev, void *data) 1170 { 1171 struct hci_conn *conn = data; 1172 1173 bt_dev_dbg(hdev, "conn %p", conn); 1174 1175 return hci_le_create_conn_sync(hdev, conn); 1176 } 1177 1178 struct hci_conn *hci_connect_le(struct hci_dev *hdev, bdaddr_t *dst, 1179 u8 dst_type, bool dst_resolved, u8 sec_level, 1180 u16 conn_timeout, u8 role) 1181 { 1182 struct hci_conn *conn; 1183 struct smp_irk *irk; 1184 int err; 1185 1186 /* Let's make sure that le is enabled.*/ 1187 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED)) { 1188 if (lmp_le_capable(hdev)) 1189 return ERR_PTR(-ECONNREFUSED); 1190 1191 return ERR_PTR(-EOPNOTSUPP); 1192 } 1193 1194 /* Since the controller supports only one LE connection attempt at a 1195 * time, we return -EBUSY if there is any connection attempt running. 1196 */ 1197 if (hci_lookup_le_connect(hdev)) 1198 return ERR_PTR(-EBUSY); 1199 1200 /* If there's already a connection object but it's not in 1201 * scanning state it means it must already be established, in 1202 * which case we can't do anything else except report a failure 1203 * to connect. 1204 */ 1205 conn = hci_conn_hash_lookup_le(hdev, dst, dst_type); 1206 if (conn && !test_bit(HCI_CONN_SCANNING, &conn->flags)) { 1207 return ERR_PTR(-EBUSY); 1208 } 1209 1210 /* Check if the destination address has been resolved by the controller 1211 * since if it did then the identity address shall be used. 1212 */ 1213 if (!dst_resolved) { 1214 /* When given an identity address with existing identity 1215 * resolving key, the connection needs to be established 1216 * to a resolvable random address. 1217 * 1218 * Storing the resolvable random address is required here 1219 * to handle connection failures. The address will later 1220 * be resolved back into the original identity address 1221 * from the connect request. 1222 */ 1223 irk = hci_find_irk_by_addr(hdev, dst, dst_type); 1224 if (irk && bacmp(&irk->rpa, BDADDR_ANY)) { 1225 dst = &irk->rpa; 1226 dst_type = ADDR_LE_DEV_RANDOM; 1227 } 1228 } 1229 1230 if (conn) { 1231 bacpy(&conn->dst, dst); 1232 } else { 1233 conn = hci_conn_add(hdev, LE_LINK, dst, role); 1234 if (!conn) 1235 return ERR_PTR(-ENOMEM); 1236 hci_conn_hold(conn); 1237 conn->pending_sec_level = sec_level; 1238 } 1239 1240 conn->dst_type = dst_type; 1241 conn->sec_level = BT_SECURITY_LOW; 1242 conn->conn_timeout = conn_timeout; 1243 1244 conn->state = BT_CONNECT; 1245 clear_bit(HCI_CONN_SCANNING, &conn->flags); 1246 1247 err = hci_cmd_sync_queue(hdev, hci_connect_le_sync, conn, 1248 create_le_conn_complete); 1249 if (err) { 1250 hci_conn_del(conn); 1251 return ERR_PTR(err); 1252 } 1253 1254 return conn; 1255 } 1256 1257 static bool is_connected(struct hci_dev *hdev, bdaddr_t *addr, u8 type) 1258 { 1259 struct hci_conn *conn; 1260 1261 conn = hci_conn_hash_lookup_le(hdev, addr, type); 1262 if (!conn) 1263 return false; 1264 1265 if (conn->state != BT_CONNECTED) 1266 return false; 1267 1268 return true; 1269 } 1270 1271 /* This function requires the caller holds hdev->lock */ 1272 static int hci_explicit_conn_params_set(struct hci_dev *hdev, 1273 bdaddr_t *addr, u8 addr_type) 1274 { 1275 struct hci_conn_params *params; 1276 1277 if (is_connected(hdev, addr, addr_type)) 1278 return -EISCONN; 1279 1280 params = hci_conn_params_lookup(hdev, addr, addr_type); 1281 if (!params) { 1282 params = hci_conn_params_add(hdev, addr, addr_type); 1283 if (!params) 1284 return -ENOMEM; 1285 1286 /* If we created new params, mark them to be deleted in 1287 * hci_connect_le_scan_cleanup. It's different case than 1288 * existing disabled params, those will stay after cleanup. 1289 */ 1290 params->auto_connect = HCI_AUTO_CONN_EXPLICIT; 1291 } 1292 1293 /* We're trying to connect, so make sure params are at pend_le_conns */ 1294 if (params->auto_connect == HCI_AUTO_CONN_DISABLED || 1295 params->auto_connect == HCI_AUTO_CONN_REPORT || 1296 params->auto_connect == HCI_AUTO_CONN_EXPLICIT) { 1297 list_del_init(¶ms->action); 1298 list_add(¶ms->action, &hdev->pend_le_conns); 1299 } 1300 1301 params->explicit_connect = true; 1302 1303 BT_DBG("addr %pMR (type %u) auto_connect %u", addr, addr_type, 1304 params->auto_connect); 1305 1306 return 0; 1307 } 1308 1309 static int qos_set_big(struct hci_dev *hdev, struct bt_iso_qos *qos) 1310 { 1311 struct iso_list_data data; 1312 1313 /* Allocate a BIG if not set */ 1314 if (qos->big == BT_ISO_QOS_BIG_UNSET) { 1315 for (data.big = 0x00; data.big < 0xef; data.big++) { 1316 data.count = 0; 1317 data.bis = 0xff; 1318 1319 hci_conn_hash_list_state(hdev, bis_list, ISO_LINK, 1320 BT_BOUND, &data); 1321 if (!data.count) 1322 break; 1323 } 1324 1325 if (data.big == 0xef) 1326 return -EADDRNOTAVAIL; 1327 1328 /* Update BIG */ 1329 qos->big = data.big; 1330 } 1331 1332 return 0; 1333 } 1334 1335 static int qos_set_bis(struct hci_dev *hdev, struct bt_iso_qos *qos) 1336 { 1337 struct iso_list_data data; 1338 1339 /* Allocate BIS if not set */ 1340 if (qos->bis == BT_ISO_QOS_BIS_UNSET) { 1341 /* Find an unused adv set to advertise BIS, skip instance 0x00 1342 * since it is reserved as general purpose set. 1343 */ 1344 for (data.bis = 0x01; data.bis < hdev->le_num_of_adv_sets; 1345 data.bis++) { 1346 data.count = 0; 1347 1348 hci_conn_hash_list_state(hdev, bis_list, ISO_LINK, 1349 BT_BOUND, &data); 1350 if (!data.count) 1351 break; 1352 } 1353 1354 if (data.bis == hdev->le_num_of_adv_sets) 1355 return -EADDRNOTAVAIL; 1356 1357 /* Update BIS */ 1358 qos->bis = data.bis; 1359 } 1360 1361 return 0; 1362 } 1363 1364 /* This function requires the caller holds hdev->lock */ 1365 static struct hci_conn *hci_add_bis(struct hci_dev *hdev, bdaddr_t *dst, 1366 struct bt_iso_qos *qos) 1367 { 1368 struct hci_conn *conn; 1369 struct iso_list_data data; 1370 int err; 1371 1372 /* Let's make sure that le is enabled.*/ 1373 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED)) { 1374 if (lmp_le_capable(hdev)) 1375 return ERR_PTR(-ECONNREFUSED); 1376 return ERR_PTR(-EOPNOTSUPP); 1377 } 1378 1379 err = qos_set_big(hdev, qos); 1380 if (err) 1381 return ERR_PTR(err); 1382 1383 err = qos_set_bis(hdev, qos); 1384 if (err) 1385 return ERR_PTR(err); 1386 1387 data.big = qos->big; 1388 data.bis = qos->bis; 1389 data.count = 0; 1390 1391 /* Check if there is already a matching BIG/BIS */ 1392 hci_conn_hash_list_state(hdev, bis_list, ISO_LINK, BT_BOUND, &data); 1393 if (data.count) 1394 return ERR_PTR(-EADDRINUSE); 1395 1396 conn = hci_conn_hash_lookup_bis(hdev, dst, qos->big, qos->bis); 1397 if (conn) 1398 return ERR_PTR(-EADDRINUSE); 1399 1400 conn = hci_conn_add(hdev, ISO_LINK, dst, HCI_ROLE_MASTER); 1401 if (!conn) 1402 return ERR_PTR(-ENOMEM); 1403 1404 set_bit(HCI_CONN_PER_ADV, &conn->flags); 1405 conn->state = BT_CONNECT; 1406 1407 hci_conn_hold(conn); 1408 return conn; 1409 } 1410 1411 /* This function requires the caller holds hdev->lock */ 1412 struct hci_conn *hci_connect_le_scan(struct hci_dev *hdev, bdaddr_t *dst, 1413 u8 dst_type, u8 sec_level, 1414 u16 conn_timeout, 1415 enum conn_reasons conn_reason) 1416 { 1417 struct hci_conn *conn; 1418 1419 /* Let's make sure that le is enabled.*/ 1420 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED)) { 1421 if (lmp_le_capable(hdev)) 1422 return ERR_PTR(-ECONNREFUSED); 1423 1424 return ERR_PTR(-EOPNOTSUPP); 1425 } 1426 1427 /* Some devices send ATT messages as soon as the physical link is 1428 * established. To be able to handle these ATT messages, the user- 1429 * space first establishes the connection and then starts the pairing 1430 * process. 1431 * 1432 * So if a hci_conn object already exists for the following connection 1433 * attempt, we simply update pending_sec_level and auth_type fields 1434 * and return the object found. 1435 */ 1436 conn = hci_conn_hash_lookup_le(hdev, dst, dst_type); 1437 if (conn) { 1438 if (conn->pending_sec_level < sec_level) 1439 conn->pending_sec_level = sec_level; 1440 goto done; 1441 } 1442 1443 BT_DBG("requesting refresh of dst_addr"); 1444 1445 conn = hci_conn_add(hdev, LE_LINK, dst, HCI_ROLE_MASTER); 1446 if (!conn) 1447 return ERR_PTR(-ENOMEM); 1448 1449 if (hci_explicit_conn_params_set(hdev, dst, dst_type) < 0) { 1450 hci_conn_del(conn); 1451 return ERR_PTR(-EBUSY); 1452 } 1453 1454 conn->state = BT_CONNECT; 1455 set_bit(HCI_CONN_SCANNING, &conn->flags); 1456 conn->dst_type = dst_type; 1457 conn->sec_level = BT_SECURITY_LOW; 1458 conn->pending_sec_level = sec_level; 1459 conn->conn_timeout = conn_timeout; 1460 conn->conn_reason = conn_reason; 1461 1462 hci_update_passive_scan(hdev); 1463 1464 done: 1465 hci_conn_hold(conn); 1466 return conn; 1467 } 1468 1469 struct hci_conn *hci_connect_acl(struct hci_dev *hdev, bdaddr_t *dst, 1470 u8 sec_level, u8 auth_type, 1471 enum conn_reasons conn_reason) 1472 { 1473 struct hci_conn *acl; 1474 1475 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) { 1476 if (lmp_bredr_capable(hdev)) 1477 return ERR_PTR(-ECONNREFUSED); 1478 1479 return ERR_PTR(-EOPNOTSUPP); 1480 } 1481 1482 acl = hci_conn_hash_lookup_ba(hdev, ACL_LINK, dst); 1483 if (!acl) { 1484 acl = hci_conn_add(hdev, ACL_LINK, dst, HCI_ROLE_MASTER); 1485 if (!acl) 1486 return ERR_PTR(-ENOMEM); 1487 } 1488 1489 hci_conn_hold(acl); 1490 1491 acl->conn_reason = conn_reason; 1492 if (acl->state == BT_OPEN || acl->state == BT_CLOSED) { 1493 acl->sec_level = BT_SECURITY_LOW; 1494 acl->pending_sec_level = sec_level; 1495 acl->auth_type = auth_type; 1496 hci_acl_create_connection(acl); 1497 } 1498 1499 return acl; 1500 } 1501 1502 struct hci_conn *hci_connect_sco(struct hci_dev *hdev, int type, bdaddr_t *dst, 1503 __u16 setting, struct bt_codec *codec) 1504 { 1505 struct hci_conn *acl; 1506 struct hci_conn *sco; 1507 1508 acl = hci_connect_acl(hdev, dst, BT_SECURITY_LOW, HCI_AT_NO_BONDING, 1509 CONN_REASON_SCO_CONNECT); 1510 if (IS_ERR(acl)) 1511 return acl; 1512 1513 sco = hci_conn_hash_lookup_ba(hdev, type, dst); 1514 if (!sco) { 1515 sco = hci_conn_add(hdev, type, dst, HCI_ROLE_MASTER); 1516 if (!sco) { 1517 hci_conn_drop(acl); 1518 return ERR_PTR(-ENOMEM); 1519 } 1520 } 1521 1522 acl->link = sco; 1523 sco->link = acl; 1524 1525 hci_conn_hold(sco); 1526 1527 sco->setting = setting; 1528 sco->codec = *codec; 1529 1530 if (acl->state == BT_CONNECTED && 1531 (sco->state == BT_OPEN || sco->state == BT_CLOSED)) { 1532 set_bit(HCI_CONN_POWER_SAVE, &acl->flags); 1533 hci_conn_enter_active_mode(acl, BT_POWER_FORCE_ACTIVE_ON); 1534 1535 if (test_bit(HCI_CONN_MODE_CHANGE_PEND, &acl->flags)) { 1536 /* defer SCO setup until mode change completed */ 1537 set_bit(HCI_CONN_SCO_SETUP_PEND, &acl->flags); 1538 return sco; 1539 } 1540 1541 hci_sco_setup(acl, 0x00); 1542 } 1543 1544 return sco; 1545 } 1546 1547 static void cis_add(struct iso_list_data *d, struct bt_iso_qos *qos) 1548 { 1549 struct hci_cis_params *cis = &d->pdu.cis[d->pdu.cp.num_cis]; 1550 1551 cis->cis_id = qos->cis; 1552 cis->c_sdu = cpu_to_le16(qos->out.sdu); 1553 cis->p_sdu = cpu_to_le16(qos->in.sdu); 1554 cis->c_phy = qos->out.phy; 1555 cis->p_phy = qos->in.phy; 1556 cis->c_rtn = qos->out.rtn; 1557 cis->p_rtn = qos->in.rtn; 1558 1559 d->pdu.cp.num_cis++; 1560 } 1561 1562 static void cis_list(struct hci_conn *conn, void *data) 1563 { 1564 struct iso_list_data *d = data; 1565 1566 /* Skip if broadcast/ANY address */ 1567 if (!bacmp(&conn->dst, BDADDR_ANY)) 1568 return; 1569 1570 if (d->cig != conn->iso_qos.cig || d->cis == BT_ISO_QOS_CIS_UNSET || 1571 d->cis != conn->iso_qos.cis) 1572 return; 1573 1574 d->count++; 1575 1576 if (d->pdu.cp.cig_id == BT_ISO_QOS_CIG_UNSET || 1577 d->count >= ARRAY_SIZE(d->pdu.cis)) 1578 return; 1579 1580 cis_add(d, &conn->iso_qos); 1581 } 1582 1583 static int hci_le_create_big(struct hci_conn *conn, struct bt_iso_qos *qos) 1584 { 1585 struct hci_dev *hdev = conn->hdev; 1586 struct hci_cp_le_create_big cp; 1587 1588 memset(&cp, 0, sizeof(cp)); 1589 1590 cp.handle = qos->big; 1591 cp.adv_handle = qos->bis; 1592 cp.num_bis = 0x01; 1593 hci_cpu_to_le24(qos->out.interval, cp.bis.sdu_interval); 1594 cp.bis.sdu = cpu_to_le16(qos->out.sdu); 1595 cp.bis.latency = cpu_to_le16(qos->out.latency); 1596 cp.bis.rtn = qos->out.rtn; 1597 cp.bis.phy = qos->out.phy; 1598 cp.bis.packing = qos->packing; 1599 cp.bis.framing = qos->framing; 1600 cp.bis.encryption = 0x00; 1601 memset(&cp.bis.bcode, 0, sizeof(cp.bis.bcode)); 1602 1603 return hci_send_cmd(hdev, HCI_OP_LE_CREATE_BIG, sizeof(cp), &cp); 1604 } 1605 1606 static bool hci_le_set_cig_params(struct hci_conn *conn, struct bt_iso_qos *qos) 1607 { 1608 struct hci_dev *hdev = conn->hdev; 1609 struct iso_list_data data; 1610 1611 memset(&data, 0, sizeof(data)); 1612 1613 /* Allocate a CIG if not set */ 1614 if (qos->cig == BT_ISO_QOS_CIG_UNSET) { 1615 for (data.cig = 0x00; data.cig < 0xff; data.cig++) { 1616 data.count = 0; 1617 data.cis = 0xff; 1618 1619 hci_conn_hash_list_state(hdev, cis_list, ISO_LINK, 1620 BT_BOUND, &data); 1621 if (data.count) 1622 continue; 1623 1624 hci_conn_hash_list_state(hdev, cis_list, ISO_LINK, 1625 BT_CONNECTED, &data); 1626 if (!data.count) 1627 break; 1628 } 1629 1630 if (data.cig == 0xff) 1631 return false; 1632 1633 /* Update CIG */ 1634 qos->cig = data.cig; 1635 } 1636 1637 data.pdu.cp.cig_id = qos->cig; 1638 hci_cpu_to_le24(qos->out.interval, data.pdu.cp.c_interval); 1639 hci_cpu_to_le24(qos->in.interval, data.pdu.cp.p_interval); 1640 data.pdu.cp.sca = qos->sca; 1641 data.pdu.cp.packing = qos->packing; 1642 data.pdu.cp.framing = qos->framing; 1643 data.pdu.cp.c_latency = cpu_to_le16(qos->out.latency); 1644 data.pdu.cp.p_latency = cpu_to_le16(qos->in.latency); 1645 1646 if (qos->cis != BT_ISO_QOS_CIS_UNSET) { 1647 data.count = 0; 1648 data.cig = qos->cig; 1649 data.cis = qos->cis; 1650 1651 hci_conn_hash_list_state(hdev, cis_list, ISO_LINK, BT_BOUND, 1652 &data); 1653 if (data.count) 1654 return false; 1655 1656 cis_add(&data, qos); 1657 } 1658 1659 /* Reprogram all CIS(s) with the same CIG */ 1660 for (data.cig = qos->cig, data.cis = 0x00; data.cis < 0x11; 1661 data.cis++) { 1662 data.count = 0; 1663 1664 hci_conn_hash_list_state(hdev, cis_list, ISO_LINK, BT_BOUND, 1665 &data); 1666 if (data.count) 1667 continue; 1668 1669 /* Allocate a CIS if not set */ 1670 if (qos->cis == BT_ISO_QOS_CIS_UNSET) { 1671 /* Update CIS */ 1672 qos->cis = data.cis; 1673 cis_add(&data, qos); 1674 } 1675 } 1676 1677 if (qos->cis == BT_ISO_QOS_CIS_UNSET || !data.pdu.cp.num_cis) 1678 return false; 1679 1680 if (hci_send_cmd(hdev, HCI_OP_LE_SET_CIG_PARAMS, 1681 sizeof(data.pdu.cp) + 1682 (data.pdu.cp.num_cis * sizeof(*data.pdu.cis)), 1683 &data.pdu) < 0) 1684 return false; 1685 1686 return true; 1687 } 1688 1689 struct hci_conn *hci_bind_cis(struct hci_dev *hdev, bdaddr_t *dst, 1690 __u8 dst_type, struct bt_iso_qos *qos) 1691 { 1692 struct hci_conn *cis; 1693 1694 cis = hci_conn_hash_lookup_cis(hdev, dst, dst_type); 1695 if (!cis) { 1696 cis = hci_conn_add(hdev, ISO_LINK, dst, HCI_ROLE_MASTER); 1697 if (!cis) 1698 return ERR_PTR(-ENOMEM); 1699 cis->cleanup = cis_cleanup; 1700 } 1701 1702 if (cis->state == BT_CONNECTED) 1703 return cis; 1704 1705 /* Check if CIS has been set and the settings matches */ 1706 if (cis->state == BT_BOUND && 1707 !memcmp(&cis->iso_qos, qos, sizeof(*qos))) 1708 return cis; 1709 1710 /* Update LINK PHYs according to QoS preference */ 1711 cis->le_tx_phy = qos->out.phy; 1712 cis->le_rx_phy = qos->in.phy; 1713 1714 /* If output interval is not set use the input interval as it cannot be 1715 * 0x000000. 1716 */ 1717 if (!qos->out.interval) 1718 qos->out.interval = qos->in.interval; 1719 1720 /* If input interval is not set use the output interval as it cannot be 1721 * 0x000000. 1722 */ 1723 if (!qos->in.interval) 1724 qos->in.interval = qos->out.interval; 1725 1726 /* If output latency is not set use the input latency as it cannot be 1727 * 0x0000. 1728 */ 1729 if (!qos->out.latency) 1730 qos->out.latency = qos->in.latency; 1731 1732 /* If input latency is not set use the output latency as it cannot be 1733 * 0x0000. 1734 */ 1735 if (!qos->in.latency) 1736 qos->in.latency = qos->out.latency; 1737 1738 /* Mirror PHYs that are disabled as SDU will be set to 0 */ 1739 if (!qos->in.phy) 1740 qos->in.phy = qos->out.phy; 1741 1742 if (!qos->out.phy) 1743 qos->out.phy = qos->in.phy; 1744 1745 if (!hci_le_set_cig_params(cis, qos)) { 1746 hci_conn_drop(cis); 1747 return ERR_PTR(-EINVAL); 1748 } 1749 1750 cis->iso_qos = *qos; 1751 cis->state = BT_BOUND; 1752 1753 return cis; 1754 } 1755 1756 bool hci_iso_setup_path(struct hci_conn *conn) 1757 { 1758 struct hci_dev *hdev = conn->hdev; 1759 struct hci_cp_le_setup_iso_path cmd; 1760 1761 memset(&cmd, 0, sizeof(cmd)); 1762 1763 if (conn->iso_qos.out.sdu) { 1764 cmd.handle = cpu_to_le16(conn->handle); 1765 cmd.direction = 0x00; /* Input (Host to Controller) */ 1766 cmd.path = 0x00; /* HCI path if enabled */ 1767 cmd.codec = 0x03; /* Transparent Data */ 1768 1769 if (hci_send_cmd(hdev, HCI_OP_LE_SETUP_ISO_PATH, sizeof(cmd), 1770 &cmd) < 0) 1771 return false; 1772 } 1773 1774 if (conn->iso_qos.in.sdu) { 1775 cmd.handle = cpu_to_le16(conn->handle); 1776 cmd.direction = 0x01; /* Output (Controller to Host) */ 1777 cmd.path = 0x00; /* HCI path if enabled */ 1778 cmd.codec = 0x03; /* Transparent Data */ 1779 1780 if (hci_send_cmd(hdev, HCI_OP_LE_SETUP_ISO_PATH, sizeof(cmd), 1781 &cmd) < 0) 1782 return false; 1783 } 1784 1785 return true; 1786 } 1787 1788 static int hci_create_cis_sync(struct hci_dev *hdev, void *data) 1789 { 1790 struct { 1791 struct hci_cp_le_create_cis cp; 1792 struct hci_cis cis[0x1f]; 1793 } cmd; 1794 struct hci_conn *conn = data; 1795 u8 cig; 1796 1797 memset(&cmd, 0, sizeof(cmd)); 1798 cmd.cis[0].acl_handle = cpu_to_le16(conn->link->handle); 1799 cmd.cis[0].cis_handle = cpu_to_le16(conn->handle); 1800 cmd.cp.num_cis++; 1801 cig = conn->iso_qos.cig; 1802 1803 hci_dev_lock(hdev); 1804 1805 rcu_read_lock(); 1806 1807 list_for_each_entry_rcu(conn, &hdev->conn_hash.list, list) { 1808 struct hci_cis *cis = &cmd.cis[cmd.cp.num_cis]; 1809 1810 if (conn == data || conn->type != ISO_LINK || 1811 conn->state == BT_CONNECTED || conn->iso_qos.cig != cig) 1812 continue; 1813 1814 /* Check if all CIS(s) belonging to a CIG are ready */ 1815 if (conn->link->state != BT_CONNECTED || 1816 conn->state != BT_CONNECT) { 1817 cmd.cp.num_cis = 0; 1818 break; 1819 } 1820 1821 /* Group all CIS with state BT_CONNECT since the spec don't 1822 * allow to send them individually: 1823 * 1824 * BLUETOOTH CORE SPECIFICATION Version 5.3 | Vol 4, Part E 1825 * page 2566: 1826 * 1827 * If the Host issues this command before all the 1828 * HCI_LE_CIS_Established events from the previous use of the 1829 * command have been generated, the Controller shall return the 1830 * error code Command Disallowed (0x0C). 1831 */ 1832 cis->acl_handle = cpu_to_le16(conn->link->handle); 1833 cis->cis_handle = cpu_to_le16(conn->handle); 1834 cmd.cp.num_cis++; 1835 } 1836 1837 rcu_read_unlock(); 1838 1839 hci_dev_unlock(hdev); 1840 1841 if (!cmd.cp.num_cis) 1842 return 0; 1843 1844 return hci_send_cmd(hdev, HCI_OP_LE_CREATE_CIS, sizeof(cmd.cp) + 1845 sizeof(cmd.cis[0]) * cmd.cp.num_cis, &cmd); 1846 } 1847 1848 int hci_le_create_cis(struct hci_conn *conn) 1849 { 1850 struct hci_conn *cis; 1851 struct hci_dev *hdev = conn->hdev; 1852 int err; 1853 1854 switch (conn->type) { 1855 case LE_LINK: 1856 if (!conn->link || conn->state != BT_CONNECTED) 1857 return -EINVAL; 1858 cis = conn->link; 1859 break; 1860 case ISO_LINK: 1861 cis = conn; 1862 break; 1863 default: 1864 return -EINVAL; 1865 } 1866 1867 if (cis->state == BT_CONNECT) 1868 return 0; 1869 1870 /* Queue Create CIS */ 1871 err = hci_cmd_sync_queue(hdev, hci_create_cis_sync, cis, NULL); 1872 if (err) 1873 return err; 1874 1875 cis->state = BT_CONNECT; 1876 1877 return 0; 1878 } 1879 1880 static void hci_iso_qos_setup(struct hci_dev *hdev, struct hci_conn *conn, 1881 struct bt_iso_io_qos *qos, __u8 phy) 1882 { 1883 /* Only set MTU if PHY is enabled */ 1884 if (!qos->sdu && qos->phy) { 1885 if (hdev->iso_mtu > 0) 1886 qos->sdu = hdev->iso_mtu; 1887 else if (hdev->le_mtu > 0) 1888 qos->sdu = hdev->le_mtu; 1889 else 1890 qos->sdu = hdev->acl_mtu; 1891 } 1892 1893 /* Use the same PHY as ACL if set to any */ 1894 if (qos->phy == BT_ISO_PHY_ANY) 1895 qos->phy = phy; 1896 1897 /* Use LE ACL connection interval if not set */ 1898 if (!qos->interval) 1899 /* ACL interval unit in 1.25 ms to us */ 1900 qos->interval = conn->le_conn_interval * 1250; 1901 1902 /* Use LE ACL connection latency if not set */ 1903 if (!qos->latency) 1904 qos->latency = conn->le_conn_latency; 1905 } 1906 1907 static struct hci_conn *hci_bind_bis(struct hci_conn *conn, 1908 struct bt_iso_qos *qos) 1909 { 1910 /* Update LINK PHYs according to QoS preference */ 1911 conn->le_tx_phy = qos->out.phy; 1912 conn->le_tx_phy = qos->out.phy; 1913 conn->iso_qos = *qos; 1914 conn->state = BT_BOUND; 1915 1916 return conn; 1917 } 1918 1919 static int create_big_sync(struct hci_dev *hdev, void *data) 1920 { 1921 struct hci_conn *conn = data; 1922 struct bt_iso_qos *qos = &conn->iso_qos; 1923 u16 interval, sync_interval = 0; 1924 u32 flags = 0; 1925 int err; 1926 1927 if (qos->out.phy == 0x02) 1928 flags |= MGMT_ADV_FLAG_SEC_2M; 1929 1930 /* Align intervals */ 1931 interval = qos->out.interval / 1250; 1932 1933 if (qos->bis) 1934 sync_interval = qos->sync_interval * 1600; 1935 1936 err = hci_start_per_adv_sync(hdev, qos->bis, conn->le_per_adv_data_len, 1937 conn->le_per_adv_data, flags, interval, 1938 interval, sync_interval); 1939 if (err) 1940 return err; 1941 1942 return hci_le_create_big(conn, &conn->iso_qos); 1943 } 1944 1945 static void create_pa_complete(struct hci_dev *hdev, void *data, int err) 1946 { 1947 struct hci_cp_le_pa_create_sync *cp = data; 1948 1949 bt_dev_dbg(hdev, ""); 1950 1951 if (err) 1952 bt_dev_err(hdev, "Unable to create PA: %d", err); 1953 1954 kfree(cp); 1955 } 1956 1957 static int create_pa_sync(struct hci_dev *hdev, void *data) 1958 { 1959 struct hci_cp_le_pa_create_sync *cp = data; 1960 int err; 1961 1962 err = __hci_cmd_sync_status(hdev, HCI_OP_LE_PA_CREATE_SYNC, 1963 sizeof(*cp), cp, HCI_CMD_TIMEOUT); 1964 if (err) { 1965 hci_dev_clear_flag(hdev, HCI_PA_SYNC); 1966 return err; 1967 } 1968 1969 return hci_update_passive_scan_sync(hdev); 1970 } 1971 1972 int hci_pa_create_sync(struct hci_dev *hdev, bdaddr_t *dst, __u8 dst_type, 1973 __u8 sid) 1974 { 1975 struct hci_cp_le_pa_create_sync *cp; 1976 1977 if (hci_dev_test_and_set_flag(hdev, HCI_PA_SYNC)) 1978 return -EBUSY; 1979 1980 cp = kmalloc(sizeof(*cp), GFP_KERNEL); 1981 if (!cp) { 1982 hci_dev_clear_flag(hdev, HCI_PA_SYNC); 1983 return -ENOMEM; 1984 } 1985 1986 /* Convert from ISO socket address type to HCI address type */ 1987 if (dst_type == BDADDR_LE_PUBLIC) 1988 dst_type = ADDR_LE_DEV_PUBLIC; 1989 else 1990 dst_type = ADDR_LE_DEV_RANDOM; 1991 1992 memset(cp, 0, sizeof(*cp)); 1993 cp->sid = sid; 1994 cp->addr_type = dst_type; 1995 bacpy(&cp->addr, dst); 1996 1997 /* Queue start pa_create_sync and scan */ 1998 return hci_cmd_sync_queue(hdev, create_pa_sync, cp, create_pa_complete); 1999 } 2000 2001 int hci_le_big_create_sync(struct hci_dev *hdev, struct bt_iso_qos *qos, 2002 __u16 sync_handle, __u8 num_bis, __u8 bis[]) 2003 { 2004 struct _packed { 2005 struct hci_cp_le_big_create_sync cp; 2006 __u8 bis[0x11]; 2007 } pdu; 2008 int err; 2009 2010 if (num_bis > sizeof(pdu.bis)) 2011 return -EINVAL; 2012 2013 err = qos_set_big(hdev, qos); 2014 if (err) 2015 return err; 2016 2017 memset(&pdu, 0, sizeof(pdu)); 2018 pdu.cp.handle = qos->big; 2019 pdu.cp.sync_handle = cpu_to_le16(sync_handle); 2020 pdu.cp.num_bis = num_bis; 2021 memcpy(pdu.bis, bis, num_bis); 2022 2023 return hci_send_cmd(hdev, HCI_OP_LE_BIG_CREATE_SYNC, 2024 sizeof(pdu.cp) + num_bis, &pdu); 2025 } 2026 2027 static void create_big_complete(struct hci_dev *hdev, void *data, int err) 2028 { 2029 struct hci_conn *conn = data; 2030 2031 bt_dev_dbg(hdev, "conn %p", conn); 2032 2033 if (err) { 2034 bt_dev_err(hdev, "Unable to create BIG: %d", err); 2035 hci_connect_cfm(conn, err); 2036 hci_conn_del(conn); 2037 } 2038 } 2039 2040 struct hci_conn *hci_connect_bis(struct hci_dev *hdev, bdaddr_t *dst, 2041 __u8 dst_type, struct bt_iso_qos *qos, 2042 __u8 base_len, __u8 *base) 2043 { 2044 struct hci_conn *conn; 2045 int err; 2046 2047 /* We need hci_conn object using the BDADDR_ANY as dst */ 2048 conn = hci_add_bis(hdev, dst, qos); 2049 if (IS_ERR(conn)) 2050 return conn; 2051 2052 conn = hci_bind_bis(conn, qos); 2053 if (!conn) { 2054 hci_conn_drop(conn); 2055 return ERR_PTR(-ENOMEM); 2056 } 2057 2058 /* Add Basic Announcement into Peridic Adv Data if BASE is set */ 2059 if (base_len && base) { 2060 base_len = eir_append_service_data(conn->le_per_adv_data, 0, 2061 0x1851, base, base_len); 2062 conn->le_per_adv_data_len = base_len; 2063 } 2064 2065 /* Queue start periodic advertising and create BIG */ 2066 err = hci_cmd_sync_queue(hdev, create_big_sync, conn, 2067 create_big_complete); 2068 if (err < 0) { 2069 hci_conn_drop(conn); 2070 return ERR_PTR(err); 2071 } 2072 2073 hci_iso_qos_setup(hdev, conn, &qos->out, 2074 conn->le_tx_phy ? conn->le_tx_phy : 2075 hdev->le_tx_def_phys); 2076 2077 return conn; 2078 } 2079 2080 struct hci_conn *hci_connect_cis(struct hci_dev *hdev, bdaddr_t *dst, 2081 __u8 dst_type, struct bt_iso_qos *qos) 2082 { 2083 struct hci_conn *le; 2084 struct hci_conn *cis; 2085 2086 /* Convert from ISO socket address type to HCI address type */ 2087 if (dst_type == BDADDR_LE_PUBLIC) 2088 dst_type = ADDR_LE_DEV_PUBLIC; 2089 else 2090 dst_type = ADDR_LE_DEV_RANDOM; 2091 2092 if (hci_dev_test_flag(hdev, HCI_ADVERTISING)) 2093 le = hci_connect_le(hdev, dst, dst_type, false, 2094 BT_SECURITY_LOW, 2095 HCI_LE_CONN_TIMEOUT, 2096 HCI_ROLE_SLAVE); 2097 else 2098 le = hci_connect_le_scan(hdev, dst, dst_type, 2099 BT_SECURITY_LOW, 2100 HCI_LE_CONN_TIMEOUT, 2101 CONN_REASON_ISO_CONNECT); 2102 if (IS_ERR(le)) 2103 return le; 2104 2105 hci_iso_qos_setup(hdev, le, &qos->out, 2106 le->le_tx_phy ? le->le_tx_phy : hdev->le_tx_def_phys); 2107 hci_iso_qos_setup(hdev, le, &qos->in, 2108 le->le_rx_phy ? le->le_rx_phy : hdev->le_rx_def_phys); 2109 2110 cis = hci_bind_cis(hdev, dst, dst_type, qos); 2111 if (IS_ERR(cis)) { 2112 hci_conn_drop(le); 2113 return cis; 2114 } 2115 2116 le->link = cis; 2117 cis->link = le; 2118 2119 hci_conn_hold(cis); 2120 2121 /* If LE is already connected and CIS handle is already set proceed to 2122 * Create CIS immediately. 2123 */ 2124 if (le->state == BT_CONNECTED && cis->handle != HCI_CONN_HANDLE_UNSET) 2125 hci_le_create_cis(le); 2126 2127 return cis; 2128 } 2129 2130 /* Check link security requirement */ 2131 int hci_conn_check_link_mode(struct hci_conn *conn) 2132 { 2133 BT_DBG("hcon %p", conn); 2134 2135 /* In Secure Connections Only mode, it is required that Secure 2136 * Connections is used and the link is encrypted with AES-CCM 2137 * using a P-256 authenticated combination key. 2138 */ 2139 if (hci_dev_test_flag(conn->hdev, HCI_SC_ONLY)) { 2140 if (!hci_conn_sc_enabled(conn) || 2141 !test_bit(HCI_CONN_AES_CCM, &conn->flags) || 2142 conn->key_type != HCI_LK_AUTH_COMBINATION_P256) 2143 return 0; 2144 } 2145 2146 /* AES encryption is required for Level 4: 2147 * 2148 * BLUETOOTH CORE SPECIFICATION Version 5.2 | Vol 3, Part C 2149 * page 1319: 2150 * 2151 * 128-bit equivalent strength for link and encryption keys 2152 * required using FIPS approved algorithms (E0 not allowed, 2153 * SAFER+ not allowed, and P-192 not allowed; encryption key 2154 * not shortened) 2155 */ 2156 if (conn->sec_level == BT_SECURITY_FIPS && 2157 !test_bit(HCI_CONN_AES_CCM, &conn->flags)) { 2158 bt_dev_err(conn->hdev, 2159 "Invalid security: Missing AES-CCM usage"); 2160 return 0; 2161 } 2162 2163 if (hci_conn_ssp_enabled(conn) && 2164 !test_bit(HCI_CONN_ENCRYPT, &conn->flags)) 2165 return 0; 2166 2167 return 1; 2168 } 2169 2170 /* Authenticate remote device */ 2171 static int hci_conn_auth(struct hci_conn *conn, __u8 sec_level, __u8 auth_type) 2172 { 2173 BT_DBG("hcon %p", conn); 2174 2175 if (conn->pending_sec_level > sec_level) 2176 sec_level = conn->pending_sec_level; 2177 2178 if (sec_level > conn->sec_level) 2179 conn->pending_sec_level = sec_level; 2180 else if (test_bit(HCI_CONN_AUTH, &conn->flags)) 2181 return 1; 2182 2183 /* Make sure we preserve an existing MITM requirement*/ 2184 auth_type |= (conn->auth_type & 0x01); 2185 2186 conn->auth_type = auth_type; 2187 2188 if (!test_and_set_bit(HCI_CONN_AUTH_PEND, &conn->flags)) { 2189 struct hci_cp_auth_requested cp; 2190 2191 cp.handle = cpu_to_le16(conn->handle); 2192 hci_send_cmd(conn->hdev, HCI_OP_AUTH_REQUESTED, 2193 sizeof(cp), &cp); 2194 2195 /* If we're already encrypted set the REAUTH_PEND flag, 2196 * otherwise set the ENCRYPT_PEND. 2197 */ 2198 if (test_bit(HCI_CONN_ENCRYPT, &conn->flags)) 2199 set_bit(HCI_CONN_REAUTH_PEND, &conn->flags); 2200 else 2201 set_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags); 2202 } 2203 2204 return 0; 2205 } 2206 2207 /* Encrypt the link */ 2208 static void hci_conn_encrypt(struct hci_conn *conn) 2209 { 2210 BT_DBG("hcon %p", conn); 2211 2212 if (!test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags)) { 2213 struct hci_cp_set_conn_encrypt cp; 2214 cp.handle = cpu_to_le16(conn->handle); 2215 cp.encrypt = 0x01; 2216 hci_send_cmd(conn->hdev, HCI_OP_SET_CONN_ENCRYPT, sizeof(cp), 2217 &cp); 2218 } 2219 } 2220 2221 /* Enable security */ 2222 int hci_conn_security(struct hci_conn *conn, __u8 sec_level, __u8 auth_type, 2223 bool initiator) 2224 { 2225 BT_DBG("hcon %p", conn); 2226 2227 if (conn->type == LE_LINK) 2228 return smp_conn_security(conn, sec_level); 2229 2230 /* For sdp we don't need the link key. */ 2231 if (sec_level == BT_SECURITY_SDP) 2232 return 1; 2233 2234 /* For non 2.1 devices and low security level we don't need the link 2235 key. */ 2236 if (sec_level == BT_SECURITY_LOW && !hci_conn_ssp_enabled(conn)) 2237 return 1; 2238 2239 /* For other security levels we need the link key. */ 2240 if (!test_bit(HCI_CONN_AUTH, &conn->flags)) 2241 goto auth; 2242 2243 /* An authenticated FIPS approved combination key has sufficient 2244 * security for security level 4. */ 2245 if (conn->key_type == HCI_LK_AUTH_COMBINATION_P256 && 2246 sec_level == BT_SECURITY_FIPS) 2247 goto encrypt; 2248 2249 /* An authenticated combination key has sufficient security for 2250 security level 3. */ 2251 if ((conn->key_type == HCI_LK_AUTH_COMBINATION_P192 || 2252 conn->key_type == HCI_LK_AUTH_COMBINATION_P256) && 2253 sec_level == BT_SECURITY_HIGH) 2254 goto encrypt; 2255 2256 /* An unauthenticated combination key has sufficient security for 2257 security level 1 and 2. */ 2258 if ((conn->key_type == HCI_LK_UNAUTH_COMBINATION_P192 || 2259 conn->key_type == HCI_LK_UNAUTH_COMBINATION_P256) && 2260 (sec_level == BT_SECURITY_MEDIUM || sec_level == BT_SECURITY_LOW)) 2261 goto encrypt; 2262 2263 /* A combination key has always sufficient security for the security 2264 levels 1 or 2. High security level requires the combination key 2265 is generated using maximum PIN code length (16). 2266 For pre 2.1 units. */ 2267 if (conn->key_type == HCI_LK_COMBINATION && 2268 (sec_level == BT_SECURITY_MEDIUM || sec_level == BT_SECURITY_LOW || 2269 conn->pin_length == 16)) 2270 goto encrypt; 2271 2272 auth: 2273 if (test_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags)) 2274 return 0; 2275 2276 if (initiator) 2277 set_bit(HCI_CONN_AUTH_INITIATOR, &conn->flags); 2278 2279 if (!hci_conn_auth(conn, sec_level, auth_type)) 2280 return 0; 2281 2282 encrypt: 2283 if (test_bit(HCI_CONN_ENCRYPT, &conn->flags)) { 2284 /* Ensure that the encryption key size has been read, 2285 * otherwise stall the upper layer responses. 2286 */ 2287 if (!conn->enc_key_size) 2288 return 0; 2289 2290 /* Nothing else needed, all requirements are met */ 2291 return 1; 2292 } 2293 2294 hci_conn_encrypt(conn); 2295 return 0; 2296 } 2297 EXPORT_SYMBOL(hci_conn_security); 2298 2299 /* Check secure link requirement */ 2300 int hci_conn_check_secure(struct hci_conn *conn, __u8 sec_level) 2301 { 2302 BT_DBG("hcon %p", conn); 2303 2304 /* Accept if non-secure or higher security level is required */ 2305 if (sec_level != BT_SECURITY_HIGH && sec_level != BT_SECURITY_FIPS) 2306 return 1; 2307 2308 /* Accept if secure or higher security level is already present */ 2309 if (conn->sec_level == BT_SECURITY_HIGH || 2310 conn->sec_level == BT_SECURITY_FIPS) 2311 return 1; 2312 2313 /* Reject not secure link */ 2314 return 0; 2315 } 2316 EXPORT_SYMBOL(hci_conn_check_secure); 2317 2318 /* Switch role */ 2319 int hci_conn_switch_role(struct hci_conn *conn, __u8 role) 2320 { 2321 BT_DBG("hcon %p", conn); 2322 2323 if (role == conn->role) 2324 return 1; 2325 2326 if (!test_and_set_bit(HCI_CONN_RSWITCH_PEND, &conn->flags)) { 2327 struct hci_cp_switch_role cp; 2328 bacpy(&cp.bdaddr, &conn->dst); 2329 cp.role = role; 2330 hci_send_cmd(conn->hdev, HCI_OP_SWITCH_ROLE, sizeof(cp), &cp); 2331 } 2332 2333 return 0; 2334 } 2335 EXPORT_SYMBOL(hci_conn_switch_role); 2336 2337 /* Enter active mode */ 2338 void hci_conn_enter_active_mode(struct hci_conn *conn, __u8 force_active) 2339 { 2340 struct hci_dev *hdev = conn->hdev; 2341 2342 BT_DBG("hcon %p mode %d", conn, conn->mode); 2343 2344 if (conn->mode != HCI_CM_SNIFF) 2345 goto timer; 2346 2347 if (!test_bit(HCI_CONN_POWER_SAVE, &conn->flags) && !force_active) 2348 goto timer; 2349 2350 if (!test_and_set_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->flags)) { 2351 struct hci_cp_exit_sniff_mode cp; 2352 cp.handle = cpu_to_le16(conn->handle); 2353 hci_send_cmd(hdev, HCI_OP_EXIT_SNIFF_MODE, sizeof(cp), &cp); 2354 } 2355 2356 timer: 2357 if (hdev->idle_timeout > 0) 2358 queue_delayed_work(hdev->workqueue, &conn->idle_work, 2359 msecs_to_jiffies(hdev->idle_timeout)); 2360 } 2361 2362 /* Drop all connection on the device */ 2363 void hci_conn_hash_flush(struct hci_dev *hdev) 2364 { 2365 struct hci_conn_hash *h = &hdev->conn_hash; 2366 struct hci_conn *c, *n; 2367 2368 BT_DBG("hdev %s", hdev->name); 2369 2370 list_for_each_entry_safe(c, n, &h->list, list) { 2371 c->state = BT_CLOSED; 2372 2373 hci_disconn_cfm(c, HCI_ERROR_LOCAL_HOST_TERM); 2374 hci_conn_del(c); 2375 } 2376 } 2377 2378 /* Check pending connect attempts */ 2379 void hci_conn_check_pending(struct hci_dev *hdev) 2380 { 2381 struct hci_conn *conn; 2382 2383 BT_DBG("hdev %s", hdev->name); 2384 2385 hci_dev_lock(hdev); 2386 2387 conn = hci_conn_hash_lookup_state(hdev, ACL_LINK, BT_CONNECT2); 2388 if (conn) 2389 hci_acl_create_connection(conn); 2390 2391 hci_dev_unlock(hdev); 2392 } 2393 2394 static u32 get_link_mode(struct hci_conn *conn) 2395 { 2396 u32 link_mode = 0; 2397 2398 if (conn->role == HCI_ROLE_MASTER) 2399 link_mode |= HCI_LM_MASTER; 2400 2401 if (test_bit(HCI_CONN_ENCRYPT, &conn->flags)) 2402 link_mode |= HCI_LM_ENCRYPT; 2403 2404 if (test_bit(HCI_CONN_AUTH, &conn->flags)) 2405 link_mode |= HCI_LM_AUTH; 2406 2407 if (test_bit(HCI_CONN_SECURE, &conn->flags)) 2408 link_mode |= HCI_LM_SECURE; 2409 2410 if (test_bit(HCI_CONN_FIPS, &conn->flags)) 2411 link_mode |= HCI_LM_FIPS; 2412 2413 return link_mode; 2414 } 2415 2416 int hci_get_conn_list(void __user *arg) 2417 { 2418 struct hci_conn *c; 2419 struct hci_conn_list_req req, *cl; 2420 struct hci_conn_info *ci; 2421 struct hci_dev *hdev; 2422 int n = 0, size, err; 2423 2424 if (copy_from_user(&req, arg, sizeof(req))) 2425 return -EFAULT; 2426 2427 if (!req.conn_num || req.conn_num > (PAGE_SIZE * 2) / sizeof(*ci)) 2428 return -EINVAL; 2429 2430 size = sizeof(req) + req.conn_num * sizeof(*ci); 2431 2432 cl = kmalloc(size, GFP_KERNEL); 2433 if (!cl) 2434 return -ENOMEM; 2435 2436 hdev = hci_dev_get(req.dev_id); 2437 if (!hdev) { 2438 kfree(cl); 2439 return -ENODEV; 2440 } 2441 2442 ci = cl->conn_info; 2443 2444 hci_dev_lock(hdev); 2445 list_for_each_entry(c, &hdev->conn_hash.list, list) { 2446 bacpy(&(ci + n)->bdaddr, &c->dst); 2447 (ci + n)->handle = c->handle; 2448 (ci + n)->type = c->type; 2449 (ci + n)->out = c->out; 2450 (ci + n)->state = c->state; 2451 (ci + n)->link_mode = get_link_mode(c); 2452 if (++n >= req.conn_num) 2453 break; 2454 } 2455 hci_dev_unlock(hdev); 2456 2457 cl->dev_id = hdev->id; 2458 cl->conn_num = n; 2459 size = sizeof(req) + n * sizeof(*ci); 2460 2461 hci_dev_put(hdev); 2462 2463 err = copy_to_user(arg, cl, size); 2464 kfree(cl); 2465 2466 return err ? -EFAULT : 0; 2467 } 2468 2469 int hci_get_conn_info(struct hci_dev *hdev, void __user *arg) 2470 { 2471 struct hci_conn_info_req req; 2472 struct hci_conn_info ci; 2473 struct hci_conn *conn; 2474 char __user *ptr = arg + sizeof(req); 2475 2476 if (copy_from_user(&req, arg, sizeof(req))) 2477 return -EFAULT; 2478 2479 hci_dev_lock(hdev); 2480 conn = hci_conn_hash_lookup_ba(hdev, req.type, &req.bdaddr); 2481 if (conn) { 2482 bacpy(&ci.bdaddr, &conn->dst); 2483 ci.handle = conn->handle; 2484 ci.type = conn->type; 2485 ci.out = conn->out; 2486 ci.state = conn->state; 2487 ci.link_mode = get_link_mode(conn); 2488 } 2489 hci_dev_unlock(hdev); 2490 2491 if (!conn) 2492 return -ENOENT; 2493 2494 return copy_to_user(ptr, &ci, sizeof(ci)) ? -EFAULT : 0; 2495 } 2496 2497 int hci_get_auth_info(struct hci_dev *hdev, void __user *arg) 2498 { 2499 struct hci_auth_info_req req; 2500 struct hci_conn *conn; 2501 2502 if (copy_from_user(&req, arg, sizeof(req))) 2503 return -EFAULT; 2504 2505 hci_dev_lock(hdev); 2506 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &req.bdaddr); 2507 if (conn) 2508 req.type = conn->auth_type; 2509 hci_dev_unlock(hdev); 2510 2511 if (!conn) 2512 return -ENOENT; 2513 2514 return copy_to_user(arg, &req, sizeof(req)) ? -EFAULT : 0; 2515 } 2516 2517 struct hci_chan *hci_chan_create(struct hci_conn *conn) 2518 { 2519 struct hci_dev *hdev = conn->hdev; 2520 struct hci_chan *chan; 2521 2522 BT_DBG("%s hcon %p", hdev->name, conn); 2523 2524 if (test_bit(HCI_CONN_DROP, &conn->flags)) { 2525 BT_DBG("Refusing to create new hci_chan"); 2526 return NULL; 2527 } 2528 2529 chan = kzalloc(sizeof(*chan), GFP_KERNEL); 2530 if (!chan) 2531 return NULL; 2532 2533 chan->conn = hci_conn_get(conn); 2534 skb_queue_head_init(&chan->data_q); 2535 chan->state = BT_CONNECTED; 2536 2537 list_add_rcu(&chan->list, &conn->chan_list); 2538 2539 return chan; 2540 } 2541 2542 void hci_chan_del(struct hci_chan *chan) 2543 { 2544 struct hci_conn *conn = chan->conn; 2545 struct hci_dev *hdev = conn->hdev; 2546 2547 BT_DBG("%s hcon %p chan %p", hdev->name, conn, chan); 2548 2549 list_del_rcu(&chan->list); 2550 2551 synchronize_rcu(); 2552 2553 /* Prevent new hci_chan's to be created for this hci_conn */ 2554 set_bit(HCI_CONN_DROP, &conn->flags); 2555 2556 hci_conn_put(conn); 2557 2558 skb_queue_purge(&chan->data_q); 2559 kfree(chan); 2560 } 2561 2562 void hci_chan_list_flush(struct hci_conn *conn) 2563 { 2564 struct hci_chan *chan, *n; 2565 2566 BT_DBG("hcon %p", conn); 2567 2568 list_for_each_entry_safe(chan, n, &conn->chan_list, list) 2569 hci_chan_del(chan); 2570 } 2571 2572 static struct hci_chan *__hci_chan_lookup_handle(struct hci_conn *hcon, 2573 __u16 handle) 2574 { 2575 struct hci_chan *hchan; 2576 2577 list_for_each_entry(hchan, &hcon->chan_list, list) { 2578 if (hchan->handle == handle) 2579 return hchan; 2580 } 2581 2582 return NULL; 2583 } 2584 2585 struct hci_chan *hci_chan_lookup_handle(struct hci_dev *hdev, __u16 handle) 2586 { 2587 struct hci_conn_hash *h = &hdev->conn_hash; 2588 struct hci_conn *hcon; 2589 struct hci_chan *hchan = NULL; 2590 2591 rcu_read_lock(); 2592 2593 list_for_each_entry_rcu(hcon, &h->list, list) { 2594 hchan = __hci_chan_lookup_handle(hcon, handle); 2595 if (hchan) 2596 break; 2597 } 2598 2599 rcu_read_unlock(); 2600 2601 return hchan; 2602 } 2603 2604 u32 hci_conn_get_phy(struct hci_conn *conn) 2605 { 2606 u32 phys = 0; 2607 2608 /* BLUETOOTH CORE SPECIFICATION Version 5.2 | Vol 2, Part B page 471: 2609 * Table 6.2: Packets defined for synchronous, asynchronous, and 2610 * CPB logical transport types. 2611 */ 2612 switch (conn->type) { 2613 case SCO_LINK: 2614 /* SCO logical transport (1 Mb/s): 2615 * HV1, HV2, HV3 and DV. 2616 */ 2617 phys |= BT_PHY_BR_1M_1SLOT; 2618 2619 break; 2620 2621 case ACL_LINK: 2622 /* ACL logical transport (1 Mb/s) ptt=0: 2623 * DH1, DM3, DH3, DM5 and DH5. 2624 */ 2625 phys |= BT_PHY_BR_1M_1SLOT; 2626 2627 if (conn->pkt_type & (HCI_DM3 | HCI_DH3)) 2628 phys |= BT_PHY_BR_1M_3SLOT; 2629 2630 if (conn->pkt_type & (HCI_DM5 | HCI_DH5)) 2631 phys |= BT_PHY_BR_1M_5SLOT; 2632 2633 /* ACL logical transport (2 Mb/s) ptt=1: 2634 * 2-DH1, 2-DH3 and 2-DH5. 2635 */ 2636 if (!(conn->pkt_type & HCI_2DH1)) 2637 phys |= BT_PHY_EDR_2M_1SLOT; 2638 2639 if (!(conn->pkt_type & HCI_2DH3)) 2640 phys |= BT_PHY_EDR_2M_3SLOT; 2641 2642 if (!(conn->pkt_type & HCI_2DH5)) 2643 phys |= BT_PHY_EDR_2M_5SLOT; 2644 2645 /* ACL logical transport (3 Mb/s) ptt=1: 2646 * 3-DH1, 3-DH3 and 3-DH5. 2647 */ 2648 if (!(conn->pkt_type & HCI_3DH1)) 2649 phys |= BT_PHY_EDR_3M_1SLOT; 2650 2651 if (!(conn->pkt_type & HCI_3DH3)) 2652 phys |= BT_PHY_EDR_3M_3SLOT; 2653 2654 if (!(conn->pkt_type & HCI_3DH5)) 2655 phys |= BT_PHY_EDR_3M_5SLOT; 2656 2657 break; 2658 2659 case ESCO_LINK: 2660 /* eSCO logical transport (1 Mb/s): EV3, EV4 and EV5 */ 2661 phys |= BT_PHY_BR_1M_1SLOT; 2662 2663 if (!(conn->pkt_type & (ESCO_EV4 | ESCO_EV5))) 2664 phys |= BT_PHY_BR_1M_3SLOT; 2665 2666 /* eSCO logical transport (2 Mb/s): 2-EV3, 2-EV5 */ 2667 if (!(conn->pkt_type & ESCO_2EV3)) 2668 phys |= BT_PHY_EDR_2M_1SLOT; 2669 2670 if (!(conn->pkt_type & ESCO_2EV5)) 2671 phys |= BT_PHY_EDR_2M_3SLOT; 2672 2673 /* eSCO logical transport (3 Mb/s): 3-EV3, 3-EV5 */ 2674 if (!(conn->pkt_type & ESCO_3EV3)) 2675 phys |= BT_PHY_EDR_3M_1SLOT; 2676 2677 if (!(conn->pkt_type & ESCO_3EV5)) 2678 phys |= BT_PHY_EDR_3M_3SLOT; 2679 2680 break; 2681 2682 case LE_LINK: 2683 if (conn->le_tx_phy & HCI_LE_SET_PHY_1M) 2684 phys |= BT_PHY_LE_1M_TX; 2685 2686 if (conn->le_rx_phy & HCI_LE_SET_PHY_1M) 2687 phys |= BT_PHY_LE_1M_RX; 2688 2689 if (conn->le_tx_phy & HCI_LE_SET_PHY_2M) 2690 phys |= BT_PHY_LE_2M_TX; 2691 2692 if (conn->le_rx_phy & HCI_LE_SET_PHY_2M) 2693 phys |= BT_PHY_LE_2M_RX; 2694 2695 if (conn->le_tx_phy & HCI_LE_SET_PHY_CODED) 2696 phys |= BT_PHY_LE_CODED_TX; 2697 2698 if (conn->le_rx_phy & HCI_LE_SET_PHY_CODED) 2699 phys |= BT_PHY_LE_CODED_RX; 2700 2701 break; 2702 } 2703 2704 return phys; 2705 } 2706