blob: f87be20849c26d48f186d99f43b04897c3cfdbb1 [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);
Archana Ramachandrand6d25e12012-01-23 16:31:09 -080068 cp.supervision_timeout = cpu_to_le16(1000);
Ville Tervofcd89c02011-02-10 22:38:47 -030069 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
Archana Ramachandran26a752b2011-12-20 11:27:40 -0800228void hci_read_rssi(struct hci_conn *conn)
229{
230 struct hci_cp_read_rssi cp;
231 struct hci_dev *hdev = conn->hdev;
232
233 memset(&cp, 0, sizeof(cp));
234 cp.handle = cpu_to_le16(conn->handle);
235
236 hci_send_cmd(hdev, HCI_OP_READ_RSSI, sizeof(cp), &cp);
237}
238EXPORT_SYMBOL(hci_read_rssi);
239
Vinicius Costa Gomes735038c2011-06-09 18:50:47 -0300240void hci_le_start_enc(struct hci_conn *conn, __le16 ediv, __u8 rand[8],
241 __u8 ltk[16])
242{
243 struct hci_dev *hdev = conn->hdev;
244 struct hci_cp_le_start_enc cp;
245
246 BT_DBG("%p", conn);
247
248 memset(&cp, 0, sizeof(cp));
249
250 cp.handle = cpu_to_le16(conn->handle);
251 memcpy(cp.ltk, ltk, sizeof(cp.ltk));
252 cp.ediv = ediv;
Brian Gix842bc5e2011-09-07 08:13:41 -0700253 memcpy(cp.rand, rand, sizeof(cp.rand));
Vinicius Costa Gomes735038c2011-06-09 18:50:47 -0300254
255 hci_send_cmd(hdev, HCI_OP_LE_START_ENC, sizeof(cp), &cp);
256}
257EXPORT_SYMBOL(hci_le_start_enc);
258
259void hci_le_ltk_reply(struct hci_conn *conn, u8 ltk[16])
260{
261 struct hci_dev *hdev = conn->hdev;
262 struct hci_cp_le_ltk_reply cp;
263
264 BT_DBG("%p", conn);
265
266 memset(&cp, 0, sizeof(cp));
267
268 cp.handle = cpu_to_le16(conn->handle);
269 memcpy(cp.ltk, ltk, sizeof(ltk));
270
271 hci_send_cmd(hdev, HCI_OP_LE_LTK_REPLY, sizeof(cp), &cp);
272}
273EXPORT_SYMBOL(hci_le_ltk_reply);
274
275void hci_le_ltk_neg_reply(struct hci_conn *conn)
276{
277 struct hci_dev *hdev = conn->hdev;
278 struct hci_cp_le_ltk_neg_reply cp;
279
280 BT_DBG("%p", conn);
281
282 memset(&cp, 0, sizeof(cp));
283
284 cp.handle = cpu_to_le16(conn->handle);
285
286 hci_send_cmd(hdev, HCI_OP_LE_LTK_NEG_REPLY, sizeof(cp), &cp);
287}
288
Marcel Holtmanne73439d2010-07-26 10:06:00 -0400289/* Device _must_ be locked */
290void hci_sco_setup(struct hci_conn *conn, __u8 status)
291{
292 struct hci_conn *sco = conn->link;
293
294 BT_DBG("%p", conn);
295
296 if (!sco)
297 return;
298
299 if (!status) {
300 if (lmp_esco_capable(conn->hdev))
301 hci_setup_sync(sco, conn->handle);
302 else
303 hci_add_sco(sco, conn->handle);
304 } else {
305 hci_proto_connect_cfm(sco, status);
306 hci_conn_del(sco);
307 }
308}
309
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310static void hci_conn_timeout(unsigned long arg)
311{
Marcel Holtmann04837f62006-07-03 10:02:33 +0200312 struct hci_conn *conn = (void *) arg;
313 struct hci_dev *hdev = conn->hdev;
Marcel Holtmann2950f212009-02-12 14:02:50 +0100314 __u8 reason;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
316 BT_DBG("conn %p state %d", conn, conn->state);
317
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 hci_dev_lock(hdev);
Marcel Holtmann6ac59342006-09-26 09:43:48 +0200319
320 switch (conn->state) {
321 case BT_CONNECT:
Marcel Holtmann769be972008-07-14 20:13:49 +0200322 case BT_CONNECT2:
Ville Tervofcd89c02011-02-10 22:38:47 -0300323 if (conn->out) {
324 if (conn->type == ACL_LINK)
325 hci_acl_connect_cancel(conn);
326 else if (conn->type == LE_LINK)
327 hci_le_connect_cancel(conn);
328 }
Marcel Holtmann6ac59342006-09-26 09:43:48 +0200329 break;
Marcel Holtmann769be972008-07-14 20:13:49 +0200330 case BT_CONFIG:
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900331 case BT_CONNECTED:
Brian Gix114f3a62011-09-27 14:02:20 -0700332 if (!atomic_read(&conn->refcnt)) {
333 reason = hci_proto_disconn_ind(conn);
334 hci_acl_disconn(conn, reason);
335 }
Marcel Holtmann6ac59342006-09-26 09:43:48 +0200336 break;
337 default:
Brian Gix114f3a62011-09-27 14:02:20 -0700338 if (!atomic_read(&conn->refcnt))
339 conn->state = BT_CLOSED;
Marcel Holtmann6ac59342006-09-26 09:43:48 +0200340 break;
341 }
342
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344}
345
Marcel Holtmann04837f62006-07-03 10:02:33 +0200346static void hci_conn_idle(unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347{
Marcel Holtmann04837f62006-07-03 10:02:33 +0200348 struct hci_conn *conn = (void *) arg;
349
350 BT_DBG("conn %p mode %d", conn, conn->mode);
351
352 hci_conn_enter_sniff_mode(conn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353}
354
Archana Ramachandran26a752b2011-12-20 11:27:40 -0800355static void hci_conn_rssi_update(struct work_struct *work)
356{
357 struct delayed_work *delayed =
358 container_of(work, struct delayed_work, work);
359 struct hci_conn *conn =
360 container_of(delayed, struct hci_conn, rssi_update_work);
361
362 BT_DBG("conn %p mode %d", conn, conn->mode);
363
364 hci_read_rssi(conn);
365}
366
Nick Pellybbcda3b2010-02-11 11:54:28 -0800367struct hci_conn *hci_conn_add(struct hci_dev *hdev, int type,
368 __u16 pkt_type, bdaddr_t *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369{
370 struct hci_conn *conn;
371
372 BT_DBG("%s dst %s", hdev->name, batostr(dst));
373
Marcel Holtmann04837f62006-07-03 10:02:33 +0200374 conn = kzalloc(sizeof(struct hci_conn), GFP_ATOMIC);
375 if (!conn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
378 bacpy(&conn->dst, dst);
Marcel Holtmanna8746412008-07-14 20:13:46 +0200379 conn->hdev = hdev;
380 conn->type = type;
381 conn->mode = HCI_CM_ACTIVE;
382 conn->state = BT_OPEN;
Andrei Emeltchenko93f19c92009-09-03 12:34:19 +0300383 conn->auth_type = HCI_AT_GENERAL_BONDING;
Johan Hedberg17fa4b92011-01-25 13:28:33 +0200384 conn->io_capability = hdev->io_capability;
Johan Hedberga9583552011-02-19 12:06:01 -0300385 conn->remote_auth = 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Marcel Holtmann04837f62006-07-03 10:02:33 +0200387 conn->power_save = 1;
Marcel Holtmann052b30b2009-04-26 20:01:22 +0200388 conn->disc_timeout = HCI_DISCONN_TIMEOUT;
Marcel Holtmann04837f62006-07-03 10:02:33 +0200389
Marcel Holtmanna8746412008-07-14 20:13:46 +0200390 switch (type) {
391 case ACL_LINK:
392 conn->pkt_type = hdev->pkt_type & ACL_PTYPE_MASK;
Rahul Kashyapd5553ba2012-01-02 19:27:19 +0530393 conn->link_policy = hdev->link_policy;
Marcel Holtmanna8746412008-07-14 20:13:46 +0200394 break;
395 case SCO_LINK:
Nick Pellybbcda3b2010-02-11 11:54:28 -0800396 if (!pkt_type)
397 pkt_type = SCO_ESCO_MASK;
Marcel Holtmanna8746412008-07-14 20:13:46 +0200398 case ESCO_LINK:
Nick Pellybbcda3b2010-02-11 11:54:28 -0800399 if (!pkt_type)
400 pkt_type = ALL_ESCO_MASK;
401 if (lmp_esco_capable(hdev)) {
402 /* HCI Setup Synchronous Connection Command uses
403 reverse logic on the EDR_ESCO_MASK bits */
404 conn->pkt_type = (pkt_type ^ EDR_ESCO_MASK) &
405 hdev->esco_type;
406 } else {
407 /* Legacy HCI Add Sco Connection Command uses a
408 shifted bitmask */
409 conn->pkt_type = (pkt_type << 5) & hdev->pkt_type &
410 SCO_PTYPE_MASK;
411 }
Marcel Holtmanna8746412008-07-14 20:13:46 +0200412 break;
413 }
414
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 skb_queue_head_init(&conn->data_q);
Marcel Holtmann04837f62006-07-03 10:02:33 +0200416
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800417 setup_timer(&conn->disc_timer, hci_conn_timeout, (unsigned long)conn);
418 setup_timer(&conn->idle_timer, hci_conn_idle, (unsigned long)conn);
Archana Ramachandran26a752b2011-12-20 11:27:40 -0800419 INIT_DELAYED_WORK(&conn->rssi_update_work, hci_conn_rssi_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
421 atomic_set(&conn->refcnt, 0);
422
423 hci_dev_hold(hdev);
424
425 tasklet_disable(&hdev->tx_task);
426
427 hci_conn_hash_add(hdev, conn);
428 if (hdev->notify)
429 hdev->notify(hdev, HCI_NOTIFY_CONN_ADD);
430
Marcel Holtmann9eba32b2009-08-22 14:19:26 -0700431 atomic_set(&conn->devref, 0);
432
Marcel Holtmanna67e8992009-05-02 18:24:06 -0700433 hci_conn_init_sysfs(conn);
434
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 tasklet_enable(&hdev->tx_task);
436
437 return conn;
438}
439
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700440struct hci_conn *hci_le_conn_add(struct hci_dev *hdev, bdaddr_t *dst,
441 __u8 addr_type)
442{
443 struct hci_conn *conn = hci_conn_add(hdev, LE_LINK, 0, dst);
444 if (!conn)
445 return NULL;
446
447 conn->dst_type = addr_type;
448
449 return conn;
450}
451
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452int hci_conn_del(struct hci_conn *conn)
453{
454 struct hci_dev *hdev = conn->hdev;
455
456 BT_DBG("%s conn %p handle %d", hdev->name, conn, conn->handle);
457
Brian Gix3cd62042012-01-11 15:18:17 -0800458 /* Make sure no timers are running */
Marcel Holtmann04837f62006-07-03 10:02:33 +0200459 del_timer(&conn->idle_timer);
Marcel Holtmann04837f62006-07-03 10:02:33 +0200460 del_timer(&conn->disc_timer);
Brian Gix3cd62042012-01-11 15:18:17 -0800461 del_timer(&conn->smp_timer);
Archana Ramachandran26a752b2011-12-20 11:27:40 -0800462 __cancel_delayed_work(&conn->rssi_update_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200464 if (conn->type == ACL_LINK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 struct hci_conn *sco = conn->link;
466 if (sco)
467 sco->link = NULL;
468
469 /* Unacked frames */
470 hdev->acl_cnt += conn->sent;
Ville Tervo6ed58ec2011-02-10 22:38:48 -0300471 } else if (conn->type == LE_LINK) {
472 if (hdev->le_pkts)
473 hdev->le_cnt += conn->sent;
474 else
475 hdev->acl_cnt += conn->sent;
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200476 } else {
477 struct hci_conn *acl = conn->link;
478 if (acl) {
479 acl->link = NULL;
480 hci_conn_put(acl);
481 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 }
483
484 tasklet_disable(&hdev->tx_task);
Marcel Holtmann7d0db0a2008-07-14 20:13:51 +0200485
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 hci_conn_hash_del(hdev, conn);
487 if (hdev->notify)
488 hdev->notify(hdev, HCI_NOTIFY_CONN_DEL);
Marcel Holtmann7d0db0a2008-07-14 20:13:51 +0200489
AnubhavGupta01d9e362011-12-17 17:14:51 +0530490 tasklet_schedule(&hdev->tx_task);
491
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 tasklet_enable(&hdev->tx_task);
Marcel Holtmann7d0db0a2008-07-14 20:13:51 +0200493
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 skb_queue_purge(&conn->data_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
Marcel Holtmann9eba32b2009-08-22 14:19:26 -0700496 hci_conn_put_device(conn);
Dave Young2ae9a6b2009-02-21 16:13:34 +0800497
Marcel Holtmann384943e2009-05-08 18:20:43 -0700498 hci_dev_put(hdev);
499
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700500 return 0;
501}
502
503struct hci_chan *hci_chan_add(struct hci_dev *hdev)
504{
505 struct hci_chan *chan;
506
507 BT_DBG("%s", hdev->name);
508
509 chan = kzalloc(sizeof(struct hci_chan), GFP_ATOMIC);
510 if (!chan)
511 return NULL;
512
513 atomic_set(&chan->refcnt, 0);
514
515 hci_dev_hold(hdev);
516
517 chan->hdev = hdev;
518
519 list_add(&chan->list, &hdev->chan_list.list);
520
521 return chan;
522}
Peter Krystada8417e62012-03-21 16:58:17 -0700523EXPORT_SYMBOL(hci_chan_add);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700524
525int hci_chan_del(struct hci_chan *chan)
526{
527 BT_DBG("%s chan %p", chan->hdev->name, chan);
528
529 list_del(&chan->list);
530
531 hci_conn_put(chan->conn);
532 hci_dev_put(chan->hdev);
533
534 kfree(chan);
Tomas Targownik1be668d2011-06-30 16:30:44 -0300535
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 return 0;
537}
538
Peter Krystadd6a9ceb2011-12-01 15:44:54 -0800539int hci_chan_put(struct hci_chan *chan)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700540{
541 struct hci_cp_disconn_logical_link cp;
Mat Martineau9f8d4672011-12-14 12:10:46 -0800542 struct hci_conn *hcon;
543 u16 ll_handle;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700544
545 BT_DBG("chan %p refcnt %d", chan, atomic_read(&chan->refcnt));
546 if (!atomic_dec_and_test(&chan->refcnt))
Peter Krystadd6a9ceb2011-12-01 15:44:54 -0800547 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700548
Mat Martineau9f8d4672011-12-14 12:10:46 -0800549 hcon = chan->conn;
550 ll_handle = chan->ll_handle;
551
552 hci_chan_del(chan);
553
554 BT_DBG("chan->conn->state %d", hcon->state);
555 if (hcon->state == BT_CONNECTED) {
556 cp.log_handle = cpu_to_le16(ll_handle);
557 hci_send_cmd(hcon->hdev, HCI_OP_DISCONN_LOGICAL_LINK,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700558 sizeof(cp), &cp);
Mat Martineau9f8d4672011-12-14 12:10:46 -0800559 }
Peter Krystadd6a9ceb2011-12-01 15:44:54 -0800560
561 return 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700562}
563EXPORT_SYMBOL(hci_chan_put);
564
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565struct hci_dev *hci_get_route(bdaddr_t *dst, bdaddr_t *src)
566{
567 int use_src = bacmp(src, BDADDR_ANY);
568 struct hci_dev *hdev = NULL;
569 struct list_head *p;
570
571 BT_DBG("%s -> %s", batostr(src), batostr(dst));
572
573 read_lock_bh(&hci_dev_list_lock);
574
575 list_for_each(p, &hci_dev_list) {
576 struct hci_dev *d = list_entry(p, struct hci_dev, list);
577
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700578 if (d->dev_type != HCI_BREDR)
579 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 if (!test_bit(HCI_UP, &d->flags) || test_bit(HCI_RAW, &d->flags))
581 continue;
582
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900583 /* Simple routing:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 * No source address - find interface with bdaddr != dst
585 * Source address - find interface with bdaddr == src
586 */
587
588 if (use_src) {
589 if (!bacmp(&d->bdaddr, src)) {
590 hdev = d; break;
591 }
592 } else {
593 if (bacmp(&d->bdaddr, dst)) {
594 hdev = d; break;
595 }
596 }
597 }
598
599 if (hdev)
600 hdev = hci_dev_hold(hdev);
601
602 read_unlock_bh(&hci_dev_list_lock);
603 return hdev;
604}
605EXPORT_SYMBOL(hci_get_route);
606
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700607struct hci_dev *hci_dev_get_type(u8 amp_type)
608{
609 struct hci_dev *hdev = NULL;
610 struct hci_dev *d;
611
612 BT_DBG("amp_type %d", amp_type);
613
614 read_lock_bh(&hci_dev_list_lock);
615
616 list_for_each_entry(d, &hci_dev_list, list) {
617 if ((d->amp_type == amp_type) && test_bit(HCI_UP, &d->flags)) {
618 hdev = d;
619 break;
620 }
621 }
622
623 if (hdev)
624 hdev = hci_dev_hold(hdev);
625
626 read_unlock_bh(&hci_dev_list_lock);
627 return hdev;
628}
629EXPORT_SYMBOL(hci_dev_get_type);
630
631struct hci_dev *hci_dev_get_amp(bdaddr_t *dst)
632{
633 struct hci_dev *d;
634 struct hci_dev *hdev = NULL;
635
636 BT_DBG("%s dst %s", hdev->name, batostr(dst));
637
638 read_lock_bh(&hci_dev_list_lock);
639
640 list_for_each_entry(d, &hci_dev_list, list) {
641 struct hci_conn *conn;
642 if (d->dev_type == HCI_BREDR)
643 continue;
644 conn = hci_conn_hash_lookup_ba(d, ACL_LINK, dst);
645 if (conn) {
646 hdev = d;
647 break;
648 }
649 }
650
651 if (hdev)
652 hdev = hci_dev_hold(hdev);
653
654 read_unlock_bh(&hci_dev_list_lock);
655 return hdev;
656}
657EXPORT_SYMBOL(hci_dev_get_amp);
658
Ville Tervofcd89c02011-02-10 22:38:47 -0300659/* Create SCO, ACL or LE connection.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 * Device _must_ be locked */
Nick Pellybbcda3b2010-02-11 11:54:28 -0800661struct hci_conn *hci_connect(struct hci_dev *hdev, int type,
662 __u16 pkt_type, bdaddr_t *dst,
663 __u8 sec_level, __u8 auth_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664{
665 struct hci_conn *acl;
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200666 struct hci_conn *sco;
Ville Tervofcd89c02011-02-10 22:38:47 -0300667 struct hci_conn *le;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
669 BT_DBG("%s dst %s", hdev->name, batostr(dst));
670
Ville Tervofcd89c02011-02-10 22:38:47 -0300671 if (type == LE_LINK) {
Andre Guedes5e89ece2011-05-31 14:20:56 -0300672 struct adv_entry *entry;
Brian Gixcf956772011-10-20 15:18:51 -0700673 struct link_key *key;
Andre Guedes5e89ece2011-05-31 14:20:56 -0300674
Ville Tervofcd89c02011-02-10 22:38:47 -0300675 le = hci_conn_hash_lookup_ba(hdev, LE_LINK, dst);
Brian Gix2e2f50d2011-09-13 12:36:04 -0700676 if (le) {
677 hci_conn_hold(le);
Brian Gixa68668b2011-08-11 15:49:36 -0700678 return le;
Brian Gix2e2f50d2011-09-13 12:36:04 -0700679 }
Andre Guedes5e89ece2011-05-31 14:20:56 -0300680
Brian Gixcf956772011-10-20 15:18:51 -0700681 key = hci_find_link_key_type(hdev, dst, KEY_TYPE_LTK);
682 if (!key) {
683 entry = hci_find_adv_entry(hdev, dst);
684 if (entry)
685 le = hci_le_conn_add(hdev, dst,
686 entry->bdaddr_type);
687 else
688 le = hci_le_conn_add(hdev, dst, 0);
689 } else {
690 le = hci_le_conn_add(hdev, dst, key->addr_type);
691 }
Andre Guedes5e89ece2011-05-31 14:20:56 -0300692
Ville Tervofcd89c02011-02-10 22:38:47 -0300693 if (!le)
Ville Tervo30e76272011-02-22 16:10:53 -0300694 return ERR_PTR(-ENOMEM);
Andre Guedes92398c82011-05-31 14:20:55 -0300695
696 hci_le_connect(le);
Ville Tervofcd89c02011-02-10 22:38:47 -0300697
698 hci_conn_hold(le);
699
700 return le;
701 }
702
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200703 acl = hci_conn_hash_lookup_ba(hdev, ACL_LINK, dst);
704 if (!acl) {
Nick Pellybbcda3b2010-02-11 11:54:28 -0800705 acl = hci_conn_add(hdev, ACL_LINK, 0, dst);
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200706 if (!acl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 return NULL;
708 }
709
710 hci_conn_hold(acl);
711
Marcel Holtmann09ab6f42008-09-09 07:19:20 +0200712 if (acl->state == BT_OPEN || acl->state == BT_CLOSED) {
Johan Hedberg765c2a92011-01-19 12:06:52 +0530713 acl->sec_level = BT_SECURITY_LOW;
714 acl->pending_sec_level = sec_level;
Marcel Holtmann09ab6f42008-09-09 07:19:20 +0200715 acl->auth_type = auth_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 hci_acl_connect(acl);
Marcel Holtmann09ab6f42008-09-09 07:19:20 +0200717 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200719 if (type == ACL_LINK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 return acl;
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200721
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200722 sco = hci_conn_hash_lookup_ba(hdev, type, dst);
723 if (!sco) {
Nick Pellybbcda3b2010-02-11 11:54:28 -0800724 sco = hci_conn_add(hdev, type, pkt_type, dst);
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200725 if (!sco) {
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200726 hci_conn_put(acl);
727 return NULL;
728 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 }
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200730
731 acl->link = sco;
732 sco->link = acl;
733
734 hci_conn_hold(sco);
735
736 if (acl->state == BT_CONNECTED &&
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200737 (sco->state == BT_OPEN || sco->state == BT_CLOSED)) {
Nick Pellyc3902162009-11-13 14:16:32 -0800738 acl->power_save = 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700739 hci_conn_enter_active_mode(acl, 1);
Nick Pellyc3902162009-11-13 14:16:32 -0800740
Marcel Holtmanne73439d2010-07-26 10:06:00 -0400741 if (test_bit(HCI_CONN_MODE_CHANGE_PEND, &acl->pend)) {
742 /* defer SCO setup until mode change completed */
743 set_bit(HCI_CONN_SCO_SETUP_PEND, &acl->pend);
744 return sco;
745 }
746
747 hci_sco_setup(acl, 0x00);
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200748 }
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200749
750 return sco;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751}
752EXPORT_SYMBOL(hci_connect);
753
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700754void hci_disconnect(struct hci_conn *conn, __u8 reason)
755{
756 BT_DBG("conn %p", conn);
757
Mat Martineau3b9239a2012-02-16 11:54:30 -0800758 hci_proto_disconn_cfm(conn, reason, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700759}
760EXPORT_SYMBOL(hci_disconnect);
761
762void hci_disconnect_amp(struct hci_conn *conn, __u8 reason)
763{
764 struct hci_dev *hdev = NULL;
765
766 BT_DBG("conn %p", conn);
767
768 read_lock_bh(&hci_dev_list_lock);
769
770 list_for_each_entry(hdev, &hci_dev_list, list) {
771 struct hci_conn *c;
772 if (hdev == conn->hdev)
773 continue;
774 if (hdev->amp_type == HCI_BREDR)
775 continue;
776 c = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &conn->dst);
777 if (c)
778 hci_disconnect(c, reason);
779 }
780
781 read_unlock_bh(&hci_dev_list_lock);
782}
783
Marcel Holtmanne7c29cb2008-09-09 07:19:20 +0200784/* Check link security requirement */
785int hci_conn_check_link_mode(struct hci_conn *conn)
786{
787 BT_DBG("conn %p", conn);
788
789 if (conn->ssp_mode > 0 && conn->hdev->ssp_mode > 0 &&
790 !(conn->link_mode & HCI_LM_ENCRYPT))
791 return 0;
792
793 return 1;
794}
795EXPORT_SYMBOL(hci_conn_check_link_mode);
796
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797/* Authenticate remote device */
Marcel Holtmann0684e5f2009-02-09 02:48:38 +0100798static int hci_conn_auth(struct hci_conn *conn, __u8 sec_level, __u8 auth_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799{
800 BT_DBG("conn %p", conn);
801
Johan Hedberg765c2a92011-01-19 12:06:52 +0530802 if (conn->pending_sec_level > sec_level)
803 sec_level = conn->pending_sec_level;
804
Marcel Holtmann96a31832009-02-12 16:23:03 +0100805 if (sec_level > conn->sec_level)
Johan Hedberg765c2a92011-01-19 12:06:52 +0530806 conn->pending_sec_level = sec_level;
Marcel Holtmann96a31832009-02-12 16:23:03 +0100807 else if (conn->link_mode & HCI_LM_AUTH)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 return 1;
809
Johan Hedberg65cf6862011-01-19 12:06:49 +0530810 /* Make sure we preserve an existing MITM requirement*/
811 auth_type |= (conn->auth_type & 0x01);
Marcel Holtmann96a31832009-02-12 16:23:03 +0100812 conn->auth_type = auth_type;
Brian Gixa68668b2011-08-11 15:49:36 -0700813 conn->auth_initiator = 1;
Marcel Holtmann96a31832009-02-12 16:23:03 +0100814
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 if (!test_and_set_bit(HCI_CONN_AUTH_PEND, &conn->pend)) {
816 struct hci_cp_auth_requested cp;
Peter Hurleya5e5b082012-01-13 15:11:30 +0100817
818 /* encrypt must be pending if auth is also pending */
819 set_bit(HCI_CONN_ENCRYPT_PEND, &conn->pend);
820
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -0700821 cp.handle = cpu_to_le16(conn->handle);
Marcel Holtmann40be4922008-07-14 20:13:50 +0200822 hci_send_cmd(conn->hdev, HCI_OP_AUTH_REQUESTED,
823 sizeof(cp), &cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 }
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100825
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 return 0;
827}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100829/* Enable security */
Marcel Holtmann0684e5f2009-02-09 02:48:38 +0100830int hci_conn_security(struct hci_conn *conn, __u8 sec_level, __u8 auth_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831{
Brian Gixa68668b2011-08-11 15:49:36 -0700832 BT_DBG("conn %p %d %d", conn, sec_level, auth_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100834 if (sec_level == BT_SECURITY_SDP)
835 return 1;
836
Marcel Holtmann3fdca1e2009-04-28 09:04:55 -0700837 if (sec_level == BT_SECURITY_LOW &&
838 (!conn->ssp_mode || !conn->hdev->ssp_mode))
839 return 1;
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100840
Brian Gix2e2f50d2011-09-13 12:36:04 -0700841 if (conn->type == LE_LINK) {
842 if (conn->pending_sec_level > sec_level)
843 sec_level = conn->pending_sec_level;
Waldemar Rymarkiewicz13d39312011-04-28 12:07:55 +0200844
Brian Gix2e2f50d2011-09-13 12:36:04 -0700845 if (sec_level > conn->sec_level)
846 conn->pending_sec_level = sec_level;
847 hci_proto_connect_cfm(conn, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700848 return 0;
Brian Gix2e2f50d2011-09-13 12:36:04 -0700849 } else if (conn->link_mode & HCI_LM_ENCRYPT) {
850 return hci_conn_auth(conn, sec_level, auth_type);
Ilia Kolomisnkyedc44dd2011-06-15 06:52:26 +0300851 } else if (test_bit(HCI_CONN_ENCRYPT_PEND, &conn->pend)) {
Brian Gix2e2f50d2011-09-13 12:36:04 -0700852 return 0;
853 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700854
855 if (hci_conn_auth(conn, sec_level, auth_type)) {
856 struct hci_cp_set_conn_encrypt cp;
857 cp.handle = cpu_to_le16(conn->handle);
858 cp.encrypt = 1;
859 hci_send_cmd(conn->hdev, HCI_OP_SET_CONN_ENCRYPT,
860 sizeof(cp), &cp);
861 }
862
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 return 0;
864}
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100865EXPORT_SYMBOL(hci_conn_security);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
867/* Change link key */
868int hci_conn_change_link_key(struct hci_conn *conn)
869{
870 BT_DBG("conn %p", conn);
871
872 if (!test_and_set_bit(HCI_CONN_AUTH_PEND, &conn->pend)) {
873 struct hci_cp_change_conn_link_key cp;
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -0700874 cp.handle = cpu_to_le16(conn->handle);
Marcel Holtmann40be4922008-07-14 20:13:50 +0200875 hci_send_cmd(conn->hdev, HCI_OP_CHANGE_CONN_LINK_KEY,
876 sizeof(cp), &cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 }
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100878
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 return 0;
880}
881EXPORT_SYMBOL(hci_conn_change_link_key);
882
883/* Switch role */
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100884int hci_conn_switch_role(struct hci_conn *conn, __u8 role)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885{
886 BT_DBG("conn %p", conn);
887
888 if (!role && conn->link_mode & HCI_LM_MASTER)
889 return 1;
890
891 if (!test_and_set_bit(HCI_CONN_RSWITCH_PEND, &conn->pend)) {
892 struct hci_cp_switch_role cp;
893 bacpy(&cp.bdaddr, &conn->dst);
894 cp.role = role;
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200895 hci_send_cmd(conn->hdev, HCI_OP_SWITCH_ROLE, sizeof(cp), &cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 }
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100897
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 return 0;
899}
900EXPORT_SYMBOL(hci_conn_switch_role);
901
Marcel Holtmann04837f62006-07-03 10:02:33 +0200902/* Enter active mode */
Jaikumar Ganesh514abe62011-05-23 18:06:04 -0700903void hci_conn_enter_active_mode(struct hci_conn *conn, __u8 force_active)
Marcel Holtmann04837f62006-07-03 10:02:33 +0200904{
905 struct hci_dev *hdev = conn->hdev;
906
907 BT_DBG("conn %p mode %d", conn, conn->mode);
908
909 if (test_bit(HCI_RAW, &hdev->flags))
910 return;
911
Jaikumar Ganesh514abe62011-05-23 18:06:04 -0700912 if (conn->mode != HCI_CM_SNIFF)
913 goto timer;
914
915 if (!conn->power_save && !force_active)
Marcel Holtmann04837f62006-07-03 10:02:33 +0200916 goto timer;
917
918 if (!test_and_set_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->pend)) {
919 struct hci_cp_exit_sniff_mode cp;
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -0700920 cp.handle = cpu_to_le16(conn->handle);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200921 hci_send_cmd(hdev, HCI_OP_EXIT_SNIFF_MODE, sizeof(cp), &cp);
Marcel Holtmann04837f62006-07-03 10:02:33 +0200922 }
923
924timer:
925 if (hdev->idle_timeout > 0)
926 mod_timer(&conn->idle_timer,
927 jiffies + msecs_to_jiffies(hdev->idle_timeout));
928}
929
Archana Ramachandran26a752b2011-12-20 11:27:40 -0800930static inline void hci_conn_stop_rssi_timer(struct hci_conn *conn)
931{
932 BT_DBG("conn %p", conn);
933 cancel_delayed_work(&conn->rssi_update_work);
934}
935
936static inline void hci_conn_start_rssi_timer(struct hci_conn *conn,
937 u16 interval)
938{
939 struct hci_dev *hdev = conn->hdev;
940 BT_DBG("conn %p, pending %d", conn,
941 delayed_work_pending(&conn->rssi_update_work));
942 if (!delayed_work_pending(&conn->rssi_update_work)) {
943 queue_delayed_work(hdev->workqueue, &conn->rssi_update_work,
944 msecs_to_jiffies(interval));
945 }
946}
947
948void hci_conn_set_rssi_reporter(struct hci_conn *conn,
949 s8 rssi_threshold, u16 interval, u8 updateOnThreshExceed)
950{
951 if (conn) {
952 conn->rssi_threshold = rssi_threshold;
953 conn->rssi_update_interval = interval;
954 conn->rssi_update_thresh_exceed = updateOnThreshExceed;
955 hci_conn_start_rssi_timer(conn, interval);
956 }
957}
958
959void hci_conn_unset_rssi_reporter(struct hci_conn *conn)
960{
961 if (conn) {
962 BT_DBG("Deleting the rssi_update_timer");
963 hci_conn_stop_rssi_timer(conn);
964 }
965}
966
Marcel Holtmann04837f62006-07-03 10:02:33 +0200967/* Enter sniff mode */
968void hci_conn_enter_sniff_mode(struct hci_conn *conn)
969{
970 struct hci_dev *hdev = conn->hdev;
971
972 BT_DBG("conn %p mode %d", conn, conn->mode);
973
974 if (test_bit(HCI_RAW, &hdev->flags))
975 return;
976
977 if (!lmp_sniff_capable(hdev) || !lmp_sniff_capable(conn))
978 return;
979
Srinivas Krovvidi32ba9352012-01-26 10:48:08 +0530980 if (conn->mode != HCI_CM_ACTIVE ||
981 !(conn->link_policy & HCI_LP_SNIFF) ||
982 (hci_find_link_key(hdev, &conn->dst) == NULL))
Marcel Holtmann04837f62006-07-03 10:02:33 +0200983 return;
984
985 if (lmp_sniffsubr_capable(hdev) && lmp_sniffsubr_capable(conn)) {
986 struct hci_cp_sniff_subrate cp;
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -0700987 cp.handle = cpu_to_le16(conn->handle);
988 cp.max_latency = cpu_to_le16(0);
989 cp.min_remote_timeout = cpu_to_le16(0);
990 cp.min_local_timeout = cpu_to_le16(0);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200991 hci_send_cmd(hdev, HCI_OP_SNIFF_SUBRATE, sizeof(cp), &cp);
Marcel Holtmann04837f62006-07-03 10:02:33 +0200992 }
993
994 if (!test_and_set_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->pend)) {
995 struct hci_cp_sniff_mode cp;
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -0700996 cp.handle = cpu_to_le16(conn->handle);
997 cp.max_interval = cpu_to_le16(hdev->sniff_max_interval);
998 cp.min_interval = cpu_to_le16(hdev->sniff_min_interval);
999 cp.attempt = cpu_to_le16(4);
1000 cp.timeout = cpu_to_le16(1);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001001 hci_send_cmd(hdev, HCI_OP_SNIFF_MODE, sizeof(cp), &cp);
Marcel Holtmann04837f62006-07-03 10:02:33 +02001002 }
1003}
1004
Peter Krystada8417e62012-03-21 16:58:17 -07001005struct hci_chan *hci_chan_create(struct hci_chan *chan,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001006 struct hci_ext_fs *tx_fs, struct hci_ext_fs *rx_fs)
1007{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001008 struct hci_cp_create_logical_link cp;
1009
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001010 chan->state = BT_CONNECT;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001011 chan->tx_fs = *tx_fs;
1012 chan->rx_fs = *rx_fs;
1013 cp.phy_handle = chan->conn->handle;
1014 cp.tx_fs.id = chan->tx_fs.id;
1015 cp.tx_fs.type = chan->tx_fs.type;
1016 cp.tx_fs.max_sdu = cpu_to_le16(chan->tx_fs.max_sdu);
1017 cp.tx_fs.sdu_arr_time = cpu_to_le32(chan->tx_fs.sdu_arr_time);
1018 cp.tx_fs.acc_latency = cpu_to_le32(chan->tx_fs.acc_latency);
1019 cp.tx_fs.flush_to = cpu_to_le32(chan->tx_fs.flush_to);
1020 cp.rx_fs.id = chan->rx_fs.id;
1021 cp.rx_fs.type = chan->rx_fs.type;
1022 cp.rx_fs.max_sdu = cpu_to_le16(chan->rx_fs.max_sdu);
1023 cp.rx_fs.sdu_arr_time = cpu_to_le32(chan->rx_fs.sdu_arr_time);
1024 cp.rx_fs.acc_latency = cpu_to_le32(chan->rx_fs.acc_latency);
1025 cp.rx_fs.flush_to = cpu_to_le32(chan->rx_fs.flush_to);
1026 hci_conn_hold(chan->conn);
Peter Krystada8417e62012-03-21 16:58:17 -07001027 if (chan->conn->out)
1028 hci_send_cmd(chan->conn->hdev, HCI_OP_CREATE_LOGICAL_LINK,
1029 sizeof(cp), &cp);
1030 else
1031 hci_send_cmd(chan->conn->hdev, HCI_OP_ACCEPT_LOGICAL_LINK,
1032 sizeof(cp), &cp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001033 return chan;
1034}
1035EXPORT_SYMBOL(hci_chan_create);
1036
1037void hci_chan_modify(struct hci_chan *chan,
1038 struct hci_ext_fs *tx_fs, struct hci_ext_fs *rx_fs)
1039{
1040 struct hci_cp_flow_spec_modify cp;
1041
1042 chan->tx_fs = *tx_fs;
1043 chan->rx_fs = *rx_fs;
1044 cp.log_handle = cpu_to_le16(chan->ll_handle);
1045 cp.tx_fs.id = tx_fs->id;
1046 cp.tx_fs.type = tx_fs->type;
1047 cp.tx_fs.max_sdu = cpu_to_le16(tx_fs->max_sdu);
1048 cp.tx_fs.sdu_arr_time = cpu_to_le32(tx_fs->sdu_arr_time);
1049 cp.tx_fs.acc_latency = cpu_to_le32(tx_fs->acc_latency);
1050 cp.tx_fs.flush_to = cpu_to_le32(tx_fs->flush_to);
1051 cp.rx_fs.id = rx_fs->id;
1052 cp.rx_fs.type = rx_fs->type;
1053 cp.rx_fs.max_sdu = cpu_to_le16(rx_fs->max_sdu);
1054 cp.rx_fs.sdu_arr_time = cpu_to_le32(rx_fs->sdu_arr_time);
1055 cp.rx_fs.acc_latency = cpu_to_le32(rx_fs->acc_latency);
1056 cp.rx_fs.flush_to = cpu_to_le32(rx_fs->flush_to);
1057 hci_conn_hold(chan->conn);
1058 hci_send_cmd(chan->conn->hdev, HCI_OP_FLOW_SPEC_MODIFY, sizeof(cp),
1059 &cp);
1060}
1061EXPORT_SYMBOL(hci_chan_modify);
1062
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063/* Drop all connection on the device */
Mat Martineau3b9239a2012-02-16 11:54:30 -08001064void hci_conn_hash_flush(struct hci_dev *hdev, u8 is_process)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065{
1066 struct hci_conn_hash *h = &hdev->conn_hash;
1067 struct list_head *p;
1068
1069 BT_DBG("hdev %s", hdev->name);
1070
1071 p = h->list.next;
1072 while (p != &h->list) {
1073 struct hci_conn *c;
1074
1075 c = list_entry(p, struct hci_conn, list);
1076 p = p->next;
1077
1078 c->state = BT_CLOSED;
1079
Mat Martineau3b9239a2012-02-16 11:54:30 -08001080 hci_proto_disconn_cfm(c, 0x16, is_process);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 hci_conn_del(c);
1082 }
1083}
1084
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001085/* Check pending connect attempts */
1086void hci_conn_check_pending(struct hci_dev *hdev)
1087{
1088 struct hci_conn *conn;
1089
1090 BT_DBG("hdev %s", hdev->name);
1091
1092 hci_dev_lock(hdev);
1093
1094 conn = hci_conn_hash_lookup_state(hdev, ACL_LINK, BT_CONNECT2);
1095 if (conn)
1096 hci_acl_connect(conn);
1097
1098 hci_dev_unlock(hdev);
1099}
1100
Marcel Holtmann9eba32b2009-08-22 14:19:26 -07001101void hci_conn_hold_device(struct hci_conn *conn)
1102{
1103 atomic_inc(&conn->devref);
1104}
1105EXPORT_SYMBOL(hci_conn_hold_device);
1106
1107void hci_conn_put_device(struct hci_conn *conn)
1108{
1109 if (atomic_dec_and_test(&conn->devref))
1110 hci_conn_del_sysfs(conn);
1111}
1112EXPORT_SYMBOL(hci_conn_put_device);
1113
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114int hci_get_conn_list(void __user *arg)
1115{
1116 struct hci_conn_list_req req, *cl;
1117 struct hci_conn_info *ci;
1118 struct hci_dev *hdev;
1119 struct list_head *p;
1120 int n = 0, size, err;
1121
1122 if (copy_from_user(&req, arg, sizeof(req)))
1123 return -EFAULT;
1124
1125 if (!req.conn_num || req.conn_num > (PAGE_SIZE * 2) / sizeof(*ci))
1126 return -EINVAL;
1127
1128 size = sizeof(req) + req.conn_num * sizeof(*ci);
1129
Andrei Emeltchenko70f230202010-12-01 16:58:25 +02001130 cl = kmalloc(size, GFP_KERNEL);
1131 if (!cl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 return -ENOMEM;
1133
Andrei Emeltchenko70f230202010-12-01 16:58:25 +02001134 hdev = hci_dev_get(req.dev_id);
1135 if (!hdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 kfree(cl);
1137 return -ENODEV;
1138 }
1139
1140 ci = cl->conn_info;
1141
1142 hci_dev_lock_bh(hdev);
1143 list_for_each(p, &hdev->conn_hash.list) {
1144 register struct hci_conn *c;
1145 c = list_entry(p, struct hci_conn, list);
1146
1147 bacpy(&(ci + n)->bdaddr, &c->dst);
1148 (ci + n)->handle = c->handle;
1149 (ci + n)->type = c->type;
1150 (ci + n)->out = c->out;
1151 (ci + n)->state = c->state;
1152 (ci + n)->link_mode = c->link_mode;
Nick Pellyc1728492009-12-09 00:15:41 -08001153 if (c->type == SCO_LINK) {
1154 (ci + n)->mtu = hdev->sco_mtu;
1155 (ci + n)->cnt = hdev->sco_cnt;
1156 (ci + n)->pkts = hdev->sco_pkts;
1157 } else {
1158 (ci + n)->mtu = hdev->acl_mtu;
1159 (ci + n)->cnt = hdev->acl_cnt;
1160 (ci + n)->pkts = hdev->acl_pkts;
1161 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 if (++n >= req.conn_num)
1163 break;
1164 }
1165 hci_dev_unlock_bh(hdev);
1166
1167 cl->dev_id = hdev->id;
1168 cl->conn_num = n;
1169 size = sizeof(req) + n * sizeof(*ci);
1170
1171 hci_dev_put(hdev);
1172
1173 err = copy_to_user(arg, cl, size);
1174 kfree(cl);
1175
1176 return err ? -EFAULT : 0;
1177}
1178
1179int hci_get_conn_info(struct hci_dev *hdev, void __user *arg)
1180{
1181 struct hci_conn_info_req req;
1182 struct hci_conn_info ci;
1183 struct hci_conn *conn;
1184 char __user *ptr = arg + sizeof(req);
1185
1186 if (copy_from_user(&req, arg, sizeof(req)))
1187 return -EFAULT;
1188
1189 hci_dev_lock_bh(hdev);
1190 conn = hci_conn_hash_lookup_ba(hdev, req.type, &req.bdaddr);
1191 if (conn) {
1192 bacpy(&ci.bdaddr, &conn->dst);
1193 ci.handle = conn->handle;
1194 ci.type = conn->type;
1195 ci.out = conn->out;
1196 ci.state = conn->state;
1197 ci.link_mode = conn->link_mode;
Nick Pellyc1728492009-12-09 00:15:41 -08001198 if (req.type == SCO_LINK) {
1199 ci.mtu = hdev->sco_mtu;
1200 ci.cnt = hdev->sco_cnt;
1201 ci.pkts = hdev->sco_pkts;
1202 } else {
1203 ci.mtu = hdev->acl_mtu;
1204 ci.cnt = hdev->acl_cnt;
1205 ci.pkts = hdev->acl_pkts;
1206 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001207 ci.pending_sec_level = conn->pending_sec_level;
1208 ci.ssp_mode = conn->ssp_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 }
1210 hci_dev_unlock_bh(hdev);
1211
1212 if (!conn)
1213 return -ENOENT;
1214
1215 return copy_to_user(ptr, &ci, sizeof(ci)) ? -EFAULT : 0;
1216}
Marcel Holtmann40be4922008-07-14 20:13:50 +02001217
1218int hci_get_auth_info(struct hci_dev *hdev, void __user *arg)
1219{
1220 struct hci_auth_info_req req;
1221 struct hci_conn *conn;
1222
1223 if (copy_from_user(&req, arg, sizeof(req)))
1224 return -EFAULT;
1225
1226 hci_dev_lock_bh(hdev);
1227 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &req.bdaddr);
1228 if (conn)
1229 req.type = conn->auth_type;
1230 hci_dev_unlock_bh(hdev);
1231
1232 if (!conn)
1233 return -ENOENT;
1234
1235 return copy_to_user(arg, &req, sizeof(req)) ? -EFAULT : 0;
1236}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001237
1238int hci_set_auth_info(struct hci_dev *hdev, void __user *arg)
1239{
1240 struct hci_auth_info_req req;
1241 struct hci_conn *conn;
1242
1243 if (copy_from_user(&req, arg, sizeof(req)))
1244 return -EFAULT;
1245
1246 hci_dev_lock_bh(hdev);
1247 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &req.bdaddr);
1248 if (conn)
1249 conn->auth_type = req.type;
1250 hci_dev_unlock_bh(hdev);
1251
1252 if (!conn)
1253 return -ENOENT;
1254
1255 return copy_to_user(arg, &req, sizeof(req)) ? -EFAULT : 0;
1256}