blob: 0ecb942c6f7e9ed6edfa76e66bbbb512ff9cdead [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
53 conn->state = BT_CONNECT;
54 conn->out = 1;
Vinicius Costa Gomesb92a6222011-02-10 22:38:52 -030055 conn->link_mode |= HCI_LM_MASTER;
Vinicius Costa Gomes403d2c82011-06-09 18:50:50 -030056 conn->sec_level = BT_SECURITY_LOW;
Ville Tervofcd89c02011-02-10 22:38:47 -030057
58 memset(&cp, 0, sizeof(cp));
59 cp.scan_interval = cpu_to_le16(0x0004);
60 cp.scan_window = cpu_to_le16(0x0004);
61 bacpy(&cp.peer_addr, &conn->dst);
Andre Guedesc7f0d992011-05-31 14:20:57 -030062 cp.peer_addr_type = conn->dst_type;
Ville Tervofcd89c02011-02-10 22:38:47 -030063 cp.conn_interval_min = cpu_to_le16(0x0008);
64 cp.conn_interval_max = cpu_to_le16(0x0100);
65 cp.supervision_timeout = cpu_to_le16(0x0064);
66 cp.min_ce_len = cpu_to_le16(0x0001);
67 cp.max_ce_len = cpu_to_le16(0x0001);
68
69 hci_send_cmd(hdev, HCI_OP_LE_CREATE_CONN, sizeof(cp), &cp);
70}
71
72static void hci_le_connect_cancel(struct hci_conn *conn)
73{
74 hci_send_cmd(conn->hdev, HCI_OP_LE_CREATE_CONN_CANCEL, 0, NULL);
75}
76
Marcel Holtmann4c67bc72006-10-15 17:30:56 +020077void hci_acl_connect(struct hci_conn *conn)
Linus Torvalds1da177e2005-04-16 15:20:36 -070078{
79 struct hci_dev *hdev = conn->hdev;
80 struct inquiry_entry *ie;
81 struct hci_cp_create_conn cp;
82
83 BT_DBG("%p", conn);
84
85 conn->state = BT_CONNECT;
Marcel Holtmanna8746412008-07-14 20:13:46 +020086 conn->out = 1;
87
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 conn->link_mode = HCI_LM_MASTER;
89
Marcel Holtmann4c67bc72006-10-15 17:30:56 +020090 conn->attempt++;
91
Marcel Holtmanne4e8e372008-07-14 20:13:47 +020092 conn->link_policy = hdev->link_policy;
93
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 memset(&cp, 0, sizeof(cp));
95 bacpy(&cp.bdaddr, &conn->dst);
96 cp.pscan_rep_mode = 0x02;
97
Andrei Emeltchenko70f230202010-12-01 16:58:25 +020098 ie = hci_inquiry_cache_lookup(hdev, &conn->dst);
99 if (ie) {
Marcel Holtmann41a96212008-07-14 20:13:48 +0200100 if (inquiry_entry_age(ie) <= INQUIRY_ENTRY_AGE_MAX) {
101 cp.pscan_rep_mode = ie->data.pscan_rep_mode;
102 cp.pscan_mode = ie->data.pscan_mode;
103 cp.clock_offset = ie->data.clock_offset |
104 cpu_to_le16(0x8000);
105 }
106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 memcpy(conn->dev_class, ie->data.dev_class, 3);
Marcel Holtmann41a96212008-07-14 20:13:48 +0200108 conn->ssp_mode = ie->data.ssp_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 }
110
Marcel Holtmanna8746412008-07-14 20:13:46 +0200111 cp.pkt_type = cpu_to_le16(conn->pkt_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 if (lmp_rswitch_capable(hdev) && !(hdev->link_mode & HCI_LM_MASTER))
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200113 cp.role_switch = 0x01;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 else
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200115 cp.role_switch = 0x00;
Marcel Holtmann4c67bc72006-10-15 17:30:56 +0200116
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200117 hci_send_cmd(hdev, HCI_OP_CREATE_CONN, sizeof(cp), &cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118}
119
Marcel Holtmann6ac59342006-09-26 09:43:48 +0200120static void hci_acl_connect_cancel(struct hci_conn *conn)
121{
122 struct hci_cp_create_conn_cancel cp;
123
124 BT_DBG("%p", conn);
125
126 if (conn->hdev->hci_ver < 2)
127 return;
128
129 bacpy(&cp.bdaddr, &conn->dst);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200130 hci_send_cmd(conn->hdev, HCI_OP_CREATE_CONN_CANCEL, sizeof(cp), &cp);
Marcel Holtmann6ac59342006-09-26 09:43:48 +0200131}
132
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133void hci_acl_disconn(struct hci_conn *conn, __u8 reason)
134{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 BT_DBG("%p", conn);
136
137 conn->state = BT_DISCONN;
138
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700139 if (conn->hdev->dev_type == HCI_BREDR) {
140 struct hci_cp_disconnect cp;
141 cp.handle = cpu_to_le16(conn->handle);
142 cp.reason = reason;
143 hci_send_cmd(conn->hdev, HCI_OP_DISCONNECT, sizeof(cp), &cp);
144 } else {
145 struct hci_cp_disconn_phys_link cp;
146 cp.phy_handle = (u8) conn->handle;
147 cp.reason = reason;
148 hci_send_cmd(conn->hdev, HCI_OP_DISCONN_PHYS_LINK,
149 sizeof(cp), &cp);
150 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151}
152
153void hci_add_sco(struct hci_conn *conn, __u16 handle)
154{
155 struct hci_dev *hdev = conn->hdev;
156 struct hci_cp_add_sco cp;
157
158 BT_DBG("%p", conn);
159
160 conn->state = BT_CONNECT;
161 conn->out = 1;
162
Marcel Holtmannefc76882009-02-06 09:13:37 +0100163 conn->attempt++;
164
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -0700165 cp.handle = cpu_to_le16(handle);
Marcel Holtmanna8746412008-07-14 20:13:46 +0200166 cp.pkt_type = cpu_to_le16(conn->pkt_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200168 hci_send_cmd(hdev, HCI_OP_ADD_SCO, sizeof(cp), &cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169}
170
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200171void hci_setup_sync(struct hci_conn *conn, __u16 handle)
172{
173 struct hci_dev *hdev = conn->hdev;
174 struct hci_cp_setup_sync_conn cp;
175
176 BT_DBG("%p", conn);
177
178 conn->state = BT_CONNECT;
179 conn->out = 1;
180
Marcel Holtmannefc76882009-02-06 09:13:37 +0100181 conn->attempt++;
182
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200183 cp.handle = cpu_to_le16(handle);
Marcel Holtmanna8746412008-07-14 20:13:46 +0200184 cp.pkt_type = cpu_to_le16(conn->pkt_type);
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200185
186 cp.tx_bandwidth = cpu_to_le32(0x00001f40);
187 cp.rx_bandwidth = cpu_to_le32(0x00001f40);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700188 cp.max_latency = cpu_to_le16(0x000A);
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200189 cp.voice_setting = cpu_to_le16(hdev->voice_setting);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700190 cp.retrans_effort = 0x01;
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200191
192 hci_send_cmd(hdev, HCI_OP_SETUP_SYNC_CONN, sizeof(cp), &cp);
193}
194
Claudio Takahasi2ce603e2011-02-16 20:44:53 -0200195void hci_le_conn_update(struct hci_conn *conn, u16 min, u16 max,
196 u16 latency, u16 to_multiplier)
197{
198 struct hci_cp_le_conn_update cp;
199 struct hci_dev *hdev = conn->hdev;
200
201 memset(&cp, 0, sizeof(cp));
202
203 cp.handle = cpu_to_le16(conn->handle);
204 cp.conn_interval_min = cpu_to_le16(min);
205 cp.conn_interval_max = cpu_to_le16(max);
206 cp.conn_latency = cpu_to_le16(latency);
207 cp.supervision_timeout = cpu_to_le16(to_multiplier);
208 cp.min_ce_len = cpu_to_le16(0x0001);
209 cp.max_ce_len = cpu_to_le16(0x0001);
210
211 hci_send_cmd(hdev, HCI_OP_LE_CONN_UPDATE, sizeof(cp), &cp);
212}
213EXPORT_SYMBOL(hci_le_conn_update);
214
Vinicius Costa Gomes735038c2011-06-09 18:50:47 -0300215void hci_le_start_enc(struct hci_conn *conn, __le16 ediv, __u8 rand[8],
216 __u8 ltk[16])
217{
218 struct hci_dev *hdev = conn->hdev;
219 struct hci_cp_le_start_enc cp;
220
221 BT_DBG("%p", conn);
222
223 memset(&cp, 0, sizeof(cp));
224
225 cp.handle = cpu_to_le16(conn->handle);
226 memcpy(cp.ltk, ltk, sizeof(cp.ltk));
227 cp.ediv = ediv;
228 memcpy(cp.rand, rand, sizeof(rand));
229
230 hci_send_cmd(hdev, HCI_OP_LE_START_ENC, sizeof(cp), &cp);
231}
232EXPORT_SYMBOL(hci_le_start_enc);
233
234void hci_le_ltk_reply(struct hci_conn *conn, u8 ltk[16])
235{
236 struct hci_dev *hdev = conn->hdev;
237 struct hci_cp_le_ltk_reply cp;
238
239 BT_DBG("%p", conn);
240
241 memset(&cp, 0, sizeof(cp));
242
243 cp.handle = cpu_to_le16(conn->handle);
244 memcpy(cp.ltk, ltk, sizeof(ltk));
245
246 hci_send_cmd(hdev, HCI_OP_LE_LTK_REPLY, sizeof(cp), &cp);
247}
248EXPORT_SYMBOL(hci_le_ltk_reply);
249
250void hci_le_ltk_neg_reply(struct hci_conn *conn)
251{
252 struct hci_dev *hdev = conn->hdev;
253 struct hci_cp_le_ltk_neg_reply cp;
254
255 BT_DBG("%p", conn);
256
257 memset(&cp, 0, sizeof(cp));
258
259 cp.handle = cpu_to_le16(conn->handle);
260
261 hci_send_cmd(hdev, HCI_OP_LE_LTK_NEG_REPLY, sizeof(cp), &cp);
262}
263
Marcel Holtmanne73439d2010-07-26 10:06:00 -0400264/* Device _must_ be locked */
265void hci_sco_setup(struct hci_conn *conn, __u8 status)
266{
267 struct hci_conn *sco = conn->link;
268
269 BT_DBG("%p", conn);
270
271 if (!sco)
272 return;
273
274 if (!status) {
275 if (lmp_esco_capable(conn->hdev))
276 hci_setup_sync(sco, conn->handle);
277 else
278 hci_add_sco(sco, conn->handle);
279 } else {
280 hci_proto_connect_cfm(sco, status);
281 hci_conn_del(sco);
282 }
283}
284
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285static void hci_conn_timeout(unsigned long arg)
286{
Marcel Holtmann04837f62006-07-03 10:02:33 +0200287 struct hci_conn *conn = (void *) arg;
288 struct hci_dev *hdev = conn->hdev;
Marcel Holtmann2950f212009-02-12 14:02:50 +0100289 __u8 reason;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
291 BT_DBG("conn %p state %d", conn, conn->state);
292
293 if (atomic_read(&conn->refcnt))
294 return;
295
296 hci_dev_lock(hdev);
Marcel Holtmann6ac59342006-09-26 09:43:48 +0200297
298 switch (conn->state) {
299 case BT_CONNECT:
Marcel Holtmann769be972008-07-14 20:13:49 +0200300 case BT_CONNECT2:
Ville Tervofcd89c02011-02-10 22:38:47 -0300301 if (conn->out) {
302 if (conn->type == ACL_LINK)
303 hci_acl_connect_cancel(conn);
304 else if (conn->type == LE_LINK)
305 hci_le_connect_cancel(conn);
306 }
Marcel Holtmann6ac59342006-09-26 09:43:48 +0200307 break;
Marcel Holtmann769be972008-07-14 20:13:49 +0200308 case BT_CONFIG:
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900309 case BT_CONNECTED:
Marcel Holtmann2950f212009-02-12 14:02:50 +0100310 reason = hci_proto_disconn_ind(conn);
311 hci_acl_disconn(conn, reason);
Marcel Holtmann6ac59342006-09-26 09:43:48 +0200312 break;
313 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 conn->state = BT_CLOSED;
Marcel Holtmann6ac59342006-09-26 09:43:48 +0200315 break;
316 }
317
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319}
320
Marcel Holtmann04837f62006-07-03 10:02:33 +0200321static void hci_conn_idle(unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322{
Marcel Holtmann04837f62006-07-03 10:02:33 +0200323 struct hci_conn *conn = (void *) arg;
324
325 BT_DBG("conn %p mode %d", conn, conn->mode);
326
327 hci_conn_enter_sniff_mode(conn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328}
329
Nick Pellybbcda3b2010-02-11 11:54:28 -0800330struct hci_conn *hci_conn_add(struct hci_dev *hdev, int type,
331 __u16 pkt_type, bdaddr_t *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332{
333 struct hci_conn *conn;
334
335 BT_DBG("%s dst %s", hdev->name, batostr(dst));
336
Marcel Holtmann04837f62006-07-03 10:02:33 +0200337 conn = kzalloc(sizeof(struct hci_conn), GFP_ATOMIC);
338 if (!conn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
341 bacpy(&conn->dst, dst);
Marcel Holtmanna8746412008-07-14 20:13:46 +0200342 conn->hdev = hdev;
343 conn->type = type;
344 conn->mode = HCI_CM_ACTIVE;
345 conn->state = BT_OPEN;
Andrei Emeltchenko93f19c92009-09-03 12:34:19 +0300346 conn->auth_type = HCI_AT_GENERAL_BONDING;
Johan Hedberg17fa4b92011-01-25 13:28:33 +0200347 conn->io_capability = hdev->io_capability;
Johan Hedberga9583552011-02-19 12:06:01 -0300348 conn->remote_auth = 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
Marcel Holtmann04837f62006-07-03 10:02:33 +0200350 conn->power_save = 1;
Marcel Holtmann052b30b2009-04-26 20:01:22 +0200351 conn->disc_timeout = HCI_DISCONN_TIMEOUT;
Marcel Holtmann04837f62006-07-03 10:02:33 +0200352
Marcel Holtmanna8746412008-07-14 20:13:46 +0200353 switch (type) {
354 case ACL_LINK:
355 conn->pkt_type = hdev->pkt_type & ACL_PTYPE_MASK;
356 break;
357 case SCO_LINK:
Nick Pellybbcda3b2010-02-11 11:54:28 -0800358 if (!pkt_type)
359 pkt_type = SCO_ESCO_MASK;
Marcel Holtmanna8746412008-07-14 20:13:46 +0200360 case ESCO_LINK:
Nick Pellybbcda3b2010-02-11 11:54:28 -0800361 if (!pkt_type)
362 pkt_type = ALL_ESCO_MASK;
363 if (lmp_esco_capable(hdev)) {
364 /* HCI Setup Synchronous Connection Command uses
365 reverse logic on the EDR_ESCO_MASK bits */
366 conn->pkt_type = (pkt_type ^ EDR_ESCO_MASK) &
367 hdev->esco_type;
368 } else {
369 /* Legacy HCI Add Sco Connection Command uses a
370 shifted bitmask */
371 conn->pkt_type = (pkt_type << 5) & hdev->pkt_type &
372 SCO_PTYPE_MASK;
373 }
Marcel Holtmanna8746412008-07-14 20:13:46 +0200374 break;
375 }
376
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 skb_queue_head_init(&conn->data_q);
Marcel Holtmann04837f62006-07-03 10:02:33 +0200378
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800379 setup_timer(&conn->disc_timer, hci_conn_timeout, (unsigned long)conn);
380 setup_timer(&conn->idle_timer, hci_conn_idle, (unsigned long)conn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
382 atomic_set(&conn->refcnt, 0);
383
384 hci_dev_hold(hdev);
385
386 tasklet_disable(&hdev->tx_task);
387
388 hci_conn_hash_add(hdev, conn);
389 if (hdev->notify)
390 hdev->notify(hdev, HCI_NOTIFY_CONN_ADD);
391
Marcel Holtmann9eba32b2009-08-22 14:19:26 -0700392 atomic_set(&conn->devref, 0);
393
Marcel Holtmanna67e8992009-05-02 18:24:06 -0700394 hci_conn_init_sysfs(conn);
395
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 tasklet_enable(&hdev->tx_task);
397
398 return conn;
399}
400
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700401struct hci_conn *hci_le_conn_add(struct hci_dev *hdev, bdaddr_t *dst,
402 __u8 addr_type)
403{
404 struct hci_conn *conn = hci_conn_add(hdev, LE_LINK, 0, dst);
405 if (!conn)
406 return NULL;
407
408 conn->dst_type = addr_type;
409
410 return conn;
411}
412
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413int hci_conn_del(struct hci_conn *conn)
414{
415 struct hci_dev *hdev = conn->hdev;
416
417 BT_DBG("%s conn %p handle %d", hdev->name, conn, conn->handle);
418
Marcel Holtmann04837f62006-07-03 10:02:33 +0200419 del_timer(&conn->idle_timer);
420
421 del_timer(&conn->disc_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200423 if (conn->type == ACL_LINK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 struct hci_conn *sco = conn->link;
425 if (sco)
426 sco->link = NULL;
427
428 /* Unacked frames */
429 hdev->acl_cnt += conn->sent;
Ville Tervo6ed58ec2011-02-10 22:38:48 -0300430 } else if (conn->type == LE_LINK) {
431 if (hdev->le_pkts)
432 hdev->le_cnt += conn->sent;
433 else
434 hdev->acl_cnt += conn->sent;
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200435 } else {
436 struct hci_conn *acl = conn->link;
437 if (acl) {
438 acl->link = NULL;
439 hci_conn_put(acl);
440 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 }
442
443 tasklet_disable(&hdev->tx_task);
Marcel Holtmann7d0db0a2008-07-14 20:13:51 +0200444
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 hci_conn_hash_del(hdev, conn);
446 if (hdev->notify)
447 hdev->notify(hdev, HCI_NOTIFY_CONN_DEL);
Marcel Holtmann7d0db0a2008-07-14 20:13:51 +0200448
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 tasklet_enable(&hdev->tx_task);
Marcel Holtmann7d0db0a2008-07-14 20:13:51 +0200450
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 skb_queue_purge(&conn->data_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
Marcel Holtmann9eba32b2009-08-22 14:19:26 -0700453 hci_conn_put_device(conn);
Dave Young2ae9a6b2009-02-21 16:13:34 +0800454
Marcel Holtmann384943e2009-05-08 18:20:43 -0700455 hci_dev_put(hdev);
456
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700457 return 0;
458}
459
460struct hci_chan *hci_chan_add(struct hci_dev *hdev)
461{
462 struct hci_chan *chan;
463
464 BT_DBG("%s", hdev->name);
465
466 chan = kzalloc(sizeof(struct hci_chan), GFP_ATOMIC);
467 if (!chan)
468 return NULL;
469
470 atomic_set(&chan->refcnt, 0);
471
472 hci_dev_hold(hdev);
473
474 chan->hdev = hdev;
475
476 list_add(&chan->list, &hdev->chan_list.list);
477
478 return chan;
479}
480
481int hci_chan_del(struct hci_chan *chan)
482{
483 BT_DBG("%s chan %p", chan->hdev->name, chan);
484
485 list_del(&chan->list);
486
487 hci_conn_put(chan->conn);
488 hci_dev_put(chan->hdev);
489
490 kfree(chan);
Tomas Targownik1be668d2011-06-30 16:30:44 -0300491
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 return 0;
493}
494
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700495void hci_chan_put(struct hci_chan *chan)
496{
497 struct hci_cp_disconn_logical_link cp;
498
499 BT_DBG("chan %p refcnt %d", chan, atomic_read(&chan->refcnt));
500 if (!atomic_dec_and_test(&chan->refcnt))
501 return;
502
503 BT_DBG("chan->conn->state %d", chan->conn->state);
504 if (chan->conn->state == BT_CONNECTED) {
505 cp.log_handle = cpu_to_le16(chan->ll_handle);
506 hci_send_cmd(chan->conn->hdev, HCI_OP_DISCONN_LOGICAL_LINK,
507 sizeof(cp), &cp);
508 } else
509 hci_chan_del(chan);
510}
511EXPORT_SYMBOL(hci_chan_put);
512
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513struct hci_dev *hci_get_route(bdaddr_t *dst, bdaddr_t *src)
514{
515 int use_src = bacmp(src, BDADDR_ANY);
516 struct hci_dev *hdev = NULL;
517 struct list_head *p;
518
519 BT_DBG("%s -> %s", batostr(src), batostr(dst));
520
521 read_lock_bh(&hci_dev_list_lock);
522
523 list_for_each(p, &hci_dev_list) {
524 struct hci_dev *d = list_entry(p, struct hci_dev, list);
525
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700526 if (d->dev_type != HCI_BREDR)
527 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 if (!test_bit(HCI_UP, &d->flags) || test_bit(HCI_RAW, &d->flags))
529 continue;
530
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900531 /* Simple routing:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 * No source address - find interface with bdaddr != dst
533 * Source address - find interface with bdaddr == src
534 */
535
536 if (use_src) {
537 if (!bacmp(&d->bdaddr, src)) {
538 hdev = d; break;
539 }
540 } else {
541 if (bacmp(&d->bdaddr, dst)) {
542 hdev = d; break;
543 }
544 }
545 }
546
547 if (hdev)
548 hdev = hci_dev_hold(hdev);
549
550 read_unlock_bh(&hci_dev_list_lock);
551 return hdev;
552}
553EXPORT_SYMBOL(hci_get_route);
554
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700555struct hci_dev *hci_dev_get_type(u8 amp_type)
556{
557 struct hci_dev *hdev = NULL;
558 struct hci_dev *d;
559
560 BT_DBG("amp_type %d", amp_type);
561
562 read_lock_bh(&hci_dev_list_lock);
563
564 list_for_each_entry(d, &hci_dev_list, list) {
565 if ((d->amp_type == amp_type) && test_bit(HCI_UP, &d->flags)) {
566 hdev = d;
567 break;
568 }
569 }
570
571 if (hdev)
572 hdev = hci_dev_hold(hdev);
573
574 read_unlock_bh(&hci_dev_list_lock);
575 return hdev;
576}
577EXPORT_SYMBOL(hci_dev_get_type);
578
579struct hci_dev *hci_dev_get_amp(bdaddr_t *dst)
580{
581 struct hci_dev *d;
582 struct hci_dev *hdev = NULL;
583
584 BT_DBG("%s dst %s", hdev->name, batostr(dst));
585
586 read_lock_bh(&hci_dev_list_lock);
587
588 list_for_each_entry(d, &hci_dev_list, list) {
589 struct hci_conn *conn;
590 if (d->dev_type == HCI_BREDR)
591 continue;
592 conn = hci_conn_hash_lookup_ba(d, ACL_LINK, dst);
593 if (conn) {
594 hdev = d;
595 break;
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_dev_get_amp);
606
Ville Tervofcd89c02011-02-10 22:38:47 -0300607/* Create SCO, ACL or LE connection.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 * Device _must_ be locked */
Nick Pellybbcda3b2010-02-11 11:54:28 -0800609struct hci_conn *hci_connect(struct hci_dev *hdev, int type,
610 __u16 pkt_type, bdaddr_t *dst,
611 __u8 sec_level, __u8 auth_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612{
613 struct hci_conn *acl;
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200614 struct hci_conn *sco;
Ville Tervofcd89c02011-02-10 22:38:47 -0300615 struct hci_conn *le;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
617 BT_DBG("%s dst %s", hdev->name, batostr(dst));
618
Ville Tervofcd89c02011-02-10 22:38:47 -0300619 if (type == LE_LINK) {
Andre Guedes5e89ece2011-05-31 14:20:56 -0300620 struct adv_entry *entry;
621
Ville Tervofcd89c02011-02-10 22:38:47 -0300622 le = hci_conn_hash_lookup_ba(hdev, LE_LINK, dst);
Anderson Briglia15c47942011-02-21 15:09:23 -0300623 if (le)
Ville Tervo30e76272011-02-22 16:10:53 -0300624 return ERR_PTR(-EBUSY);
Andre Guedes5e89ece2011-05-31 14:20:56 -0300625
626 entry = hci_find_adv_entry(hdev, dst);
627 if (!entry)
628 return ERR_PTR(-EHOSTUNREACH);
629
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700630 le = hci_le_conn_add(hdev, dst, entry->bdaddr_type);
Ville Tervofcd89c02011-02-10 22:38:47 -0300631 if (!le)
Ville Tervo30e76272011-02-22 16:10:53 -0300632 return ERR_PTR(-ENOMEM);
Andre Guedes92398c82011-05-31 14:20:55 -0300633
634 hci_le_connect(le);
Ville Tervofcd89c02011-02-10 22:38:47 -0300635
636 hci_conn_hold(le);
637
638 return le;
639 }
640
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200641 acl = hci_conn_hash_lookup_ba(hdev, ACL_LINK, dst);
642 if (!acl) {
Nick Pellybbcda3b2010-02-11 11:54:28 -0800643 acl = hci_conn_add(hdev, ACL_LINK, 0, dst);
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200644 if (!acl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 return NULL;
646 }
647
648 hci_conn_hold(acl);
649
Marcel Holtmann09ab6f42008-09-09 07:19:20 +0200650 if (acl->state == BT_OPEN || acl->state == BT_CLOSED) {
Johan Hedberg765c2a92011-01-19 12:06:52 +0530651 acl->sec_level = BT_SECURITY_LOW;
652 acl->pending_sec_level = sec_level;
Marcel Holtmann09ab6f42008-09-09 07:19:20 +0200653 acl->auth_type = auth_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 hci_acl_connect(acl);
Marcel Holtmann09ab6f42008-09-09 07:19:20 +0200655 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200657 if (type == ACL_LINK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 return acl;
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200659
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200660 sco = hci_conn_hash_lookup_ba(hdev, type, dst);
661 if (!sco) {
Nick Pellybbcda3b2010-02-11 11:54:28 -0800662 sco = hci_conn_add(hdev, type, pkt_type, dst);
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200663 if (!sco) {
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200664 hci_conn_put(acl);
665 return NULL;
666 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 }
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200668
669 acl->link = sco;
670 sco->link = acl;
671
672 hci_conn_hold(sco);
673
674 if (acl->state == BT_CONNECTED &&
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200675 (sco->state == BT_OPEN || sco->state == BT_CLOSED)) {
Nick Pellyc3902162009-11-13 14:16:32 -0800676 acl->power_save = 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700677 hci_conn_enter_active_mode(acl, 1);
Nick Pellyc3902162009-11-13 14:16:32 -0800678
Marcel Holtmanne73439d2010-07-26 10:06:00 -0400679 if (test_bit(HCI_CONN_MODE_CHANGE_PEND, &acl->pend)) {
680 /* defer SCO setup until mode change completed */
681 set_bit(HCI_CONN_SCO_SETUP_PEND, &acl->pend);
682 return sco;
683 }
684
685 hci_sco_setup(acl, 0x00);
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200686 }
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200687
688 return sco;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689}
690EXPORT_SYMBOL(hci_connect);
691
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700692void hci_disconnect(struct hci_conn *conn, __u8 reason)
693{
694 BT_DBG("conn %p", conn);
695
696 hci_proto_disconn_cfm(conn, reason);
697}
698EXPORT_SYMBOL(hci_disconnect);
699
700void hci_disconnect_amp(struct hci_conn *conn, __u8 reason)
701{
702 struct hci_dev *hdev = NULL;
703
704 BT_DBG("conn %p", conn);
705
706 read_lock_bh(&hci_dev_list_lock);
707
708 list_for_each_entry(hdev, &hci_dev_list, list) {
709 struct hci_conn *c;
710 if (hdev == conn->hdev)
711 continue;
712 if (hdev->amp_type == HCI_BREDR)
713 continue;
714 c = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &conn->dst);
715 if (c)
716 hci_disconnect(c, reason);
717 }
718
719 read_unlock_bh(&hci_dev_list_lock);
720}
721
Marcel Holtmanne7c29cb2008-09-09 07:19:20 +0200722/* Check link security requirement */
723int hci_conn_check_link_mode(struct hci_conn *conn)
724{
725 BT_DBG("conn %p", conn);
726
727 if (conn->ssp_mode > 0 && conn->hdev->ssp_mode > 0 &&
728 !(conn->link_mode & HCI_LM_ENCRYPT))
729 return 0;
730
731 return 1;
732}
733EXPORT_SYMBOL(hci_conn_check_link_mode);
734
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735/* Authenticate remote device */
Marcel Holtmann0684e5f2009-02-09 02:48:38 +0100736static int hci_conn_auth(struct hci_conn *conn, __u8 sec_level, __u8 auth_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737{
738 BT_DBG("conn %p", conn);
739
Johan Hedberg765c2a92011-01-19 12:06:52 +0530740 if (conn->pending_sec_level > sec_level)
741 sec_level = conn->pending_sec_level;
742
Marcel Holtmann96a31832009-02-12 16:23:03 +0100743 if (sec_level > conn->sec_level)
Johan Hedberg765c2a92011-01-19 12:06:52 +0530744 conn->pending_sec_level = sec_level;
Marcel Holtmann96a31832009-02-12 16:23:03 +0100745 else if (conn->link_mode & HCI_LM_AUTH)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 return 1;
747
Johan Hedberg65cf6862011-01-19 12:06:49 +0530748 /* Make sure we preserve an existing MITM requirement*/
749 auth_type |= (conn->auth_type & 0x01);
750
Marcel Holtmann96a31832009-02-12 16:23:03 +0100751 conn->auth_type = auth_type;
752
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 if (!test_and_set_bit(HCI_CONN_AUTH_PEND, &conn->pend)) {
754 struct hci_cp_auth_requested cp;
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -0700755 cp.handle = cpu_to_le16(conn->handle);
Marcel Holtmann40be4922008-07-14 20:13:50 +0200756 hci_send_cmd(conn->hdev, HCI_OP_AUTH_REQUESTED,
757 sizeof(cp), &cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 }
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100759
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 return 0;
761}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100763/* Enable security */
Marcel Holtmann0684e5f2009-02-09 02:48:38 +0100764int hci_conn_security(struct hci_conn *conn, __u8 sec_level, __u8 auth_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765{
766 BT_DBG("conn %p", conn);
767
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100768 if (sec_level == BT_SECURITY_SDP)
769 return 1;
770
Marcel Holtmann3fdca1e2009-04-28 09:04:55 -0700771 if (sec_level == BT_SECURITY_LOW &&
772 (!conn->ssp_mode || !conn->hdev->ssp_mode))
773 return 1;
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100774
Waldemar Rymarkiewicz13d39312011-04-28 12:07:55 +0200775 if (conn->link_mode & HCI_LM_ENCRYPT)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700776 return hci_conn_auth(conn, sec_level, auth_type);
Waldemar Rymarkiewicz13d39312011-04-28 12:07:55 +0200777
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700778 if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &conn->pend))
779 return 0;
780
781 if (hci_conn_auth(conn, sec_level, auth_type)) {
782 struct hci_cp_set_conn_encrypt cp;
783 cp.handle = cpu_to_le16(conn->handle);
784 cp.encrypt = 1;
785 hci_send_cmd(conn->hdev, HCI_OP_SET_CONN_ENCRYPT,
786 sizeof(cp), &cp);
787 }
788
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 return 0;
790}
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100791EXPORT_SYMBOL(hci_conn_security);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
793/* Change link key */
794int hci_conn_change_link_key(struct hci_conn *conn)
795{
796 BT_DBG("conn %p", conn);
797
798 if (!test_and_set_bit(HCI_CONN_AUTH_PEND, &conn->pend)) {
799 struct hci_cp_change_conn_link_key cp;
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -0700800 cp.handle = cpu_to_le16(conn->handle);
Marcel Holtmann40be4922008-07-14 20:13:50 +0200801 hci_send_cmd(conn->hdev, HCI_OP_CHANGE_CONN_LINK_KEY,
802 sizeof(cp), &cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 }
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100804
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 return 0;
806}
807EXPORT_SYMBOL(hci_conn_change_link_key);
808
809/* Switch role */
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100810int hci_conn_switch_role(struct hci_conn *conn, __u8 role)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811{
812 BT_DBG("conn %p", conn);
813
814 if (!role && conn->link_mode & HCI_LM_MASTER)
815 return 1;
816
817 if (!test_and_set_bit(HCI_CONN_RSWITCH_PEND, &conn->pend)) {
818 struct hci_cp_switch_role cp;
819 bacpy(&cp.bdaddr, &conn->dst);
820 cp.role = role;
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200821 hci_send_cmd(conn->hdev, HCI_OP_SWITCH_ROLE, sizeof(cp), &cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 }
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100823
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 return 0;
825}
826EXPORT_SYMBOL(hci_conn_switch_role);
827
Marcel Holtmann04837f62006-07-03 10:02:33 +0200828/* Enter active mode */
Jaikumar Ganesh514abe62011-05-23 18:06:04 -0700829void hci_conn_enter_active_mode(struct hci_conn *conn, __u8 force_active)
Marcel Holtmann04837f62006-07-03 10:02:33 +0200830{
831 struct hci_dev *hdev = conn->hdev;
832
833 BT_DBG("conn %p mode %d", conn, conn->mode);
834
835 if (test_bit(HCI_RAW, &hdev->flags))
836 return;
837
Jaikumar Ganesh514abe62011-05-23 18:06:04 -0700838 if (conn->mode != HCI_CM_SNIFF)
839 goto timer;
840
841 if (!conn->power_save && !force_active)
Marcel Holtmann04837f62006-07-03 10:02:33 +0200842 goto timer;
843
844 if (!test_and_set_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->pend)) {
845 struct hci_cp_exit_sniff_mode cp;
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -0700846 cp.handle = cpu_to_le16(conn->handle);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200847 hci_send_cmd(hdev, HCI_OP_EXIT_SNIFF_MODE, sizeof(cp), &cp);
Marcel Holtmann04837f62006-07-03 10:02:33 +0200848 }
849
850timer:
851 if (hdev->idle_timeout > 0)
852 mod_timer(&conn->idle_timer,
853 jiffies + msecs_to_jiffies(hdev->idle_timeout));
854}
855
856/* Enter sniff mode */
857void hci_conn_enter_sniff_mode(struct hci_conn *conn)
858{
859 struct hci_dev *hdev = conn->hdev;
860
861 BT_DBG("conn %p mode %d", conn, conn->mode);
862
863 if (test_bit(HCI_RAW, &hdev->flags))
864 return;
865
866 if (!lmp_sniff_capable(hdev) || !lmp_sniff_capable(conn))
867 return;
868
869 if (conn->mode != HCI_CM_ACTIVE || !(conn->link_policy & HCI_LP_SNIFF))
870 return;
871
872 if (lmp_sniffsubr_capable(hdev) && lmp_sniffsubr_capable(conn)) {
873 struct hci_cp_sniff_subrate cp;
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -0700874 cp.handle = cpu_to_le16(conn->handle);
875 cp.max_latency = cpu_to_le16(0);
876 cp.min_remote_timeout = cpu_to_le16(0);
877 cp.min_local_timeout = cpu_to_le16(0);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200878 hci_send_cmd(hdev, HCI_OP_SNIFF_SUBRATE, sizeof(cp), &cp);
Marcel Holtmann04837f62006-07-03 10:02:33 +0200879 }
880
881 if (!test_and_set_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->pend)) {
882 struct hci_cp_sniff_mode cp;
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -0700883 cp.handle = cpu_to_le16(conn->handle);
884 cp.max_interval = cpu_to_le16(hdev->sniff_max_interval);
885 cp.min_interval = cpu_to_le16(hdev->sniff_min_interval);
886 cp.attempt = cpu_to_le16(4);
887 cp.timeout = cpu_to_le16(1);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200888 hci_send_cmd(hdev, HCI_OP_SNIFF_MODE, sizeof(cp), &cp);
Marcel Holtmann04837f62006-07-03 10:02:33 +0200889 }
890}
891
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700892struct hci_chan *hci_chan_accept(struct hci_conn *conn,
893 struct hci_ext_fs *tx_fs, struct hci_ext_fs *rx_fs)
894{
895 struct hci_chan *chan;
896 struct hci_cp_create_logical_link cp;
897
898 chan = hci_chan_add(conn->hdev);
899 if (!chan)
900 return NULL;
901
902 chan->state = BT_CONNECT;
903 chan->conn = conn;
904 chan->tx_fs = *tx_fs;
905 chan->rx_fs = *rx_fs;
906 cp.phy_handle = chan->conn->handle;
907 cp.tx_fs.id = chan->tx_fs.id;
908 cp.tx_fs.type = chan->tx_fs.type;
909 cp.tx_fs.max_sdu = cpu_to_le16(chan->tx_fs.max_sdu);
910 cp.tx_fs.sdu_arr_time = cpu_to_le32(chan->tx_fs.sdu_arr_time);
911 cp.tx_fs.acc_latency = cpu_to_le32(chan->tx_fs.acc_latency);
912 cp.tx_fs.flush_to = cpu_to_le32(chan->tx_fs.flush_to);
913 cp.rx_fs.id = chan->rx_fs.id;
914 cp.rx_fs.type = chan->rx_fs.type;
915 cp.rx_fs.max_sdu = cpu_to_le16(chan->rx_fs.max_sdu);
916 cp.rx_fs.sdu_arr_time = cpu_to_le32(chan->rx_fs.sdu_arr_time);
917 cp.rx_fs.acc_latency = cpu_to_le32(chan->rx_fs.acc_latency);
918 cp.rx_fs.flush_to = cpu_to_le32(chan->rx_fs.flush_to);
919 hci_conn_hold(chan->conn);
920 hci_send_cmd(conn->hdev, HCI_OP_ACCEPT_LOGICAL_LINK, sizeof(cp), &cp);
921 return chan;
922}
923EXPORT_SYMBOL(hci_chan_accept);
924
925struct hci_chan *hci_chan_create(struct hci_conn *conn,
926 struct hci_ext_fs *tx_fs, struct hci_ext_fs *rx_fs)
927{
928 struct hci_chan *chan;
929 struct hci_cp_create_logical_link cp;
930
931 chan = hci_chan_add(conn->hdev);
932 if (!chan)
933 return NULL;
934
935 chan->state = BT_CONNECT;
936 chan->conn = conn;
937 chan->tx_fs = *tx_fs;
938 chan->rx_fs = *rx_fs;
939 cp.phy_handle = chan->conn->handle;
940 cp.tx_fs.id = chan->tx_fs.id;
941 cp.tx_fs.type = chan->tx_fs.type;
942 cp.tx_fs.max_sdu = cpu_to_le16(chan->tx_fs.max_sdu);
943 cp.tx_fs.sdu_arr_time = cpu_to_le32(chan->tx_fs.sdu_arr_time);
944 cp.tx_fs.acc_latency = cpu_to_le32(chan->tx_fs.acc_latency);
945 cp.tx_fs.flush_to = cpu_to_le32(chan->tx_fs.flush_to);
946 cp.rx_fs.id = chan->rx_fs.id;
947 cp.rx_fs.type = chan->rx_fs.type;
948 cp.rx_fs.max_sdu = cpu_to_le16(chan->rx_fs.max_sdu);
949 cp.rx_fs.sdu_arr_time = cpu_to_le32(chan->rx_fs.sdu_arr_time);
950 cp.rx_fs.acc_latency = cpu_to_le32(chan->rx_fs.acc_latency);
951 cp.rx_fs.flush_to = cpu_to_le32(chan->rx_fs.flush_to);
952 hci_conn_hold(chan->conn);
953 hci_send_cmd(conn->hdev, HCI_OP_CREATE_LOGICAL_LINK, sizeof(cp), &cp);
954 return chan;
955}
956EXPORT_SYMBOL(hci_chan_create);
957
958void hci_chan_modify(struct hci_chan *chan,
959 struct hci_ext_fs *tx_fs, struct hci_ext_fs *rx_fs)
960{
961 struct hci_cp_flow_spec_modify cp;
962
963 chan->tx_fs = *tx_fs;
964 chan->rx_fs = *rx_fs;
965 cp.log_handle = cpu_to_le16(chan->ll_handle);
966 cp.tx_fs.id = tx_fs->id;
967 cp.tx_fs.type = tx_fs->type;
968 cp.tx_fs.max_sdu = cpu_to_le16(tx_fs->max_sdu);
969 cp.tx_fs.sdu_arr_time = cpu_to_le32(tx_fs->sdu_arr_time);
970 cp.tx_fs.acc_latency = cpu_to_le32(tx_fs->acc_latency);
971 cp.tx_fs.flush_to = cpu_to_le32(tx_fs->flush_to);
972 cp.rx_fs.id = rx_fs->id;
973 cp.rx_fs.type = rx_fs->type;
974 cp.rx_fs.max_sdu = cpu_to_le16(rx_fs->max_sdu);
975 cp.rx_fs.sdu_arr_time = cpu_to_le32(rx_fs->sdu_arr_time);
976 cp.rx_fs.acc_latency = cpu_to_le32(rx_fs->acc_latency);
977 cp.rx_fs.flush_to = cpu_to_le32(rx_fs->flush_to);
978 hci_conn_hold(chan->conn);
979 hci_send_cmd(chan->conn->hdev, HCI_OP_FLOW_SPEC_MODIFY, sizeof(cp),
980 &cp);
981}
982EXPORT_SYMBOL(hci_chan_modify);
983
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984/* Drop all connection on the device */
985void hci_conn_hash_flush(struct hci_dev *hdev)
986{
987 struct hci_conn_hash *h = &hdev->conn_hash;
988 struct list_head *p;
989
990 BT_DBG("hdev %s", hdev->name);
991
992 p = h->list.next;
993 while (p != &h->list) {
994 struct hci_conn *c;
995
996 c = list_entry(p, struct hci_conn, list);
997 p = p->next;
998
999 c->state = BT_CLOSED;
1000
Marcel Holtmann2950f212009-02-12 14:02:50 +01001001 hci_proto_disconn_cfm(c, 0x16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 hci_conn_del(c);
1003 }
1004}
1005
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001006/* Check pending connect attempts */
1007void hci_conn_check_pending(struct hci_dev *hdev)
1008{
1009 struct hci_conn *conn;
1010
1011 BT_DBG("hdev %s", hdev->name);
1012
1013 hci_dev_lock(hdev);
1014
1015 conn = hci_conn_hash_lookup_state(hdev, ACL_LINK, BT_CONNECT2);
1016 if (conn)
1017 hci_acl_connect(conn);
1018
1019 hci_dev_unlock(hdev);
1020}
1021
Marcel Holtmann9eba32b2009-08-22 14:19:26 -07001022void hci_conn_hold_device(struct hci_conn *conn)
1023{
1024 atomic_inc(&conn->devref);
1025}
1026EXPORT_SYMBOL(hci_conn_hold_device);
1027
1028void hci_conn_put_device(struct hci_conn *conn)
1029{
1030 if (atomic_dec_and_test(&conn->devref))
1031 hci_conn_del_sysfs(conn);
1032}
1033EXPORT_SYMBOL(hci_conn_put_device);
1034
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035int hci_get_conn_list(void __user *arg)
1036{
1037 struct hci_conn_list_req req, *cl;
1038 struct hci_conn_info *ci;
1039 struct hci_dev *hdev;
1040 struct list_head *p;
1041 int n = 0, size, err;
1042
1043 if (copy_from_user(&req, arg, sizeof(req)))
1044 return -EFAULT;
1045
1046 if (!req.conn_num || req.conn_num > (PAGE_SIZE * 2) / sizeof(*ci))
1047 return -EINVAL;
1048
1049 size = sizeof(req) + req.conn_num * sizeof(*ci);
1050
Andrei Emeltchenko70f230202010-12-01 16:58:25 +02001051 cl = kmalloc(size, GFP_KERNEL);
1052 if (!cl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 return -ENOMEM;
1054
Andrei Emeltchenko70f230202010-12-01 16:58:25 +02001055 hdev = hci_dev_get(req.dev_id);
1056 if (!hdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 kfree(cl);
1058 return -ENODEV;
1059 }
1060
1061 ci = cl->conn_info;
1062
1063 hci_dev_lock_bh(hdev);
1064 list_for_each(p, &hdev->conn_hash.list) {
1065 register struct hci_conn *c;
1066 c = list_entry(p, struct hci_conn, list);
1067
1068 bacpy(&(ci + n)->bdaddr, &c->dst);
1069 (ci + n)->handle = c->handle;
1070 (ci + n)->type = c->type;
1071 (ci + n)->out = c->out;
1072 (ci + n)->state = c->state;
1073 (ci + n)->link_mode = c->link_mode;
Nick Pellyc1728492009-12-09 00:15:41 -08001074 if (c->type == SCO_LINK) {
1075 (ci + n)->mtu = hdev->sco_mtu;
1076 (ci + n)->cnt = hdev->sco_cnt;
1077 (ci + n)->pkts = hdev->sco_pkts;
1078 } else {
1079 (ci + n)->mtu = hdev->acl_mtu;
1080 (ci + n)->cnt = hdev->acl_cnt;
1081 (ci + n)->pkts = hdev->acl_pkts;
1082 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 if (++n >= req.conn_num)
1084 break;
1085 }
1086 hci_dev_unlock_bh(hdev);
1087
1088 cl->dev_id = hdev->id;
1089 cl->conn_num = n;
1090 size = sizeof(req) + n * sizeof(*ci);
1091
1092 hci_dev_put(hdev);
1093
1094 err = copy_to_user(arg, cl, size);
1095 kfree(cl);
1096
1097 return err ? -EFAULT : 0;
1098}
1099
1100int hci_get_conn_info(struct hci_dev *hdev, void __user *arg)
1101{
1102 struct hci_conn_info_req req;
1103 struct hci_conn_info ci;
1104 struct hci_conn *conn;
1105 char __user *ptr = arg + sizeof(req);
1106
1107 if (copy_from_user(&req, arg, sizeof(req)))
1108 return -EFAULT;
1109
1110 hci_dev_lock_bh(hdev);
1111 conn = hci_conn_hash_lookup_ba(hdev, req.type, &req.bdaddr);
1112 if (conn) {
1113 bacpy(&ci.bdaddr, &conn->dst);
1114 ci.handle = conn->handle;
1115 ci.type = conn->type;
1116 ci.out = conn->out;
1117 ci.state = conn->state;
1118 ci.link_mode = conn->link_mode;
Nick Pellyc1728492009-12-09 00:15:41 -08001119 if (req.type == SCO_LINK) {
1120 ci.mtu = hdev->sco_mtu;
1121 ci.cnt = hdev->sco_cnt;
1122 ci.pkts = hdev->sco_pkts;
1123 } else {
1124 ci.mtu = hdev->acl_mtu;
1125 ci.cnt = hdev->acl_cnt;
1126 ci.pkts = hdev->acl_pkts;
1127 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001128 ci.pending_sec_level = conn->pending_sec_level;
1129 ci.ssp_mode = conn->ssp_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 }
1131 hci_dev_unlock_bh(hdev);
1132
1133 if (!conn)
1134 return -ENOENT;
1135
1136 return copy_to_user(ptr, &ci, sizeof(ci)) ? -EFAULT : 0;
1137}
Marcel Holtmann40be4922008-07-14 20:13:50 +02001138
1139int hci_get_auth_info(struct hci_dev *hdev, void __user *arg)
1140{
1141 struct hci_auth_info_req req;
1142 struct hci_conn *conn;
1143
1144 if (copy_from_user(&req, arg, sizeof(req)))
1145 return -EFAULT;
1146
1147 hci_dev_lock_bh(hdev);
1148 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &req.bdaddr);
1149 if (conn)
1150 req.type = conn->auth_type;
1151 hci_dev_unlock_bh(hdev);
1152
1153 if (!conn)
1154 return -ENOENT;
1155
1156 return copy_to_user(arg, &req, sizeof(req)) ? -EFAULT : 0;
1157}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001158
1159int hci_set_auth_info(struct hci_dev *hdev, void __user *arg)
1160{
1161 struct hci_auth_info_req req;
1162 struct hci_conn *conn;
1163
1164 if (copy_from_user(&req, arg, sizeof(req)))
1165 return -EFAULT;
1166
1167 hci_dev_lock_bh(hdev);
1168 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &req.bdaddr);
1169 if (conn)
1170 conn->auth_type = req.type;
1171 hci_dev_unlock_bh(hdev);
1172
1173 if (!conn)
1174 return -ENOENT;
1175
1176 return copy_to_user(arg, &req, sizeof(req)) ? -EFAULT : 0;
1177}