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