blob: 0563af9fdee79d462161c0da0c029d1f9d5651c3 [file] [log] [blame]
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 BlueZ - Bluetooth protocol stack for Linux
Brian Gix3cd62042012-01-11 15:18:17 -08003 Copyright (c) 2000-2001, 2010-2012 Code Aurora Forum. All rights reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004
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 Hideaki8e87d142007-02-09 23:24:33 +090015 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 Torvalds1da177e2005-04-16 15:20:36 -070018 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +090020 ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
21 COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 SOFTWARE IS DISCLAIMED.
23*/
24
25/* Bluetooth HCI connection handling. */
26
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/module.h>
28
29#include <linux/types.h>
30#include <linux/errno.h>
31#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#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 Emeltchenko70f230202010-12-01 16:58:25 +020042#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <asm/unaligned.h>
44
45#include <net/bluetooth/bluetooth.h>
46#include <net/bluetooth/hci_core.h>
Brian Gixa94b6122012-02-23 16:07:10 -080047#include <net/bluetooth/l2cap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Brian Gixa94b6122012-02-23 16:07:10 -080049struct 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 Tervofcd89c02011-02-10 22:38:47 -030052{
Brian Gixa94b6122012-02-23 16:07:10 -080053 struct hci_conn *le;
Ville Tervofcd89c02011-02-10 22:38:47 -030054 struct hci_cp_le_create_conn cp;
Brian Gixa94b6122012-02-23 16:07:10 -080055 struct adv_entry *entry;
56 struct link_key *key;
Ville Tervofcd89c02011-02-10 22:38:47 -030057
Brian Gixa94b6122012-02-23 16:07:10 -080058 BT_DBG("%p", hdev);
Brian Gix114f3a62011-09-27 14:02:20 -070059
Brian Gixa94b6122012-02-23 16:07:10 -080060 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 Tervofcd89c02011-02-10 22:38:47 -030088
89 memset(&cp, 0, sizeof(cp));
Brian Gixa94b6122012-02-23 16:07:10 -080090 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 Tervofcd89c02011-02-10 22:38:47 -0300112
113 hci_send_cmd(hdev, HCI_OP_LE_CREATE_CONN, sizeof(cp), &cp);
Brian Gixa94b6122012-02-23 16:07:10 -0800114
115 return le;
Ville Tervofcd89c02011-02-10 22:38:47 -0300116}
Brian Gixa94b6122012-02-23 16:07:10 -0800117EXPORT_SYMBOL(hci_le_connect);
Ville Tervofcd89c02011-02-10 22:38:47 -0300118
119static 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 Holtmann4c67bc72006-10-15 17:30:56 +0200124void hci_acl_connect(struct hci_conn *conn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125{
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 Holtmanna8746412008-07-14 20:13:46 +0200133 conn->out = 1;
134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 conn->link_mode = HCI_LM_MASTER;
136
Marcel Holtmann4c67bc72006-10-15 17:30:56 +0200137 conn->attempt++;
138
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200139 conn->link_policy = hdev->link_policy;
140
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 memset(&cp, 0, sizeof(cp));
142 bacpy(&cp.bdaddr, &conn->dst);
143 cp.pscan_rep_mode = 0x02;
144
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200145 ie = hci_inquiry_cache_lookup(hdev, &conn->dst);
146 if (ie) {
Marcel Holtmann41a96212008-07-14 20:13:48 +0200147 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 Torvalds1da177e2005-04-16 15:20:36 -0700154 memcpy(conn->dev_class, ie->data.dev_class, 3);
Marcel Holtmann41a96212008-07-14 20:13:48 +0200155 conn->ssp_mode = ie->data.ssp_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 }
157
Marcel Holtmanna8746412008-07-14 20:13:46 +0200158 cp.pkt_type = cpu_to_le16(conn->pkt_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 if (lmp_rswitch_capable(hdev) && !(hdev->link_mode & HCI_LM_MASTER))
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200160 cp.role_switch = 0x01;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 else
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200162 cp.role_switch = 0x00;
Marcel Holtmann4c67bc72006-10-15 17:30:56 +0200163
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200164 hci_send_cmd(hdev, HCI_OP_CREATE_CONN, sizeof(cp), &cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165}
166
Marcel Holtmann6ac59342006-09-26 09:43:48 +0200167static 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 Holtmanna9de9242007-10-20 13:33:56 +0200177 hci_send_cmd(conn->hdev, HCI_OP_CREATE_CONN_CANCEL, sizeof(cp), &cp);
Marcel Holtmann6ac59342006-09-26 09:43:48 +0200178}
179
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180void hci_acl_disconn(struct hci_conn *conn, __u8 reason)
181{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 BT_DBG("%p", conn);
183
184 conn->state = BT_DISCONN;
185
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700186 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 Torvalds1da177e2005-04-16 15:20:36 -0700198}
199
200void 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 Holtmannefc76882009-02-06 09:13:37 +0100210 conn->attempt++;
211
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -0700212 cp.handle = cpu_to_le16(handle);
Marcel Holtmanna8746412008-07-14 20:13:46 +0200213 cp.pkt_type = cpu_to_le16(conn->pkt_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200215 hci_send_cmd(hdev, HCI_OP_ADD_SCO, sizeof(cp), &cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216}
217
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200218void 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 Holtmannefc76882009-02-06 09:13:37 +0100228 conn->attempt++;
229
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200230 cp.handle = cpu_to_le16(handle);
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200231
232 cp.tx_bandwidth = cpu_to_le32(0x00001f40);
233 cp.rx_bandwidth = cpu_to_le32(0x00001f40);
Kun Han Kim15b911f2011-07-08 09:30:28 -0700234 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 Holtmannb6a0dc82007-10-20 14:55:10 +0200248
249 hci_send_cmd(hdev, HCI_OP_SETUP_SYNC_CONN, sizeof(cp), &cp);
250}
251
Claudio Takahasi2ce603e2011-02-16 20:44:53 -0200252void 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}
270EXPORT_SYMBOL(hci_le_conn_update);
271
Archana Ramachandran26a752b2011-12-20 11:27:40 -0800272void 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}
282EXPORT_SYMBOL(hci_read_rssi);
283
Vinicius Costa Gomes735038c2011-06-09 18:50:47 -0300284void 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 Gix842bc5e2011-09-07 08:13:41 -0700297 memcpy(cp.rand, rand, sizeof(cp.rand));
Vinicius Costa Gomes735038c2011-06-09 18:50:47 -0300298
299 hci_send_cmd(hdev, HCI_OP_LE_START_ENC, sizeof(cp), &cp);
300}
301EXPORT_SYMBOL(hci_le_start_enc);
302
303void 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}
317EXPORT_SYMBOL(hci_le_ltk_reply);
318
319void 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 Holtmanne73439d2010-07-26 10:06:00 -0400333/* Device _must_ be locked */
334void 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 Torvalds1da177e2005-04-16 15:20:36 -0700354static void hci_conn_timeout(unsigned long arg)
355{
Marcel Holtmann04837f62006-07-03 10:02:33 +0200356 struct hci_conn *conn = (void *) arg;
357 struct hci_dev *hdev = conn->hdev;
Marcel Holtmann2950f212009-02-12 14:02:50 +0100358 __u8 reason;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
360 BT_DBG("conn %p state %d", conn, conn->state);
361
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 hci_dev_lock(hdev);
Marcel Holtmann6ac59342006-09-26 09:43:48 +0200363
364 switch (conn->state) {
365 case BT_CONNECT:
Marcel Holtmann769be972008-07-14 20:13:49 +0200366 case BT_CONNECT2:
Ville Tervofcd89c02011-02-10 22:38:47 -0300367 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 Holtmann6ac59342006-09-26 09:43:48 +0200373 break;
Marcel Holtmann769be972008-07-14 20:13:49 +0200374 case BT_CONFIG:
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900375 case BT_CONNECTED:
Brian Gix114f3a62011-09-27 14:02:20 -0700376 if (!atomic_read(&conn->refcnt)) {
377 reason = hci_proto_disconn_ind(conn);
378 hci_acl_disconn(conn, reason);
379 }
Marcel Holtmann6ac59342006-09-26 09:43:48 +0200380 break;
381 default:
Brian Gix114f3a62011-09-27 14:02:20 -0700382 if (!atomic_read(&conn->refcnt))
383 conn->state = BT_CLOSED;
Marcel Holtmann6ac59342006-09-26 09:43:48 +0200384 break;
385 }
386
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388}
389
Marcel Holtmann04837f62006-07-03 10:02:33 +0200390static void hci_conn_idle(unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391{
Marcel Holtmann04837f62006-07-03 10:02:33 +0200392 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 Torvalds1da177e2005-04-16 15:20:36 -0700397}
398
Archana Ramachandran26a752b2011-12-20 11:27:40 -0800399static 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 Mcafface82012-04-10 11:38:35 +0530411static 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 Pellybbcda3b2010-02-11 11:54:28 -0800429struct hci_conn *hci_conn_add(struct hci_dev *hdev, int type,
430 __u16 pkt_type, bdaddr_t *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431{
432 struct hci_conn *conn;
433
434 BT_DBG("%s dst %s", hdev->name, batostr(dst));
435
Marcel Holtmann04837f62006-07-03 10:02:33 +0200436 conn = kzalloc(sizeof(struct hci_conn), GFP_ATOMIC);
437 if (!conn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
440 bacpy(&conn->dst, dst);
Marcel Holtmanna8746412008-07-14 20:13:46 +0200441 conn->hdev = hdev;
442 conn->type = type;
443 conn->mode = HCI_CM_ACTIVE;
444 conn->state = BT_OPEN;
Andrei Emeltchenko93f19c92009-09-03 12:34:19 +0300445 conn->auth_type = HCI_AT_GENERAL_BONDING;
Johan Hedberg17fa4b92011-01-25 13:28:33 +0200446 conn->io_capability = hdev->io_capability;
Johan Hedberga9583552011-02-19 12:06:01 -0300447 conn->remote_auth = 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
Marcel Holtmann04837f62006-07-03 10:02:33 +0200449 conn->power_save = 1;
Marcel Holtmann052b30b2009-04-26 20:01:22 +0200450 conn->disc_timeout = HCI_DISCONN_TIMEOUT;
Rahul Kashyapb44f9e22012-05-03 16:45:17 +0530451 wake_lock_init(&conn->idle_lock, WAKE_LOCK_SUSPEND, "bt_idle");
Marcel Holtmann04837f62006-07-03 10:02:33 +0200452
Marcel Holtmanna8746412008-07-14 20:13:46 +0200453 switch (type) {
454 case ACL_LINK:
455 conn->pkt_type = hdev->pkt_type & ACL_PTYPE_MASK;
Rahul Kashyapd5553ba2012-01-02 19:27:19 +0530456 conn->link_policy = hdev->link_policy;
Marcel Holtmanna8746412008-07-14 20:13:46 +0200457 break;
458 case SCO_LINK:
Nick Pellybbcda3b2010-02-11 11:54:28 -0800459 if (!pkt_type)
460 pkt_type = SCO_ESCO_MASK;
Marcel Holtmanna8746412008-07-14 20:13:46 +0200461 case ESCO_LINK:
Nick Pellybbcda3b2010-02-11 11:54:28 -0800462 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 Holtmanna8746412008-07-14 20:13:46 +0200475 break;
476 }
477
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 skb_queue_head_init(&conn->data_q);
Marcel Holtmann04837f62006-07-03 10:02:33 +0200479
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800480 setup_timer(&conn->disc_timer, hci_conn_timeout, (unsigned long)conn);
481 setup_timer(&conn->idle_timer, hci_conn_idle, (unsigned long)conn);
Archana Ramachandran26a752b2011-12-20 11:27:40 -0800482 INIT_DELAYED_WORK(&conn->rssi_update_work, hci_conn_rssi_update);
Prabhakaran Mcafface82012-04-10 11:38:35 +0530483 setup_timer(&conn->encrypt_pause_timer, encryption_disabled_timeout,
484 (unsigned long)conn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
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 Holtmann9eba32b2009-08-22 14:19:26 -0700496 atomic_set(&conn->devref, 0);
497
Marcel Holtmanna67e8992009-05-02 18:24:06 -0700498 hci_conn_init_sysfs(conn);
499
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 tasklet_enable(&hdev->tx_task);
501
502 return conn;
503}
504
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700505struct 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 Torvalds1da177e2005-04-16 15:20:36 -0700517int 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 Gix3cd62042012-01-11 15:18:17 -0800523 /* Make sure no timers are running */
Marcel Holtmann04837f62006-07-03 10:02:33 +0200524 del_timer(&conn->idle_timer);
Rahul Kashyapb44f9e22012-05-03 16:45:17 +0530525 wake_lock_destroy(&conn->idle_lock);
Marcel Holtmann04837f62006-07-03 10:02:33 +0200526 del_timer(&conn->disc_timer);
Brian Gix3cd62042012-01-11 15:18:17 -0800527 del_timer(&conn->smp_timer);
Archana Ramachandran26a752b2011-12-20 11:27:40 -0800528 __cancel_delayed_work(&conn->rssi_update_work);
Prabhakaran Mcafface82012-04-10 11:38:35 +0530529 del_timer(&conn->encrypt_pause_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
Marcel Holtmann5b7f99092007-07-11 09:51:55 +0200531 if (conn->type == ACL_LINK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 struct hci_conn *sco = conn->link;
533 if (sco)
534 sco->link = NULL;
535
536 /* Unacked frames */
537 hdev->acl_cnt += conn->sent;
Ville Tervo6ed58ec2011-02-10 22:38:48 -0300538 } 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 Holtmann5b7f99092007-07-11 09:51:55 +0200543 } else {
544 struct hci_conn *acl = conn->link;
545 if (acl) {
546 acl->link = NULL;
547 hci_conn_put(acl);
548 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 }
550
551 tasklet_disable(&hdev->tx_task);
Marcel Holtmann7d0db0a2008-07-14 20:13:51 +0200552
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 hci_conn_hash_del(hdev, conn);
554 if (hdev->notify)
555 hdev->notify(hdev, HCI_NOTIFY_CONN_DEL);
Marcel Holtmann7d0db0a2008-07-14 20:13:51 +0200556
AnubhavGupta01d9e362011-12-17 17:14:51 +0530557 tasklet_schedule(&hdev->tx_task);
558
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 tasklet_enable(&hdev->tx_task);
Marcel Holtmann7d0db0a2008-07-14 20:13:51 +0200560
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 skb_queue_purge(&conn->data_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
Marcel Holtmann9eba32b2009-08-22 14:19:26 -0700563 hci_conn_put_device(conn);
Dave Young2ae9a6b2009-02-21 16:13:34 +0800564
Marcel Holtmann384943e2009-05-08 18:20:43 -0700565 hci_dev_put(hdev);
566
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700567 return 0;
568}
569
570struct 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 Krystada8417e62012-03-21 16:58:17 -0700590EXPORT_SYMBOL(hci_chan_add);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700591
592int 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 Targownik1be668d2011-06-30 16:30:44 -0300602
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 return 0;
604}
605
Peter Krystadd6a9ceb2011-12-01 15:44:54 -0800606int hci_chan_put(struct hci_chan *chan)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700607{
608 struct hci_cp_disconn_logical_link cp;
Mat Martineau9f8d4672011-12-14 12:10:46 -0800609 struct hci_conn *hcon;
610 u16 ll_handle;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700611
612 BT_DBG("chan %p refcnt %d", chan, atomic_read(&chan->refcnt));
613 if (!atomic_dec_and_test(&chan->refcnt))
Peter Krystadd6a9ceb2011-12-01 15:44:54 -0800614 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700615
Mat Martineau9f8d4672011-12-14 12:10:46 -0800616 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 Huntsman3f2bc4d2011-08-16 17:27:22 -0700625 sizeof(cp), &cp);
Mat Martineau9f8d4672011-12-14 12:10:46 -0800626 }
Peter Krystadd6a9ceb2011-12-01 15:44:54 -0800627
628 return 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700629}
630EXPORT_SYMBOL(hci_chan_put);
631
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632struct 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 Huntsman3f2bc4d2011-08-16 17:27:22 -0700645 if (d->dev_type != HCI_BREDR)
646 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 if (!test_bit(HCI_UP, &d->flags) || test_bit(HCI_RAW, &d->flags))
648 continue;
649
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900650 /* Simple routing:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 * 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}
672EXPORT_SYMBOL(hci_get_route);
673
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700674struct 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}
696EXPORT_SYMBOL(hci_dev_get_type);
697
698struct 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}
724EXPORT_SYMBOL(hci_dev_get_amp);
725
Ville Tervofcd89c02011-02-10 22:38:47 -0300726/* Create SCO, ACL or LE connection.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 * Device _must_ be locked */
Nick Pellybbcda3b2010-02-11 11:54:28 -0800728struct 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 Torvalds1da177e2005-04-16 15:20:36 -0700731{
732 struct hci_conn *acl;
Marcel Holtmann5b7f99092007-07-11 09:51:55 +0200733 struct hci_conn *sco;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
735 BT_DBG("%s dst %s", hdev->name, batostr(dst));
736
Brian Gixa94b6122012-02-23 16:07:10 -0800737 if (type == LE_LINK)
738 return hci_le_connect(hdev, pkt_type, dst, sec_level,
739 auth_type, NULL);
Ville Tervofcd89c02011-02-10 22:38:47 -0300740
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200741 acl = hci_conn_hash_lookup_ba(hdev, ACL_LINK, dst);
742 if (!acl) {
Nick Pellybbcda3b2010-02-11 11:54:28 -0800743 acl = hci_conn_add(hdev, ACL_LINK, 0, dst);
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200744 if (!acl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 return NULL;
746 }
747
748 hci_conn_hold(acl);
749
Marcel Holtmann09ab6f42008-09-09 07:19:20 +0200750 if (acl->state == BT_OPEN || acl->state == BT_CLOSED) {
Johan Hedberg765c2a92011-01-19 12:06:52 +0530751 acl->sec_level = BT_SECURITY_LOW;
752 acl->pending_sec_level = sec_level;
Marcel Holtmann09ab6f42008-09-09 07:19:20 +0200753 acl->auth_type = auth_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 hci_acl_connect(acl);
Marcel Holtmann09ab6f42008-09-09 07:19:20 +0200755 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
Marcel Holtmann5b7f99092007-07-11 09:51:55 +0200757 if (type == ACL_LINK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 return acl;
Marcel Holtmann5b7f99092007-07-11 09:51:55 +0200759
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200760 sco = hci_conn_hash_lookup_ba(hdev, type, dst);
761 if (!sco) {
Nick Pellybbcda3b2010-02-11 11:54:28 -0800762 sco = hci_conn_add(hdev, type, pkt_type, dst);
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200763 if (!sco) {
Marcel Holtmann5b7f99092007-07-11 09:51:55 +0200764 hci_conn_put(acl);
765 return NULL;
766 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 }
Marcel Holtmann5b7f99092007-07-11 09:51:55 +0200768
769 acl->link = sco;
770 sco->link = acl;
771
772 hci_conn_hold(sco);
773
774 if (acl->state == BT_CONNECTED &&
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200775 (sco->state == BT_OPEN || sco->state == BT_CLOSED)) {
Nick Pellyc3902162009-11-13 14:16:32 -0800776 acl->power_save = 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700777 hci_conn_enter_active_mode(acl, 1);
Nick Pellyc3902162009-11-13 14:16:32 -0800778
Marcel Holtmanne73439d2010-07-26 10:06:00 -0400779 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 Holtmannb6a0dc82007-10-20 14:55:10 +0200786 }
Marcel Holtmann5b7f99092007-07-11 09:51:55 +0200787
788 return sco;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789}
790EXPORT_SYMBOL(hci_connect);
791
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700792void hci_disconnect(struct hci_conn *conn, __u8 reason)
793{
794 BT_DBG("conn %p", conn);
795
Mat Martineau3b9239a2012-02-16 11:54:30 -0800796 hci_proto_disconn_cfm(conn, reason, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700797}
798EXPORT_SYMBOL(hci_disconnect);
799
800void 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 Holtmanne7c29cb2008-09-09 07:19:20 +0200822/* Check link security requirement */
823int 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}
833EXPORT_SYMBOL(hci_conn_check_link_mode);
834
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835/* Authenticate remote device */
Marcel Holtmann0684e5f2009-02-09 02:48:38 +0100836static int hci_conn_auth(struct hci_conn *conn, __u8 sec_level, __u8 auth_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837{
838 BT_DBG("conn %p", conn);
839
Johan Hedberg765c2a92011-01-19 12:06:52 +0530840 if (conn->pending_sec_level > sec_level)
841 sec_level = conn->pending_sec_level;
842
Marcel Holtmann96a31832009-02-12 16:23:03 +0100843 if (sec_level > conn->sec_level)
Johan Hedberg765c2a92011-01-19 12:06:52 +0530844 conn->pending_sec_level = sec_level;
Marcel Holtmann96a31832009-02-12 16:23:03 +0100845 else if (conn->link_mode & HCI_LM_AUTH)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 return 1;
847
Johan Hedberg65cf6862011-01-19 12:06:49 +0530848 /* Make sure we preserve an existing MITM requirement*/
849 auth_type |= (conn->auth_type & 0x01);
Marcel Holtmann96a31832009-02-12 16:23:03 +0100850 conn->auth_type = auth_type;
Brian Gixa68668b2011-08-11 15:49:36 -0700851 conn->auth_initiator = 1;
Marcel Holtmann96a31832009-02-12 16:23:03 +0100852
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 if (!test_and_set_bit(HCI_CONN_AUTH_PEND, &conn->pend)) {
854 struct hci_cp_auth_requested cp;
Peter Hurleya5e5b082012-01-13 15:11:30 +0100855
856 /* encrypt must be pending if auth is also pending */
857 set_bit(HCI_CONN_ENCRYPT_PEND, &conn->pend);
858
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -0700859 cp.handle = cpu_to_le16(conn->handle);
Marcel Holtmann40be4922008-07-14 20:13:50 +0200860 hci_send_cmd(conn->hdev, HCI_OP_AUTH_REQUESTED,
861 sizeof(cp), &cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 }
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100863
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 return 0;
865}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100867/* Enable security */
Marcel Holtmann0684e5f2009-02-09 02:48:38 +0100868int hci_conn_security(struct hci_conn *conn, __u8 sec_level, __u8 auth_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869{
Brian Gixa68668b2011-08-11 15:49:36 -0700870 BT_DBG("conn %p %d %d", conn, sec_level, auth_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100872 if (sec_level == BT_SECURITY_SDP)
873 return 1;
874
Marcel Holtmann3fdca1e2009-04-28 09:04:55 -0700875 if (sec_level == BT_SECURITY_LOW &&
876 (!conn->ssp_mode || !conn->hdev->ssp_mode))
877 return 1;
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100878
Brian Gix2e2f50d2011-09-13 12:36:04 -0700879 if (conn->type == LE_LINK) {
880 if (conn->pending_sec_level > sec_level)
881 sec_level = conn->pending_sec_level;
Waldemar Rymarkiewicz13d39312011-04-28 12:07:55 +0200882
Brian Gix2e2f50d2011-09-13 12:36:04 -0700883 if (sec_level > conn->sec_level)
884 conn->pending_sec_level = sec_level;
885 hci_proto_connect_cfm(conn, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700886 return 0;
Brian Gix2e2f50d2011-09-13 12:36:04 -0700887 } else if (conn->link_mode & HCI_LM_ENCRYPT) {
888 return hci_conn_auth(conn, sec_level, auth_type);
Ilia Kolomisnkyedc44dd2011-06-15 06:52:26 +0300889 } else if (test_bit(HCI_CONN_ENCRYPT_PEND, &conn->pend)) {
Brian Gix2e2f50d2011-09-13 12:36:04 -0700890 return 0;
891 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700892
893 if (hci_conn_auth(conn, sec_level, auth_type)) {
894 struct hci_cp_set_conn_encrypt cp;
Prabhakaran Mcafface82012-04-10 11:38:35 +0530895 if (timer_pending(&conn->encrypt_pause_timer)) {
896 BT_INFO("encrypt_pause_timer is pending");
897 return 0;
898 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700899 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 Torvalds1da177e2005-04-16 15:20:36 -0700905 return 0;
906}
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100907EXPORT_SYMBOL(hci_conn_security);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
909/* Change link key */
910int 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 Hideakiaca31922007-03-25 20:12:50 -0700916 cp.handle = cpu_to_le16(conn->handle);
Marcel Holtmann40be4922008-07-14 20:13:50 +0200917 hci_send_cmd(conn->hdev, HCI_OP_CHANGE_CONN_LINK_KEY,
918 sizeof(cp), &cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 }
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100920
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 return 0;
922}
923EXPORT_SYMBOL(hci_conn_change_link_key);
924
925/* Switch role */
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100926int hci_conn_switch_role(struct hci_conn *conn, __u8 role)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927{
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 Holtmanna9de9242007-10-20 13:33:56 +0200937 hci_send_cmd(conn->hdev, HCI_OP_SWITCH_ROLE, sizeof(cp), &cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 }
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100939
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 return 0;
941}
942EXPORT_SYMBOL(hci_conn_switch_role);
943
Marcel Holtmann04837f62006-07-03 10:02:33 +0200944/* Enter active mode */
Jaikumar Ganesh514abe62011-05-23 18:06:04 -0700945void hci_conn_enter_active_mode(struct hci_conn *conn, __u8 force_active)
Marcel Holtmann04837f62006-07-03 10:02:33 +0200946{
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 Kapdi39eaba32012-07-22 21:29:38 -0700954 if (conn->type == LE_LINK)
955 return;
956
Jaikumar Ganesh514abe62011-05-23 18:06:04 -0700957 if (conn->mode != HCI_CM_SNIFF)
958 goto timer;
959
960 if (!conn->power_save && !force_active)
Marcel Holtmann04837f62006-07-03 10:02:33 +0200961 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 Hideakiaca31922007-03-25 20:12:50 -0700965 cp.handle = cpu_to_le16(conn->handle);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200966 hci_send_cmd(hdev, HCI_OP_EXIT_SNIFF_MODE, sizeof(cp), &cp);
Marcel Holtmann04837f62006-07-03 10:02:33 +0200967 }
968
969timer:
Rahul Kashyapb44f9e22012-05-03 16:45:17 +0530970 if (hdev->idle_timeout > 0) {
Marcel Holtmann04837f62006-07-03 10:02:33 +0200971 mod_timer(&conn->idle_timer,
972 jiffies + msecs_to_jiffies(hdev->idle_timeout));
Rahul Kashyapb44f9e22012-05-03 16:45:17 +0530973 wake_lock(&conn->idle_lock);
974 }
Marcel Holtmann04837f62006-07-03 10:02:33 +0200975}
976
Archana Ramachandran26a752b2011-12-20 11:27:40 -0800977static 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
983static 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
995void 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
1006void 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 Holtmann04837f62006-07-03 10:02:33 +02001014/* Enter sniff mode */
1015void 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 Kapdi39eaba32012-07-22 21:29:38 -07001024 if (conn->type == LE_LINK)
1025 return;
1026
Marcel Holtmann04837f62006-07-03 10:02:33 +02001027 if (!lmp_sniff_capable(hdev) || !lmp_sniff_capable(conn))
1028 return;
1029
Srinivas Krovvidi32ba9352012-01-26 10:48:08 +05301030 if (conn->mode != HCI_CM_ACTIVE ||
1031 !(conn->link_policy & HCI_LP_SNIFF) ||
1032 (hci_find_link_key(hdev, &conn->dst) == NULL))
Marcel Holtmann04837f62006-07-03 10:02:33 +02001033 return;
1034
1035 if (lmp_sniffsubr_capable(hdev) && lmp_sniffsubr_capable(conn)) {
1036 struct hci_cp_sniff_subrate cp;
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -07001037 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 Holtmanna9de9242007-10-20 13:33:56 +02001041 hci_send_cmd(hdev, HCI_OP_SNIFF_SUBRATE, sizeof(cp), &cp);
Marcel Holtmann04837f62006-07-03 10:02:33 +02001042 }
1043
1044 if (!test_and_set_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->pend)) {
1045 struct hci_cp_sniff_mode cp;
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -07001046 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 Holtmanna9de9242007-10-20 13:33:56 +02001051 hci_send_cmd(hdev, HCI_OP_SNIFF_MODE, sizeof(cp), &cp);
Marcel Holtmann04837f62006-07-03 10:02:33 +02001052 }
1053}
1054
Peter Krystada8417e62012-03-21 16:58:17 -07001055struct hci_chan *hci_chan_create(struct hci_chan *chan,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001056 struct hci_ext_fs *tx_fs, struct hci_ext_fs *rx_fs)
1057{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001058 struct hci_cp_create_logical_link cp;
1059
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001060 chan->state = BT_CONNECT;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001061 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 Krystada8417e62012-03-21 16:58:17 -07001077 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 Huntsman3f2bc4d2011-08-16 17:27:22 -07001083 return chan;
1084}
1085EXPORT_SYMBOL(hci_chan_create);
1086
1087void 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}
1111EXPORT_SYMBOL(hci_chan_modify);
1112
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113/* Drop all connection on the device */
Mat Martineau3b9239a2012-02-16 11:54:30 -08001114void hci_conn_hash_flush(struct hci_dev *hdev, u8 is_process)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115{
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 Martineau3b9239a2012-02-16 11:54:30 -08001130 hci_proto_disconn_cfm(c, 0x16, is_process);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 hci_conn_del(c);
1132 }
1133}
1134
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001135/* Check pending connect attempts */
1136void 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 Holtmann9eba32b2009-08-22 14:19:26 -07001151void hci_conn_hold_device(struct hci_conn *conn)
1152{
1153 atomic_inc(&conn->devref);
1154}
1155EXPORT_SYMBOL(hci_conn_hold_device);
1156
1157void hci_conn_put_device(struct hci_conn *conn)
1158{
1159 if (atomic_dec_and_test(&conn->devref))
1160 hci_conn_del_sysfs(conn);
1161}
1162EXPORT_SYMBOL(hci_conn_put_device);
1163
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164int 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 Emeltchenko70f230202010-12-01 16:58:25 +02001180 cl = kmalloc(size, GFP_KERNEL);
1181 if (!cl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 return -ENOMEM;
1183
Andrei Emeltchenko70f230202010-12-01 16:58:25 +02001184 hdev = hci_dev_get(req.dev_id);
1185 if (!hdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 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 Pellyc1728492009-12-09 00:15:41 -08001203 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 Torvalds1da177e2005-04-16 15:20:36 -07001212 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
1229int 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 Pellyc1728492009-12-09 00:15:41 -08001248 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 Huntsman3f2bc4d2011-08-16 17:27:22 -07001257 ci.pending_sec_level = conn->pending_sec_level;
1258 ci.ssp_mode = conn->ssp_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 }
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 Holtmann40be4922008-07-14 20:13:50 +02001267
1268int 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 Huntsman3f2bc4d2011-08-16 17:27:22 -07001287
1288int 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}