blob: 733e9d29eda963c3890a00656ef0c9f8e01096d3 [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
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003 Copyright (c) 2000-2001, 2010-2011 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;
369 break;
370 case SCO_LINK:
Nick Pellybbcda3b2010-02-11 11:54:28 -0800371 if (!pkt_type)
372 pkt_type = SCO_ESCO_MASK;
Marcel Holtmanna8746412008-07-14 20:13:46 +0200373 case ESCO_LINK:
Nick Pellybbcda3b2010-02-11 11:54:28 -0800374 if (!pkt_type)
375 pkt_type = ALL_ESCO_MASK;
376 if (lmp_esco_capable(hdev)) {
377 /* HCI Setup Synchronous Connection Command uses
378 reverse logic on the EDR_ESCO_MASK bits */
379 conn->pkt_type = (pkt_type ^ EDR_ESCO_MASK) &
380 hdev->esco_type;
381 } else {
382 /* Legacy HCI Add Sco Connection Command uses a
383 shifted bitmask */
384 conn->pkt_type = (pkt_type << 5) & hdev->pkt_type &
385 SCO_PTYPE_MASK;
386 }
Marcel Holtmanna8746412008-07-14 20:13:46 +0200387 break;
388 }
389
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 skb_queue_head_init(&conn->data_q);
Marcel Holtmann04837f62006-07-03 10:02:33 +0200391
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800392 setup_timer(&conn->disc_timer, hci_conn_timeout, (unsigned long)conn);
393 setup_timer(&conn->idle_timer, hci_conn_idle, (unsigned long)conn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
395 atomic_set(&conn->refcnt, 0);
396
397 hci_dev_hold(hdev);
398
399 tasklet_disable(&hdev->tx_task);
400
401 hci_conn_hash_add(hdev, conn);
402 if (hdev->notify)
403 hdev->notify(hdev, HCI_NOTIFY_CONN_ADD);
404
Marcel Holtmann9eba32b2009-08-22 14:19:26 -0700405 atomic_set(&conn->devref, 0);
406
Marcel Holtmanna67e8992009-05-02 18:24:06 -0700407 hci_conn_init_sysfs(conn);
408
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 tasklet_enable(&hdev->tx_task);
410
411 return conn;
412}
413
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700414struct hci_conn *hci_le_conn_add(struct hci_dev *hdev, bdaddr_t *dst,
415 __u8 addr_type)
416{
417 struct hci_conn *conn = hci_conn_add(hdev, LE_LINK, 0, dst);
418 if (!conn)
419 return NULL;
420
421 conn->dst_type = addr_type;
422
423 return conn;
424}
425
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426int hci_conn_del(struct hci_conn *conn)
427{
428 struct hci_dev *hdev = conn->hdev;
429
430 BT_DBG("%s conn %p handle %d", hdev->name, conn, conn->handle);
431
Marcel Holtmann04837f62006-07-03 10:02:33 +0200432 del_timer(&conn->idle_timer);
433
434 del_timer(&conn->disc_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200436 if (conn->type == ACL_LINK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 struct hci_conn *sco = conn->link;
438 if (sco)
439 sco->link = NULL;
440
441 /* Unacked frames */
442 hdev->acl_cnt += conn->sent;
Ville Tervo6ed58ec2011-02-10 22:38:48 -0300443 } else if (conn->type == LE_LINK) {
444 if (hdev->le_pkts)
445 hdev->le_cnt += conn->sent;
446 else
447 hdev->acl_cnt += conn->sent;
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200448 } else {
449 struct hci_conn *acl = conn->link;
450 if (acl) {
451 acl->link = NULL;
452 hci_conn_put(acl);
453 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 }
455
456 tasklet_disable(&hdev->tx_task);
Marcel Holtmann7d0db0a2008-07-14 20:13:51 +0200457
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 hci_conn_hash_del(hdev, conn);
459 if (hdev->notify)
460 hdev->notify(hdev, HCI_NOTIFY_CONN_DEL);
Marcel Holtmann7d0db0a2008-07-14 20:13:51 +0200461
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 tasklet_enable(&hdev->tx_task);
Marcel Holtmann7d0db0a2008-07-14 20:13:51 +0200463
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 skb_queue_purge(&conn->data_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
Marcel Holtmann9eba32b2009-08-22 14:19:26 -0700466 hci_conn_put_device(conn);
Dave Young2ae9a6b2009-02-21 16:13:34 +0800467
Marcel Holtmann384943e2009-05-08 18:20:43 -0700468 hci_dev_put(hdev);
469
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700470 return 0;
471}
472
473struct hci_chan *hci_chan_add(struct hci_dev *hdev)
474{
475 struct hci_chan *chan;
476
477 BT_DBG("%s", hdev->name);
478
479 chan = kzalloc(sizeof(struct hci_chan), GFP_ATOMIC);
480 if (!chan)
481 return NULL;
482
483 atomic_set(&chan->refcnt, 0);
484
485 hci_dev_hold(hdev);
486
487 chan->hdev = hdev;
488
489 list_add(&chan->list, &hdev->chan_list.list);
490
491 return chan;
492}
493
494int hci_chan_del(struct hci_chan *chan)
495{
496 BT_DBG("%s chan %p", chan->hdev->name, chan);
497
498 list_del(&chan->list);
499
500 hci_conn_put(chan->conn);
501 hci_dev_put(chan->hdev);
502
503 kfree(chan);
Tomas Targownik1be668d2011-06-30 16:30:44 -0300504
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 return 0;
506}
507
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700508void hci_chan_put(struct hci_chan *chan)
509{
510 struct hci_cp_disconn_logical_link cp;
511
512 BT_DBG("chan %p refcnt %d", chan, atomic_read(&chan->refcnt));
513 if (!atomic_dec_and_test(&chan->refcnt))
514 return;
515
516 BT_DBG("chan->conn->state %d", chan->conn->state);
517 if (chan->conn->state == BT_CONNECTED) {
518 cp.log_handle = cpu_to_le16(chan->ll_handle);
519 hci_send_cmd(chan->conn->hdev, HCI_OP_DISCONN_LOGICAL_LINK,
520 sizeof(cp), &cp);
521 } else
522 hci_chan_del(chan);
523}
524EXPORT_SYMBOL(hci_chan_put);
525
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526struct hci_dev *hci_get_route(bdaddr_t *dst, bdaddr_t *src)
527{
528 int use_src = bacmp(src, BDADDR_ANY);
529 struct hci_dev *hdev = NULL;
530 struct list_head *p;
531
532 BT_DBG("%s -> %s", batostr(src), batostr(dst));
533
534 read_lock_bh(&hci_dev_list_lock);
535
536 list_for_each(p, &hci_dev_list) {
537 struct hci_dev *d = list_entry(p, struct hci_dev, list);
538
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700539 if (d->dev_type != HCI_BREDR)
540 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 if (!test_bit(HCI_UP, &d->flags) || test_bit(HCI_RAW, &d->flags))
542 continue;
543
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900544 /* Simple routing:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 * No source address - find interface with bdaddr != dst
546 * Source address - find interface with bdaddr == src
547 */
548
549 if (use_src) {
550 if (!bacmp(&d->bdaddr, src)) {
551 hdev = d; break;
552 }
553 } else {
554 if (bacmp(&d->bdaddr, dst)) {
555 hdev = d; break;
556 }
557 }
558 }
559
560 if (hdev)
561 hdev = hci_dev_hold(hdev);
562
563 read_unlock_bh(&hci_dev_list_lock);
564 return hdev;
565}
566EXPORT_SYMBOL(hci_get_route);
567
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700568struct hci_dev *hci_dev_get_type(u8 amp_type)
569{
570 struct hci_dev *hdev = NULL;
571 struct hci_dev *d;
572
573 BT_DBG("amp_type %d", amp_type);
574
575 read_lock_bh(&hci_dev_list_lock);
576
577 list_for_each_entry(d, &hci_dev_list, list) {
578 if ((d->amp_type == amp_type) && test_bit(HCI_UP, &d->flags)) {
579 hdev = d;
580 break;
581 }
582 }
583
584 if (hdev)
585 hdev = hci_dev_hold(hdev);
586
587 read_unlock_bh(&hci_dev_list_lock);
588 return hdev;
589}
590EXPORT_SYMBOL(hci_dev_get_type);
591
592struct hci_dev *hci_dev_get_amp(bdaddr_t *dst)
593{
594 struct hci_dev *d;
595 struct hci_dev *hdev = NULL;
596
597 BT_DBG("%s dst %s", hdev->name, batostr(dst));
598
599 read_lock_bh(&hci_dev_list_lock);
600
601 list_for_each_entry(d, &hci_dev_list, list) {
602 struct hci_conn *conn;
603 if (d->dev_type == HCI_BREDR)
604 continue;
605 conn = hci_conn_hash_lookup_ba(d, ACL_LINK, dst);
606 if (conn) {
607 hdev = d;
608 break;
609 }
610 }
611
612 if (hdev)
613 hdev = hci_dev_hold(hdev);
614
615 read_unlock_bh(&hci_dev_list_lock);
616 return hdev;
617}
618EXPORT_SYMBOL(hci_dev_get_amp);
619
Ville Tervofcd89c02011-02-10 22:38:47 -0300620/* Create SCO, ACL or LE connection.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 * Device _must_ be locked */
Nick Pellybbcda3b2010-02-11 11:54:28 -0800622struct hci_conn *hci_connect(struct hci_dev *hdev, int type,
623 __u16 pkt_type, bdaddr_t *dst,
624 __u8 sec_level, __u8 auth_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625{
626 struct hci_conn *acl;
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200627 struct hci_conn *sco;
Ville Tervofcd89c02011-02-10 22:38:47 -0300628 struct hci_conn *le;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
630 BT_DBG("%s dst %s", hdev->name, batostr(dst));
631
Ville Tervofcd89c02011-02-10 22:38:47 -0300632 if (type == LE_LINK) {
Andre Guedes5e89ece2011-05-31 14:20:56 -0300633 struct adv_entry *entry;
634
Ville Tervofcd89c02011-02-10 22:38:47 -0300635 le = hci_conn_hash_lookup_ba(hdev, LE_LINK, dst);
Brian Gix2e2f50d2011-09-13 12:36:04 -0700636 if (le) {
637 hci_conn_hold(le);
Brian Gixa68668b2011-08-11 15:49:36 -0700638 return le;
Brian Gix2e2f50d2011-09-13 12:36:04 -0700639 }
Andre Guedes5e89ece2011-05-31 14:20:56 -0300640
641 entry = hci_find_adv_entry(hdev, dst);
642 if (!entry)
Brian Gixa68668b2011-08-11 15:49:36 -0700643 le = hci_le_conn_add(hdev, dst, 0);
644 else
645 le = hci_le_conn_add(hdev, dst, entry->bdaddr_type);
Andre Guedes5e89ece2011-05-31 14:20:56 -0300646
Ville Tervofcd89c02011-02-10 22:38:47 -0300647 if (!le)
Ville Tervo30e76272011-02-22 16:10:53 -0300648 return ERR_PTR(-ENOMEM);
Andre Guedes92398c82011-05-31 14:20:55 -0300649
650 hci_le_connect(le);
Ville Tervofcd89c02011-02-10 22:38:47 -0300651
652 hci_conn_hold(le);
653
654 return le;
655 }
656
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200657 acl = hci_conn_hash_lookup_ba(hdev, ACL_LINK, dst);
658 if (!acl) {
Nick Pellybbcda3b2010-02-11 11:54:28 -0800659 acl = hci_conn_add(hdev, ACL_LINK, 0, dst);
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200660 if (!acl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 return NULL;
662 }
663
664 hci_conn_hold(acl);
665
Marcel Holtmann09ab6f42008-09-09 07:19:20 +0200666 if (acl->state == BT_OPEN || acl->state == BT_CLOSED) {
Johan Hedberg765c2a92011-01-19 12:06:52 +0530667 acl->sec_level = BT_SECURITY_LOW;
668 acl->pending_sec_level = sec_level;
Marcel Holtmann09ab6f42008-09-09 07:19:20 +0200669 acl->auth_type = auth_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 hci_acl_connect(acl);
Marcel Holtmann09ab6f42008-09-09 07:19:20 +0200671 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200673 if (type == ACL_LINK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 return acl;
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200675
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200676 sco = hci_conn_hash_lookup_ba(hdev, type, dst);
677 if (!sco) {
Nick Pellybbcda3b2010-02-11 11:54:28 -0800678 sco = hci_conn_add(hdev, type, pkt_type, dst);
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200679 if (!sco) {
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200680 hci_conn_put(acl);
681 return NULL;
682 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 }
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200684
685 acl->link = sco;
686 sco->link = acl;
687
688 hci_conn_hold(sco);
689
690 if (acl->state == BT_CONNECTED &&
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200691 (sco->state == BT_OPEN || sco->state == BT_CLOSED)) {
Nick Pellyc3902162009-11-13 14:16:32 -0800692 acl->power_save = 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700693 hci_conn_enter_active_mode(acl, 1);
Nick Pellyc3902162009-11-13 14:16:32 -0800694
Marcel Holtmanne73439d2010-07-26 10:06:00 -0400695 if (test_bit(HCI_CONN_MODE_CHANGE_PEND, &acl->pend)) {
696 /* defer SCO setup until mode change completed */
697 set_bit(HCI_CONN_SCO_SETUP_PEND, &acl->pend);
698 return sco;
699 }
700
701 hci_sco_setup(acl, 0x00);
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200702 }
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200703
704 return sco;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705}
706EXPORT_SYMBOL(hci_connect);
707
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700708void hci_disconnect(struct hci_conn *conn, __u8 reason)
709{
710 BT_DBG("conn %p", conn);
711
712 hci_proto_disconn_cfm(conn, reason);
713}
714EXPORT_SYMBOL(hci_disconnect);
715
716void hci_disconnect_amp(struct hci_conn *conn, __u8 reason)
717{
718 struct hci_dev *hdev = NULL;
719
720 BT_DBG("conn %p", conn);
721
722 read_lock_bh(&hci_dev_list_lock);
723
724 list_for_each_entry(hdev, &hci_dev_list, list) {
725 struct hci_conn *c;
726 if (hdev == conn->hdev)
727 continue;
728 if (hdev->amp_type == HCI_BREDR)
729 continue;
730 c = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &conn->dst);
731 if (c)
732 hci_disconnect(c, reason);
733 }
734
735 read_unlock_bh(&hci_dev_list_lock);
736}
737
Marcel Holtmanne7c29cb2008-09-09 07:19:20 +0200738/* Check link security requirement */
739int hci_conn_check_link_mode(struct hci_conn *conn)
740{
741 BT_DBG("conn %p", conn);
742
743 if (conn->ssp_mode > 0 && conn->hdev->ssp_mode > 0 &&
744 !(conn->link_mode & HCI_LM_ENCRYPT))
745 return 0;
746
747 return 1;
748}
749EXPORT_SYMBOL(hci_conn_check_link_mode);
750
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751/* Authenticate remote device */
Marcel Holtmann0684e5f2009-02-09 02:48:38 +0100752static int hci_conn_auth(struct hci_conn *conn, __u8 sec_level, __u8 auth_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753{
754 BT_DBG("conn %p", conn);
755
Johan Hedberg765c2a92011-01-19 12:06:52 +0530756 if (conn->pending_sec_level > sec_level)
757 sec_level = conn->pending_sec_level;
758
Marcel Holtmann96a31832009-02-12 16:23:03 +0100759 if (sec_level > conn->sec_level)
Johan Hedberg765c2a92011-01-19 12:06:52 +0530760 conn->pending_sec_level = sec_level;
Marcel Holtmann96a31832009-02-12 16:23:03 +0100761 else if (conn->link_mode & HCI_LM_AUTH)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 return 1;
763
Johan Hedberg65cf6862011-01-19 12:06:49 +0530764 /* Make sure we preserve an existing MITM requirement*/
765 auth_type |= (conn->auth_type & 0x01);
Marcel Holtmann96a31832009-02-12 16:23:03 +0100766 conn->auth_type = auth_type;
Brian Gixa68668b2011-08-11 15:49:36 -0700767 conn->auth_initiator = 1;
Marcel Holtmann96a31832009-02-12 16:23:03 +0100768
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 if (!test_and_set_bit(HCI_CONN_AUTH_PEND, &conn->pend)) {
770 struct hci_cp_auth_requested cp;
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -0700771 cp.handle = cpu_to_le16(conn->handle);
Marcel Holtmann40be4922008-07-14 20:13:50 +0200772 hci_send_cmd(conn->hdev, HCI_OP_AUTH_REQUESTED,
773 sizeof(cp), &cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 }
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100775
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 return 0;
777}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100779/* Enable security */
Marcel Holtmann0684e5f2009-02-09 02:48:38 +0100780int hci_conn_security(struct hci_conn *conn, __u8 sec_level, __u8 auth_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781{
Brian Gixa68668b2011-08-11 15:49:36 -0700782 BT_DBG("conn %p %d %d", conn, sec_level, auth_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100784 if (sec_level == BT_SECURITY_SDP)
785 return 1;
786
Marcel Holtmann3fdca1e2009-04-28 09:04:55 -0700787 if (sec_level == BT_SECURITY_LOW &&
788 (!conn->ssp_mode || !conn->hdev->ssp_mode))
789 return 1;
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100790
Brian Gix2e2f50d2011-09-13 12:36:04 -0700791 if (conn->type == LE_LINK) {
792 if (conn->pending_sec_level > sec_level)
793 sec_level = conn->pending_sec_level;
Waldemar Rymarkiewicz13d39312011-04-28 12:07:55 +0200794
Brian Gix2e2f50d2011-09-13 12:36:04 -0700795 if (sec_level > conn->sec_level)
796 conn->pending_sec_level = sec_level;
797 hci_proto_connect_cfm(conn, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700798 return 0;
Brian Gix2e2f50d2011-09-13 12:36:04 -0700799 } else if (conn->link_mode & HCI_LM_ENCRYPT) {
800 return hci_conn_auth(conn, sec_level, auth_type);
801 } else if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &conn->pend)) {
802 return 0;
803 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700804
805 if (hci_conn_auth(conn, sec_level, auth_type)) {
806 struct hci_cp_set_conn_encrypt cp;
807 cp.handle = cpu_to_le16(conn->handle);
808 cp.encrypt = 1;
809 hci_send_cmd(conn->hdev, HCI_OP_SET_CONN_ENCRYPT,
810 sizeof(cp), &cp);
811 }
812
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 return 0;
814}
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100815EXPORT_SYMBOL(hci_conn_security);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
817/* Change link key */
818int hci_conn_change_link_key(struct hci_conn *conn)
819{
820 BT_DBG("conn %p", conn);
821
822 if (!test_and_set_bit(HCI_CONN_AUTH_PEND, &conn->pend)) {
823 struct hci_cp_change_conn_link_key cp;
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -0700824 cp.handle = cpu_to_le16(conn->handle);
Marcel Holtmann40be4922008-07-14 20:13:50 +0200825 hci_send_cmd(conn->hdev, HCI_OP_CHANGE_CONN_LINK_KEY,
826 sizeof(cp), &cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 }
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100828
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 return 0;
830}
831EXPORT_SYMBOL(hci_conn_change_link_key);
832
833/* Switch role */
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100834int hci_conn_switch_role(struct hci_conn *conn, __u8 role)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835{
836 BT_DBG("conn %p", conn);
837
838 if (!role && conn->link_mode & HCI_LM_MASTER)
839 return 1;
840
841 if (!test_and_set_bit(HCI_CONN_RSWITCH_PEND, &conn->pend)) {
842 struct hci_cp_switch_role cp;
843 bacpy(&cp.bdaddr, &conn->dst);
844 cp.role = role;
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200845 hci_send_cmd(conn->hdev, HCI_OP_SWITCH_ROLE, 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_switch_role);
851
Marcel Holtmann04837f62006-07-03 10:02:33 +0200852/* Enter active mode */
Jaikumar Ganesh514abe62011-05-23 18:06:04 -0700853void hci_conn_enter_active_mode(struct hci_conn *conn, __u8 force_active)
Marcel Holtmann04837f62006-07-03 10:02:33 +0200854{
855 struct hci_dev *hdev = conn->hdev;
856
857 BT_DBG("conn %p mode %d", conn, conn->mode);
858
859 if (test_bit(HCI_RAW, &hdev->flags))
860 return;
861
Jaikumar Ganesh514abe62011-05-23 18:06:04 -0700862 if (conn->mode != HCI_CM_SNIFF)
863 goto timer;
864
865 if (!conn->power_save && !force_active)
Marcel Holtmann04837f62006-07-03 10:02:33 +0200866 goto timer;
867
868 if (!test_and_set_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->pend)) {
869 struct hci_cp_exit_sniff_mode cp;
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -0700870 cp.handle = cpu_to_le16(conn->handle);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200871 hci_send_cmd(hdev, HCI_OP_EXIT_SNIFF_MODE, sizeof(cp), &cp);
Marcel Holtmann04837f62006-07-03 10:02:33 +0200872 }
873
874timer:
875 if (hdev->idle_timeout > 0)
876 mod_timer(&conn->idle_timer,
877 jiffies + msecs_to_jiffies(hdev->idle_timeout));
878}
879
880/* Enter sniff mode */
881void hci_conn_enter_sniff_mode(struct hci_conn *conn)
882{
883 struct hci_dev *hdev = conn->hdev;
884
885 BT_DBG("conn %p mode %d", conn, conn->mode);
886
887 if (test_bit(HCI_RAW, &hdev->flags))
888 return;
889
890 if (!lmp_sniff_capable(hdev) || !lmp_sniff_capable(conn))
891 return;
892
893 if (conn->mode != HCI_CM_ACTIVE || !(conn->link_policy & HCI_LP_SNIFF))
894 return;
895
896 if (lmp_sniffsubr_capable(hdev) && lmp_sniffsubr_capable(conn)) {
897 struct hci_cp_sniff_subrate cp;
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -0700898 cp.handle = cpu_to_le16(conn->handle);
899 cp.max_latency = cpu_to_le16(0);
900 cp.min_remote_timeout = cpu_to_le16(0);
901 cp.min_local_timeout = cpu_to_le16(0);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200902 hci_send_cmd(hdev, HCI_OP_SNIFF_SUBRATE, sizeof(cp), &cp);
Marcel Holtmann04837f62006-07-03 10:02:33 +0200903 }
904
905 if (!test_and_set_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->pend)) {
906 struct hci_cp_sniff_mode cp;
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -0700907 cp.handle = cpu_to_le16(conn->handle);
908 cp.max_interval = cpu_to_le16(hdev->sniff_max_interval);
909 cp.min_interval = cpu_to_le16(hdev->sniff_min_interval);
910 cp.attempt = cpu_to_le16(4);
911 cp.timeout = cpu_to_le16(1);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200912 hci_send_cmd(hdev, HCI_OP_SNIFF_MODE, sizeof(cp), &cp);
Marcel Holtmann04837f62006-07-03 10:02:33 +0200913 }
914}
915
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700916struct hci_chan *hci_chan_accept(struct hci_conn *conn,
917 struct hci_ext_fs *tx_fs, struct hci_ext_fs *rx_fs)
918{
919 struct hci_chan *chan;
920 struct hci_cp_create_logical_link cp;
921
922 chan = hci_chan_add(conn->hdev);
923 if (!chan)
924 return NULL;
925
926 chan->state = BT_CONNECT;
927 chan->conn = conn;
928 chan->tx_fs = *tx_fs;
929 chan->rx_fs = *rx_fs;
930 cp.phy_handle = chan->conn->handle;
931 cp.tx_fs.id = chan->tx_fs.id;
932 cp.tx_fs.type = chan->tx_fs.type;
933 cp.tx_fs.max_sdu = cpu_to_le16(chan->tx_fs.max_sdu);
934 cp.tx_fs.sdu_arr_time = cpu_to_le32(chan->tx_fs.sdu_arr_time);
935 cp.tx_fs.acc_latency = cpu_to_le32(chan->tx_fs.acc_latency);
936 cp.tx_fs.flush_to = cpu_to_le32(chan->tx_fs.flush_to);
937 cp.rx_fs.id = chan->rx_fs.id;
938 cp.rx_fs.type = chan->rx_fs.type;
939 cp.rx_fs.max_sdu = cpu_to_le16(chan->rx_fs.max_sdu);
940 cp.rx_fs.sdu_arr_time = cpu_to_le32(chan->rx_fs.sdu_arr_time);
941 cp.rx_fs.acc_latency = cpu_to_le32(chan->rx_fs.acc_latency);
942 cp.rx_fs.flush_to = cpu_to_le32(chan->rx_fs.flush_to);
943 hci_conn_hold(chan->conn);
944 hci_send_cmd(conn->hdev, HCI_OP_ACCEPT_LOGICAL_LINK, sizeof(cp), &cp);
945 return chan;
946}
947EXPORT_SYMBOL(hci_chan_accept);
948
949struct hci_chan *hci_chan_create(struct hci_conn *conn,
950 struct hci_ext_fs *tx_fs, struct hci_ext_fs *rx_fs)
951{
952 struct hci_chan *chan;
953 struct hci_cp_create_logical_link cp;
954
955 chan = hci_chan_add(conn->hdev);
956 if (!chan)
957 return NULL;
958
959 chan->state = BT_CONNECT;
960 chan->conn = conn;
961 chan->tx_fs = *tx_fs;
962 chan->rx_fs = *rx_fs;
963 cp.phy_handle = chan->conn->handle;
964 cp.tx_fs.id = chan->tx_fs.id;
965 cp.tx_fs.type = chan->tx_fs.type;
966 cp.tx_fs.max_sdu = cpu_to_le16(chan->tx_fs.max_sdu);
967 cp.tx_fs.sdu_arr_time = cpu_to_le32(chan->tx_fs.sdu_arr_time);
968 cp.tx_fs.acc_latency = cpu_to_le32(chan->tx_fs.acc_latency);
969 cp.tx_fs.flush_to = cpu_to_le32(chan->tx_fs.flush_to);
970 cp.rx_fs.id = chan->rx_fs.id;
971 cp.rx_fs.type = chan->rx_fs.type;
972 cp.rx_fs.max_sdu = cpu_to_le16(chan->rx_fs.max_sdu);
973 cp.rx_fs.sdu_arr_time = cpu_to_le32(chan->rx_fs.sdu_arr_time);
974 cp.rx_fs.acc_latency = cpu_to_le32(chan->rx_fs.acc_latency);
975 cp.rx_fs.flush_to = cpu_to_le32(chan->rx_fs.flush_to);
976 hci_conn_hold(chan->conn);
977 hci_send_cmd(conn->hdev, HCI_OP_CREATE_LOGICAL_LINK, sizeof(cp), &cp);
978 return chan;
979}
980EXPORT_SYMBOL(hci_chan_create);
981
982void hci_chan_modify(struct hci_chan *chan,
983 struct hci_ext_fs *tx_fs, struct hci_ext_fs *rx_fs)
984{
985 struct hci_cp_flow_spec_modify cp;
986
987 chan->tx_fs = *tx_fs;
988 chan->rx_fs = *rx_fs;
989 cp.log_handle = cpu_to_le16(chan->ll_handle);
990 cp.tx_fs.id = tx_fs->id;
991 cp.tx_fs.type = tx_fs->type;
992 cp.tx_fs.max_sdu = cpu_to_le16(tx_fs->max_sdu);
993 cp.tx_fs.sdu_arr_time = cpu_to_le32(tx_fs->sdu_arr_time);
994 cp.tx_fs.acc_latency = cpu_to_le32(tx_fs->acc_latency);
995 cp.tx_fs.flush_to = cpu_to_le32(tx_fs->flush_to);
996 cp.rx_fs.id = rx_fs->id;
997 cp.rx_fs.type = rx_fs->type;
998 cp.rx_fs.max_sdu = cpu_to_le16(rx_fs->max_sdu);
999 cp.rx_fs.sdu_arr_time = cpu_to_le32(rx_fs->sdu_arr_time);
1000 cp.rx_fs.acc_latency = cpu_to_le32(rx_fs->acc_latency);
1001 cp.rx_fs.flush_to = cpu_to_le32(rx_fs->flush_to);
1002 hci_conn_hold(chan->conn);
1003 hci_send_cmd(chan->conn->hdev, HCI_OP_FLOW_SPEC_MODIFY, sizeof(cp),
1004 &cp);
1005}
1006EXPORT_SYMBOL(hci_chan_modify);
1007
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008/* Drop all connection on the device */
1009void hci_conn_hash_flush(struct hci_dev *hdev)
1010{
1011 struct hci_conn_hash *h = &hdev->conn_hash;
1012 struct list_head *p;
1013
1014 BT_DBG("hdev %s", hdev->name);
1015
1016 p = h->list.next;
1017 while (p != &h->list) {
1018 struct hci_conn *c;
1019
1020 c = list_entry(p, struct hci_conn, list);
1021 p = p->next;
1022
1023 c->state = BT_CLOSED;
1024
Marcel Holtmann2950f212009-02-12 14:02:50 +01001025 hci_proto_disconn_cfm(c, 0x16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 hci_conn_del(c);
1027 }
1028}
1029
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001030/* Check pending connect attempts */
1031void hci_conn_check_pending(struct hci_dev *hdev)
1032{
1033 struct hci_conn *conn;
1034
1035 BT_DBG("hdev %s", hdev->name);
1036
1037 hci_dev_lock(hdev);
1038
1039 conn = hci_conn_hash_lookup_state(hdev, ACL_LINK, BT_CONNECT2);
1040 if (conn)
1041 hci_acl_connect(conn);
1042
1043 hci_dev_unlock(hdev);
1044}
1045
Marcel Holtmann9eba32b2009-08-22 14:19:26 -07001046void hci_conn_hold_device(struct hci_conn *conn)
1047{
1048 atomic_inc(&conn->devref);
1049}
1050EXPORT_SYMBOL(hci_conn_hold_device);
1051
1052void hci_conn_put_device(struct hci_conn *conn)
1053{
1054 if (atomic_dec_and_test(&conn->devref))
1055 hci_conn_del_sysfs(conn);
1056}
1057EXPORT_SYMBOL(hci_conn_put_device);
1058
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059int hci_get_conn_list(void __user *arg)
1060{
1061 struct hci_conn_list_req req, *cl;
1062 struct hci_conn_info *ci;
1063 struct hci_dev *hdev;
1064 struct list_head *p;
1065 int n = 0, size, err;
1066
1067 if (copy_from_user(&req, arg, sizeof(req)))
1068 return -EFAULT;
1069
1070 if (!req.conn_num || req.conn_num > (PAGE_SIZE * 2) / sizeof(*ci))
1071 return -EINVAL;
1072
1073 size = sizeof(req) + req.conn_num * sizeof(*ci);
1074
Andrei Emeltchenko70f230202010-12-01 16:58:25 +02001075 cl = kmalloc(size, GFP_KERNEL);
1076 if (!cl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 return -ENOMEM;
1078
Andrei Emeltchenko70f230202010-12-01 16:58:25 +02001079 hdev = hci_dev_get(req.dev_id);
1080 if (!hdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 kfree(cl);
1082 return -ENODEV;
1083 }
1084
1085 ci = cl->conn_info;
1086
1087 hci_dev_lock_bh(hdev);
1088 list_for_each(p, &hdev->conn_hash.list) {
1089 register struct hci_conn *c;
1090 c = list_entry(p, struct hci_conn, list);
1091
1092 bacpy(&(ci + n)->bdaddr, &c->dst);
1093 (ci + n)->handle = c->handle;
1094 (ci + n)->type = c->type;
1095 (ci + n)->out = c->out;
1096 (ci + n)->state = c->state;
1097 (ci + n)->link_mode = c->link_mode;
Nick Pellyc1728492009-12-09 00:15:41 -08001098 if (c->type == SCO_LINK) {
1099 (ci + n)->mtu = hdev->sco_mtu;
1100 (ci + n)->cnt = hdev->sco_cnt;
1101 (ci + n)->pkts = hdev->sco_pkts;
1102 } else {
1103 (ci + n)->mtu = hdev->acl_mtu;
1104 (ci + n)->cnt = hdev->acl_cnt;
1105 (ci + n)->pkts = hdev->acl_pkts;
1106 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 if (++n >= req.conn_num)
1108 break;
1109 }
1110 hci_dev_unlock_bh(hdev);
1111
1112 cl->dev_id = hdev->id;
1113 cl->conn_num = n;
1114 size = sizeof(req) + n * sizeof(*ci);
1115
1116 hci_dev_put(hdev);
1117
1118 err = copy_to_user(arg, cl, size);
1119 kfree(cl);
1120
1121 return err ? -EFAULT : 0;
1122}
1123
1124int hci_get_conn_info(struct hci_dev *hdev, void __user *arg)
1125{
1126 struct hci_conn_info_req req;
1127 struct hci_conn_info ci;
1128 struct hci_conn *conn;
1129 char __user *ptr = arg + sizeof(req);
1130
1131 if (copy_from_user(&req, arg, sizeof(req)))
1132 return -EFAULT;
1133
1134 hci_dev_lock_bh(hdev);
1135 conn = hci_conn_hash_lookup_ba(hdev, req.type, &req.bdaddr);
1136 if (conn) {
1137 bacpy(&ci.bdaddr, &conn->dst);
1138 ci.handle = conn->handle;
1139 ci.type = conn->type;
1140 ci.out = conn->out;
1141 ci.state = conn->state;
1142 ci.link_mode = conn->link_mode;
Nick Pellyc1728492009-12-09 00:15:41 -08001143 if (req.type == SCO_LINK) {
1144 ci.mtu = hdev->sco_mtu;
1145 ci.cnt = hdev->sco_cnt;
1146 ci.pkts = hdev->sco_pkts;
1147 } else {
1148 ci.mtu = hdev->acl_mtu;
1149 ci.cnt = hdev->acl_cnt;
1150 ci.pkts = hdev->acl_pkts;
1151 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001152 ci.pending_sec_level = conn->pending_sec_level;
1153 ci.ssp_mode = conn->ssp_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 }
1155 hci_dev_unlock_bh(hdev);
1156
1157 if (!conn)
1158 return -ENOENT;
1159
1160 return copy_to_user(ptr, &ci, sizeof(ci)) ? -EFAULT : 0;
1161}
Marcel Holtmann40be4922008-07-14 20:13:50 +02001162
1163int hci_get_auth_info(struct hci_dev *hdev, void __user *arg)
1164{
1165 struct hci_auth_info_req req;
1166 struct hci_conn *conn;
1167
1168 if (copy_from_user(&req, arg, sizeof(req)))
1169 return -EFAULT;
1170
1171 hci_dev_lock_bh(hdev);
1172 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &req.bdaddr);
1173 if (conn)
1174 req.type = conn->auth_type;
1175 hci_dev_unlock_bh(hdev);
1176
1177 if (!conn)
1178 return -ENOENT;
1179
1180 return copy_to_user(arg, &req, sizeof(req)) ? -EFAULT : 0;
1181}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001182
1183int hci_set_auth_info(struct hci_dev *hdev, void __user *arg)
1184{
1185 struct hci_auth_info_req req;
1186 struct hci_conn *conn;
1187
1188 if (copy_from_user(&req, arg, sizeof(req)))
1189 return -EFAULT;
1190
1191 hci_dev_lock_bh(hdev);
1192 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &req.bdaddr);
1193 if (conn)
1194 conn->auth_type = req.type;
1195 hci_dev_unlock_bh(hdev);
1196
1197 if (!conn)
1198 return -ENOENT;
1199
1200 return copy_to_user(arg, &req, sizeof(req)) ? -EFAULT : 0;
1201}