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 |
Brian Gix | 3cd6204 | 2012-01-11 15:18:17 -0800 | [diff] [blame] | 3 | Copyright (c) 2000-2001, 2010-2012 Code Aurora Forum. All rights reserved. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | |
| 5 | Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com> |
| 6 | |
| 7 | This program is free software; you can redistribute it and/or modify |
| 8 | it under the terms of the GNU General Public License version 2 as |
| 9 | published by the Free Software Foundation; |
| 10 | |
| 11 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 12 | OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 13 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. |
| 14 | IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 15 | CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES |
| 16 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 17 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 19 | |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 20 | ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, |
| 21 | COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | SOFTWARE IS DISCLAIMED. |
| 23 | */ |
| 24 | |
| 25 | /* Bluetooth HCI connection handling. */ |
| 26 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #include <linux/module.h> |
| 28 | |
| 29 | #include <linux/types.h> |
| 30 | #include <linux/errno.h> |
| 31 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #include <linux/slab.h> |
| 33 | #include <linux/poll.h> |
| 34 | #include <linux/fcntl.h> |
| 35 | #include <linux/init.h> |
| 36 | #include <linux/skbuff.h> |
| 37 | #include <linux/interrupt.h> |
| 38 | #include <linux/notifier.h> |
| 39 | #include <net/sock.h> |
| 40 | |
| 41 | #include <asm/system.h> |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 42 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | #include <asm/unaligned.h> |
| 44 | |
| 45 | #include <net/bluetooth/bluetooth.h> |
| 46 | #include <net/bluetooth/hci_core.h> |
Brian Gix | a94b612 | 2012-02-23 16:07:10 -0800 | [diff] [blame] | 47 | #include <net/bluetooth/l2cap.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | |
Brian Gix | a94b612 | 2012-02-23 16:07:10 -0800 | [diff] [blame] | 49 | struct hci_conn *hci_le_connect(struct hci_dev *hdev, __u16 pkt_type, |
| 50 | bdaddr_t *dst, __u8 sec_level, __u8 auth_type, |
| 51 | struct bt_le_params *le_params) |
Ville Tervo | fcd89c0 | 2011-02-10 22:38:47 -0300 | [diff] [blame] | 52 | { |
Brian Gix | a94b612 | 2012-02-23 16:07:10 -0800 | [diff] [blame] | 53 | struct hci_conn *le; |
Ville Tervo | fcd89c0 | 2011-02-10 22:38:47 -0300 | [diff] [blame] | 54 | struct hci_cp_le_create_conn cp; |
Brian Gix | a94b612 | 2012-02-23 16:07:10 -0800 | [diff] [blame] | 55 | struct adv_entry *entry; |
| 56 | struct link_key *key; |
Ville Tervo | fcd89c0 | 2011-02-10 22:38:47 -0300 | [diff] [blame] | 57 | |
Brian Gix | a94b612 | 2012-02-23 16:07:10 -0800 | [diff] [blame] | 58 | BT_DBG("%p", hdev); |
Brian Gix | 114f3a6 | 2011-09-27 14:02:20 -0700 | [diff] [blame] | 59 | |
Brian Gix | a94b612 | 2012-02-23 16:07:10 -0800 | [diff] [blame] | 60 | le = hci_conn_hash_lookup_ba(hdev, LE_LINK, dst); |
| 61 | if (le) { |
| 62 | hci_conn_hold(le); |
| 63 | return le; |
| 64 | } |
| 65 | |
| 66 | key = hci_find_link_key_type(hdev, dst, KEY_TYPE_LTK); |
| 67 | if (!key) { |
| 68 | entry = hci_find_adv_entry(hdev, dst); |
| 69 | if (entry) |
| 70 | le = hci_le_conn_add(hdev, dst, |
| 71 | entry->bdaddr_type); |
| 72 | else |
| 73 | le = hci_le_conn_add(hdev, dst, 0); |
| 74 | } else { |
| 75 | le = hci_le_conn_add(hdev, dst, key->addr_type); |
| 76 | } |
| 77 | |
| 78 | if (!le) |
| 79 | return ERR_PTR(-ENOMEM); |
| 80 | |
| 81 | hci_conn_hold(le); |
| 82 | |
| 83 | le->state = BT_CONNECT; |
| 84 | le->out = 1; |
| 85 | le->link_mode |= HCI_LM_MASTER; |
| 86 | le->sec_level = BT_SECURITY_LOW; |
| 87 | le->type = LE_LINK; |
Ville Tervo | fcd89c0 | 2011-02-10 22:38:47 -0300 | [diff] [blame] | 88 | |
| 89 | memset(&cp, 0, sizeof(cp)); |
Brian Gix | a94b612 | 2012-02-23 16:07:10 -0800 | [diff] [blame] | 90 | if (l2cap_sock_le_params_valid(le_params)) { |
| 91 | cp.supervision_timeout = |
| 92 | cpu_to_le16(le_params->supervision_timeout); |
| 93 | cp.scan_interval = cpu_to_le16(le_params->scan_interval); |
| 94 | cp.scan_window = cpu_to_le16(le_params->scan_window); |
| 95 | cp.conn_interval_min = cpu_to_le16(le_params->interval_min); |
| 96 | cp.conn_interval_max = cpu_to_le16(le_params->interval_max); |
| 97 | cp.conn_latency = cpu_to_le16(le_params->latency); |
| 98 | cp.min_ce_len = cpu_to_le16(le_params->min_ce_len); |
| 99 | cp.max_ce_len = cpu_to_le16(le_params->max_ce_len); |
| 100 | le->conn_timeout = le_params->conn_timeout; |
| 101 | } else { |
| 102 | cp.supervision_timeout = cpu_to_le16(BT_LE_SUP_TO_DEFAULT); |
| 103 | cp.scan_interval = cpu_to_le16(BT_LE_SCAN_INTERVAL_DEF); |
| 104 | cp.scan_window = cpu_to_le16(BT_LE_SCAN_WINDOW_DEF); |
| 105 | cp.conn_interval_min = cpu_to_le16(BT_LE_CONN_INTERVAL_MIN_DEF); |
| 106 | cp.conn_interval_max = cpu_to_le16(BT_LE_CONN_INTERVAL_MAX_DEF); |
| 107 | cp.conn_latency = cpu_to_le16(BT_LE_LATENCY_DEF); |
| 108 | le->conn_timeout = 5; |
| 109 | } |
| 110 | bacpy(&cp.peer_addr, &le->dst); |
| 111 | cp.peer_addr_type = le->dst_type; |
Ville Tervo | fcd89c0 | 2011-02-10 22:38:47 -0300 | [diff] [blame] | 112 | |
| 113 | hci_send_cmd(hdev, HCI_OP_LE_CREATE_CONN, sizeof(cp), &cp); |
Brian Gix | a94b612 | 2012-02-23 16:07:10 -0800 | [diff] [blame] | 114 | |
| 115 | return le; |
Ville Tervo | fcd89c0 | 2011-02-10 22:38:47 -0300 | [diff] [blame] | 116 | } |
Brian Gix | a94b612 | 2012-02-23 16:07:10 -0800 | [diff] [blame] | 117 | EXPORT_SYMBOL(hci_le_connect); |
Ville Tervo | fcd89c0 | 2011-02-10 22:38:47 -0300 | [diff] [blame] | 118 | |
| 119 | static void hci_le_connect_cancel(struct hci_conn *conn) |
| 120 | { |
| 121 | hci_send_cmd(conn->hdev, HCI_OP_LE_CREATE_CONN_CANCEL, 0, NULL); |
| 122 | } |
| 123 | |
Marcel Holtmann | 4c67bc7 | 2006-10-15 17:30:56 +0200 | [diff] [blame] | 124 | void hci_acl_connect(struct hci_conn *conn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | { |
| 126 | struct hci_dev *hdev = conn->hdev; |
| 127 | struct inquiry_entry *ie; |
| 128 | struct hci_cp_create_conn cp; |
| 129 | |
| 130 | BT_DBG("%p", conn); |
| 131 | |
| 132 | conn->state = BT_CONNECT; |
Marcel Holtmann | a874641 | 2008-07-14 20:13:46 +0200 | [diff] [blame] | 133 | conn->out = 1; |
| 134 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | conn->link_mode = HCI_LM_MASTER; |
| 136 | |
Marcel Holtmann | 4c67bc7 | 2006-10-15 17:30:56 +0200 | [diff] [blame] | 137 | conn->attempt++; |
| 138 | |
Marcel Holtmann | e4e8e37 | 2008-07-14 20:13:47 +0200 | [diff] [blame] | 139 | conn->link_policy = hdev->link_policy; |
| 140 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | memset(&cp, 0, sizeof(cp)); |
| 142 | bacpy(&cp.bdaddr, &conn->dst); |
| 143 | cp.pscan_rep_mode = 0x02; |
| 144 | |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 145 | ie = hci_inquiry_cache_lookup(hdev, &conn->dst); |
| 146 | if (ie) { |
Marcel Holtmann | 41a9621 | 2008-07-14 20:13:48 +0200 | [diff] [blame] | 147 | if (inquiry_entry_age(ie) <= INQUIRY_ENTRY_AGE_MAX) { |
| 148 | cp.pscan_rep_mode = ie->data.pscan_rep_mode; |
| 149 | cp.pscan_mode = ie->data.pscan_mode; |
| 150 | cp.clock_offset = ie->data.clock_offset | |
| 151 | cpu_to_le16(0x8000); |
| 152 | } |
| 153 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | memcpy(conn->dev_class, ie->data.dev_class, 3); |
Marcel Holtmann | 41a9621 | 2008-07-14 20:13:48 +0200 | [diff] [blame] | 155 | conn->ssp_mode = ie->data.ssp_mode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | } |
| 157 | |
Marcel Holtmann | a874641 | 2008-07-14 20:13:46 +0200 | [diff] [blame] | 158 | cp.pkt_type = cpu_to_le16(conn->pkt_type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | if (lmp_rswitch_capable(hdev) && !(hdev->link_mode & HCI_LM_MASTER)) |
Marcel Holtmann | b6a0dc8 | 2007-10-20 14:55:10 +0200 | [diff] [blame] | 160 | cp.role_switch = 0x01; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | else |
Marcel Holtmann | b6a0dc8 | 2007-10-20 14:55:10 +0200 | [diff] [blame] | 162 | cp.role_switch = 0x00; |
Marcel Holtmann | 4c67bc7 | 2006-10-15 17:30:56 +0200 | [diff] [blame] | 163 | |
Marcel Holtmann | a9de924 | 2007-10-20 13:33:56 +0200 | [diff] [blame] | 164 | hci_send_cmd(hdev, HCI_OP_CREATE_CONN, sizeof(cp), &cp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | } |
| 166 | |
Marcel Holtmann | 6ac5934 | 2006-09-26 09:43:48 +0200 | [diff] [blame] | 167 | static void hci_acl_connect_cancel(struct hci_conn *conn) |
| 168 | { |
| 169 | struct hci_cp_create_conn_cancel cp; |
| 170 | |
| 171 | BT_DBG("%p", conn); |
| 172 | |
| 173 | if (conn->hdev->hci_ver < 2) |
| 174 | return; |
| 175 | |
| 176 | bacpy(&cp.bdaddr, &conn->dst); |
Marcel Holtmann | a9de924 | 2007-10-20 13:33:56 +0200 | [diff] [blame] | 177 | hci_send_cmd(conn->hdev, HCI_OP_CREATE_CONN_CANCEL, sizeof(cp), &cp); |
Marcel Holtmann | 6ac5934 | 2006-09-26 09:43:48 +0200 | [diff] [blame] | 178 | } |
| 179 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | void hci_acl_disconn(struct hci_conn *conn, __u8 reason) |
| 181 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | BT_DBG("%p", conn); |
| 183 | |
| 184 | conn->state = BT_DISCONN; |
| 185 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 186 | if (conn->hdev->dev_type == HCI_BREDR) { |
| 187 | struct hci_cp_disconnect cp; |
| 188 | cp.handle = cpu_to_le16(conn->handle); |
| 189 | cp.reason = reason; |
| 190 | hci_send_cmd(conn->hdev, HCI_OP_DISCONNECT, sizeof(cp), &cp); |
| 191 | } else { |
| 192 | struct hci_cp_disconn_phys_link cp; |
| 193 | cp.phy_handle = (u8) conn->handle; |
| 194 | cp.reason = reason; |
| 195 | hci_send_cmd(conn->hdev, HCI_OP_DISCONN_PHYS_LINK, |
| 196 | sizeof(cp), &cp); |
| 197 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | void hci_add_sco(struct hci_conn *conn, __u16 handle) |
| 201 | { |
| 202 | struct hci_dev *hdev = conn->hdev; |
| 203 | struct hci_cp_add_sco cp; |
| 204 | |
| 205 | BT_DBG("%p", conn); |
| 206 | |
| 207 | conn->state = BT_CONNECT; |
| 208 | conn->out = 1; |
| 209 | |
Marcel Holtmann | efc7688 | 2009-02-06 09:13:37 +0100 | [diff] [blame] | 210 | conn->attempt++; |
| 211 | |
YOSHIFUJI Hideaki | aca3192 | 2007-03-25 20:12:50 -0700 | [diff] [blame] | 212 | cp.handle = cpu_to_le16(handle); |
Marcel Holtmann | a874641 | 2008-07-14 20:13:46 +0200 | [diff] [blame] | 213 | cp.pkt_type = cpu_to_le16(conn->pkt_type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | |
Marcel Holtmann | a9de924 | 2007-10-20 13:33:56 +0200 | [diff] [blame] | 215 | hci_send_cmd(hdev, HCI_OP_ADD_SCO, sizeof(cp), &cp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | } |
| 217 | |
Marcel Holtmann | b6a0dc8 | 2007-10-20 14:55:10 +0200 | [diff] [blame] | 218 | void hci_setup_sync(struct hci_conn *conn, __u16 handle) |
| 219 | { |
| 220 | struct hci_dev *hdev = conn->hdev; |
| 221 | struct hci_cp_setup_sync_conn cp; |
| 222 | |
| 223 | BT_DBG("%p", conn); |
| 224 | |
| 225 | conn->state = BT_CONNECT; |
| 226 | conn->out = 1; |
| 227 | |
Marcel Holtmann | efc7688 | 2009-02-06 09:13:37 +0100 | [diff] [blame] | 228 | conn->attempt++; |
| 229 | |
Marcel Holtmann | b6a0dc8 | 2007-10-20 14:55:10 +0200 | [diff] [blame] | 230 | cp.handle = cpu_to_le16(handle); |
Marcel Holtmann | b6a0dc8 | 2007-10-20 14:55:10 +0200 | [diff] [blame] | 231 | |
| 232 | cp.tx_bandwidth = cpu_to_le32(0x00001f40); |
| 233 | cp.rx_bandwidth = cpu_to_le32(0x00001f40); |
Kun Han Kim | 15b911f | 2011-07-08 09:30:28 -0700 | [diff] [blame] | 234 | if (conn->hdev->is_wbs) { |
| 235 | /* Transparent Data */ |
| 236 | uint16_t voice_setting = hdev->voice_setting | ACF_TRANS; |
| 237 | cp.max_latency = cpu_to_le16(0x000D); |
| 238 | cp.pkt_type = cpu_to_le16(ESCO_WBS); |
| 239 | cp.voice_setting = cpu_to_le16(voice_setting); |
| 240 | /* Retransmission Effort */ |
| 241 | cp.retrans_effort = RE_LINK_QUALITY; |
| 242 | } else { |
| 243 | cp.max_latency = cpu_to_le16(0x000A); |
| 244 | cp.pkt_type = cpu_to_le16(conn->pkt_type); |
| 245 | cp.voice_setting = cpu_to_le16(hdev->voice_setting); |
| 246 | cp.retrans_effort = RE_POWER_CONSUMP; |
| 247 | } |
Marcel Holtmann | b6a0dc8 | 2007-10-20 14:55:10 +0200 | [diff] [blame] | 248 | |
| 249 | hci_send_cmd(hdev, HCI_OP_SETUP_SYNC_CONN, sizeof(cp), &cp); |
| 250 | } |
| 251 | |
Claudio Takahasi | 2ce603e | 2011-02-16 20:44:53 -0200 | [diff] [blame] | 252 | void hci_le_conn_update(struct hci_conn *conn, u16 min, u16 max, |
| 253 | u16 latency, u16 to_multiplier) |
| 254 | { |
| 255 | struct hci_cp_le_conn_update cp; |
| 256 | struct hci_dev *hdev = conn->hdev; |
| 257 | |
| 258 | memset(&cp, 0, sizeof(cp)); |
| 259 | |
| 260 | cp.handle = cpu_to_le16(conn->handle); |
| 261 | cp.conn_interval_min = cpu_to_le16(min); |
| 262 | cp.conn_interval_max = cpu_to_le16(max); |
| 263 | cp.conn_latency = cpu_to_le16(latency); |
| 264 | cp.supervision_timeout = cpu_to_le16(to_multiplier); |
| 265 | cp.min_ce_len = cpu_to_le16(0x0001); |
| 266 | cp.max_ce_len = cpu_to_le16(0x0001); |
| 267 | |
| 268 | hci_send_cmd(hdev, HCI_OP_LE_CONN_UPDATE, sizeof(cp), &cp); |
| 269 | } |
| 270 | EXPORT_SYMBOL(hci_le_conn_update); |
| 271 | |
Archana Ramachandran | 26a752b | 2011-12-20 11:27:40 -0800 | [diff] [blame] | 272 | void hci_read_rssi(struct hci_conn *conn) |
| 273 | { |
| 274 | struct hci_cp_read_rssi cp; |
| 275 | struct hci_dev *hdev = conn->hdev; |
| 276 | |
| 277 | memset(&cp, 0, sizeof(cp)); |
| 278 | cp.handle = cpu_to_le16(conn->handle); |
| 279 | |
| 280 | hci_send_cmd(hdev, HCI_OP_READ_RSSI, sizeof(cp), &cp); |
| 281 | } |
| 282 | EXPORT_SYMBOL(hci_read_rssi); |
| 283 | |
Vinicius Costa Gomes | 735038c | 2011-06-09 18:50:47 -0300 | [diff] [blame] | 284 | void hci_le_start_enc(struct hci_conn *conn, __le16 ediv, __u8 rand[8], |
| 285 | __u8 ltk[16]) |
| 286 | { |
| 287 | struct hci_dev *hdev = conn->hdev; |
| 288 | struct hci_cp_le_start_enc cp; |
| 289 | |
| 290 | BT_DBG("%p", conn); |
| 291 | |
| 292 | memset(&cp, 0, sizeof(cp)); |
| 293 | |
| 294 | cp.handle = cpu_to_le16(conn->handle); |
| 295 | memcpy(cp.ltk, ltk, sizeof(cp.ltk)); |
| 296 | cp.ediv = ediv; |
Brian Gix | 842bc5e | 2011-09-07 08:13:41 -0700 | [diff] [blame] | 297 | memcpy(cp.rand, rand, sizeof(cp.rand)); |
Vinicius Costa Gomes | 735038c | 2011-06-09 18:50:47 -0300 | [diff] [blame] | 298 | |
| 299 | hci_send_cmd(hdev, HCI_OP_LE_START_ENC, sizeof(cp), &cp); |
| 300 | } |
| 301 | EXPORT_SYMBOL(hci_le_start_enc); |
| 302 | |
| 303 | void hci_le_ltk_reply(struct hci_conn *conn, u8 ltk[16]) |
| 304 | { |
| 305 | struct hci_dev *hdev = conn->hdev; |
| 306 | struct hci_cp_le_ltk_reply cp; |
| 307 | |
| 308 | BT_DBG("%p", conn); |
| 309 | |
| 310 | memset(&cp, 0, sizeof(cp)); |
| 311 | |
| 312 | cp.handle = cpu_to_le16(conn->handle); |
| 313 | memcpy(cp.ltk, ltk, sizeof(ltk)); |
| 314 | |
| 315 | hci_send_cmd(hdev, HCI_OP_LE_LTK_REPLY, sizeof(cp), &cp); |
| 316 | } |
| 317 | EXPORT_SYMBOL(hci_le_ltk_reply); |
| 318 | |
| 319 | void hci_le_ltk_neg_reply(struct hci_conn *conn) |
| 320 | { |
| 321 | struct hci_dev *hdev = conn->hdev; |
| 322 | struct hci_cp_le_ltk_neg_reply cp; |
| 323 | |
| 324 | BT_DBG("%p", conn); |
| 325 | |
| 326 | memset(&cp, 0, sizeof(cp)); |
| 327 | |
| 328 | cp.handle = cpu_to_le16(conn->handle); |
| 329 | |
| 330 | hci_send_cmd(hdev, HCI_OP_LE_LTK_NEG_REPLY, sizeof(cp), &cp); |
| 331 | } |
| 332 | |
Marcel Holtmann | e73439d | 2010-07-26 10:06:00 -0400 | [diff] [blame] | 333 | /* Device _must_ be locked */ |
| 334 | void hci_sco_setup(struct hci_conn *conn, __u8 status) |
| 335 | { |
| 336 | struct hci_conn *sco = conn->link; |
| 337 | |
| 338 | BT_DBG("%p", conn); |
| 339 | |
| 340 | if (!sco) |
| 341 | return; |
| 342 | |
| 343 | if (!status) { |
| 344 | if (lmp_esco_capable(conn->hdev)) |
| 345 | hci_setup_sync(sco, conn->handle); |
| 346 | else |
| 347 | hci_add_sco(sco, conn->handle); |
| 348 | } else { |
| 349 | hci_proto_connect_cfm(sco, status); |
| 350 | hci_conn_del(sco); |
| 351 | } |
| 352 | } |
| 353 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | static void hci_conn_timeout(unsigned long arg) |
| 355 | { |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 356 | struct hci_conn *conn = (void *) arg; |
| 357 | struct hci_dev *hdev = conn->hdev; |
Marcel Holtmann | 2950f21 | 2009-02-12 14:02:50 +0100 | [diff] [blame] | 358 | __u8 reason; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | |
| 360 | BT_DBG("conn %p state %d", conn, conn->state); |
| 361 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | hci_dev_lock(hdev); |
Marcel Holtmann | 6ac5934 | 2006-09-26 09:43:48 +0200 | [diff] [blame] | 363 | |
| 364 | switch (conn->state) { |
| 365 | case BT_CONNECT: |
Marcel Holtmann | 769be97 | 2008-07-14 20:13:49 +0200 | [diff] [blame] | 366 | case BT_CONNECT2: |
Ville Tervo | fcd89c0 | 2011-02-10 22:38:47 -0300 | [diff] [blame] | 367 | if (conn->out) { |
| 368 | if (conn->type == ACL_LINK) |
| 369 | hci_acl_connect_cancel(conn); |
| 370 | else if (conn->type == LE_LINK) |
| 371 | hci_le_connect_cancel(conn); |
| 372 | } |
Marcel Holtmann | 6ac5934 | 2006-09-26 09:43:48 +0200 | [diff] [blame] | 373 | break; |
Marcel Holtmann | 769be97 | 2008-07-14 20:13:49 +0200 | [diff] [blame] | 374 | case BT_CONFIG: |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 375 | case BT_CONNECTED: |
Brian Gix | 114f3a6 | 2011-09-27 14:02:20 -0700 | [diff] [blame] | 376 | if (!atomic_read(&conn->refcnt)) { |
| 377 | reason = hci_proto_disconn_ind(conn); |
| 378 | hci_acl_disconn(conn, reason); |
| 379 | } |
Marcel Holtmann | 6ac5934 | 2006-09-26 09:43:48 +0200 | [diff] [blame] | 380 | break; |
| 381 | default: |
Brian Gix | 114f3a6 | 2011-09-27 14:02:20 -0700 | [diff] [blame] | 382 | if (!atomic_read(&conn->refcnt)) |
| 383 | conn->state = BT_CLOSED; |
Marcel Holtmann | 6ac5934 | 2006-09-26 09:43:48 +0200 | [diff] [blame] | 384 | break; |
| 385 | } |
| 386 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | hci_dev_unlock(hdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | } |
| 389 | |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 390 | static void hci_conn_idle(unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | { |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 392 | struct hci_conn *conn = (void *) arg; |
| 393 | |
| 394 | BT_DBG("conn %p mode %d", conn, conn->mode); |
| 395 | |
| 396 | hci_conn_enter_sniff_mode(conn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | } |
| 398 | |
Archana Ramachandran | 26a752b | 2011-12-20 11:27:40 -0800 | [diff] [blame] | 399 | static void hci_conn_rssi_update(struct work_struct *work) |
| 400 | { |
| 401 | struct delayed_work *delayed = |
| 402 | container_of(work, struct delayed_work, work); |
| 403 | struct hci_conn *conn = |
| 404 | container_of(delayed, struct hci_conn, rssi_update_work); |
| 405 | |
| 406 | BT_DBG("conn %p mode %d", conn, conn->mode); |
| 407 | |
| 408 | hci_read_rssi(conn); |
| 409 | } |
| 410 | |
Prabhakaran Mc | afface8 | 2012-04-10 11:38:35 +0530 | [diff] [blame] | 411 | static void encryption_disabled_timeout(unsigned long userdata) |
| 412 | { |
| 413 | struct hci_conn *conn = (struct hci_conn *)userdata; |
| 414 | BT_INFO("conn %p Grace Prd Exp ", conn); |
| 415 | |
| 416 | hci_encrypt_cfm(conn, 0, 0); |
| 417 | |
| 418 | if (test_bit(HCI_CONN_ENCRYPT_PEND, &conn->pend)) { |
| 419 | struct hci_cp_set_conn_encrypt cp; |
| 420 | BT_INFO("HCI_CONN_ENCRYPT_PEND is set"); |
| 421 | cp.handle = cpu_to_le16(conn->handle); |
| 422 | cp.encrypt = 1; |
| 423 | hci_send_cmd(conn->hdev, HCI_OP_SET_CONN_ENCRYPT, |
| 424 | sizeof(cp), &cp); |
| 425 | } |
| 426 | |
| 427 | } |
| 428 | |
Nick Pelly | bbcda3b | 2010-02-11 11:54:28 -0800 | [diff] [blame] | 429 | struct hci_conn *hci_conn_add(struct hci_dev *hdev, int type, |
| 430 | __u16 pkt_type, bdaddr_t *dst) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | { |
| 432 | struct hci_conn *conn; |
| 433 | |
| 434 | BT_DBG("%s dst %s", hdev->name, batostr(dst)); |
| 435 | |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 436 | conn = kzalloc(sizeof(struct hci_conn), GFP_ATOMIC); |
| 437 | if (!conn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | |
| 440 | bacpy(&conn->dst, dst); |
Marcel Holtmann | a874641 | 2008-07-14 20:13:46 +0200 | [diff] [blame] | 441 | conn->hdev = hdev; |
| 442 | conn->type = type; |
| 443 | conn->mode = HCI_CM_ACTIVE; |
| 444 | conn->state = BT_OPEN; |
Andrei Emeltchenko | 93f19c9 | 2009-09-03 12:34:19 +0300 | [diff] [blame] | 445 | conn->auth_type = HCI_AT_GENERAL_BONDING; |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 446 | conn->io_capability = hdev->io_capability; |
Johan Hedberg | a958355 | 2011-02-19 12:06:01 -0300 | [diff] [blame] | 447 | conn->remote_auth = 0xff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 449 | conn->power_save = 1; |
Marcel Holtmann | 052b30b | 2009-04-26 20:01:22 +0200 | [diff] [blame] | 450 | conn->disc_timeout = HCI_DISCONN_TIMEOUT; |
Rahul Kashyap | b44f9e2 | 2012-05-03 16:45:17 +0530 | [diff] [blame] | 451 | wake_lock_init(&conn->idle_lock, WAKE_LOCK_SUSPEND, "bt_idle"); |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 452 | |
Marcel Holtmann | a874641 | 2008-07-14 20:13:46 +0200 | [diff] [blame] | 453 | switch (type) { |
| 454 | case ACL_LINK: |
| 455 | conn->pkt_type = hdev->pkt_type & ACL_PTYPE_MASK; |
Rahul Kashyap | d5553ba | 2012-01-02 19:27:19 +0530 | [diff] [blame] | 456 | conn->link_policy = hdev->link_policy; |
Marcel Holtmann | a874641 | 2008-07-14 20:13:46 +0200 | [diff] [blame] | 457 | break; |
| 458 | case SCO_LINK: |
Nick Pelly | bbcda3b | 2010-02-11 11:54:28 -0800 | [diff] [blame] | 459 | if (!pkt_type) |
| 460 | pkt_type = SCO_ESCO_MASK; |
Marcel Holtmann | a874641 | 2008-07-14 20:13:46 +0200 | [diff] [blame] | 461 | case ESCO_LINK: |
Nick Pelly | bbcda3b | 2010-02-11 11:54:28 -0800 | [diff] [blame] | 462 | if (!pkt_type) |
| 463 | pkt_type = ALL_ESCO_MASK; |
| 464 | if (lmp_esco_capable(hdev)) { |
| 465 | /* HCI Setup Synchronous Connection Command uses |
| 466 | reverse logic on the EDR_ESCO_MASK bits */ |
| 467 | conn->pkt_type = (pkt_type ^ EDR_ESCO_MASK) & |
| 468 | hdev->esco_type; |
| 469 | } else { |
| 470 | /* Legacy HCI Add Sco Connection Command uses a |
| 471 | shifted bitmask */ |
| 472 | conn->pkt_type = (pkt_type << 5) & hdev->pkt_type & |
| 473 | SCO_PTYPE_MASK; |
| 474 | } |
Marcel Holtmann | a874641 | 2008-07-14 20:13:46 +0200 | [diff] [blame] | 475 | break; |
| 476 | } |
| 477 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | skb_queue_head_init(&conn->data_q); |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 479 | |
Pavel Emelyanov | b24b8a2 | 2008-01-23 21:20:07 -0800 | [diff] [blame] | 480 | setup_timer(&conn->disc_timer, hci_conn_timeout, (unsigned long)conn); |
| 481 | setup_timer(&conn->idle_timer, hci_conn_idle, (unsigned long)conn); |
Archana Ramachandran | 26a752b | 2011-12-20 11:27:40 -0800 | [diff] [blame] | 482 | INIT_DELAYED_WORK(&conn->rssi_update_work, hci_conn_rssi_update); |
Prabhakaran Mc | afface8 | 2012-04-10 11:38:35 +0530 | [diff] [blame] | 483 | setup_timer(&conn->encrypt_pause_timer, encryption_disabled_timeout, |
| 484 | (unsigned long)conn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | |
| 486 | atomic_set(&conn->refcnt, 0); |
| 487 | |
| 488 | hci_dev_hold(hdev); |
| 489 | |
| 490 | tasklet_disable(&hdev->tx_task); |
| 491 | |
| 492 | hci_conn_hash_add(hdev, conn); |
| 493 | if (hdev->notify) |
| 494 | hdev->notify(hdev, HCI_NOTIFY_CONN_ADD); |
| 495 | |
Marcel Holtmann | 9eba32b | 2009-08-22 14:19:26 -0700 | [diff] [blame] | 496 | atomic_set(&conn->devref, 0); |
| 497 | |
Marcel Holtmann | a67e899 | 2009-05-02 18:24:06 -0700 | [diff] [blame] | 498 | hci_conn_init_sysfs(conn); |
| 499 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | tasklet_enable(&hdev->tx_task); |
| 501 | |
| 502 | return conn; |
| 503 | } |
| 504 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 505 | struct hci_conn *hci_le_conn_add(struct hci_dev *hdev, bdaddr_t *dst, |
| 506 | __u8 addr_type) |
| 507 | { |
| 508 | struct hci_conn *conn = hci_conn_add(hdev, LE_LINK, 0, dst); |
| 509 | if (!conn) |
| 510 | return NULL; |
| 511 | |
| 512 | conn->dst_type = addr_type; |
| 513 | |
| 514 | return conn; |
| 515 | } |
| 516 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | int hci_conn_del(struct hci_conn *conn) |
| 518 | { |
| 519 | struct hci_dev *hdev = conn->hdev; |
| 520 | |
| 521 | BT_DBG("%s conn %p handle %d", hdev->name, conn, conn->handle); |
| 522 | |
Brian Gix | 3cd6204 | 2012-01-11 15:18:17 -0800 | [diff] [blame] | 523 | /* Make sure no timers are running */ |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 524 | del_timer(&conn->idle_timer); |
Rahul Kashyap | b44f9e2 | 2012-05-03 16:45:17 +0530 | [diff] [blame] | 525 | wake_lock_destroy(&conn->idle_lock); |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 526 | del_timer(&conn->disc_timer); |
Brian Gix | 3cd6204 | 2012-01-11 15:18:17 -0800 | [diff] [blame] | 527 | del_timer(&conn->smp_timer); |
Archana Ramachandran | 26a752b | 2011-12-20 11:27:40 -0800 | [diff] [blame] | 528 | __cancel_delayed_work(&conn->rssi_update_work); |
Prabhakaran Mc | afface8 | 2012-04-10 11:38:35 +0530 | [diff] [blame] | 529 | del_timer(&conn->encrypt_pause_timer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 530 | |
Marcel Holtmann | 5b7f9909 | 2007-07-11 09:51:55 +0200 | [diff] [blame] | 531 | if (conn->type == ACL_LINK) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | struct hci_conn *sco = conn->link; |
| 533 | if (sco) |
| 534 | sco->link = NULL; |
| 535 | |
| 536 | /* Unacked frames */ |
| 537 | hdev->acl_cnt += conn->sent; |
Ville Tervo | 6ed58ec | 2011-02-10 22:38:48 -0300 | [diff] [blame] | 538 | } else if (conn->type == LE_LINK) { |
| 539 | if (hdev->le_pkts) |
| 540 | hdev->le_cnt += conn->sent; |
| 541 | else |
| 542 | hdev->acl_cnt += conn->sent; |
Marcel Holtmann | 5b7f9909 | 2007-07-11 09:51:55 +0200 | [diff] [blame] | 543 | } else { |
| 544 | struct hci_conn *acl = conn->link; |
| 545 | if (acl) { |
| 546 | acl->link = NULL; |
| 547 | hci_conn_put(acl); |
| 548 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 549 | } |
| 550 | |
| 551 | tasklet_disable(&hdev->tx_task); |
Marcel Holtmann | 7d0db0a | 2008-07-14 20:13:51 +0200 | [diff] [blame] | 552 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | hci_conn_hash_del(hdev, conn); |
| 554 | if (hdev->notify) |
| 555 | hdev->notify(hdev, HCI_NOTIFY_CONN_DEL); |
Marcel Holtmann | 7d0db0a | 2008-07-14 20:13:51 +0200 | [diff] [blame] | 556 | |
AnubhavGupta | 01d9e36 | 2011-12-17 17:14:51 +0530 | [diff] [blame] | 557 | tasklet_schedule(&hdev->tx_task); |
| 558 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 559 | tasklet_enable(&hdev->tx_task); |
Marcel Holtmann | 7d0db0a | 2008-07-14 20:13:51 +0200 | [diff] [blame] | 560 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | skb_queue_purge(&conn->data_q); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 562 | |
Marcel Holtmann | 9eba32b | 2009-08-22 14:19:26 -0700 | [diff] [blame] | 563 | hci_conn_put_device(conn); |
Dave Young | 2ae9a6b | 2009-02-21 16:13:34 +0800 | [diff] [blame] | 564 | |
Marcel Holtmann | 384943e | 2009-05-08 18:20:43 -0700 | [diff] [blame] | 565 | hci_dev_put(hdev); |
| 566 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 567 | return 0; |
| 568 | } |
| 569 | |
| 570 | struct hci_chan *hci_chan_add(struct hci_dev *hdev) |
| 571 | { |
| 572 | struct hci_chan *chan; |
| 573 | |
| 574 | BT_DBG("%s", hdev->name); |
| 575 | |
| 576 | chan = kzalloc(sizeof(struct hci_chan), GFP_ATOMIC); |
| 577 | if (!chan) |
| 578 | return NULL; |
| 579 | |
| 580 | atomic_set(&chan->refcnt, 0); |
| 581 | |
| 582 | hci_dev_hold(hdev); |
| 583 | |
| 584 | chan->hdev = hdev; |
| 585 | |
| 586 | list_add(&chan->list, &hdev->chan_list.list); |
| 587 | |
| 588 | return chan; |
| 589 | } |
Peter Krystad | a8417e6 | 2012-03-21 16:58:17 -0700 | [diff] [blame] | 590 | EXPORT_SYMBOL(hci_chan_add); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 591 | |
| 592 | int hci_chan_del(struct hci_chan *chan) |
| 593 | { |
| 594 | BT_DBG("%s chan %p", chan->hdev->name, chan); |
| 595 | |
| 596 | list_del(&chan->list); |
| 597 | |
| 598 | hci_conn_put(chan->conn); |
| 599 | hci_dev_put(chan->hdev); |
| 600 | |
| 601 | kfree(chan); |
Tomas Targownik | 1be668d | 2011-06-30 16:30:44 -0300 | [diff] [blame] | 602 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 603 | return 0; |
| 604 | } |
| 605 | |
Peter Krystad | d6a9ceb | 2011-12-01 15:44:54 -0800 | [diff] [blame] | 606 | int hci_chan_put(struct hci_chan *chan) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 607 | { |
| 608 | struct hci_cp_disconn_logical_link cp; |
Mat Martineau | 9f8d467 | 2011-12-14 12:10:46 -0800 | [diff] [blame] | 609 | struct hci_conn *hcon; |
| 610 | u16 ll_handle; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 611 | |
| 612 | BT_DBG("chan %p refcnt %d", chan, atomic_read(&chan->refcnt)); |
| 613 | if (!atomic_dec_and_test(&chan->refcnt)) |
Peter Krystad | d6a9ceb | 2011-12-01 15:44:54 -0800 | [diff] [blame] | 614 | return 0; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 615 | |
Mat Martineau | 9f8d467 | 2011-12-14 12:10:46 -0800 | [diff] [blame] | 616 | hcon = chan->conn; |
| 617 | ll_handle = chan->ll_handle; |
| 618 | |
| 619 | hci_chan_del(chan); |
| 620 | |
| 621 | BT_DBG("chan->conn->state %d", hcon->state); |
| 622 | if (hcon->state == BT_CONNECTED) { |
| 623 | cp.log_handle = cpu_to_le16(ll_handle); |
| 624 | hci_send_cmd(hcon->hdev, HCI_OP_DISCONN_LOGICAL_LINK, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 625 | sizeof(cp), &cp); |
Mat Martineau | 9f8d467 | 2011-12-14 12:10:46 -0800 | [diff] [blame] | 626 | } |
Peter Krystad | d6a9ceb | 2011-12-01 15:44:54 -0800 | [diff] [blame] | 627 | |
| 628 | return 1; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 629 | } |
| 630 | EXPORT_SYMBOL(hci_chan_put); |
| 631 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | struct hci_dev *hci_get_route(bdaddr_t *dst, bdaddr_t *src) |
| 633 | { |
| 634 | int use_src = bacmp(src, BDADDR_ANY); |
| 635 | struct hci_dev *hdev = NULL; |
| 636 | struct list_head *p; |
| 637 | |
| 638 | BT_DBG("%s -> %s", batostr(src), batostr(dst)); |
| 639 | |
| 640 | read_lock_bh(&hci_dev_list_lock); |
| 641 | |
| 642 | list_for_each(p, &hci_dev_list) { |
| 643 | struct hci_dev *d = list_entry(p, struct hci_dev, list); |
| 644 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 645 | if (d->dev_type != HCI_BREDR) |
| 646 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 647 | if (!test_bit(HCI_UP, &d->flags) || test_bit(HCI_RAW, &d->flags)) |
| 648 | continue; |
| 649 | |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 650 | /* Simple routing: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 651 | * No source address - find interface with bdaddr != dst |
| 652 | * Source address - find interface with bdaddr == src |
| 653 | */ |
| 654 | |
| 655 | if (use_src) { |
| 656 | if (!bacmp(&d->bdaddr, src)) { |
| 657 | hdev = d; break; |
| 658 | } |
| 659 | } else { |
| 660 | if (bacmp(&d->bdaddr, dst)) { |
| 661 | hdev = d; break; |
| 662 | } |
| 663 | } |
| 664 | } |
| 665 | |
| 666 | if (hdev) |
| 667 | hdev = hci_dev_hold(hdev); |
| 668 | |
| 669 | read_unlock_bh(&hci_dev_list_lock); |
| 670 | return hdev; |
| 671 | } |
| 672 | EXPORT_SYMBOL(hci_get_route); |
| 673 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 674 | struct hci_dev *hci_dev_get_type(u8 amp_type) |
| 675 | { |
| 676 | struct hci_dev *hdev = NULL; |
| 677 | struct hci_dev *d; |
| 678 | |
| 679 | BT_DBG("amp_type %d", amp_type); |
| 680 | |
| 681 | read_lock_bh(&hci_dev_list_lock); |
| 682 | |
| 683 | list_for_each_entry(d, &hci_dev_list, list) { |
| 684 | if ((d->amp_type == amp_type) && test_bit(HCI_UP, &d->flags)) { |
| 685 | hdev = d; |
| 686 | break; |
| 687 | } |
| 688 | } |
| 689 | |
| 690 | if (hdev) |
| 691 | hdev = hci_dev_hold(hdev); |
| 692 | |
| 693 | read_unlock_bh(&hci_dev_list_lock); |
| 694 | return hdev; |
| 695 | } |
| 696 | EXPORT_SYMBOL(hci_dev_get_type); |
| 697 | |
| 698 | struct hci_dev *hci_dev_get_amp(bdaddr_t *dst) |
| 699 | { |
| 700 | struct hci_dev *d; |
| 701 | struct hci_dev *hdev = NULL; |
| 702 | |
| 703 | BT_DBG("%s dst %s", hdev->name, batostr(dst)); |
| 704 | |
| 705 | read_lock_bh(&hci_dev_list_lock); |
| 706 | |
| 707 | list_for_each_entry(d, &hci_dev_list, list) { |
| 708 | struct hci_conn *conn; |
| 709 | if (d->dev_type == HCI_BREDR) |
| 710 | continue; |
| 711 | conn = hci_conn_hash_lookup_ba(d, ACL_LINK, dst); |
| 712 | if (conn) { |
| 713 | hdev = d; |
| 714 | break; |
| 715 | } |
| 716 | } |
| 717 | |
| 718 | if (hdev) |
| 719 | hdev = hci_dev_hold(hdev); |
| 720 | |
| 721 | read_unlock_bh(&hci_dev_list_lock); |
| 722 | return hdev; |
| 723 | } |
| 724 | EXPORT_SYMBOL(hci_dev_get_amp); |
| 725 | |
Ville Tervo | fcd89c0 | 2011-02-10 22:38:47 -0300 | [diff] [blame] | 726 | /* Create SCO, ACL or LE connection. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 727 | * Device _must_ be locked */ |
Nick Pelly | bbcda3b | 2010-02-11 11:54:28 -0800 | [diff] [blame] | 728 | struct hci_conn *hci_connect(struct hci_dev *hdev, int type, |
| 729 | __u16 pkt_type, bdaddr_t *dst, |
| 730 | __u8 sec_level, __u8 auth_type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 731 | { |
| 732 | struct hci_conn *acl; |
Marcel Holtmann | 5b7f9909 | 2007-07-11 09:51:55 +0200 | [diff] [blame] | 733 | struct hci_conn *sco; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 734 | |
| 735 | BT_DBG("%s dst %s", hdev->name, batostr(dst)); |
| 736 | |
Brian Gix | a94b612 | 2012-02-23 16:07:10 -0800 | [diff] [blame] | 737 | if (type == LE_LINK) |
| 738 | return hci_le_connect(hdev, pkt_type, dst, sec_level, |
| 739 | auth_type, NULL); |
Ville Tervo | fcd89c0 | 2011-02-10 22:38:47 -0300 | [diff] [blame] | 740 | |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 741 | acl = hci_conn_hash_lookup_ba(hdev, ACL_LINK, dst); |
| 742 | if (!acl) { |
Nick Pelly | bbcda3b | 2010-02-11 11:54:28 -0800 | [diff] [blame] | 743 | acl = hci_conn_add(hdev, ACL_LINK, 0, dst); |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 744 | if (!acl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 745 | return NULL; |
| 746 | } |
| 747 | |
| 748 | hci_conn_hold(acl); |
| 749 | |
Marcel Holtmann | 09ab6f4 | 2008-09-09 07:19:20 +0200 | [diff] [blame] | 750 | if (acl->state == BT_OPEN || acl->state == BT_CLOSED) { |
Johan Hedberg | 765c2a9 | 2011-01-19 12:06:52 +0530 | [diff] [blame] | 751 | acl->sec_level = BT_SECURITY_LOW; |
| 752 | acl->pending_sec_level = sec_level; |
Marcel Holtmann | 09ab6f4 | 2008-09-09 07:19:20 +0200 | [diff] [blame] | 753 | acl->auth_type = auth_type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 754 | hci_acl_connect(acl); |
Marcel Holtmann | 09ab6f4 | 2008-09-09 07:19:20 +0200 | [diff] [blame] | 755 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 756 | |
Marcel Holtmann | 5b7f9909 | 2007-07-11 09:51:55 +0200 | [diff] [blame] | 757 | if (type == ACL_LINK) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 758 | return acl; |
Marcel Holtmann | 5b7f9909 | 2007-07-11 09:51:55 +0200 | [diff] [blame] | 759 | |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 760 | sco = hci_conn_hash_lookup_ba(hdev, type, dst); |
| 761 | if (!sco) { |
Nick Pelly | bbcda3b | 2010-02-11 11:54:28 -0800 | [diff] [blame] | 762 | sco = hci_conn_add(hdev, type, pkt_type, dst); |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 763 | if (!sco) { |
Marcel Holtmann | 5b7f9909 | 2007-07-11 09:51:55 +0200 | [diff] [blame] | 764 | hci_conn_put(acl); |
| 765 | return NULL; |
| 766 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 767 | } |
Marcel Holtmann | 5b7f9909 | 2007-07-11 09:51:55 +0200 | [diff] [blame] | 768 | |
| 769 | acl->link = sco; |
| 770 | sco->link = acl; |
| 771 | |
| 772 | hci_conn_hold(sco); |
| 773 | |
| 774 | if (acl->state == BT_CONNECTED && |
Marcel Holtmann | b6a0dc8 | 2007-10-20 14:55:10 +0200 | [diff] [blame] | 775 | (sco->state == BT_OPEN || sco->state == BT_CLOSED)) { |
Nick Pelly | c390216 | 2009-11-13 14:16:32 -0800 | [diff] [blame] | 776 | acl->power_save = 1; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 777 | hci_conn_enter_active_mode(acl, 1); |
Nick Pelly | c390216 | 2009-11-13 14:16:32 -0800 | [diff] [blame] | 778 | |
Marcel Holtmann | e73439d | 2010-07-26 10:06:00 -0400 | [diff] [blame] | 779 | if (test_bit(HCI_CONN_MODE_CHANGE_PEND, &acl->pend)) { |
| 780 | /* defer SCO setup until mode change completed */ |
| 781 | set_bit(HCI_CONN_SCO_SETUP_PEND, &acl->pend); |
| 782 | return sco; |
| 783 | } |
| 784 | |
| 785 | hci_sco_setup(acl, 0x00); |
Marcel Holtmann | b6a0dc8 | 2007-10-20 14:55:10 +0200 | [diff] [blame] | 786 | } |
Marcel Holtmann | 5b7f9909 | 2007-07-11 09:51:55 +0200 | [diff] [blame] | 787 | |
| 788 | return sco; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 789 | } |
| 790 | EXPORT_SYMBOL(hci_connect); |
| 791 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 792 | void hci_disconnect(struct hci_conn *conn, __u8 reason) |
| 793 | { |
| 794 | BT_DBG("conn %p", conn); |
| 795 | |
Mat Martineau | 3b9239a | 2012-02-16 11:54:30 -0800 | [diff] [blame] | 796 | hci_proto_disconn_cfm(conn, reason, 0); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 797 | } |
| 798 | EXPORT_SYMBOL(hci_disconnect); |
| 799 | |
| 800 | void hci_disconnect_amp(struct hci_conn *conn, __u8 reason) |
| 801 | { |
| 802 | struct hci_dev *hdev = NULL; |
| 803 | |
| 804 | BT_DBG("conn %p", conn); |
| 805 | |
| 806 | read_lock_bh(&hci_dev_list_lock); |
| 807 | |
| 808 | list_for_each_entry(hdev, &hci_dev_list, list) { |
| 809 | struct hci_conn *c; |
| 810 | if (hdev == conn->hdev) |
| 811 | continue; |
| 812 | if (hdev->amp_type == HCI_BREDR) |
| 813 | continue; |
| 814 | c = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &conn->dst); |
| 815 | if (c) |
| 816 | hci_disconnect(c, reason); |
| 817 | } |
| 818 | |
| 819 | read_unlock_bh(&hci_dev_list_lock); |
| 820 | } |
| 821 | |
Marcel Holtmann | e7c29cb | 2008-09-09 07:19:20 +0200 | [diff] [blame] | 822 | /* Check link security requirement */ |
| 823 | int hci_conn_check_link_mode(struct hci_conn *conn) |
| 824 | { |
| 825 | BT_DBG("conn %p", conn); |
| 826 | |
| 827 | if (conn->ssp_mode > 0 && conn->hdev->ssp_mode > 0 && |
| 828 | !(conn->link_mode & HCI_LM_ENCRYPT)) |
| 829 | return 0; |
| 830 | |
| 831 | return 1; |
| 832 | } |
| 833 | EXPORT_SYMBOL(hci_conn_check_link_mode); |
| 834 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 835 | /* Authenticate remote device */ |
Marcel Holtmann | 0684e5f | 2009-02-09 02:48:38 +0100 | [diff] [blame] | 836 | static int hci_conn_auth(struct hci_conn *conn, __u8 sec_level, __u8 auth_type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 837 | { |
| 838 | BT_DBG("conn %p", conn); |
| 839 | |
Johan Hedberg | 765c2a9 | 2011-01-19 12:06:52 +0530 | [diff] [blame] | 840 | if (conn->pending_sec_level > sec_level) |
| 841 | sec_level = conn->pending_sec_level; |
| 842 | |
Marcel Holtmann | 96a3183 | 2009-02-12 16:23:03 +0100 | [diff] [blame] | 843 | if (sec_level > conn->sec_level) |
Johan Hedberg | 765c2a9 | 2011-01-19 12:06:52 +0530 | [diff] [blame] | 844 | conn->pending_sec_level = sec_level; |
Marcel Holtmann | 96a3183 | 2009-02-12 16:23:03 +0100 | [diff] [blame] | 845 | else if (conn->link_mode & HCI_LM_AUTH) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 846 | return 1; |
| 847 | |
Johan Hedberg | 65cf686 | 2011-01-19 12:06:49 +0530 | [diff] [blame] | 848 | /* Make sure we preserve an existing MITM requirement*/ |
| 849 | auth_type |= (conn->auth_type & 0x01); |
Marcel Holtmann | 96a3183 | 2009-02-12 16:23:03 +0100 | [diff] [blame] | 850 | conn->auth_type = auth_type; |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 851 | conn->auth_initiator = 1; |
Marcel Holtmann | 96a3183 | 2009-02-12 16:23:03 +0100 | [diff] [blame] | 852 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 853 | if (!test_and_set_bit(HCI_CONN_AUTH_PEND, &conn->pend)) { |
| 854 | struct hci_cp_auth_requested cp; |
Peter Hurley | a5e5b08 | 2012-01-13 15:11:30 +0100 | [diff] [blame] | 855 | |
| 856 | /* encrypt must be pending if auth is also pending */ |
| 857 | set_bit(HCI_CONN_ENCRYPT_PEND, &conn->pend); |
| 858 | |
YOSHIFUJI Hideaki | aca3192 | 2007-03-25 20:12:50 -0700 | [diff] [blame] | 859 | cp.handle = cpu_to_le16(conn->handle); |
Marcel Holtmann | 40be492 | 2008-07-14 20:13:50 +0200 | [diff] [blame] | 860 | hci_send_cmd(conn->hdev, HCI_OP_AUTH_REQUESTED, |
| 861 | sizeof(cp), &cp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 862 | } |
Marcel Holtmann | 8c1b235 | 2009-01-15 21:58:04 +0100 | [diff] [blame] | 863 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 864 | return 0; |
| 865 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 866 | |
Marcel Holtmann | 8c1b235 | 2009-01-15 21:58:04 +0100 | [diff] [blame] | 867 | /* Enable security */ |
Marcel Holtmann | 0684e5f | 2009-02-09 02:48:38 +0100 | [diff] [blame] | 868 | int hci_conn_security(struct hci_conn *conn, __u8 sec_level, __u8 auth_type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 869 | { |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 870 | BT_DBG("conn %p %d %d", conn, sec_level, auth_type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 871 | |
Marcel Holtmann | 8c1b235 | 2009-01-15 21:58:04 +0100 | [diff] [blame] | 872 | if (sec_level == BT_SECURITY_SDP) |
| 873 | return 1; |
| 874 | |
Marcel Holtmann | 3fdca1e | 2009-04-28 09:04:55 -0700 | [diff] [blame] | 875 | if (sec_level == BT_SECURITY_LOW && |
| 876 | (!conn->ssp_mode || !conn->hdev->ssp_mode)) |
| 877 | return 1; |
Marcel Holtmann | 8c1b235 | 2009-01-15 21:58:04 +0100 | [diff] [blame] | 878 | |
Brian Gix | 2e2f50d | 2011-09-13 12:36:04 -0700 | [diff] [blame] | 879 | if (conn->type == LE_LINK) { |
| 880 | if (conn->pending_sec_level > sec_level) |
| 881 | sec_level = conn->pending_sec_level; |
Waldemar Rymarkiewicz | 13d3931 | 2011-04-28 12:07:55 +0200 | [diff] [blame] | 882 | |
Brian Gix | 2e2f50d | 2011-09-13 12:36:04 -0700 | [diff] [blame] | 883 | if (sec_level > conn->sec_level) |
| 884 | conn->pending_sec_level = sec_level; |
| 885 | hci_proto_connect_cfm(conn, 0); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 886 | return 0; |
Brian Gix | 2e2f50d | 2011-09-13 12:36:04 -0700 | [diff] [blame] | 887 | } else if (conn->link_mode & HCI_LM_ENCRYPT) { |
| 888 | return hci_conn_auth(conn, sec_level, auth_type); |
Ilia Kolomisnky | edc44dd | 2011-06-15 06:52:26 +0300 | [diff] [blame] | 889 | } else if (test_bit(HCI_CONN_ENCRYPT_PEND, &conn->pend)) { |
Brian Gix | 2e2f50d | 2011-09-13 12:36:04 -0700 | [diff] [blame] | 890 | return 0; |
| 891 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 892 | |
| 893 | if (hci_conn_auth(conn, sec_level, auth_type)) { |
| 894 | struct hci_cp_set_conn_encrypt cp; |
Prabhakaran Mc | afface8 | 2012-04-10 11:38:35 +0530 | [diff] [blame] | 895 | if (timer_pending(&conn->encrypt_pause_timer)) { |
| 896 | BT_INFO("encrypt_pause_timer is pending"); |
| 897 | return 0; |
| 898 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 899 | cp.handle = cpu_to_le16(conn->handle); |
| 900 | cp.encrypt = 1; |
| 901 | hci_send_cmd(conn->hdev, HCI_OP_SET_CONN_ENCRYPT, |
| 902 | sizeof(cp), &cp); |
| 903 | } |
| 904 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 905 | return 0; |
| 906 | } |
Marcel Holtmann | 8c1b235 | 2009-01-15 21:58:04 +0100 | [diff] [blame] | 907 | EXPORT_SYMBOL(hci_conn_security); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 908 | |
| 909 | /* Change link key */ |
| 910 | int hci_conn_change_link_key(struct hci_conn *conn) |
| 911 | { |
| 912 | BT_DBG("conn %p", conn); |
| 913 | |
| 914 | if (!test_and_set_bit(HCI_CONN_AUTH_PEND, &conn->pend)) { |
| 915 | struct hci_cp_change_conn_link_key cp; |
YOSHIFUJI Hideaki | aca3192 | 2007-03-25 20:12:50 -0700 | [diff] [blame] | 916 | cp.handle = cpu_to_le16(conn->handle); |
Marcel Holtmann | 40be492 | 2008-07-14 20:13:50 +0200 | [diff] [blame] | 917 | hci_send_cmd(conn->hdev, HCI_OP_CHANGE_CONN_LINK_KEY, |
| 918 | sizeof(cp), &cp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 919 | } |
Marcel Holtmann | 8c1b235 | 2009-01-15 21:58:04 +0100 | [diff] [blame] | 920 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 921 | return 0; |
| 922 | } |
| 923 | EXPORT_SYMBOL(hci_conn_change_link_key); |
| 924 | |
| 925 | /* Switch role */ |
Marcel Holtmann | 8c1b235 | 2009-01-15 21:58:04 +0100 | [diff] [blame] | 926 | int hci_conn_switch_role(struct hci_conn *conn, __u8 role) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 927 | { |
| 928 | BT_DBG("conn %p", conn); |
| 929 | |
| 930 | if (!role && conn->link_mode & HCI_LM_MASTER) |
| 931 | return 1; |
| 932 | |
| 933 | if (!test_and_set_bit(HCI_CONN_RSWITCH_PEND, &conn->pend)) { |
| 934 | struct hci_cp_switch_role cp; |
| 935 | bacpy(&cp.bdaddr, &conn->dst); |
| 936 | cp.role = role; |
Marcel Holtmann | a9de924 | 2007-10-20 13:33:56 +0200 | [diff] [blame] | 937 | hci_send_cmd(conn->hdev, HCI_OP_SWITCH_ROLE, sizeof(cp), &cp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 938 | } |
Marcel Holtmann | 8c1b235 | 2009-01-15 21:58:04 +0100 | [diff] [blame] | 939 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 940 | return 0; |
| 941 | } |
| 942 | EXPORT_SYMBOL(hci_conn_switch_role); |
| 943 | |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 944 | /* Enter active mode */ |
Jaikumar Ganesh | 514abe6 | 2011-05-23 18:06:04 -0700 | [diff] [blame] | 945 | 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] | 946 | { |
| 947 | struct hci_dev *hdev = conn->hdev; |
| 948 | |
| 949 | BT_DBG("conn %p mode %d", conn, conn->mode); |
| 950 | |
| 951 | if (test_bit(HCI_RAW, &hdev->flags)) |
| 952 | return; |
| 953 | |
Sunny Kapdi | 39eaba3 | 2012-07-22 21:29:38 -0700 | [diff] [blame^] | 954 | if (conn->type == LE_LINK) |
| 955 | return; |
| 956 | |
Jaikumar Ganesh | 514abe6 | 2011-05-23 18:06:04 -0700 | [diff] [blame] | 957 | if (conn->mode != HCI_CM_SNIFF) |
| 958 | goto timer; |
| 959 | |
| 960 | if (!conn->power_save && !force_active) |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 961 | goto timer; |
| 962 | |
| 963 | if (!test_and_set_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->pend)) { |
| 964 | struct hci_cp_exit_sniff_mode cp; |
YOSHIFUJI Hideaki | aca3192 | 2007-03-25 20:12:50 -0700 | [diff] [blame] | 965 | cp.handle = cpu_to_le16(conn->handle); |
Marcel Holtmann | a9de924 | 2007-10-20 13:33:56 +0200 | [diff] [blame] | 966 | hci_send_cmd(hdev, HCI_OP_EXIT_SNIFF_MODE, sizeof(cp), &cp); |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 967 | } |
| 968 | |
| 969 | timer: |
Rahul Kashyap | b44f9e2 | 2012-05-03 16:45:17 +0530 | [diff] [blame] | 970 | if (hdev->idle_timeout > 0) { |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 971 | mod_timer(&conn->idle_timer, |
| 972 | jiffies + msecs_to_jiffies(hdev->idle_timeout)); |
Rahul Kashyap | b44f9e2 | 2012-05-03 16:45:17 +0530 | [diff] [blame] | 973 | wake_lock(&conn->idle_lock); |
| 974 | } |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 975 | } |
| 976 | |
Archana Ramachandran | 26a752b | 2011-12-20 11:27:40 -0800 | [diff] [blame] | 977 | static inline void hci_conn_stop_rssi_timer(struct hci_conn *conn) |
| 978 | { |
| 979 | BT_DBG("conn %p", conn); |
| 980 | cancel_delayed_work(&conn->rssi_update_work); |
| 981 | } |
| 982 | |
| 983 | static inline void hci_conn_start_rssi_timer(struct hci_conn *conn, |
| 984 | u16 interval) |
| 985 | { |
| 986 | struct hci_dev *hdev = conn->hdev; |
| 987 | BT_DBG("conn %p, pending %d", conn, |
| 988 | delayed_work_pending(&conn->rssi_update_work)); |
| 989 | if (!delayed_work_pending(&conn->rssi_update_work)) { |
| 990 | queue_delayed_work(hdev->workqueue, &conn->rssi_update_work, |
| 991 | msecs_to_jiffies(interval)); |
| 992 | } |
| 993 | } |
| 994 | |
| 995 | void hci_conn_set_rssi_reporter(struct hci_conn *conn, |
| 996 | s8 rssi_threshold, u16 interval, u8 updateOnThreshExceed) |
| 997 | { |
| 998 | if (conn) { |
| 999 | conn->rssi_threshold = rssi_threshold; |
| 1000 | conn->rssi_update_interval = interval; |
| 1001 | conn->rssi_update_thresh_exceed = updateOnThreshExceed; |
| 1002 | hci_conn_start_rssi_timer(conn, interval); |
| 1003 | } |
| 1004 | } |
| 1005 | |
| 1006 | void hci_conn_unset_rssi_reporter(struct hci_conn *conn) |
| 1007 | { |
| 1008 | if (conn) { |
| 1009 | BT_DBG("Deleting the rssi_update_timer"); |
| 1010 | hci_conn_stop_rssi_timer(conn); |
| 1011 | } |
| 1012 | } |
| 1013 | |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 1014 | /* Enter sniff mode */ |
| 1015 | void hci_conn_enter_sniff_mode(struct hci_conn *conn) |
| 1016 | { |
| 1017 | struct hci_dev *hdev = conn->hdev; |
| 1018 | |
| 1019 | BT_DBG("conn %p mode %d", conn, conn->mode); |
| 1020 | |
| 1021 | if (test_bit(HCI_RAW, &hdev->flags)) |
| 1022 | return; |
| 1023 | |
Sunny Kapdi | 39eaba3 | 2012-07-22 21:29:38 -0700 | [diff] [blame^] | 1024 | if (conn->type == LE_LINK) |
| 1025 | return; |
| 1026 | |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 1027 | if (!lmp_sniff_capable(hdev) || !lmp_sniff_capable(conn)) |
| 1028 | return; |
| 1029 | |
Srinivas Krovvidi | 32ba935 | 2012-01-26 10:48:08 +0530 | [diff] [blame] | 1030 | if (conn->mode != HCI_CM_ACTIVE || |
| 1031 | !(conn->link_policy & HCI_LP_SNIFF) || |
| 1032 | (hci_find_link_key(hdev, &conn->dst) == NULL)) |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 1033 | return; |
| 1034 | |
| 1035 | if (lmp_sniffsubr_capable(hdev) && lmp_sniffsubr_capable(conn)) { |
| 1036 | struct hci_cp_sniff_subrate cp; |
YOSHIFUJI Hideaki | aca3192 | 2007-03-25 20:12:50 -0700 | [diff] [blame] | 1037 | cp.handle = cpu_to_le16(conn->handle); |
| 1038 | cp.max_latency = cpu_to_le16(0); |
| 1039 | cp.min_remote_timeout = cpu_to_le16(0); |
| 1040 | cp.min_local_timeout = cpu_to_le16(0); |
Marcel Holtmann | a9de924 | 2007-10-20 13:33:56 +0200 | [diff] [blame] | 1041 | hci_send_cmd(hdev, HCI_OP_SNIFF_SUBRATE, sizeof(cp), &cp); |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 1042 | } |
| 1043 | |
| 1044 | if (!test_and_set_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->pend)) { |
| 1045 | struct hci_cp_sniff_mode cp; |
YOSHIFUJI Hideaki | aca3192 | 2007-03-25 20:12:50 -0700 | [diff] [blame] | 1046 | cp.handle = cpu_to_le16(conn->handle); |
| 1047 | cp.max_interval = cpu_to_le16(hdev->sniff_max_interval); |
| 1048 | cp.min_interval = cpu_to_le16(hdev->sniff_min_interval); |
| 1049 | cp.attempt = cpu_to_le16(4); |
| 1050 | cp.timeout = cpu_to_le16(1); |
Marcel Holtmann | a9de924 | 2007-10-20 13:33:56 +0200 | [diff] [blame] | 1051 | hci_send_cmd(hdev, HCI_OP_SNIFF_MODE, sizeof(cp), &cp); |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 1052 | } |
| 1053 | } |
| 1054 | |
Peter Krystad | a8417e6 | 2012-03-21 16:58:17 -0700 | [diff] [blame] | 1055 | struct hci_chan *hci_chan_create(struct hci_chan *chan, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1056 | struct hci_ext_fs *tx_fs, struct hci_ext_fs *rx_fs) |
| 1057 | { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1058 | struct hci_cp_create_logical_link cp; |
| 1059 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1060 | chan->state = BT_CONNECT; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1061 | chan->tx_fs = *tx_fs; |
| 1062 | chan->rx_fs = *rx_fs; |
| 1063 | cp.phy_handle = chan->conn->handle; |
| 1064 | cp.tx_fs.id = chan->tx_fs.id; |
| 1065 | cp.tx_fs.type = chan->tx_fs.type; |
| 1066 | cp.tx_fs.max_sdu = cpu_to_le16(chan->tx_fs.max_sdu); |
| 1067 | cp.tx_fs.sdu_arr_time = cpu_to_le32(chan->tx_fs.sdu_arr_time); |
| 1068 | cp.tx_fs.acc_latency = cpu_to_le32(chan->tx_fs.acc_latency); |
| 1069 | cp.tx_fs.flush_to = cpu_to_le32(chan->tx_fs.flush_to); |
| 1070 | cp.rx_fs.id = chan->rx_fs.id; |
| 1071 | cp.rx_fs.type = chan->rx_fs.type; |
| 1072 | cp.rx_fs.max_sdu = cpu_to_le16(chan->rx_fs.max_sdu); |
| 1073 | cp.rx_fs.sdu_arr_time = cpu_to_le32(chan->rx_fs.sdu_arr_time); |
| 1074 | cp.rx_fs.acc_latency = cpu_to_le32(chan->rx_fs.acc_latency); |
| 1075 | cp.rx_fs.flush_to = cpu_to_le32(chan->rx_fs.flush_to); |
| 1076 | hci_conn_hold(chan->conn); |
Peter Krystad | a8417e6 | 2012-03-21 16:58:17 -0700 | [diff] [blame] | 1077 | if (chan->conn->out) |
| 1078 | hci_send_cmd(chan->conn->hdev, HCI_OP_CREATE_LOGICAL_LINK, |
| 1079 | sizeof(cp), &cp); |
| 1080 | else |
| 1081 | hci_send_cmd(chan->conn->hdev, HCI_OP_ACCEPT_LOGICAL_LINK, |
| 1082 | sizeof(cp), &cp); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1083 | return chan; |
| 1084 | } |
| 1085 | EXPORT_SYMBOL(hci_chan_create); |
| 1086 | |
| 1087 | void hci_chan_modify(struct hci_chan *chan, |
| 1088 | struct hci_ext_fs *tx_fs, struct hci_ext_fs *rx_fs) |
| 1089 | { |
| 1090 | struct hci_cp_flow_spec_modify cp; |
| 1091 | |
| 1092 | chan->tx_fs = *tx_fs; |
| 1093 | chan->rx_fs = *rx_fs; |
| 1094 | cp.log_handle = cpu_to_le16(chan->ll_handle); |
| 1095 | cp.tx_fs.id = tx_fs->id; |
| 1096 | cp.tx_fs.type = tx_fs->type; |
| 1097 | cp.tx_fs.max_sdu = cpu_to_le16(tx_fs->max_sdu); |
| 1098 | cp.tx_fs.sdu_arr_time = cpu_to_le32(tx_fs->sdu_arr_time); |
| 1099 | cp.tx_fs.acc_latency = cpu_to_le32(tx_fs->acc_latency); |
| 1100 | cp.tx_fs.flush_to = cpu_to_le32(tx_fs->flush_to); |
| 1101 | cp.rx_fs.id = rx_fs->id; |
| 1102 | cp.rx_fs.type = rx_fs->type; |
| 1103 | cp.rx_fs.max_sdu = cpu_to_le16(rx_fs->max_sdu); |
| 1104 | cp.rx_fs.sdu_arr_time = cpu_to_le32(rx_fs->sdu_arr_time); |
| 1105 | cp.rx_fs.acc_latency = cpu_to_le32(rx_fs->acc_latency); |
| 1106 | cp.rx_fs.flush_to = cpu_to_le32(rx_fs->flush_to); |
| 1107 | hci_conn_hold(chan->conn); |
| 1108 | hci_send_cmd(chan->conn->hdev, HCI_OP_FLOW_SPEC_MODIFY, sizeof(cp), |
| 1109 | &cp); |
| 1110 | } |
| 1111 | EXPORT_SYMBOL(hci_chan_modify); |
| 1112 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1113 | /* Drop all connection on the device */ |
Mat Martineau | 3b9239a | 2012-02-16 11:54:30 -0800 | [diff] [blame] | 1114 | void hci_conn_hash_flush(struct hci_dev *hdev, u8 is_process) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1115 | { |
| 1116 | struct hci_conn_hash *h = &hdev->conn_hash; |
| 1117 | struct list_head *p; |
| 1118 | |
| 1119 | BT_DBG("hdev %s", hdev->name); |
| 1120 | |
| 1121 | p = h->list.next; |
| 1122 | while (p != &h->list) { |
| 1123 | struct hci_conn *c; |
| 1124 | |
| 1125 | c = list_entry(p, struct hci_conn, list); |
| 1126 | p = p->next; |
| 1127 | |
| 1128 | c->state = BT_CLOSED; |
| 1129 | |
Mat Martineau | 3b9239a | 2012-02-16 11:54:30 -0800 | [diff] [blame] | 1130 | hci_proto_disconn_cfm(c, 0x16, is_process); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1131 | hci_conn_del(c); |
| 1132 | } |
| 1133 | } |
| 1134 | |
Marcel Holtmann | a9de924 | 2007-10-20 13:33:56 +0200 | [diff] [blame] | 1135 | /* Check pending connect attempts */ |
| 1136 | void hci_conn_check_pending(struct hci_dev *hdev) |
| 1137 | { |
| 1138 | struct hci_conn *conn; |
| 1139 | |
| 1140 | BT_DBG("hdev %s", hdev->name); |
| 1141 | |
| 1142 | hci_dev_lock(hdev); |
| 1143 | |
| 1144 | conn = hci_conn_hash_lookup_state(hdev, ACL_LINK, BT_CONNECT2); |
| 1145 | if (conn) |
| 1146 | hci_acl_connect(conn); |
| 1147 | |
| 1148 | hci_dev_unlock(hdev); |
| 1149 | } |
| 1150 | |
Marcel Holtmann | 9eba32b | 2009-08-22 14:19:26 -0700 | [diff] [blame] | 1151 | void hci_conn_hold_device(struct hci_conn *conn) |
| 1152 | { |
| 1153 | atomic_inc(&conn->devref); |
| 1154 | } |
| 1155 | EXPORT_SYMBOL(hci_conn_hold_device); |
| 1156 | |
| 1157 | void hci_conn_put_device(struct hci_conn *conn) |
| 1158 | { |
| 1159 | if (atomic_dec_and_test(&conn->devref)) |
| 1160 | hci_conn_del_sysfs(conn); |
| 1161 | } |
| 1162 | EXPORT_SYMBOL(hci_conn_put_device); |
| 1163 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1164 | int hci_get_conn_list(void __user *arg) |
| 1165 | { |
| 1166 | struct hci_conn_list_req req, *cl; |
| 1167 | struct hci_conn_info *ci; |
| 1168 | struct hci_dev *hdev; |
| 1169 | struct list_head *p; |
| 1170 | int n = 0, size, err; |
| 1171 | |
| 1172 | if (copy_from_user(&req, arg, sizeof(req))) |
| 1173 | return -EFAULT; |
| 1174 | |
| 1175 | if (!req.conn_num || req.conn_num > (PAGE_SIZE * 2) / sizeof(*ci)) |
| 1176 | return -EINVAL; |
| 1177 | |
| 1178 | size = sizeof(req) + req.conn_num * sizeof(*ci); |
| 1179 | |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 1180 | cl = kmalloc(size, GFP_KERNEL); |
| 1181 | if (!cl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1182 | return -ENOMEM; |
| 1183 | |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 1184 | hdev = hci_dev_get(req.dev_id); |
| 1185 | if (!hdev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1186 | kfree(cl); |
| 1187 | return -ENODEV; |
| 1188 | } |
| 1189 | |
| 1190 | ci = cl->conn_info; |
| 1191 | |
| 1192 | hci_dev_lock_bh(hdev); |
| 1193 | list_for_each(p, &hdev->conn_hash.list) { |
| 1194 | register struct hci_conn *c; |
| 1195 | c = list_entry(p, struct hci_conn, list); |
| 1196 | |
| 1197 | bacpy(&(ci + n)->bdaddr, &c->dst); |
| 1198 | (ci + n)->handle = c->handle; |
| 1199 | (ci + n)->type = c->type; |
| 1200 | (ci + n)->out = c->out; |
| 1201 | (ci + n)->state = c->state; |
| 1202 | (ci + n)->link_mode = c->link_mode; |
Nick Pelly | c172849 | 2009-12-09 00:15:41 -0800 | [diff] [blame] | 1203 | if (c->type == SCO_LINK) { |
| 1204 | (ci + n)->mtu = hdev->sco_mtu; |
| 1205 | (ci + n)->cnt = hdev->sco_cnt; |
| 1206 | (ci + n)->pkts = hdev->sco_pkts; |
| 1207 | } else { |
| 1208 | (ci + n)->mtu = hdev->acl_mtu; |
| 1209 | (ci + n)->cnt = hdev->acl_cnt; |
| 1210 | (ci + n)->pkts = hdev->acl_pkts; |
| 1211 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1212 | if (++n >= req.conn_num) |
| 1213 | break; |
| 1214 | } |
| 1215 | hci_dev_unlock_bh(hdev); |
| 1216 | |
| 1217 | cl->dev_id = hdev->id; |
| 1218 | cl->conn_num = n; |
| 1219 | size = sizeof(req) + n * sizeof(*ci); |
| 1220 | |
| 1221 | hci_dev_put(hdev); |
| 1222 | |
| 1223 | err = copy_to_user(arg, cl, size); |
| 1224 | kfree(cl); |
| 1225 | |
| 1226 | return err ? -EFAULT : 0; |
| 1227 | } |
| 1228 | |
| 1229 | int hci_get_conn_info(struct hci_dev *hdev, void __user *arg) |
| 1230 | { |
| 1231 | struct hci_conn_info_req req; |
| 1232 | struct hci_conn_info ci; |
| 1233 | struct hci_conn *conn; |
| 1234 | char __user *ptr = arg + sizeof(req); |
| 1235 | |
| 1236 | if (copy_from_user(&req, arg, sizeof(req))) |
| 1237 | return -EFAULT; |
| 1238 | |
| 1239 | hci_dev_lock_bh(hdev); |
| 1240 | conn = hci_conn_hash_lookup_ba(hdev, req.type, &req.bdaddr); |
| 1241 | if (conn) { |
| 1242 | bacpy(&ci.bdaddr, &conn->dst); |
| 1243 | ci.handle = conn->handle; |
| 1244 | ci.type = conn->type; |
| 1245 | ci.out = conn->out; |
| 1246 | ci.state = conn->state; |
| 1247 | ci.link_mode = conn->link_mode; |
Nick Pelly | c172849 | 2009-12-09 00:15:41 -0800 | [diff] [blame] | 1248 | if (req.type == SCO_LINK) { |
| 1249 | ci.mtu = hdev->sco_mtu; |
| 1250 | ci.cnt = hdev->sco_cnt; |
| 1251 | ci.pkts = hdev->sco_pkts; |
| 1252 | } else { |
| 1253 | ci.mtu = hdev->acl_mtu; |
| 1254 | ci.cnt = hdev->acl_cnt; |
| 1255 | ci.pkts = hdev->acl_pkts; |
| 1256 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1257 | ci.pending_sec_level = conn->pending_sec_level; |
| 1258 | ci.ssp_mode = conn->ssp_mode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1259 | } |
| 1260 | hci_dev_unlock_bh(hdev); |
| 1261 | |
| 1262 | if (!conn) |
| 1263 | return -ENOENT; |
| 1264 | |
| 1265 | return copy_to_user(ptr, &ci, sizeof(ci)) ? -EFAULT : 0; |
| 1266 | } |
Marcel Holtmann | 40be492 | 2008-07-14 20:13:50 +0200 | [diff] [blame] | 1267 | |
| 1268 | int hci_get_auth_info(struct hci_dev *hdev, void __user *arg) |
| 1269 | { |
| 1270 | struct hci_auth_info_req req; |
| 1271 | struct hci_conn *conn; |
| 1272 | |
| 1273 | if (copy_from_user(&req, arg, sizeof(req))) |
| 1274 | return -EFAULT; |
| 1275 | |
| 1276 | hci_dev_lock_bh(hdev); |
| 1277 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &req.bdaddr); |
| 1278 | if (conn) |
| 1279 | req.type = conn->auth_type; |
| 1280 | hci_dev_unlock_bh(hdev); |
| 1281 | |
| 1282 | if (!conn) |
| 1283 | return -ENOENT; |
| 1284 | |
| 1285 | return copy_to_user(arg, &req, sizeof(req)) ? -EFAULT : 0; |
| 1286 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1287 | |
| 1288 | int hci_set_auth_info(struct hci_dev *hdev, void __user *arg) |
| 1289 | { |
| 1290 | struct hci_auth_info_req req; |
| 1291 | struct hci_conn *conn; |
| 1292 | |
| 1293 | if (copy_from_user(&req, arg, sizeof(req))) |
| 1294 | return -EFAULT; |
| 1295 | |
| 1296 | hci_dev_lock_bh(hdev); |
| 1297 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &req.bdaddr); |
| 1298 | if (conn) |
| 1299 | conn->auth_type = req.type; |
| 1300 | hci_dev_unlock_bh(hdev); |
| 1301 | |
| 1302 | if (!conn) |
| 1303 | return -ENOENT; |
| 1304 | |
| 1305 | return copy_to_user(arg, &req, sizeof(req)) ? -EFAULT : 0; |
| 1306 | } |