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