blob: f0f2ac1508af54d1cb9acb6bbe6660d56b1ba9a0 [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>
47
Ville Tervofcd89c02011-02-10 22:38:47 -030048static void hci_le_connect(struct hci_conn *conn)
49{
50 struct hci_dev *hdev = conn->hdev;
51 struct hci_cp_le_create_conn cp;
52
Brian Gix114f3a62011-09-27 14:02:20 -070053 BT_DBG("%p", conn);
54
Ville Tervofcd89c02011-02-10 22:38:47 -030055 conn->state = BT_CONNECT;
56 conn->out = 1;
Vinicius Costa Gomesb92a6222011-02-10 22:38:52 -030057 conn->link_mode |= HCI_LM_MASTER;
Vinicius Costa Gomes403d2c82011-06-09 18:50:50 -030058 conn->sec_level = BT_SECURITY_LOW;
Brian Gix114f3a62011-09-27 14:02:20 -070059 conn->type = LE_LINK;
Ville Tervofcd89c02011-02-10 22:38:47 -030060
61 memset(&cp, 0, sizeof(cp));
62 cp.scan_interval = cpu_to_le16(0x0004);
63 cp.scan_window = cpu_to_le16(0x0004);
64 bacpy(&cp.peer_addr, &conn->dst);
Andre Guedesc7f0d992011-05-31 14:20:57 -030065 cp.peer_addr_type = conn->dst_type;
Ville Tervofcd89c02011-02-10 22:38:47 -030066 cp.conn_interval_min = cpu_to_le16(0x0008);
67 cp.conn_interval_max = cpu_to_le16(0x0100);
68 cp.supervision_timeout = cpu_to_le16(0x0064);
69 cp.min_ce_len = cpu_to_le16(0x0001);
70 cp.max_ce_len = cpu_to_le16(0x0001);
71
72 hci_send_cmd(hdev, HCI_OP_LE_CREATE_CONN, sizeof(cp), &cp);
73}
74
75static void hci_le_connect_cancel(struct hci_conn *conn)
76{
77 hci_send_cmd(conn->hdev, HCI_OP_LE_CREATE_CONN_CANCEL, 0, NULL);
78}
79
Marcel Holtmann4c67bc72006-10-15 17:30:56 +020080void hci_acl_connect(struct hci_conn *conn)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081{
82 struct hci_dev *hdev = conn->hdev;
83 struct inquiry_entry *ie;
84 struct hci_cp_create_conn cp;
85
86 BT_DBG("%p", conn);
87
88 conn->state = BT_CONNECT;
Marcel Holtmanna8746412008-07-14 20:13:46 +020089 conn->out = 1;
90
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 conn->link_mode = HCI_LM_MASTER;
92
Marcel Holtmann4c67bc72006-10-15 17:30:56 +020093 conn->attempt++;
94
Marcel Holtmanne4e8e372008-07-14 20:13:47 +020095 conn->link_policy = hdev->link_policy;
96
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 memset(&cp, 0, sizeof(cp));
98 bacpy(&cp.bdaddr, &conn->dst);
99 cp.pscan_rep_mode = 0x02;
100
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200101 ie = hci_inquiry_cache_lookup(hdev, &conn->dst);
102 if (ie) {
Marcel Holtmann41a96212008-07-14 20:13:48 +0200103 if (inquiry_entry_age(ie) <= INQUIRY_ENTRY_AGE_MAX) {
104 cp.pscan_rep_mode = ie->data.pscan_rep_mode;
105 cp.pscan_mode = ie->data.pscan_mode;
106 cp.clock_offset = ie->data.clock_offset |
107 cpu_to_le16(0x8000);
108 }
109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 memcpy(conn->dev_class, ie->data.dev_class, 3);
Marcel Holtmann41a96212008-07-14 20:13:48 +0200111 conn->ssp_mode = ie->data.ssp_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 }
113
Marcel Holtmanna8746412008-07-14 20:13:46 +0200114 cp.pkt_type = cpu_to_le16(conn->pkt_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 if (lmp_rswitch_capable(hdev) && !(hdev->link_mode & HCI_LM_MASTER))
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200116 cp.role_switch = 0x01;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 else
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200118 cp.role_switch = 0x00;
Marcel Holtmann4c67bc72006-10-15 17:30:56 +0200119
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200120 hci_send_cmd(hdev, HCI_OP_CREATE_CONN, sizeof(cp), &cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121}
122
Marcel Holtmann6ac59342006-09-26 09:43:48 +0200123static void hci_acl_connect_cancel(struct hci_conn *conn)
124{
125 struct hci_cp_create_conn_cancel cp;
126
127 BT_DBG("%p", conn);
128
129 if (conn->hdev->hci_ver < 2)
130 return;
131
132 bacpy(&cp.bdaddr, &conn->dst);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200133 hci_send_cmd(conn->hdev, HCI_OP_CREATE_CONN_CANCEL, sizeof(cp), &cp);
Marcel Holtmann6ac59342006-09-26 09:43:48 +0200134}
135
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136void hci_acl_disconn(struct hci_conn *conn, __u8 reason)
137{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 BT_DBG("%p", conn);
139
140 conn->state = BT_DISCONN;
141
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700142 if (conn->hdev->dev_type == HCI_BREDR) {
143 struct hci_cp_disconnect cp;
144 cp.handle = cpu_to_le16(conn->handle);
145 cp.reason = reason;
146 hci_send_cmd(conn->hdev, HCI_OP_DISCONNECT, sizeof(cp), &cp);
147 } else {
148 struct hci_cp_disconn_phys_link cp;
149 cp.phy_handle = (u8) conn->handle;
150 cp.reason = reason;
151 hci_send_cmd(conn->hdev, HCI_OP_DISCONN_PHYS_LINK,
152 sizeof(cp), &cp);
153 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154}
155
156void hci_add_sco(struct hci_conn *conn, __u16 handle)
157{
158 struct hci_dev *hdev = conn->hdev;
159 struct hci_cp_add_sco cp;
160
161 BT_DBG("%p", conn);
162
163 conn->state = BT_CONNECT;
164 conn->out = 1;
165
Marcel Holtmannefc76882009-02-06 09:13:37 +0100166 conn->attempt++;
167
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -0700168 cp.handle = cpu_to_le16(handle);
Marcel Holtmanna8746412008-07-14 20:13:46 +0200169 cp.pkt_type = cpu_to_le16(conn->pkt_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200171 hci_send_cmd(hdev, HCI_OP_ADD_SCO, sizeof(cp), &cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172}
173
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200174void hci_setup_sync(struct hci_conn *conn, __u16 handle)
175{
176 struct hci_dev *hdev = conn->hdev;
177 struct hci_cp_setup_sync_conn cp;
178
179 BT_DBG("%p", conn);
180
181 conn->state = BT_CONNECT;
182 conn->out = 1;
183
Marcel Holtmannefc76882009-02-06 09:13:37 +0100184 conn->attempt++;
185
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200186 cp.handle = cpu_to_le16(handle);
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200187
188 cp.tx_bandwidth = cpu_to_le32(0x00001f40);
189 cp.rx_bandwidth = cpu_to_le32(0x00001f40);
Kun Han Kim15b911f2011-07-08 09:30:28 -0700190 if (conn->hdev->is_wbs) {
191 /* Transparent Data */
192 uint16_t voice_setting = hdev->voice_setting | ACF_TRANS;
193 cp.max_latency = cpu_to_le16(0x000D);
194 cp.pkt_type = cpu_to_le16(ESCO_WBS);
195 cp.voice_setting = cpu_to_le16(voice_setting);
196 /* Retransmission Effort */
197 cp.retrans_effort = RE_LINK_QUALITY;
198 } else {
199 cp.max_latency = cpu_to_le16(0x000A);
200 cp.pkt_type = cpu_to_le16(conn->pkt_type);
201 cp.voice_setting = cpu_to_le16(hdev->voice_setting);
202 cp.retrans_effort = RE_POWER_CONSUMP;
203 }
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200204
205 hci_send_cmd(hdev, HCI_OP_SETUP_SYNC_CONN, sizeof(cp), &cp);
206}
207
Claudio Takahasi2ce603e2011-02-16 20:44:53 -0200208void hci_le_conn_update(struct hci_conn *conn, u16 min, u16 max,
209 u16 latency, u16 to_multiplier)
210{
211 struct hci_cp_le_conn_update cp;
212 struct hci_dev *hdev = conn->hdev;
213
214 memset(&cp, 0, sizeof(cp));
215
216 cp.handle = cpu_to_le16(conn->handle);
217 cp.conn_interval_min = cpu_to_le16(min);
218 cp.conn_interval_max = cpu_to_le16(max);
219 cp.conn_latency = cpu_to_le16(latency);
220 cp.supervision_timeout = cpu_to_le16(to_multiplier);
221 cp.min_ce_len = cpu_to_le16(0x0001);
222 cp.max_ce_len = cpu_to_le16(0x0001);
223
224 hci_send_cmd(hdev, HCI_OP_LE_CONN_UPDATE, sizeof(cp), &cp);
225}
226EXPORT_SYMBOL(hci_le_conn_update);
227
Vinicius Costa Gomes735038c2011-06-09 18:50:47 -0300228void hci_le_start_enc(struct hci_conn *conn, __le16 ediv, __u8 rand[8],
229 __u8 ltk[16])
230{
231 struct hci_dev *hdev = conn->hdev;
232 struct hci_cp_le_start_enc cp;
233
234 BT_DBG("%p", conn);
235
236 memset(&cp, 0, sizeof(cp));
237
238 cp.handle = cpu_to_le16(conn->handle);
239 memcpy(cp.ltk, ltk, sizeof(cp.ltk));
240 cp.ediv = ediv;
Brian Gix842bc5e2011-09-07 08:13:41 -0700241 memcpy(cp.rand, rand, sizeof(cp.rand));
Vinicius Costa Gomes735038c2011-06-09 18:50:47 -0300242
243 hci_send_cmd(hdev, HCI_OP_LE_START_ENC, sizeof(cp), &cp);
244}
245EXPORT_SYMBOL(hci_le_start_enc);
246
247void hci_le_ltk_reply(struct hci_conn *conn, u8 ltk[16])
248{
249 struct hci_dev *hdev = conn->hdev;
250 struct hci_cp_le_ltk_reply cp;
251
252 BT_DBG("%p", conn);
253
254 memset(&cp, 0, sizeof(cp));
255
256 cp.handle = cpu_to_le16(conn->handle);
257 memcpy(cp.ltk, ltk, sizeof(ltk));
258
259 hci_send_cmd(hdev, HCI_OP_LE_LTK_REPLY, sizeof(cp), &cp);
260}
261EXPORT_SYMBOL(hci_le_ltk_reply);
262
263void hci_le_ltk_neg_reply(struct hci_conn *conn)
264{
265 struct hci_dev *hdev = conn->hdev;
266 struct hci_cp_le_ltk_neg_reply cp;
267
268 BT_DBG("%p", conn);
269
270 memset(&cp, 0, sizeof(cp));
271
272 cp.handle = cpu_to_le16(conn->handle);
273
274 hci_send_cmd(hdev, HCI_OP_LE_LTK_NEG_REPLY, sizeof(cp), &cp);
275}
276
Marcel Holtmanne73439d2010-07-26 10:06:00 -0400277/* Device _must_ be locked */
278void hci_sco_setup(struct hci_conn *conn, __u8 status)
279{
280 struct hci_conn *sco = conn->link;
281
282 BT_DBG("%p", conn);
283
284 if (!sco)
285 return;
286
287 if (!status) {
288 if (lmp_esco_capable(conn->hdev))
289 hci_setup_sync(sco, conn->handle);
290 else
291 hci_add_sco(sco, conn->handle);
292 } else {
293 hci_proto_connect_cfm(sco, status);
294 hci_conn_del(sco);
295 }
296}
297
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298static void hci_conn_timeout(unsigned long arg)
299{
Marcel Holtmann04837f62006-07-03 10:02:33 +0200300 struct hci_conn *conn = (void *) arg;
301 struct hci_dev *hdev = conn->hdev;
Marcel Holtmann2950f212009-02-12 14:02:50 +0100302 __u8 reason;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
304 BT_DBG("conn %p state %d", conn, conn->state);
305
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 hci_dev_lock(hdev);
Marcel Holtmann6ac59342006-09-26 09:43:48 +0200307
308 switch (conn->state) {
309 case BT_CONNECT:
Marcel Holtmann769be972008-07-14 20:13:49 +0200310 case BT_CONNECT2:
Ville Tervofcd89c02011-02-10 22:38:47 -0300311 if (conn->out) {
312 if (conn->type == ACL_LINK)
313 hci_acl_connect_cancel(conn);
314 else if (conn->type == LE_LINK)
315 hci_le_connect_cancel(conn);
316 }
Marcel Holtmann6ac59342006-09-26 09:43:48 +0200317 break;
Marcel Holtmann769be972008-07-14 20:13:49 +0200318 case BT_CONFIG:
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900319 case BT_CONNECTED:
Brian Gix114f3a62011-09-27 14:02:20 -0700320 if (!atomic_read(&conn->refcnt)) {
321 reason = hci_proto_disconn_ind(conn);
322 hci_acl_disconn(conn, reason);
323 }
Marcel Holtmann6ac59342006-09-26 09:43:48 +0200324 break;
325 default:
Brian Gix114f3a62011-09-27 14:02:20 -0700326 if (!atomic_read(&conn->refcnt))
327 conn->state = BT_CLOSED;
Marcel Holtmann6ac59342006-09-26 09:43:48 +0200328 break;
329 }
330
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332}
333
Marcel Holtmann04837f62006-07-03 10:02:33 +0200334static void hci_conn_idle(unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335{
Marcel Holtmann04837f62006-07-03 10:02:33 +0200336 struct hci_conn *conn = (void *) arg;
337
338 BT_DBG("conn %p mode %d", conn, conn->mode);
339
340 hci_conn_enter_sniff_mode(conn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341}
342
Nick Pellybbcda3b2010-02-11 11:54:28 -0800343struct hci_conn *hci_conn_add(struct hci_dev *hdev, int type,
344 __u16 pkt_type, bdaddr_t *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345{
346 struct hci_conn *conn;
347
348 BT_DBG("%s dst %s", hdev->name, batostr(dst));
349
Marcel Holtmann04837f62006-07-03 10:02:33 +0200350 conn = kzalloc(sizeof(struct hci_conn), GFP_ATOMIC);
351 if (!conn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
354 bacpy(&conn->dst, dst);
Marcel Holtmanna8746412008-07-14 20:13:46 +0200355 conn->hdev = hdev;
356 conn->type = type;
357 conn->mode = HCI_CM_ACTIVE;
358 conn->state = BT_OPEN;
Andrei Emeltchenko93f19c92009-09-03 12:34:19 +0300359 conn->auth_type = HCI_AT_GENERAL_BONDING;
Johan Hedberg17fa4b92011-01-25 13:28:33 +0200360 conn->io_capability = hdev->io_capability;
Johan Hedberga9583552011-02-19 12:06:01 -0300361 conn->remote_auth = 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
Marcel Holtmann04837f62006-07-03 10:02:33 +0200363 conn->power_save = 1;
Marcel Holtmann052b30b2009-04-26 20:01:22 +0200364 conn->disc_timeout = HCI_DISCONN_TIMEOUT;
Marcel Holtmann04837f62006-07-03 10:02:33 +0200365
Marcel Holtmanna8746412008-07-14 20:13:46 +0200366 switch (type) {
367 case ACL_LINK:
368 conn->pkt_type = hdev->pkt_type & ACL_PTYPE_MASK;
Rahul Kashyapd5553ba2012-01-02 19:27:19 +0530369 conn->link_policy = hdev->link_policy;
Marcel Holtmanna8746412008-07-14 20:13:46 +0200370 break;
371 case SCO_LINK:
Nick Pellybbcda3b2010-02-11 11:54:28 -0800372 if (!pkt_type)
373 pkt_type = SCO_ESCO_MASK;
Marcel Holtmanna8746412008-07-14 20:13:46 +0200374 case ESCO_LINK:
Nick Pellybbcda3b2010-02-11 11:54:28 -0800375 if (!pkt_type)
376 pkt_type = ALL_ESCO_MASK;
377 if (lmp_esco_capable(hdev)) {
378 /* HCI Setup Synchronous Connection Command uses
379 reverse logic on the EDR_ESCO_MASK bits */
380 conn->pkt_type = (pkt_type ^ EDR_ESCO_MASK) &
381 hdev->esco_type;
382 } else {
383 /* Legacy HCI Add Sco Connection Command uses a
384 shifted bitmask */
385 conn->pkt_type = (pkt_type << 5) & hdev->pkt_type &
386 SCO_PTYPE_MASK;
387 }
Marcel Holtmanna8746412008-07-14 20:13:46 +0200388 break;
389 }
390
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 skb_queue_head_init(&conn->data_q);
Marcel Holtmann04837f62006-07-03 10:02:33 +0200392
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800393 setup_timer(&conn->disc_timer, hci_conn_timeout, (unsigned long)conn);
394 setup_timer(&conn->idle_timer, hci_conn_idle, (unsigned long)conn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
396 atomic_set(&conn->refcnt, 0);
397
398 hci_dev_hold(hdev);
399
400 tasklet_disable(&hdev->tx_task);
401
402 hci_conn_hash_add(hdev, conn);
403 if (hdev->notify)
404 hdev->notify(hdev, HCI_NOTIFY_CONN_ADD);
405
Marcel Holtmann9eba32b2009-08-22 14:19:26 -0700406 atomic_set(&conn->devref, 0);
407
Marcel Holtmanna67e8992009-05-02 18:24:06 -0700408 hci_conn_init_sysfs(conn);
409
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 tasklet_enable(&hdev->tx_task);
411
412 return conn;
413}
414
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700415struct hci_conn *hci_le_conn_add(struct hci_dev *hdev, bdaddr_t *dst,
416 __u8 addr_type)
417{
418 struct hci_conn *conn = hci_conn_add(hdev, LE_LINK, 0, dst);
419 if (!conn)
420 return NULL;
421
422 conn->dst_type = addr_type;
423
424 return conn;
425}
426
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427int hci_conn_del(struct hci_conn *conn)
428{
429 struct hci_dev *hdev = conn->hdev;
430
431 BT_DBG("%s conn %p handle %d", hdev->name, conn, conn->handle);
432
Brian Gix3cd62042012-01-11 15:18:17 -0800433 /* Make sure no timers are running */
Marcel Holtmann04837f62006-07-03 10:02:33 +0200434 del_timer(&conn->idle_timer);
Marcel Holtmann04837f62006-07-03 10:02:33 +0200435 del_timer(&conn->disc_timer);
Brian Gix3cd62042012-01-11 15:18:17 -0800436 del_timer(&conn->smp_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200438 if (conn->type == ACL_LINK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 struct hci_conn *sco = conn->link;
440 if (sco)
441 sco->link = NULL;
442
443 /* Unacked frames */
444 hdev->acl_cnt += conn->sent;
Ville Tervo6ed58ec2011-02-10 22:38:48 -0300445 } else if (conn->type == LE_LINK) {
446 if (hdev->le_pkts)
447 hdev->le_cnt += conn->sent;
448 else
449 hdev->acl_cnt += conn->sent;
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200450 } else {
451 struct hci_conn *acl = conn->link;
452 if (acl) {
453 acl->link = NULL;
454 hci_conn_put(acl);
455 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 }
457
458 tasklet_disable(&hdev->tx_task);
Marcel Holtmann7d0db0a2008-07-14 20:13:51 +0200459
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 hci_conn_hash_del(hdev, conn);
461 if (hdev->notify)
462 hdev->notify(hdev, HCI_NOTIFY_CONN_DEL);
Marcel Holtmann7d0db0a2008-07-14 20:13:51 +0200463
AnubhavGupta01d9e362011-12-17 17:14:51 +0530464 tasklet_schedule(&hdev->tx_task);
465
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 tasklet_enable(&hdev->tx_task);
Marcel Holtmann7d0db0a2008-07-14 20:13:51 +0200467
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 skb_queue_purge(&conn->data_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
Marcel Holtmann9eba32b2009-08-22 14:19:26 -0700470 hci_conn_put_device(conn);
Dave Young2ae9a6b2009-02-21 16:13:34 +0800471
Marcel Holtmann384943e2009-05-08 18:20:43 -0700472 hci_dev_put(hdev);
473
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700474 return 0;
475}
476
477struct hci_chan *hci_chan_add(struct hci_dev *hdev)
478{
479 struct hci_chan *chan;
480
481 BT_DBG("%s", hdev->name);
482
483 chan = kzalloc(sizeof(struct hci_chan), GFP_ATOMIC);
484 if (!chan)
485 return NULL;
486
487 atomic_set(&chan->refcnt, 0);
488
489 hci_dev_hold(hdev);
490
491 chan->hdev = hdev;
492
493 list_add(&chan->list, &hdev->chan_list.list);
494
495 return chan;
496}
497
498int hci_chan_del(struct hci_chan *chan)
499{
500 BT_DBG("%s chan %p", chan->hdev->name, chan);
501
502 list_del(&chan->list);
503
504 hci_conn_put(chan->conn);
505 hci_dev_put(chan->hdev);
506
507 kfree(chan);
Tomas Targownik1be668d2011-06-30 16:30:44 -0300508
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 return 0;
510}
511
Peter Krystadd6a9ceb2011-12-01 15:44:54 -0800512int hci_chan_put(struct hci_chan *chan)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700513{
514 struct hci_cp_disconn_logical_link cp;
Mat Martineau9f8d4672011-12-14 12:10:46 -0800515 struct hci_conn *hcon;
516 u16 ll_handle;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700517
518 BT_DBG("chan %p refcnt %d", chan, atomic_read(&chan->refcnt));
519 if (!atomic_dec_and_test(&chan->refcnt))
Peter Krystadd6a9ceb2011-12-01 15:44:54 -0800520 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700521
Mat Martineau9f8d4672011-12-14 12:10:46 -0800522 hcon = chan->conn;
523 ll_handle = chan->ll_handle;
524
525 hci_chan_del(chan);
526
527 BT_DBG("chan->conn->state %d", hcon->state);
528 if (hcon->state == BT_CONNECTED) {
529 cp.log_handle = cpu_to_le16(ll_handle);
530 hci_send_cmd(hcon->hdev, HCI_OP_DISCONN_LOGICAL_LINK,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700531 sizeof(cp), &cp);
Mat Martineau9f8d4672011-12-14 12:10:46 -0800532 }
Peter Krystadd6a9ceb2011-12-01 15:44:54 -0800533
534 return 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700535}
536EXPORT_SYMBOL(hci_chan_put);
537
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538struct hci_dev *hci_get_route(bdaddr_t *dst, bdaddr_t *src)
539{
540 int use_src = bacmp(src, BDADDR_ANY);
541 struct hci_dev *hdev = NULL;
542 struct list_head *p;
543
544 BT_DBG("%s -> %s", batostr(src), batostr(dst));
545
546 read_lock_bh(&hci_dev_list_lock);
547
548 list_for_each(p, &hci_dev_list) {
549 struct hci_dev *d = list_entry(p, struct hci_dev, list);
550
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700551 if (d->dev_type != HCI_BREDR)
552 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 if (!test_bit(HCI_UP, &d->flags) || test_bit(HCI_RAW, &d->flags))
554 continue;
555
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900556 /* Simple routing:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 * No source address - find interface with bdaddr != dst
558 * Source address - find interface with bdaddr == src
559 */
560
561 if (use_src) {
562 if (!bacmp(&d->bdaddr, src)) {
563 hdev = d; break;
564 }
565 } else {
566 if (bacmp(&d->bdaddr, dst)) {
567 hdev = d; break;
568 }
569 }
570 }
571
572 if (hdev)
573 hdev = hci_dev_hold(hdev);
574
575 read_unlock_bh(&hci_dev_list_lock);
576 return hdev;
577}
578EXPORT_SYMBOL(hci_get_route);
579
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700580struct hci_dev *hci_dev_get_type(u8 amp_type)
581{
582 struct hci_dev *hdev = NULL;
583 struct hci_dev *d;
584
585 BT_DBG("amp_type %d", amp_type);
586
587 read_lock_bh(&hci_dev_list_lock);
588
589 list_for_each_entry(d, &hci_dev_list, list) {
590 if ((d->amp_type == amp_type) && test_bit(HCI_UP, &d->flags)) {
591 hdev = d;
592 break;
593 }
594 }
595
596 if (hdev)
597 hdev = hci_dev_hold(hdev);
598
599 read_unlock_bh(&hci_dev_list_lock);
600 return hdev;
601}
602EXPORT_SYMBOL(hci_dev_get_type);
603
604struct hci_dev *hci_dev_get_amp(bdaddr_t *dst)
605{
606 struct hci_dev *d;
607 struct hci_dev *hdev = NULL;
608
609 BT_DBG("%s dst %s", hdev->name, batostr(dst));
610
611 read_lock_bh(&hci_dev_list_lock);
612
613 list_for_each_entry(d, &hci_dev_list, list) {
614 struct hci_conn *conn;
615 if (d->dev_type == HCI_BREDR)
616 continue;
617 conn = hci_conn_hash_lookup_ba(d, ACL_LINK, dst);
618 if (conn) {
619 hdev = d;
620 break;
621 }
622 }
623
624 if (hdev)
625 hdev = hci_dev_hold(hdev);
626
627 read_unlock_bh(&hci_dev_list_lock);
628 return hdev;
629}
630EXPORT_SYMBOL(hci_dev_get_amp);
631
Ville Tervofcd89c02011-02-10 22:38:47 -0300632/* Create SCO, ACL or LE connection.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 * Device _must_ be locked */
Nick Pellybbcda3b2010-02-11 11:54:28 -0800634struct hci_conn *hci_connect(struct hci_dev *hdev, int type,
635 __u16 pkt_type, bdaddr_t *dst,
636 __u8 sec_level, __u8 auth_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637{
638 struct hci_conn *acl;
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200639 struct hci_conn *sco;
Ville Tervofcd89c02011-02-10 22:38:47 -0300640 struct hci_conn *le;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
642 BT_DBG("%s dst %s", hdev->name, batostr(dst));
643
Ville Tervofcd89c02011-02-10 22:38:47 -0300644 if (type == LE_LINK) {
Andre Guedes5e89ece2011-05-31 14:20:56 -0300645 struct adv_entry *entry;
Brian Gixcf956772011-10-20 15:18:51 -0700646 struct link_key *key;
Andre Guedes5e89ece2011-05-31 14:20:56 -0300647
Ville Tervofcd89c02011-02-10 22:38:47 -0300648 le = hci_conn_hash_lookup_ba(hdev, LE_LINK, dst);
Brian Gix2e2f50d2011-09-13 12:36:04 -0700649 if (le) {
650 hci_conn_hold(le);
Brian Gixa68668b2011-08-11 15:49:36 -0700651 return le;
Brian Gix2e2f50d2011-09-13 12:36:04 -0700652 }
Andre Guedes5e89ece2011-05-31 14:20:56 -0300653
Brian Gixcf956772011-10-20 15:18:51 -0700654 key = hci_find_link_key_type(hdev, dst, KEY_TYPE_LTK);
655 if (!key) {
656 entry = hci_find_adv_entry(hdev, dst);
657 if (entry)
658 le = hci_le_conn_add(hdev, dst,
659 entry->bdaddr_type);
660 else
661 le = hci_le_conn_add(hdev, dst, 0);
662 } else {
663 le = hci_le_conn_add(hdev, dst, key->addr_type);
664 }
Andre Guedes5e89ece2011-05-31 14:20:56 -0300665
Ville Tervofcd89c02011-02-10 22:38:47 -0300666 if (!le)
Ville Tervo30e76272011-02-22 16:10:53 -0300667 return ERR_PTR(-ENOMEM);
Andre Guedes92398c82011-05-31 14:20:55 -0300668
669 hci_le_connect(le);
Ville Tervofcd89c02011-02-10 22:38:47 -0300670
671 hci_conn_hold(le);
672
673 return le;
674 }
675
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200676 acl = hci_conn_hash_lookup_ba(hdev, ACL_LINK, dst);
677 if (!acl) {
Nick Pellybbcda3b2010-02-11 11:54:28 -0800678 acl = hci_conn_add(hdev, ACL_LINK, 0, dst);
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200679 if (!acl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 return NULL;
681 }
682
683 hci_conn_hold(acl);
684
Marcel Holtmann09ab6f42008-09-09 07:19:20 +0200685 if (acl->state == BT_OPEN || acl->state == BT_CLOSED) {
Johan Hedberg765c2a92011-01-19 12:06:52 +0530686 acl->sec_level = BT_SECURITY_LOW;
687 acl->pending_sec_level = sec_level;
Marcel Holtmann09ab6f42008-09-09 07:19:20 +0200688 acl->auth_type = auth_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 hci_acl_connect(acl);
Marcel Holtmann09ab6f42008-09-09 07:19:20 +0200690 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200692 if (type == ACL_LINK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 return acl;
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200694
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200695 sco = hci_conn_hash_lookup_ba(hdev, type, dst);
696 if (!sco) {
Nick Pellybbcda3b2010-02-11 11:54:28 -0800697 sco = hci_conn_add(hdev, type, pkt_type, dst);
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200698 if (!sco) {
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200699 hci_conn_put(acl);
700 return NULL;
701 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 }
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200703
704 acl->link = sco;
705 sco->link = acl;
706
707 hci_conn_hold(sco);
708
709 if (acl->state == BT_CONNECTED &&
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200710 (sco->state == BT_OPEN || sco->state == BT_CLOSED)) {
Nick Pellyc3902162009-11-13 14:16:32 -0800711 acl->power_save = 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700712 hci_conn_enter_active_mode(acl, 1);
Nick Pellyc3902162009-11-13 14:16:32 -0800713
Marcel Holtmanne73439d2010-07-26 10:06:00 -0400714 if (test_bit(HCI_CONN_MODE_CHANGE_PEND, &acl->pend)) {
715 /* defer SCO setup until mode change completed */
716 set_bit(HCI_CONN_SCO_SETUP_PEND, &acl->pend);
717 return sco;
718 }
719
720 hci_sco_setup(acl, 0x00);
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200721 }
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200722
723 return sco;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724}
725EXPORT_SYMBOL(hci_connect);
726
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700727void hci_disconnect(struct hci_conn *conn, __u8 reason)
728{
729 BT_DBG("conn %p", conn);
730
731 hci_proto_disconn_cfm(conn, reason);
732}
733EXPORT_SYMBOL(hci_disconnect);
734
735void hci_disconnect_amp(struct hci_conn *conn, __u8 reason)
736{
737 struct hci_dev *hdev = NULL;
738
739 BT_DBG("conn %p", conn);
740
741 read_lock_bh(&hci_dev_list_lock);
742
743 list_for_each_entry(hdev, &hci_dev_list, list) {
744 struct hci_conn *c;
745 if (hdev == conn->hdev)
746 continue;
747 if (hdev->amp_type == HCI_BREDR)
748 continue;
749 c = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &conn->dst);
750 if (c)
751 hci_disconnect(c, reason);
752 }
753
754 read_unlock_bh(&hci_dev_list_lock);
755}
756
Marcel Holtmanne7c29cb2008-09-09 07:19:20 +0200757/* Check link security requirement */
758int hci_conn_check_link_mode(struct hci_conn *conn)
759{
760 BT_DBG("conn %p", conn);
761
762 if (conn->ssp_mode > 0 && conn->hdev->ssp_mode > 0 &&
763 !(conn->link_mode & HCI_LM_ENCRYPT))
764 return 0;
765
766 return 1;
767}
768EXPORT_SYMBOL(hci_conn_check_link_mode);
769
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770/* Authenticate remote device */
Marcel Holtmann0684e5f2009-02-09 02:48:38 +0100771static int hci_conn_auth(struct hci_conn *conn, __u8 sec_level, __u8 auth_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772{
773 BT_DBG("conn %p", conn);
774
Johan Hedberg765c2a92011-01-19 12:06:52 +0530775 if (conn->pending_sec_level > sec_level)
776 sec_level = conn->pending_sec_level;
777
Marcel Holtmann96a31832009-02-12 16:23:03 +0100778 if (sec_level > conn->sec_level)
Johan Hedberg765c2a92011-01-19 12:06:52 +0530779 conn->pending_sec_level = sec_level;
Marcel Holtmann96a31832009-02-12 16:23:03 +0100780 else if (conn->link_mode & HCI_LM_AUTH)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 return 1;
782
Johan Hedberg65cf6862011-01-19 12:06:49 +0530783 /* Make sure we preserve an existing MITM requirement*/
784 auth_type |= (conn->auth_type & 0x01);
Marcel Holtmann96a31832009-02-12 16:23:03 +0100785 conn->auth_type = auth_type;
Brian Gixa68668b2011-08-11 15:49:36 -0700786 conn->auth_initiator = 1;
Marcel Holtmann96a31832009-02-12 16:23:03 +0100787
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 if (!test_and_set_bit(HCI_CONN_AUTH_PEND, &conn->pend)) {
789 struct hci_cp_auth_requested cp;
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -0700790 cp.handle = cpu_to_le16(conn->handle);
Marcel Holtmann40be4922008-07-14 20:13:50 +0200791 hci_send_cmd(conn->hdev, HCI_OP_AUTH_REQUESTED,
792 sizeof(cp), &cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 }
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100794
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 return 0;
796}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100798/* Enable security */
Marcel Holtmann0684e5f2009-02-09 02:48:38 +0100799int hci_conn_security(struct hci_conn *conn, __u8 sec_level, __u8 auth_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800{
Brian Gixa68668b2011-08-11 15:49:36 -0700801 BT_DBG("conn %p %d %d", conn, sec_level, auth_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100803 if (sec_level == BT_SECURITY_SDP)
804 return 1;
805
Marcel Holtmann3fdca1e2009-04-28 09:04:55 -0700806 if (sec_level == BT_SECURITY_LOW &&
807 (!conn->ssp_mode || !conn->hdev->ssp_mode))
808 return 1;
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100809
Brian Gix2e2f50d2011-09-13 12:36:04 -0700810 if (conn->type == LE_LINK) {
811 if (conn->pending_sec_level > sec_level)
812 sec_level = conn->pending_sec_level;
Waldemar Rymarkiewicz13d39312011-04-28 12:07:55 +0200813
Brian Gix2e2f50d2011-09-13 12:36:04 -0700814 if (sec_level > conn->sec_level)
815 conn->pending_sec_level = sec_level;
816 hci_proto_connect_cfm(conn, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700817 return 0;
Brian Gix2e2f50d2011-09-13 12:36:04 -0700818 } else if (conn->link_mode & HCI_LM_ENCRYPT) {
819 return hci_conn_auth(conn, sec_level, auth_type);
820 } else if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &conn->pend)) {
821 return 0;
822 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700823
824 if (hci_conn_auth(conn, sec_level, auth_type)) {
825 struct hci_cp_set_conn_encrypt cp;
826 cp.handle = cpu_to_le16(conn->handle);
827 cp.encrypt = 1;
828 hci_send_cmd(conn->hdev, HCI_OP_SET_CONN_ENCRYPT,
829 sizeof(cp), &cp);
830 }
831
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 return 0;
833}
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100834EXPORT_SYMBOL(hci_conn_security);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
836/* Change link key */
837int hci_conn_change_link_key(struct hci_conn *conn)
838{
839 BT_DBG("conn %p", conn);
840
841 if (!test_and_set_bit(HCI_CONN_AUTH_PEND, &conn->pend)) {
842 struct hci_cp_change_conn_link_key cp;
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -0700843 cp.handle = cpu_to_le16(conn->handle);
Marcel Holtmann40be4922008-07-14 20:13:50 +0200844 hci_send_cmd(conn->hdev, HCI_OP_CHANGE_CONN_LINK_KEY,
845 sizeof(cp), &cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 }
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100847
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 return 0;
849}
850EXPORT_SYMBOL(hci_conn_change_link_key);
851
852/* Switch role */
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100853int hci_conn_switch_role(struct hci_conn *conn, __u8 role)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854{
855 BT_DBG("conn %p", conn);
856
857 if (!role && conn->link_mode & HCI_LM_MASTER)
858 return 1;
859
860 if (!test_and_set_bit(HCI_CONN_RSWITCH_PEND, &conn->pend)) {
861 struct hci_cp_switch_role cp;
862 bacpy(&cp.bdaddr, &conn->dst);
863 cp.role = role;
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200864 hci_send_cmd(conn->hdev, HCI_OP_SWITCH_ROLE, sizeof(cp), &cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 }
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100866
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 return 0;
868}
869EXPORT_SYMBOL(hci_conn_switch_role);
870
Marcel Holtmann04837f62006-07-03 10:02:33 +0200871/* Enter active mode */
Jaikumar Ganesh514abe62011-05-23 18:06:04 -0700872void hci_conn_enter_active_mode(struct hci_conn *conn, __u8 force_active)
Marcel Holtmann04837f62006-07-03 10:02:33 +0200873{
874 struct hci_dev *hdev = conn->hdev;
875
876 BT_DBG("conn %p mode %d", conn, conn->mode);
877
878 if (test_bit(HCI_RAW, &hdev->flags))
879 return;
880
Jaikumar Ganesh514abe62011-05-23 18:06:04 -0700881 if (conn->mode != HCI_CM_SNIFF)
882 goto timer;
883
884 if (!conn->power_save && !force_active)
Marcel Holtmann04837f62006-07-03 10:02:33 +0200885 goto timer;
886
887 if (!test_and_set_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->pend)) {
888 struct hci_cp_exit_sniff_mode cp;
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -0700889 cp.handle = cpu_to_le16(conn->handle);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200890 hci_send_cmd(hdev, HCI_OP_EXIT_SNIFF_MODE, sizeof(cp), &cp);
Marcel Holtmann04837f62006-07-03 10:02:33 +0200891 }
892
893timer:
894 if (hdev->idle_timeout > 0)
895 mod_timer(&conn->idle_timer,
896 jiffies + msecs_to_jiffies(hdev->idle_timeout));
897}
898
899/* Enter sniff mode */
900void hci_conn_enter_sniff_mode(struct hci_conn *conn)
901{
902 struct hci_dev *hdev = conn->hdev;
903
904 BT_DBG("conn %p mode %d", conn, conn->mode);
905
906 if (test_bit(HCI_RAW, &hdev->flags))
907 return;
908
909 if (!lmp_sniff_capable(hdev) || !lmp_sniff_capable(conn))
910 return;
911
Srinivas Krovvidi32ba9352012-01-26 10:48:08 +0530912 if (conn->mode != HCI_CM_ACTIVE ||
913 !(conn->link_policy & HCI_LP_SNIFF) ||
914 (hci_find_link_key(hdev, &conn->dst) == NULL))
Marcel Holtmann04837f62006-07-03 10:02:33 +0200915 return;
916
917 if (lmp_sniffsubr_capable(hdev) && lmp_sniffsubr_capable(conn)) {
918 struct hci_cp_sniff_subrate cp;
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -0700919 cp.handle = cpu_to_le16(conn->handle);
920 cp.max_latency = cpu_to_le16(0);
921 cp.min_remote_timeout = cpu_to_le16(0);
922 cp.min_local_timeout = cpu_to_le16(0);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200923 hci_send_cmd(hdev, HCI_OP_SNIFF_SUBRATE, sizeof(cp), &cp);
Marcel Holtmann04837f62006-07-03 10:02:33 +0200924 }
925
926 if (!test_and_set_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->pend)) {
927 struct hci_cp_sniff_mode cp;
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -0700928 cp.handle = cpu_to_le16(conn->handle);
929 cp.max_interval = cpu_to_le16(hdev->sniff_max_interval);
930 cp.min_interval = cpu_to_le16(hdev->sniff_min_interval);
931 cp.attempt = cpu_to_le16(4);
932 cp.timeout = cpu_to_le16(1);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200933 hci_send_cmd(hdev, HCI_OP_SNIFF_MODE, sizeof(cp), &cp);
Marcel Holtmann04837f62006-07-03 10:02:33 +0200934 }
935}
936
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700937struct hci_chan *hci_chan_accept(struct hci_conn *conn,
938 struct hci_ext_fs *tx_fs, struct hci_ext_fs *rx_fs)
939{
940 struct hci_chan *chan;
941 struct hci_cp_create_logical_link cp;
942
943 chan = hci_chan_add(conn->hdev);
944 if (!chan)
945 return NULL;
946
947 chan->state = BT_CONNECT;
948 chan->conn = conn;
949 chan->tx_fs = *tx_fs;
950 chan->rx_fs = *rx_fs;
951 cp.phy_handle = chan->conn->handle;
952 cp.tx_fs.id = chan->tx_fs.id;
953 cp.tx_fs.type = chan->tx_fs.type;
954 cp.tx_fs.max_sdu = cpu_to_le16(chan->tx_fs.max_sdu);
955 cp.tx_fs.sdu_arr_time = cpu_to_le32(chan->tx_fs.sdu_arr_time);
956 cp.tx_fs.acc_latency = cpu_to_le32(chan->tx_fs.acc_latency);
957 cp.tx_fs.flush_to = cpu_to_le32(chan->tx_fs.flush_to);
958 cp.rx_fs.id = chan->rx_fs.id;
959 cp.rx_fs.type = chan->rx_fs.type;
960 cp.rx_fs.max_sdu = cpu_to_le16(chan->rx_fs.max_sdu);
961 cp.rx_fs.sdu_arr_time = cpu_to_le32(chan->rx_fs.sdu_arr_time);
962 cp.rx_fs.acc_latency = cpu_to_le32(chan->rx_fs.acc_latency);
963 cp.rx_fs.flush_to = cpu_to_le32(chan->rx_fs.flush_to);
964 hci_conn_hold(chan->conn);
965 hci_send_cmd(conn->hdev, HCI_OP_ACCEPT_LOGICAL_LINK, sizeof(cp), &cp);
966 return chan;
967}
968EXPORT_SYMBOL(hci_chan_accept);
969
970struct hci_chan *hci_chan_create(struct hci_conn *conn,
971 struct hci_ext_fs *tx_fs, struct hci_ext_fs *rx_fs)
972{
973 struct hci_chan *chan;
974 struct hci_cp_create_logical_link cp;
975
976 chan = hci_chan_add(conn->hdev);
977 if (!chan)
978 return NULL;
979
980 chan->state = BT_CONNECT;
981 chan->conn = conn;
982 chan->tx_fs = *tx_fs;
983 chan->rx_fs = *rx_fs;
984 cp.phy_handle = chan->conn->handle;
985 cp.tx_fs.id = chan->tx_fs.id;
986 cp.tx_fs.type = chan->tx_fs.type;
987 cp.tx_fs.max_sdu = cpu_to_le16(chan->tx_fs.max_sdu);
988 cp.tx_fs.sdu_arr_time = cpu_to_le32(chan->tx_fs.sdu_arr_time);
989 cp.tx_fs.acc_latency = cpu_to_le32(chan->tx_fs.acc_latency);
990 cp.tx_fs.flush_to = cpu_to_le32(chan->tx_fs.flush_to);
991 cp.rx_fs.id = chan->rx_fs.id;
992 cp.rx_fs.type = chan->rx_fs.type;
993 cp.rx_fs.max_sdu = cpu_to_le16(chan->rx_fs.max_sdu);
994 cp.rx_fs.sdu_arr_time = cpu_to_le32(chan->rx_fs.sdu_arr_time);
995 cp.rx_fs.acc_latency = cpu_to_le32(chan->rx_fs.acc_latency);
996 cp.rx_fs.flush_to = cpu_to_le32(chan->rx_fs.flush_to);
997 hci_conn_hold(chan->conn);
998 hci_send_cmd(conn->hdev, HCI_OP_CREATE_LOGICAL_LINK, sizeof(cp), &cp);
999 return chan;
1000}
1001EXPORT_SYMBOL(hci_chan_create);
1002
1003void hci_chan_modify(struct hci_chan *chan,
1004 struct hci_ext_fs *tx_fs, struct hci_ext_fs *rx_fs)
1005{
1006 struct hci_cp_flow_spec_modify cp;
1007
1008 chan->tx_fs = *tx_fs;
1009 chan->rx_fs = *rx_fs;
1010 cp.log_handle = cpu_to_le16(chan->ll_handle);
1011 cp.tx_fs.id = tx_fs->id;
1012 cp.tx_fs.type = tx_fs->type;
1013 cp.tx_fs.max_sdu = cpu_to_le16(tx_fs->max_sdu);
1014 cp.tx_fs.sdu_arr_time = cpu_to_le32(tx_fs->sdu_arr_time);
1015 cp.tx_fs.acc_latency = cpu_to_le32(tx_fs->acc_latency);
1016 cp.tx_fs.flush_to = cpu_to_le32(tx_fs->flush_to);
1017 cp.rx_fs.id = rx_fs->id;
1018 cp.rx_fs.type = rx_fs->type;
1019 cp.rx_fs.max_sdu = cpu_to_le16(rx_fs->max_sdu);
1020 cp.rx_fs.sdu_arr_time = cpu_to_le32(rx_fs->sdu_arr_time);
1021 cp.rx_fs.acc_latency = cpu_to_le32(rx_fs->acc_latency);
1022 cp.rx_fs.flush_to = cpu_to_le32(rx_fs->flush_to);
1023 hci_conn_hold(chan->conn);
1024 hci_send_cmd(chan->conn->hdev, HCI_OP_FLOW_SPEC_MODIFY, sizeof(cp),
1025 &cp);
1026}
1027EXPORT_SYMBOL(hci_chan_modify);
1028
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029/* Drop all connection on the device */
1030void hci_conn_hash_flush(struct hci_dev *hdev)
1031{
1032 struct hci_conn_hash *h = &hdev->conn_hash;
1033 struct list_head *p;
1034
1035 BT_DBG("hdev %s", hdev->name);
1036
1037 p = h->list.next;
1038 while (p != &h->list) {
1039 struct hci_conn *c;
1040
1041 c = list_entry(p, struct hci_conn, list);
1042 p = p->next;
1043
1044 c->state = BT_CLOSED;
1045
Marcel Holtmann2950f212009-02-12 14:02:50 +01001046 hci_proto_disconn_cfm(c, 0x16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 hci_conn_del(c);
1048 }
1049}
1050
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001051/* Check pending connect attempts */
1052void hci_conn_check_pending(struct hci_dev *hdev)
1053{
1054 struct hci_conn *conn;
1055
1056 BT_DBG("hdev %s", hdev->name);
1057
1058 hci_dev_lock(hdev);
1059
1060 conn = hci_conn_hash_lookup_state(hdev, ACL_LINK, BT_CONNECT2);
1061 if (conn)
1062 hci_acl_connect(conn);
1063
1064 hci_dev_unlock(hdev);
1065}
1066
Marcel Holtmann9eba32b2009-08-22 14:19:26 -07001067void hci_conn_hold_device(struct hci_conn *conn)
1068{
1069 atomic_inc(&conn->devref);
1070}
1071EXPORT_SYMBOL(hci_conn_hold_device);
1072
1073void hci_conn_put_device(struct hci_conn *conn)
1074{
1075 if (atomic_dec_and_test(&conn->devref))
1076 hci_conn_del_sysfs(conn);
1077}
1078EXPORT_SYMBOL(hci_conn_put_device);
1079
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080int hci_get_conn_list(void __user *arg)
1081{
1082 struct hci_conn_list_req req, *cl;
1083 struct hci_conn_info *ci;
1084 struct hci_dev *hdev;
1085 struct list_head *p;
1086 int n = 0, size, err;
1087
1088 if (copy_from_user(&req, arg, sizeof(req)))
1089 return -EFAULT;
1090
1091 if (!req.conn_num || req.conn_num > (PAGE_SIZE * 2) / sizeof(*ci))
1092 return -EINVAL;
1093
1094 size = sizeof(req) + req.conn_num * sizeof(*ci);
1095
Andrei Emeltchenko70f230202010-12-01 16:58:25 +02001096 cl = kmalloc(size, GFP_KERNEL);
1097 if (!cl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 return -ENOMEM;
1099
Andrei Emeltchenko70f230202010-12-01 16:58:25 +02001100 hdev = hci_dev_get(req.dev_id);
1101 if (!hdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 kfree(cl);
1103 return -ENODEV;
1104 }
1105
1106 ci = cl->conn_info;
1107
1108 hci_dev_lock_bh(hdev);
1109 list_for_each(p, &hdev->conn_hash.list) {
1110 register struct hci_conn *c;
1111 c = list_entry(p, struct hci_conn, list);
1112
1113 bacpy(&(ci + n)->bdaddr, &c->dst);
1114 (ci + n)->handle = c->handle;
1115 (ci + n)->type = c->type;
1116 (ci + n)->out = c->out;
1117 (ci + n)->state = c->state;
1118 (ci + n)->link_mode = c->link_mode;
Nick Pellyc1728492009-12-09 00:15:41 -08001119 if (c->type == SCO_LINK) {
1120 (ci + n)->mtu = hdev->sco_mtu;
1121 (ci + n)->cnt = hdev->sco_cnt;
1122 (ci + n)->pkts = hdev->sco_pkts;
1123 } else {
1124 (ci + n)->mtu = hdev->acl_mtu;
1125 (ci + n)->cnt = hdev->acl_cnt;
1126 (ci + n)->pkts = hdev->acl_pkts;
1127 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 if (++n >= req.conn_num)
1129 break;
1130 }
1131 hci_dev_unlock_bh(hdev);
1132
1133 cl->dev_id = hdev->id;
1134 cl->conn_num = n;
1135 size = sizeof(req) + n * sizeof(*ci);
1136
1137 hci_dev_put(hdev);
1138
1139 err = copy_to_user(arg, cl, size);
1140 kfree(cl);
1141
1142 return err ? -EFAULT : 0;
1143}
1144
1145int hci_get_conn_info(struct hci_dev *hdev, void __user *arg)
1146{
1147 struct hci_conn_info_req req;
1148 struct hci_conn_info ci;
1149 struct hci_conn *conn;
1150 char __user *ptr = arg + sizeof(req);
1151
1152 if (copy_from_user(&req, arg, sizeof(req)))
1153 return -EFAULT;
1154
1155 hci_dev_lock_bh(hdev);
1156 conn = hci_conn_hash_lookup_ba(hdev, req.type, &req.bdaddr);
1157 if (conn) {
1158 bacpy(&ci.bdaddr, &conn->dst);
1159 ci.handle = conn->handle;
1160 ci.type = conn->type;
1161 ci.out = conn->out;
1162 ci.state = conn->state;
1163 ci.link_mode = conn->link_mode;
Nick Pellyc1728492009-12-09 00:15:41 -08001164 if (req.type == SCO_LINK) {
1165 ci.mtu = hdev->sco_mtu;
1166 ci.cnt = hdev->sco_cnt;
1167 ci.pkts = hdev->sco_pkts;
1168 } else {
1169 ci.mtu = hdev->acl_mtu;
1170 ci.cnt = hdev->acl_cnt;
1171 ci.pkts = hdev->acl_pkts;
1172 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001173 ci.pending_sec_level = conn->pending_sec_level;
1174 ci.ssp_mode = conn->ssp_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 }
1176 hci_dev_unlock_bh(hdev);
1177
1178 if (!conn)
1179 return -ENOENT;
1180
1181 return copy_to_user(ptr, &ci, sizeof(ci)) ? -EFAULT : 0;
1182}
Marcel Holtmann40be4922008-07-14 20:13:50 +02001183
1184int hci_get_auth_info(struct hci_dev *hdev, void __user *arg)
1185{
1186 struct hci_auth_info_req req;
1187 struct hci_conn *conn;
1188
1189 if (copy_from_user(&req, arg, sizeof(req)))
1190 return -EFAULT;
1191
1192 hci_dev_lock_bh(hdev);
1193 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &req.bdaddr);
1194 if (conn)
1195 req.type = conn->auth_type;
1196 hci_dev_unlock_bh(hdev);
1197
1198 if (!conn)
1199 return -ENOENT;
1200
1201 return copy_to_user(arg, &req, sizeof(req)) ? -EFAULT : 0;
1202}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001203
1204int hci_set_auth_info(struct hci_dev *hdev, void __user *arg)
1205{
1206 struct hci_auth_info_req req;
1207 struct hci_conn *conn;
1208
1209 if (copy_from_user(&req, arg, sizeof(req)))
1210 return -EFAULT;
1211
1212 hci_dev_lock_bh(hdev);
1213 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &req.bdaddr);
1214 if (conn)
1215 conn->auth_type = req.type;
1216 hci_dev_unlock_bh(hdev);
1217
1218 if (!conn)
1219 return -ENOENT;
1220
1221 return copy_to_user(arg, &req, sizeof(req)) ? -EFAULT : 0;
1222}