blob: 08bf42da25a66236017659e6d125694df025f8af [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
Srinivas Krovvidi9dd35922012-07-26 15:10:28 +0530396 hci_dev_lock(conn->hdev);
Marcel Holtmann04837f62006-07-03 10:02:33 +0200397 hci_conn_enter_sniff_mode(conn);
Srinivas Krovvidi9dd35922012-07-26 15:10:28 +0530398 hci_dev_unlock(conn->hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399}
400
Archana Ramachandran26a752b2011-12-20 11:27:40 -0800401static void hci_conn_rssi_update(struct work_struct *work)
402{
403 struct delayed_work *delayed =
404 container_of(work, struct delayed_work, work);
405 struct hci_conn *conn =
406 container_of(delayed, struct hci_conn, rssi_update_work);
407
408 BT_DBG("conn %p mode %d", conn, conn->mode);
409
410 hci_read_rssi(conn);
411}
412
Prabhakaran Mcafface82012-04-10 11:38:35 +0530413static void encryption_disabled_timeout(unsigned long userdata)
414{
415 struct hci_conn *conn = (struct hci_conn *)userdata;
416 BT_INFO("conn %p Grace Prd Exp ", conn);
417
418 hci_encrypt_cfm(conn, 0, 0);
419
420 if (test_bit(HCI_CONN_ENCRYPT_PEND, &conn->pend)) {
421 struct hci_cp_set_conn_encrypt cp;
422 BT_INFO("HCI_CONN_ENCRYPT_PEND is set");
423 cp.handle = cpu_to_le16(conn->handle);
424 cp.encrypt = 1;
425 hci_send_cmd(conn->hdev, HCI_OP_SET_CONN_ENCRYPT,
426 sizeof(cp), &cp);
427 }
428
429}
430
Nick Pellybbcda3b2010-02-11 11:54:28 -0800431struct hci_conn *hci_conn_add(struct hci_dev *hdev, int type,
432 __u16 pkt_type, bdaddr_t *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433{
434 struct hci_conn *conn;
435
436 BT_DBG("%s dst %s", hdev->name, batostr(dst));
437
Marcel Holtmann04837f62006-07-03 10:02:33 +0200438 conn = kzalloc(sizeof(struct hci_conn), GFP_ATOMIC);
439 if (!conn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
442 bacpy(&conn->dst, dst);
Marcel Holtmanna8746412008-07-14 20:13:46 +0200443 conn->hdev = hdev;
444 conn->type = type;
445 conn->mode = HCI_CM_ACTIVE;
446 conn->state = BT_OPEN;
Andrei Emeltchenko93f19c92009-09-03 12:34:19 +0300447 conn->auth_type = HCI_AT_GENERAL_BONDING;
Johan Hedberg17fa4b92011-01-25 13:28:33 +0200448 conn->io_capability = hdev->io_capability;
Johan Hedberga9583552011-02-19 12:06:01 -0300449 conn->remote_auth = 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
Marcel Holtmann04837f62006-07-03 10:02:33 +0200451 conn->power_save = 1;
Marcel Holtmann052b30b2009-04-26 20:01:22 +0200452 conn->disc_timeout = HCI_DISCONN_TIMEOUT;
Rahul Kashyapb44f9e22012-05-03 16:45:17 +0530453 wake_lock_init(&conn->idle_lock, WAKE_LOCK_SUSPEND, "bt_idle");
Marcel Holtmann04837f62006-07-03 10:02:33 +0200454
Marcel Holtmanna8746412008-07-14 20:13:46 +0200455 switch (type) {
456 case ACL_LINK:
457 conn->pkt_type = hdev->pkt_type & ACL_PTYPE_MASK;
Rahul Kashyapd5553ba2012-01-02 19:27:19 +0530458 conn->link_policy = hdev->link_policy;
Marcel Holtmanna8746412008-07-14 20:13:46 +0200459 break;
460 case SCO_LINK:
Nick Pellybbcda3b2010-02-11 11:54:28 -0800461 if (!pkt_type)
462 pkt_type = SCO_ESCO_MASK;
Marcel Holtmanna8746412008-07-14 20:13:46 +0200463 case ESCO_LINK:
Nick Pellybbcda3b2010-02-11 11:54:28 -0800464 if (!pkt_type)
465 pkt_type = ALL_ESCO_MASK;
466 if (lmp_esco_capable(hdev)) {
467 /* HCI Setup Synchronous Connection Command uses
468 reverse logic on the EDR_ESCO_MASK bits */
469 conn->pkt_type = (pkt_type ^ EDR_ESCO_MASK) &
470 hdev->esco_type;
471 } else {
472 /* Legacy HCI Add Sco Connection Command uses a
473 shifted bitmask */
474 conn->pkt_type = (pkt_type << 5) & hdev->pkt_type &
475 SCO_PTYPE_MASK;
476 }
Marcel Holtmanna8746412008-07-14 20:13:46 +0200477 break;
478 }
479
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 skb_queue_head_init(&conn->data_q);
Marcel Holtmann04837f62006-07-03 10:02:33 +0200481
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800482 setup_timer(&conn->disc_timer, hci_conn_timeout, (unsigned long)conn);
483 setup_timer(&conn->idle_timer, hci_conn_idle, (unsigned long)conn);
Archana Ramachandran26a752b2011-12-20 11:27:40 -0800484 INIT_DELAYED_WORK(&conn->rssi_update_work, hci_conn_rssi_update);
Prabhakaran Mcafface82012-04-10 11:38:35 +0530485 setup_timer(&conn->encrypt_pause_timer, encryption_disabled_timeout,
486 (unsigned long)conn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
488 atomic_set(&conn->refcnt, 0);
489
490 hci_dev_hold(hdev);
491
492 tasklet_disable(&hdev->tx_task);
493
494 hci_conn_hash_add(hdev, conn);
495 if (hdev->notify)
496 hdev->notify(hdev, HCI_NOTIFY_CONN_ADD);
497
Marcel Holtmann9eba32b2009-08-22 14:19:26 -0700498 atomic_set(&conn->devref, 0);
499
Marcel Holtmanna67e8992009-05-02 18:24:06 -0700500 hci_conn_init_sysfs(conn);
501
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 tasklet_enable(&hdev->tx_task);
503
504 return conn;
505}
506
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700507struct hci_conn *hci_le_conn_add(struct hci_dev *hdev, bdaddr_t *dst,
508 __u8 addr_type)
509{
510 struct hci_conn *conn = hci_conn_add(hdev, LE_LINK, 0, dst);
511 if (!conn)
512 return NULL;
513
514 conn->dst_type = addr_type;
515
516 return conn;
517}
518
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519int hci_conn_del(struct hci_conn *conn)
520{
521 struct hci_dev *hdev = conn->hdev;
522
523 BT_DBG("%s conn %p handle %d", hdev->name, conn, conn->handle);
524
Brian Gix3cd62042012-01-11 15:18:17 -0800525 /* Make sure no timers are running */
Marcel Holtmann04837f62006-07-03 10:02:33 +0200526 del_timer(&conn->idle_timer);
Rahul Kashyapb44f9e22012-05-03 16:45:17 +0530527 wake_lock_destroy(&conn->idle_lock);
Marcel Holtmann04837f62006-07-03 10:02:33 +0200528 del_timer(&conn->disc_timer);
Brian Gix3cd62042012-01-11 15:18:17 -0800529 del_timer(&conn->smp_timer);
Archana Ramachandran26a752b2011-12-20 11:27:40 -0800530 __cancel_delayed_work(&conn->rssi_update_work);
Prabhakaran Mcafface82012-04-10 11:38:35 +0530531 del_timer(&conn->encrypt_pause_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200533 if (conn->type == ACL_LINK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 struct hci_conn *sco = conn->link;
535 if (sco)
536 sco->link = NULL;
537
538 /* Unacked frames */
539 hdev->acl_cnt += conn->sent;
Ville Tervo6ed58ec2011-02-10 22:38:48 -0300540 } else if (conn->type == LE_LINK) {
541 if (hdev->le_pkts)
542 hdev->le_cnt += conn->sent;
543 else
544 hdev->acl_cnt += conn->sent;
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200545 } else {
546 struct hci_conn *acl = conn->link;
547 if (acl) {
548 acl->link = NULL;
549 hci_conn_put(acl);
550 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 }
552
553 tasklet_disable(&hdev->tx_task);
Marcel Holtmann7d0db0a2008-07-14 20:13:51 +0200554
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 hci_conn_hash_del(hdev, conn);
556 if (hdev->notify)
557 hdev->notify(hdev, HCI_NOTIFY_CONN_DEL);
Marcel Holtmann7d0db0a2008-07-14 20:13:51 +0200558
AnubhavGupta01d9e362011-12-17 17:14:51 +0530559 tasklet_schedule(&hdev->tx_task);
560
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 tasklet_enable(&hdev->tx_task);
Marcel Holtmann7d0db0a2008-07-14 20:13:51 +0200562
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 skb_queue_purge(&conn->data_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
Marcel Holtmann9eba32b2009-08-22 14:19:26 -0700565 hci_conn_put_device(conn);
Dave Young2ae9a6b2009-02-21 16:13:34 +0800566
Marcel Holtmann384943e2009-05-08 18:20:43 -0700567 hci_dev_put(hdev);
568
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700569 return 0;
570}
571
572struct hci_chan *hci_chan_add(struct hci_dev *hdev)
573{
574 struct hci_chan *chan;
575
576 BT_DBG("%s", hdev->name);
577
578 chan = kzalloc(sizeof(struct hci_chan), GFP_ATOMIC);
579 if (!chan)
580 return NULL;
581
582 atomic_set(&chan->refcnt, 0);
583
584 hci_dev_hold(hdev);
585
586 chan->hdev = hdev;
587
588 list_add(&chan->list, &hdev->chan_list.list);
589
590 return chan;
591}
Peter Krystada8417e62012-03-21 16:58:17 -0700592EXPORT_SYMBOL(hci_chan_add);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700593
594int hci_chan_del(struct hci_chan *chan)
595{
596 BT_DBG("%s chan %p", chan->hdev->name, chan);
597
598 list_del(&chan->list);
599
600 hci_conn_put(chan->conn);
601 hci_dev_put(chan->hdev);
602
603 kfree(chan);
Tomas Targownik1be668d2011-06-30 16:30:44 -0300604
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 return 0;
606}
607
Peter Krystadd6a9ceb2011-12-01 15:44:54 -0800608int hci_chan_put(struct hci_chan *chan)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700609{
610 struct hci_cp_disconn_logical_link cp;
Mat Martineau9f8d4672011-12-14 12:10:46 -0800611 struct hci_conn *hcon;
612 u16 ll_handle;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700613
614 BT_DBG("chan %p refcnt %d", chan, atomic_read(&chan->refcnt));
615 if (!atomic_dec_and_test(&chan->refcnt))
Peter Krystadd6a9ceb2011-12-01 15:44:54 -0800616 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700617
Mat Martineau9f8d4672011-12-14 12:10:46 -0800618 hcon = chan->conn;
619 ll_handle = chan->ll_handle;
620
621 hci_chan_del(chan);
622
623 BT_DBG("chan->conn->state %d", hcon->state);
624 if (hcon->state == BT_CONNECTED) {
625 cp.log_handle = cpu_to_le16(ll_handle);
626 hci_send_cmd(hcon->hdev, HCI_OP_DISCONN_LOGICAL_LINK,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700627 sizeof(cp), &cp);
Mat Martineau9f8d4672011-12-14 12:10:46 -0800628 }
Peter Krystadd6a9ceb2011-12-01 15:44:54 -0800629
630 return 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700631}
632EXPORT_SYMBOL(hci_chan_put);
633
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634struct hci_dev *hci_get_route(bdaddr_t *dst, bdaddr_t *src)
635{
636 int use_src = bacmp(src, BDADDR_ANY);
637 struct hci_dev *hdev = NULL;
638 struct list_head *p;
639
640 BT_DBG("%s -> %s", batostr(src), batostr(dst));
641
642 read_lock_bh(&hci_dev_list_lock);
643
644 list_for_each(p, &hci_dev_list) {
645 struct hci_dev *d = list_entry(p, struct hci_dev, list);
646
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700647 if (d->dev_type != HCI_BREDR)
648 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 if (!test_bit(HCI_UP, &d->flags) || test_bit(HCI_RAW, &d->flags))
650 continue;
651
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900652 /* Simple routing:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 * No source address - find interface with bdaddr != dst
654 * Source address - find interface with bdaddr == src
655 */
656
657 if (use_src) {
658 if (!bacmp(&d->bdaddr, src)) {
659 hdev = d; break;
660 }
661 } else {
662 if (bacmp(&d->bdaddr, dst)) {
663 hdev = d; break;
664 }
665 }
666 }
667
668 if (hdev)
669 hdev = hci_dev_hold(hdev);
670
671 read_unlock_bh(&hci_dev_list_lock);
672 return hdev;
673}
674EXPORT_SYMBOL(hci_get_route);
675
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700676struct hci_dev *hci_dev_get_type(u8 amp_type)
677{
678 struct hci_dev *hdev = NULL;
679 struct hci_dev *d;
680
681 BT_DBG("amp_type %d", amp_type);
682
683 read_lock_bh(&hci_dev_list_lock);
684
685 list_for_each_entry(d, &hci_dev_list, list) {
686 if ((d->amp_type == amp_type) && test_bit(HCI_UP, &d->flags)) {
687 hdev = d;
688 break;
689 }
690 }
691
692 if (hdev)
693 hdev = hci_dev_hold(hdev);
694
695 read_unlock_bh(&hci_dev_list_lock);
696 return hdev;
697}
698EXPORT_SYMBOL(hci_dev_get_type);
699
700struct hci_dev *hci_dev_get_amp(bdaddr_t *dst)
701{
702 struct hci_dev *d;
703 struct hci_dev *hdev = NULL;
704
705 BT_DBG("%s dst %s", hdev->name, batostr(dst));
706
707 read_lock_bh(&hci_dev_list_lock);
708
709 list_for_each_entry(d, &hci_dev_list, list) {
710 struct hci_conn *conn;
711 if (d->dev_type == HCI_BREDR)
712 continue;
713 conn = hci_conn_hash_lookup_ba(d, ACL_LINK, dst);
714 if (conn) {
715 hdev = d;
716 break;
717 }
718 }
719
720 if (hdev)
721 hdev = hci_dev_hold(hdev);
722
723 read_unlock_bh(&hci_dev_list_lock);
724 return hdev;
725}
726EXPORT_SYMBOL(hci_dev_get_amp);
727
Ville Tervofcd89c02011-02-10 22:38:47 -0300728/* Create SCO, ACL or LE connection.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 * Device _must_ be locked */
Nick Pellybbcda3b2010-02-11 11:54:28 -0800730struct hci_conn *hci_connect(struct hci_dev *hdev, int type,
731 __u16 pkt_type, bdaddr_t *dst,
732 __u8 sec_level, __u8 auth_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733{
734 struct hci_conn *acl;
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200735 struct hci_conn *sco;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
737 BT_DBG("%s dst %s", hdev->name, batostr(dst));
738
Brian Gixa94b6122012-02-23 16:07:10 -0800739 if (type == LE_LINK)
740 return hci_le_connect(hdev, pkt_type, dst, sec_level,
741 auth_type, NULL);
Ville Tervofcd89c02011-02-10 22:38:47 -0300742
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200743 acl = hci_conn_hash_lookup_ba(hdev, ACL_LINK, dst);
744 if (!acl) {
Nick Pellybbcda3b2010-02-11 11:54:28 -0800745 acl = hci_conn_add(hdev, ACL_LINK, 0, dst);
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200746 if (!acl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 return NULL;
748 }
749
750 hci_conn_hold(acl);
751
Marcel Holtmann09ab6f42008-09-09 07:19:20 +0200752 if (acl->state == BT_OPEN || acl->state == BT_CLOSED) {
Johan Hedberg765c2a92011-01-19 12:06:52 +0530753 acl->sec_level = BT_SECURITY_LOW;
754 acl->pending_sec_level = sec_level;
Marcel Holtmann09ab6f42008-09-09 07:19:20 +0200755 acl->auth_type = auth_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 hci_acl_connect(acl);
Marcel Holtmann09ab6f42008-09-09 07:19:20 +0200757 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200759 if (type == ACL_LINK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 return acl;
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200761
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200762 sco = hci_conn_hash_lookup_ba(hdev, type, dst);
763 if (!sco) {
Nick Pellybbcda3b2010-02-11 11:54:28 -0800764 sco = hci_conn_add(hdev, type, pkt_type, dst);
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200765 if (!sco) {
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200766 hci_conn_put(acl);
767 return NULL;
768 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 }
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200770
771 acl->link = sco;
772 sco->link = acl;
773
774 hci_conn_hold(sco);
775
776 if (acl->state == BT_CONNECTED &&
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200777 (sco->state == BT_OPEN || sco->state == BT_CLOSED)) {
Nick Pellyc3902162009-11-13 14:16:32 -0800778 acl->power_save = 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700779 hci_conn_enter_active_mode(acl, 1);
Nick Pellyc3902162009-11-13 14:16:32 -0800780
Marcel Holtmanne73439d2010-07-26 10:06:00 -0400781 if (test_bit(HCI_CONN_MODE_CHANGE_PEND, &acl->pend)) {
782 /* defer SCO setup until mode change completed */
783 set_bit(HCI_CONN_SCO_SETUP_PEND, &acl->pend);
784 return sco;
785 }
786
787 hci_sco_setup(acl, 0x00);
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200788 }
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200789
790 return sco;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791}
792EXPORT_SYMBOL(hci_connect);
793
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700794void hci_disconnect(struct hci_conn *conn, __u8 reason)
795{
796 BT_DBG("conn %p", conn);
797
Mat Martineau3b9239a2012-02-16 11:54:30 -0800798 hci_proto_disconn_cfm(conn, reason, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700799}
800EXPORT_SYMBOL(hci_disconnect);
801
802void hci_disconnect_amp(struct hci_conn *conn, __u8 reason)
803{
804 struct hci_dev *hdev = NULL;
805
806 BT_DBG("conn %p", conn);
807
808 read_lock_bh(&hci_dev_list_lock);
809
810 list_for_each_entry(hdev, &hci_dev_list, list) {
811 struct hci_conn *c;
812 if (hdev == conn->hdev)
813 continue;
814 if (hdev->amp_type == HCI_BREDR)
815 continue;
816 c = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &conn->dst);
817 if (c)
818 hci_disconnect(c, reason);
819 }
820
821 read_unlock_bh(&hci_dev_list_lock);
822}
823
Marcel Holtmanne7c29cb2008-09-09 07:19:20 +0200824/* Check link security requirement */
825int hci_conn_check_link_mode(struct hci_conn *conn)
826{
827 BT_DBG("conn %p", conn);
828
829 if (conn->ssp_mode > 0 && conn->hdev->ssp_mode > 0 &&
830 !(conn->link_mode & HCI_LM_ENCRYPT))
831 return 0;
832
833 return 1;
834}
835EXPORT_SYMBOL(hci_conn_check_link_mode);
836
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837/* Authenticate remote device */
Marcel Holtmann0684e5f2009-02-09 02:48:38 +0100838static int hci_conn_auth(struct hci_conn *conn, __u8 sec_level, __u8 auth_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839{
840 BT_DBG("conn %p", conn);
841
Johan Hedberg765c2a92011-01-19 12:06:52 +0530842 if (conn->pending_sec_level > sec_level)
843 sec_level = conn->pending_sec_level;
844
Marcel Holtmann96a31832009-02-12 16:23:03 +0100845 if (sec_level > conn->sec_level)
Johan Hedberg765c2a92011-01-19 12:06:52 +0530846 conn->pending_sec_level = sec_level;
Marcel Holtmann96a31832009-02-12 16:23:03 +0100847 else if (conn->link_mode & HCI_LM_AUTH)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 return 1;
849
Johan Hedberg65cf6862011-01-19 12:06:49 +0530850 /* Make sure we preserve an existing MITM requirement*/
851 auth_type |= (conn->auth_type & 0x01);
Marcel Holtmann96a31832009-02-12 16:23:03 +0100852 conn->auth_type = auth_type;
Brian Gixa68668b2011-08-11 15:49:36 -0700853 conn->auth_initiator = 1;
Marcel Holtmann96a31832009-02-12 16:23:03 +0100854
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 if (!test_and_set_bit(HCI_CONN_AUTH_PEND, &conn->pend)) {
856 struct hci_cp_auth_requested cp;
Peter Hurleya5e5b082012-01-13 15:11:30 +0100857
858 /* encrypt must be pending if auth is also pending */
859 set_bit(HCI_CONN_ENCRYPT_PEND, &conn->pend);
860
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -0700861 cp.handle = cpu_to_le16(conn->handle);
Marcel Holtmann40be4922008-07-14 20:13:50 +0200862 hci_send_cmd(conn->hdev, HCI_OP_AUTH_REQUESTED,
863 sizeof(cp), &cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 }
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100865
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 return 0;
867}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100869/* Enable security */
Marcel Holtmann0684e5f2009-02-09 02:48:38 +0100870int hci_conn_security(struct hci_conn *conn, __u8 sec_level, __u8 auth_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871{
Brian Gixa68668b2011-08-11 15:49:36 -0700872 BT_DBG("conn %p %d %d", conn, sec_level, auth_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100874 if (sec_level == BT_SECURITY_SDP)
875 return 1;
876
Marcel Holtmann3fdca1e2009-04-28 09:04:55 -0700877 if (sec_level == BT_SECURITY_LOW &&
878 (!conn->ssp_mode || !conn->hdev->ssp_mode))
879 return 1;
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100880
Brian Gix2e2f50d2011-09-13 12:36:04 -0700881 if (conn->type == LE_LINK) {
882 if (conn->pending_sec_level > sec_level)
883 sec_level = conn->pending_sec_level;
Waldemar Rymarkiewicz13d39312011-04-28 12:07:55 +0200884
Brian Gix2e2f50d2011-09-13 12:36:04 -0700885 if (sec_level > conn->sec_level)
886 conn->pending_sec_level = sec_level;
887 hci_proto_connect_cfm(conn, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700888 return 0;
Brian Gix2e2f50d2011-09-13 12:36:04 -0700889 } else if (conn->link_mode & HCI_LM_ENCRYPT) {
890 return hci_conn_auth(conn, sec_level, auth_type);
Ilia Kolomisnkyedc44dd2011-06-15 06:52:26 +0300891 } else if (test_bit(HCI_CONN_ENCRYPT_PEND, &conn->pend)) {
Brian Gix2e2f50d2011-09-13 12:36:04 -0700892 return 0;
893 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700894
895 if (hci_conn_auth(conn, sec_level, auth_type)) {
896 struct hci_cp_set_conn_encrypt cp;
Prabhakaran Mcafface82012-04-10 11:38:35 +0530897 if (timer_pending(&conn->encrypt_pause_timer)) {
898 BT_INFO("encrypt_pause_timer is pending");
899 return 0;
900 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700901 cp.handle = cpu_to_le16(conn->handle);
902 cp.encrypt = 1;
903 hci_send_cmd(conn->hdev, HCI_OP_SET_CONN_ENCRYPT,
904 sizeof(cp), &cp);
905 }
906
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 return 0;
908}
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100909EXPORT_SYMBOL(hci_conn_security);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
911/* Change link key */
912int hci_conn_change_link_key(struct hci_conn *conn)
913{
914 BT_DBG("conn %p", conn);
915
916 if (!test_and_set_bit(HCI_CONN_AUTH_PEND, &conn->pend)) {
917 struct hci_cp_change_conn_link_key cp;
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -0700918 cp.handle = cpu_to_le16(conn->handle);
Marcel Holtmann40be4922008-07-14 20:13:50 +0200919 hci_send_cmd(conn->hdev, HCI_OP_CHANGE_CONN_LINK_KEY,
920 sizeof(cp), &cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 }
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100922
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 return 0;
924}
925EXPORT_SYMBOL(hci_conn_change_link_key);
926
927/* Switch role */
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100928int hci_conn_switch_role(struct hci_conn *conn, __u8 role)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929{
930 BT_DBG("conn %p", conn);
931
932 if (!role && conn->link_mode & HCI_LM_MASTER)
933 return 1;
934
935 if (!test_and_set_bit(HCI_CONN_RSWITCH_PEND, &conn->pend)) {
936 struct hci_cp_switch_role cp;
937 bacpy(&cp.bdaddr, &conn->dst);
938 cp.role = role;
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200939 hci_send_cmd(conn->hdev, HCI_OP_SWITCH_ROLE, sizeof(cp), &cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 }
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100941
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 return 0;
943}
944EXPORT_SYMBOL(hci_conn_switch_role);
945
Marcel Holtmann04837f62006-07-03 10:02:33 +0200946/* Enter active mode */
Jaikumar Ganesh514abe62011-05-23 18:06:04 -0700947void hci_conn_enter_active_mode(struct hci_conn *conn, __u8 force_active)
Marcel Holtmann04837f62006-07-03 10:02:33 +0200948{
949 struct hci_dev *hdev = conn->hdev;
950
951 BT_DBG("conn %p mode %d", conn, conn->mode);
952
953 if (test_bit(HCI_RAW, &hdev->flags))
954 return;
955
Sunny Kapdi39eaba32012-07-22 21:29:38 -0700956 if (conn->type == LE_LINK)
957 return;
958
Jaikumar Ganesh514abe62011-05-23 18:06:04 -0700959 if (conn->mode != HCI_CM_SNIFF)
960 goto timer;
961
962 if (!conn->power_save && !force_active)
Marcel Holtmann04837f62006-07-03 10:02:33 +0200963 goto timer;
964
965 if (!test_and_set_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->pend)) {
966 struct hci_cp_exit_sniff_mode cp;
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -0700967 cp.handle = cpu_to_le16(conn->handle);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200968 hci_send_cmd(hdev, HCI_OP_EXIT_SNIFF_MODE, sizeof(cp), &cp);
Marcel Holtmann04837f62006-07-03 10:02:33 +0200969 }
970
971timer:
Rahul Kashyapb44f9e22012-05-03 16:45:17 +0530972 if (hdev->idle_timeout > 0) {
Marcel Holtmann04837f62006-07-03 10:02:33 +0200973 mod_timer(&conn->idle_timer,
974 jiffies + msecs_to_jiffies(hdev->idle_timeout));
Rahul Kashyapb44f9e22012-05-03 16:45:17 +0530975 wake_lock(&conn->idle_lock);
976 }
Marcel Holtmann04837f62006-07-03 10:02:33 +0200977}
978
Archana Ramachandran26a752b2011-12-20 11:27:40 -0800979static inline void hci_conn_stop_rssi_timer(struct hci_conn *conn)
980{
981 BT_DBG("conn %p", conn);
982 cancel_delayed_work(&conn->rssi_update_work);
983}
984
985static inline void hci_conn_start_rssi_timer(struct hci_conn *conn,
986 u16 interval)
987{
988 struct hci_dev *hdev = conn->hdev;
989 BT_DBG("conn %p, pending %d", conn,
990 delayed_work_pending(&conn->rssi_update_work));
991 if (!delayed_work_pending(&conn->rssi_update_work)) {
992 queue_delayed_work(hdev->workqueue, &conn->rssi_update_work,
993 msecs_to_jiffies(interval));
994 }
995}
996
997void hci_conn_set_rssi_reporter(struct hci_conn *conn,
998 s8 rssi_threshold, u16 interval, u8 updateOnThreshExceed)
999{
1000 if (conn) {
1001 conn->rssi_threshold = rssi_threshold;
1002 conn->rssi_update_interval = interval;
1003 conn->rssi_update_thresh_exceed = updateOnThreshExceed;
1004 hci_conn_start_rssi_timer(conn, interval);
1005 }
1006}
1007
1008void hci_conn_unset_rssi_reporter(struct hci_conn *conn)
1009{
1010 if (conn) {
1011 BT_DBG("Deleting the rssi_update_timer");
1012 hci_conn_stop_rssi_timer(conn);
1013 }
1014}
1015
Marcel Holtmann04837f62006-07-03 10:02:33 +02001016/* Enter sniff mode */
1017void hci_conn_enter_sniff_mode(struct hci_conn *conn)
1018{
1019 struct hci_dev *hdev = conn->hdev;
1020
1021 BT_DBG("conn %p mode %d", conn, conn->mode);
1022
1023 if (test_bit(HCI_RAW, &hdev->flags))
1024 return;
1025
Sunny Kapdi39eaba32012-07-22 21:29:38 -07001026 if (conn->type == LE_LINK)
1027 return;
1028
Marcel Holtmann04837f62006-07-03 10:02:33 +02001029 if (!lmp_sniff_capable(hdev) || !lmp_sniff_capable(conn))
1030 return;
1031
Srinivas Krovvidi32ba9352012-01-26 10:48:08 +05301032 if (conn->mode != HCI_CM_ACTIVE ||
1033 !(conn->link_policy & HCI_LP_SNIFF) ||
1034 (hci_find_link_key(hdev, &conn->dst) == NULL))
Marcel Holtmann04837f62006-07-03 10:02:33 +02001035 return;
1036
1037 if (lmp_sniffsubr_capable(hdev) && lmp_sniffsubr_capable(conn)) {
1038 struct hci_cp_sniff_subrate cp;
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -07001039 cp.handle = cpu_to_le16(conn->handle);
1040 cp.max_latency = cpu_to_le16(0);
1041 cp.min_remote_timeout = cpu_to_le16(0);
1042 cp.min_local_timeout = cpu_to_le16(0);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001043 hci_send_cmd(hdev, HCI_OP_SNIFF_SUBRATE, sizeof(cp), &cp);
Marcel Holtmann04837f62006-07-03 10:02:33 +02001044 }
1045
1046 if (!test_and_set_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->pend)) {
1047 struct hci_cp_sniff_mode cp;
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -07001048 cp.handle = cpu_to_le16(conn->handle);
1049 cp.max_interval = cpu_to_le16(hdev->sniff_max_interval);
1050 cp.min_interval = cpu_to_le16(hdev->sniff_min_interval);
1051 cp.attempt = cpu_to_le16(4);
1052 cp.timeout = cpu_to_le16(1);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001053 hci_send_cmd(hdev, HCI_OP_SNIFF_MODE, sizeof(cp), &cp);
Marcel Holtmann04837f62006-07-03 10:02:33 +02001054 }
1055}
1056
Peter Krystada8417e62012-03-21 16:58:17 -07001057struct hci_chan *hci_chan_create(struct hci_chan *chan,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001058 struct hci_ext_fs *tx_fs, struct hci_ext_fs *rx_fs)
1059{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001060 struct hci_cp_create_logical_link cp;
1061
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001062 chan->state = BT_CONNECT;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001063 chan->tx_fs = *tx_fs;
1064 chan->rx_fs = *rx_fs;
1065 cp.phy_handle = chan->conn->handle;
1066 cp.tx_fs.id = chan->tx_fs.id;
1067 cp.tx_fs.type = chan->tx_fs.type;
1068 cp.tx_fs.max_sdu = cpu_to_le16(chan->tx_fs.max_sdu);
1069 cp.tx_fs.sdu_arr_time = cpu_to_le32(chan->tx_fs.sdu_arr_time);
1070 cp.tx_fs.acc_latency = cpu_to_le32(chan->tx_fs.acc_latency);
1071 cp.tx_fs.flush_to = cpu_to_le32(chan->tx_fs.flush_to);
1072 cp.rx_fs.id = chan->rx_fs.id;
1073 cp.rx_fs.type = chan->rx_fs.type;
1074 cp.rx_fs.max_sdu = cpu_to_le16(chan->rx_fs.max_sdu);
1075 cp.rx_fs.sdu_arr_time = cpu_to_le32(chan->rx_fs.sdu_arr_time);
1076 cp.rx_fs.acc_latency = cpu_to_le32(chan->rx_fs.acc_latency);
1077 cp.rx_fs.flush_to = cpu_to_le32(chan->rx_fs.flush_to);
1078 hci_conn_hold(chan->conn);
Peter Krystada8417e62012-03-21 16:58:17 -07001079 if (chan->conn->out)
1080 hci_send_cmd(chan->conn->hdev, HCI_OP_CREATE_LOGICAL_LINK,
1081 sizeof(cp), &cp);
1082 else
1083 hci_send_cmd(chan->conn->hdev, HCI_OP_ACCEPT_LOGICAL_LINK,
1084 sizeof(cp), &cp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001085 return chan;
1086}
1087EXPORT_SYMBOL(hci_chan_create);
1088
1089void hci_chan_modify(struct hci_chan *chan,
1090 struct hci_ext_fs *tx_fs, struct hci_ext_fs *rx_fs)
1091{
1092 struct hci_cp_flow_spec_modify cp;
1093
1094 chan->tx_fs = *tx_fs;
1095 chan->rx_fs = *rx_fs;
1096 cp.log_handle = cpu_to_le16(chan->ll_handle);
1097 cp.tx_fs.id = tx_fs->id;
1098 cp.tx_fs.type = tx_fs->type;
1099 cp.tx_fs.max_sdu = cpu_to_le16(tx_fs->max_sdu);
1100 cp.tx_fs.sdu_arr_time = cpu_to_le32(tx_fs->sdu_arr_time);
1101 cp.tx_fs.acc_latency = cpu_to_le32(tx_fs->acc_latency);
1102 cp.tx_fs.flush_to = cpu_to_le32(tx_fs->flush_to);
1103 cp.rx_fs.id = rx_fs->id;
1104 cp.rx_fs.type = rx_fs->type;
1105 cp.rx_fs.max_sdu = cpu_to_le16(rx_fs->max_sdu);
1106 cp.rx_fs.sdu_arr_time = cpu_to_le32(rx_fs->sdu_arr_time);
1107 cp.rx_fs.acc_latency = cpu_to_le32(rx_fs->acc_latency);
1108 cp.rx_fs.flush_to = cpu_to_le32(rx_fs->flush_to);
1109 hci_conn_hold(chan->conn);
1110 hci_send_cmd(chan->conn->hdev, HCI_OP_FLOW_SPEC_MODIFY, sizeof(cp),
1111 &cp);
1112}
1113EXPORT_SYMBOL(hci_chan_modify);
1114
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115/* Drop all connection on the device */
Mat Martineau3b9239a2012-02-16 11:54:30 -08001116void hci_conn_hash_flush(struct hci_dev *hdev, u8 is_process)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117{
1118 struct hci_conn_hash *h = &hdev->conn_hash;
1119 struct list_head *p;
1120
1121 BT_DBG("hdev %s", hdev->name);
1122
1123 p = h->list.next;
1124 while (p != &h->list) {
1125 struct hci_conn *c;
1126
1127 c = list_entry(p, struct hci_conn, list);
1128 p = p->next;
1129
1130 c->state = BT_CLOSED;
1131
Mat Martineau3b9239a2012-02-16 11:54:30 -08001132 hci_proto_disconn_cfm(c, 0x16, is_process);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 hci_conn_del(c);
1134 }
1135}
1136
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001137/* Check pending connect attempts */
1138void hci_conn_check_pending(struct hci_dev *hdev)
1139{
1140 struct hci_conn *conn;
1141
1142 BT_DBG("hdev %s", hdev->name);
1143
1144 hci_dev_lock(hdev);
1145
1146 conn = hci_conn_hash_lookup_state(hdev, ACL_LINK, BT_CONNECT2);
1147 if (conn)
1148 hci_acl_connect(conn);
1149
1150 hci_dev_unlock(hdev);
1151}
1152
Marcel Holtmann9eba32b2009-08-22 14:19:26 -07001153void hci_conn_hold_device(struct hci_conn *conn)
1154{
1155 atomic_inc(&conn->devref);
1156}
1157EXPORT_SYMBOL(hci_conn_hold_device);
1158
1159void hci_conn_put_device(struct hci_conn *conn)
1160{
1161 if (atomic_dec_and_test(&conn->devref))
1162 hci_conn_del_sysfs(conn);
1163}
1164EXPORT_SYMBOL(hci_conn_put_device);
1165
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166int hci_get_conn_list(void __user *arg)
1167{
1168 struct hci_conn_list_req req, *cl;
1169 struct hci_conn_info *ci;
1170 struct hci_dev *hdev;
1171 struct list_head *p;
1172 int n = 0, size, err;
1173
1174 if (copy_from_user(&req, arg, sizeof(req)))
1175 return -EFAULT;
1176
1177 if (!req.conn_num || req.conn_num > (PAGE_SIZE * 2) / sizeof(*ci))
1178 return -EINVAL;
1179
1180 size = sizeof(req) + req.conn_num * sizeof(*ci);
1181
Andrei Emeltchenko70f230202010-12-01 16:58:25 +02001182 cl = kmalloc(size, GFP_KERNEL);
1183 if (!cl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 return -ENOMEM;
1185
Andrei Emeltchenko70f230202010-12-01 16:58:25 +02001186 hdev = hci_dev_get(req.dev_id);
1187 if (!hdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 kfree(cl);
1189 return -ENODEV;
1190 }
1191
1192 ci = cl->conn_info;
1193
1194 hci_dev_lock_bh(hdev);
1195 list_for_each(p, &hdev->conn_hash.list) {
1196 register struct hci_conn *c;
1197 c = list_entry(p, struct hci_conn, list);
1198
1199 bacpy(&(ci + n)->bdaddr, &c->dst);
1200 (ci + n)->handle = c->handle;
1201 (ci + n)->type = c->type;
1202 (ci + n)->out = c->out;
1203 (ci + n)->state = c->state;
1204 (ci + n)->link_mode = c->link_mode;
Nick Pellyc1728492009-12-09 00:15:41 -08001205 if (c->type == SCO_LINK) {
1206 (ci + n)->mtu = hdev->sco_mtu;
1207 (ci + n)->cnt = hdev->sco_cnt;
1208 (ci + n)->pkts = hdev->sco_pkts;
1209 } else {
1210 (ci + n)->mtu = hdev->acl_mtu;
1211 (ci + n)->cnt = hdev->acl_cnt;
1212 (ci + n)->pkts = hdev->acl_pkts;
1213 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 if (++n >= req.conn_num)
1215 break;
1216 }
1217 hci_dev_unlock_bh(hdev);
1218
1219 cl->dev_id = hdev->id;
1220 cl->conn_num = n;
1221 size = sizeof(req) + n * sizeof(*ci);
1222
1223 hci_dev_put(hdev);
1224
1225 err = copy_to_user(arg, cl, size);
1226 kfree(cl);
1227
1228 return err ? -EFAULT : 0;
1229}
1230
1231int hci_get_conn_info(struct hci_dev *hdev, void __user *arg)
1232{
1233 struct hci_conn_info_req req;
1234 struct hci_conn_info ci;
1235 struct hci_conn *conn;
1236 char __user *ptr = arg + sizeof(req);
1237
1238 if (copy_from_user(&req, arg, sizeof(req)))
1239 return -EFAULT;
1240
1241 hci_dev_lock_bh(hdev);
1242 conn = hci_conn_hash_lookup_ba(hdev, req.type, &req.bdaddr);
1243 if (conn) {
1244 bacpy(&ci.bdaddr, &conn->dst);
1245 ci.handle = conn->handle;
1246 ci.type = conn->type;
1247 ci.out = conn->out;
1248 ci.state = conn->state;
1249 ci.link_mode = conn->link_mode;
Nick Pellyc1728492009-12-09 00:15:41 -08001250 if (req.type == SCO_LINK) {
1251 ci.mtu = hdev->sco_mtu;
1252 ci.cnt = hdev->sco_cnt;
1253 ci.pkts = hdev->sco_pkts;
1254 } else {
1255 ci.mtu = hdev->acl_mtu;
1256 ci.cnt = hdev->acl_cnt;
1257 ci.pkts = hdev->acl_pkts;
1258 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001259 ci.pending_sec_level = conn->pending_sec_level;
1260 ci.ssp_mode = conn->ssp_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 }
1262 hci_dev_unlock_bh(hdev);
1263
1264 if (!conn)
1265 return -ENOENT;
1266
1267 return copy_to_user(ptr, &ci, sizeof(ci)) ? -EFAULT : 0;
1268}
Marcel Holtmann40be4922008-07-14 20:13:50 +02001269
1270int hci_get_auth_info(struct hci_dev *hdev, void __user *arg)
1271{
1272 struct hci_auth_info_req req;
1273 struct hci_conn *conn;
1274
1275 if (copy_from_user(&req, arg, sizeof(req)))
1276 return -EFAULT;
1277
1278 hci_dev_lock_bh(hdev);
1279 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &req.bdaddr);
1280 if (conn)
1281 req.type = conn->auth_type;
1282 hci_dev_unlock_bh(hdev);
1283
1284 if (!conn)
1285 return -ENOENT;
1286
1287 return copy_to_user(arg, &req, sizeof(req)) ? -EFAULT : 0;
1288}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001289
1290int hci_set_auth_info(struct hci_dev *hdev, void __user *arg)
1291{
1292 struct hci_auth_info_req req;
1293 struct hci_conn *conn;
1294
1295 if (copy_from_user(&req, arg, sizeof(req)))
1296 return -EFAULT;
1297
1298 hci_dev_lock_bh(hdev);
1299 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &req.bdaddr);
1300 if (conn)
1301 conn->auth_type = req.type;
1302 hci_dev_unlock_bh(hdev);
1303
1304 if (!conn)
1305 return -ENOENT;
1306
1307 return copy_to_user(arg, &req, sizeof(req)) ? -EFAULT : 0;
1308}