blob: bcc65ff49639bbac358f553978755270cd66ebfe [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;
Brian Gix842bc5e2011-09-07 08:13:41 -0700228 memcpy(cp.rand, rand, sizeof(cp.rand));
Vinicius Costa Gomes735038c2011-06-09 18:50:47 -0300229
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)
Brian Gixa68668b2011-08-11 15:49:36 -0700624 return le;
Andre Guedes5e89ece2011-05-31 14:20:56 -0300625
626 entry = hci_find_adv_entry(hdev, dst);
627 if (!entry)
Brian Gixa68668b2011-08-11 15:49:36 -0700628 le = hci_le_conn_add(hdev, dst, 0);
629 else
630 le = hci_le_conn_add(hdev, dst, entry->bdaddr_type);
Andre Guedes5e89ece2011-05-31 14:20:56 -0300631
Ville Tervofcd89c02011-02-10 22:38:47 -0300632 if (!le)
Ville Tervo30e76272011-02-22 16:10:53 -0300633 return ERR_PTR(-ENOMEM);
Andre Guedes92398c82011-05-31 14:20:55 -0300634
635 hci_le_connect(le);
Ville Tervofcd89c02011-02-10 22:38:47 -0300636
637 hci_conn_hold(le);
638
639 return le;
640 }
641
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200642 acl = hci_conn_hash_lookup_ba(hdev, ACL_LINK, dst);
643 if (!acl) {
Nick Pellybbcda3b2010-02-11 11:54:28 -0800644 acl = hci_conn_add(hdev, ACL_LINK, 0, dst);
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200645 if (!acl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 return NULL;
647 }
648
649 hci_conn_hold(acl);
650
Marcel Holtmann09ab6f42008-09-09 07:19:20 +0200651 if (acl->state == BT_OPEN || acl->state == BT_CLOSED) {
Johan Hedberg765c2a92011-01-19 12:06:52 +0530652 acl->sec_level = BT_SECURITY_LOW;
653 acl->pending_sec_level = sec_level;
Marcel Holtmann09ab6f42008-09-09 07:19:20 +0200654 acl->auth_type = auth_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 hci_acl_connect(acl);
Marcel Holtmann09ab6f42008-09-09 07:19:20 +0200656 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200658 if (type == ACL_LINK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 return acl;
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200660
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200661 sco = hci_conn_hash_lookup_ba(hdev, type, dst);
662 if (!sco) {
Nick Pellybbcda3b2010-02-11 11:54:28 -0800663 sco = hci_conn_add(hdev, type, pkt_type, dst);
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200664 if (!sco) {
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200665 hci_conn_put(acl);
666 return NULL;
667 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 }
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200669
670 acl->link = sco;
671 sco->link = acl;
672
673 hci_conn_hold(sco);
674
675 if (acl->state == BT_CONNECTED &&
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200676 (sco->state == BT_OPEN || sco->state == BT_CLOSED)) {
Nick Pellyc3902162009-11-13 14:16:32 -0800677 acl->power_save = 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700678 hci_conn_enter_active_mode(acl, 1);
Nick Pellyc3902162009-11-13 14:16:32 -0800679
Marcel Holtmanne73439d2010-07-26 10:06:00 -0400680 if (test_bit(HCI_CONN_MODE_CHANGE_PEND, &acl->pend)) {
681 /* defer SCO setup until mode change completed */
682 set_bit(HCI_CONN_SCO_SETUP_PEND, &acl->pend);
683 return sco;
684 }
685
686 hci_sco_setup(acl, 0x00);
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200687 }
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200688
689 return sco;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690}
691EXPORT_SYMBOL(hci_connect);
692
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700693void hci_disconnect(struct hci_conn *conn, __u8 reason)
694{
695 BT_DBG("conn %p", conn);
696
697 hci_proto_disconn_cfm(conn, reason);
698}
699EXPORT_SYMBOL(hci_disconnect);
700
701void hci_disconnect_amp(struct hci_conn *conn, __u8 reason)
702{
703 struct hci_dev *hdev = NULL;
704
705 BT_DBG("conn %p", conn);
706
707 read_lock_bh(&hci_dev_list_lock);
708
709 list_for_each_entry(hdev, &hci_dev_list, list) {
710 struct hci_conn *c;
711 if (hdev == conn->hdev)
712 continue;
713 if (hdev->amp_type == HCI_BREDR)
714 continue;
715 c = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &conn->dst);
716 if (c)
717 hci_disconnect(c, reason);
718 }
719
720 read_unlock_bh(&hci_dev_list_lock);
721}
722
Marcel Holtmanne7c29cb2008-09-09 07:19:20 +0200723/* Check link security requirement */
724int hci_conn_check_link_mode(struct hci_conn *conn)
725{
726 BT_DBG("conn %p", conn);
727
728 if (conn->ssp_mode > 0 && conn->hdev->ssp_mode > 0 &&
729 !(conn->link_mode & HCI_LM_ENCRYPT))
730 return 0;
731
732 return 1;
733}
734EXPORT_SYMBOL(hci_conn_check_link_mode);
735
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736/* Authenticate remote device */
Marcel Holtmann0684e5f2009-02-09 02:48:38 +0100737static int hci_conn_auth(struct hci_conn *conn, __u8 sec_level, __u8 auth_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738{
739 BT_DBG("conn %p", conn);
740
Johan Hedberg765c2a92011-01-19 12:06:52 +0530741 if (conn->pending_sec_level > sec_level)
742 sec_level = conn->pending_sec_level;
743
Marcel Holtmann96a31832009-02-12 16:23:03 +0100744 if (sec_level > conn->sec_level)
Johan Hedberg765c2a92011-01-19 12:06:52 +0530745 conn->pending_sec_level = sec_level;
Marcel Holtmann96a31832009-02-12 16:23:03 +0100746 else if (conn->link_mode & HCI_LM_AUTH)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 return 1;
748
Johan Hedberg65cf6862011-01-19 12:06:49 +0530749 /* Make sure we preserve an existing MITM requirement*/
750 auth_type |= (conn->auth_type & 0x01);
Marcel Holtmann96a31832009-02-12 16:23:03 +0100751 conn->auth_type = auth_type;
Brian Gixa68668b2011-08-11 15:49:36 -0700752 conn->auth_initiator = 1;
Marcel Holtmann96a31832009-02-12 16:23:03 +0100753
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 if (!test_and_set_bit(HCI_CONN_AUTH_PEND, &conn->pend)) {
755 struct hci_cp_auth_requested cp;
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -0700756 cp.handle = cpu_to_le16(conn->handle);
Marcel Holtmann40be4922008-07-14 20:13:50 +0200757 hci_send_cmd(conn->hdev, HCI_OP_AUTH_REQUESTED,
758 sizeof(cp), &cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 }
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100760
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 return 0;
762}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100764/* Enable security */
Marcel Holtmann0684e5f2009-02-09 02:48:38 +0100765int hci_conn_security(struct hci_conn *conn, __u8 sec_level, __u8 auth_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766{
Brian Gixa68668b2011-08-11 15:49:36 -0700767 BT_DBG("conn %p %d %d", conn, sec_level, auth_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100769 if (sec_level == BT_SECURITY_SDP)
770 return 1;
771
Marcel Holtmann3fdca1e2009-04-28 09:04:55 -0700772 if (sec_level == BT_SECURITY_LOW &&
773 (!conn->ssp_mode || !conn->hdev->ssp_mode))
774 return 1;
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100775
Waldemar Rymarkiewicz13d39312011-04-28 12:07:55 +0200776 if (conn->link_mode & HCI_LM_ENCRYPT)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700777 return hci_conn_auth(conn, sec_level, auth_type);
Waldemar Rymarkiewicz13d39312011-04-28 12:07:55 +0200778
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700779 if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &conn->pend))
780 return 0;
781
782 if (hci_conn_auth(conn, sec_level, auth_type)) {
783 struct hci_cp_set_conn_encrypt cp;
784 cp.handle = cpu_to_le16(conn->handle);
785 cp.encrypt = 1;
786 hci_send_cmd(conn->hdev, HCI_OP_SET_CONN_ENCRYPT,
787 sizeof(cp), &cp);
788 }
789
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 return 0;
791}
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100792EXPORT_SYMBOL(hci_conn_security);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
794/* Change link key */
795int hci_conn_change_link_key(struct hci_conn *conn)
796{
797 BT_DBG("conn %p", conn);
798
799 if (!test_and_set_bit(HCI_CONN_AUTH_PEND, &conn->pend)) {
800 struct hci_cp_change_conn_link_key cp;
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -0700801 cp.handle = cpu_to_le16(conn->handle);
Marcel Holtmann40be4922008-07-14 20:13:50 +0200802 hci_send_cmd(conn->hdev, HCI_OP_CHANGE_CONN_LINK_KEY,
803 sizeof(cp), &cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 }
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100805
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 return 0;
807}
808EXPORT_SYMBOL(hci_conn_change_link_key);
809
810/* Switch role */
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100811int hci_conn_switch_role(struct hci_conn *conn, __u8 role)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812{
813 BT_DBG("conn %p", conn);
814
815 if (!role && conn->link_mode & HCI_LM_MASTER)
816 return 1;
817
818 if (!test_and_set_bit(HCI_CONN_RSWITCH_PEND, &conn->pend)) {
819 struct hci_cp_switch_role cp;
820 bacpy(&cp.bdaddr, &conn->dst);
821 cp.role = role;
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200822 hci_send_cmd(conn->hdev, HCI_OP_SWITCH_ROLE, sizeof(cp), &cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 }
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100824
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 return 0;
826}
827EXPORT_SYMBOL(hci_conn_switch_role);
828
Marcel Holtmann04837f62006-07-03 10:02:33 +0200829/* Enter active mode */
Jaikumar Ganesh514abe62011-05-23 18:06:04 -0700830void hci_conn_enter_active_mode(struct hci_conn *conn, __u8 force_active)
Marcel Holtmann04837f62006-07-03 10:02:33 +0200831{
832 struct hci_dev *hdev = conn->hdev;
833
834 BT_DBG("conn %p mode %d", conn, conn->mode);
835
836 if (test_bit(HCI_RAW, &hdev->flags))
837 return;
838
Jaikumar Ganesh514abe62011-05-23 18:06:04 -0700839 if (conn->mode != HCI_CM_SNIFF)
840 goto timer;
841
842 if (!conn->power_save && !force_active)
Marcel Holtmann04837f62006-07-03 10:02:33 +0200843 goto timer;
844
845 if (!test_and_set_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->pend)) {
846 struct hci_cp_exit_sniff_mode cp;
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -0700847 cp.handle = cpu_to_le16(conn->handle);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200848 hci_send_cmd(hdev, HCI_OP_EXIT_SNIFF_MODE, sizeof(cp), &cp);
Marcel Holtmann04837f62006-07-03 10:02:33 +0200849 }
850
851timer:
852 if (hdev->idle_timeout > 0)
853 mod_timer(&conn->idle_timer,
854 jiffies + msecs_to_jiffies(hdev->idle_timeout));
855}
856
857/* Enter sniff mode */
858void hci_conn_enter_sniff_mode(struct hci_conn *conn)
859{
860 struct hci_dev *hdev = conn->hdev;
861
862 BT_DBG("conn %p mode %d", conn, conn->mode);
863
864 if (test_bit(HCI_RAW, &hdev->flags))
865 return;
866
867 if (!lmp_sniff_capable(hdev) || !lmp_sniff_capable(conn))
868 return;
869
870 if (conn->mode != HCI_CM_ACTIVE || !(conn->link_policy & HCI_LP_SNIFF))
871 return;
872
873 if (lmp_sniffsubr_capable(hdev) && lmp_sniffsubr_capable(conn)) {
874 struct hci_cp_sniff_subrate cp;
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -0700875 cp.handle = cpu_to_le16(conn->handle);
876 cp.max_latency = cpu_to_le16(0);
877 cp.min_remote_timeout = cpu_to_le16(0);
878 cp.min_local_timeout = cpu_to_le16(0);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200879 hci_send_cmd(hdev, HCI_OP_SNIFF_SUBRATE, sizeof(cp), &cp);
Marcel Holtmann04837f62006-07-03 10:02:33 +0200880 }
881
882 if (!test_and_set_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->pend)) {
883 struct hci_cp_sniff_mode cp;
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -0700884 cp.handle = cpu_to_le16(conn->handle);
885 cp.max_interval = cpu_to_le16(hdev->sniff_max_interval);
886 cp.min_interval = cpu_to_le16(hdev->sniff_min_interval);
887 cp.attempt = cpu_to_le16(4);
888 cp.timeout = cpu_to_le16(1);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200889 hci_send_cmd(hdev, HCI_OP_SNIFF_MODE, sizeof(cp), &cp);
Marcel Holtmann04837f62006-07-03 10:02:33 +0200890 }
891}
892
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700893struct hci_chan *hci_chan_accept(struct hci_conn *conn,
894 struct hci_ext_fs *tx_fs, struct hci_ext_fs *rx_fs)
895{
896 struct hci_chan *chan;
897 struct hci_cp_create_logical_link cp;
898
899 chan = hci_chan_add(conn->hdev);
900 if (!chan)
901 return NULL;
902
903 chan->state = BT_CONNECT;
904 chan->conn = conn;
905 chan->tx_fs = *tx_fs;
906 chan->rx_fs = *rx_fs;
907 cp.phy_handle = chan->conn->handle;
908 cp.tx_fs.id = chan->tx_fs.id;
909 cp.tx_fs.type = chan->tx_fs.type;
910 cp.tx_fs.max_sdu = cpu_to_le16(chan->tx_fs.max_sdu);
911 cp.tx_fs.sdu_arr_time = cpu_to_le32(chan->tx_fs.sdu_arr_time);
912 cp.tx_fs.acc_latency = cpu_to_le32(chan->tx_fs.acc_latency);
913 cp.tx_fs.flush_to = cpu_to_le32(chan->tx_fs.flush_to);
914 cp.rx_fs.id = chan->rx_fs.id;
915 cp.rx_fs.type = chan->rx_fs.type;
916 cp.rx_fs.max_sdu = cpu_to_le16(chan->rx_fs.max_sdu);
917 cp.rx_fs.sdu_arr_time = cpu_to_le32(chan->rx_fs.sdu_arr_time);
918 cp.rx_fs.acc_latency = cpu_to_le32(chan->rx_fs.acc_latency);
919 cp.rx_fs.flush_to = cpu_to_le32(chan->rx_fs.flush_to);
920 hci_conn_hold(chan->conn);
921 hci_send_cmd(conn->hdev, HCI_OP_ACCEPT_LOGICAL_LINK, sizeof(cp), &cp);
922 return chan;
923}
924EXPORT_SYMBOL(hci_chan_accept);
925
926struct hci_chan *hci_chan_create(struct hci_conn *conn,
927 struct hci_ext_fs *tx_fs, struct hci_ext_fs *rx_fs)
928{
929 struct hci_chan *chan;
930 struct hci_cp_create_logical_link cp;
931
932 chan = hci_chan_add(conn->hdev);
933 if (!chan)
934 return NULL;
935
936 chan->state = BT_CONNECT;
937 chan->conn = conn;
938 chan->tx_fs = *tx_fs;
939 chan->rx_fs = *rx_fs;
940 cp.phy_handle = chan->conn->handle;
941 cp.tx_fs.id = chan->tx_fs.id;
942 cp.tx_fs.type = chan->tx_fs.type;
943 cp.tx_fs.max_sdu = cpu_to_le16(chan->tx_fs.max_sdu);
944 cp.tx_fs.sdu_arr_time = cpu_to_le32(chan->tx_fs.sdu_arr_time);
945 cp.tx_fs.acc_latency = cpu_to_le32(chan->tx_fs.acc_latency);
946 cp.tx_fs.flush_to = cpu_to_le32(chan->tx_fs.flush_to);
947 cp.rx_fs.id = chan->rx_fs.id;
948 cp.rx_fs.type = chan->rx_fs.type;
949 cp.rx_fs.max_sdu = cpu_to_le16(chan->rx_fs.max_sdu);
950 cp.rx_fs.sdu_arr_time = cpu_to_le32(chan->rx_fs.sdu_arr_time);
951 cp.rx_fs.acc_latency = cpu_to_le32(chan->rx_fs.acc_latency);
952 cp.rx_fs.flush_to = cpu_to_le32(chan->rx_fs.flush_to);
953 hci_conn_hold(chan->conn);
954 hci_send_cmd(conn->hdev, HCI_OP_CREATE_LOGICAL_LINK, sizeof(cp), &cp);
955 return chan;
956}
957EXPORT_SYMBOL(hci_chan_create);
958
959void hci_chan_modify(struct hci_chan *chan,
960 struct hci_ext_fs *tx_fs, struct hci_ext_fs *rx_fs)
961{
962 struct hci_cp_flow_spec_modify cp;
963
964 chan->tx_fs = *tx_fs;
965 chan->rx_fs = *rx_fs;
966 cp.log_handle = cpu_to_le16(chan->ll_handle);
967 cp.tx_fs.id = tx_fs->id;
968 cp.tx_fs.type = tx_fs->type;
969 cp.tx_fs.max_sdu = cpu_to_le16(tx_fs->max_sdu);
970 cp.tx_fs.sdu_arr_time = cpu_to_le32(tx_fs->sdu_arr_time);
971 cp.tx_fs.acc_latency = cpu_to_le32(tx_fs->acc_latency);
972 cp.tx_fs.flush_to = cpu_to_le32(tx_fs->flush_to);
973 cp.rx_fs.id = rx_fs->id;
974 cp.rx_fs.type = rx_fs->type;
975 cp.rx_fs.max_sdu = cpu_to_le16(rx_fs->max_sdu);
976 cp.rx_fs.sdu_arr_time = cpu_to_le32(rx_fs->sdu_arr_time);
977 cp.rx_fs.acc_latency = cpu_to_le32(rx_fs->acc_latency);
978 cp.rx_fs.flush_to = cpu_to_le32(rx_fs->flush_to);
979 hci_conn_hold(chan->conn);
980 hci_send_cmd(chan->conn->hdev, HCI_OP_FLOW_SPEC_MODIFY, sizeof(cp),
981 &cp);
982}
983EXPORT_SYMBOL(hci_chan_modify);
984
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985/* Drop all connection on the device */
986void hci_conn_hash_flush(struct hci_dev *hdev)
987{
988 struct hci_conn_hash *h = &hdev->conn_hash;
989 struct list_head *p;
990
991 BT_DBG("hdev %s", hdev->name);
992
993 p = h->list.next;
994 while (p != &h->list) {
995 struct hci_conn *c;
996
997 c = list_entry(p, struct hci_conn, list);
998 p = p->next;
999
1000 c->state = BT_CLOSED;
1001
Marcel Holtmann2950f212009-02-12 14:02:50 +01001002 hci_proto_disconn_cfm(c, 0x16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 hci_conn_del(c);
1004 }
1005}
1006
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001007/* Check pending connect attempts */
1008void hci_conn_check_pending(struct hci_dev *hdev)
1009{
1010 struct hci_conn *conn;
1011
1012 BT_DBG("hdev %s", hdev->name);
1013
1014 hci_dev_lock(hdev);
1015
1016 conn = hci_conn_hash_lookup_state(hdev, ACL_LINK, BT_CONNECT2);
1017 if (conn)
1018 hci_acl_connect(conn);
1019
1020 hci_dev_unlock(hdev);
1021}
1022
Marcel Holtmann9eba32b2009-08-22 14:19:26 -07001023void hci_conn_hold_device(struct hci_conn *conn)
1024{
1025 atomic_inc(&conn->devref);
1026}
1027EXPORT_SYMBOL(hci_conn_hold_device);
1028
1029void hci_conn_put_device(struct hci_conn *conn)
1030{
1031 if (atomic_dec_and_test(&conn->devref))
1032 hci_conn_del_sysfs(conn);
1033}
1034EXPORT_SYMBOL(hci_conn_put_device);
1035
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036int hci_get_conn_list(void __user *arg)
1037{
1038 struct hci_conn_list_req req, *cl;
1039 struct hci_conn_info *ci;
1040 struct hci_dev *hdev;
1041 struct list_head *p;
1042 int n = 0, size, err;
1043
1044 if (copy_from_user(&req, arg, sizeof(req)))
1045 return -EFAULT;
1046
1047 if (!req.conn_num || req.conn_num > (PAGE_SIZE * 2) / sizeof(*ci))
1048 return -EINVAL;
1049
1050 size = sizeof(req) + req.conn_num * sizeof(*ci);
1051
Andrei Emeltchenko70f230202010-12-01 16:58:25 +02001052 cl = kmalloc(size, GFP_KERNEL);
1053 if (!cl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 return -ENOMEM;
1055
Andrei Emeltchenko70f230202010-12-01 16:58:25 +02001056 hdev = hci_dev_get(req.dev_id);
1057 if (!hdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 kfree(cl);
1059 return -ENODEV;
1060 }
1061
1062 ci = cl->conn_info;
1063
1064 hci_dev_lock_bh(hdev);
1065 list_for_each(p, &hdev->conn_hash.list) {
1066 register struct hci_conn *c;
1067 c = list_entry(p, struct hci_conn, list);
1068
1069 bacpy(&(ci + n)->bdaddr, &c->dst);
1070 (ci + n)->handle = c->handle;
1071 (ci + n)->type = c->type;
1072 (ci + n)->out = c->out;
1073 (ci + n)->state = c->state;
1074 (ci + n)->link_mode = c->link_mode;
Nick Pellyc1728492009-12-09 00:15:41 -08001075 if (c->type == SCO_LINK) {
1076 (ci + n)->mtu = hdev->sco_mtu;
1077 (ci + n)->cnt = hdev->sco_cnt;
1078 (ci + n)->pkts = hdev->sco_pkts;
1079 } else {
1080 (ci + n)->mtu = hdev->acl_mtu;
1081 (ci + n)->cnt = hdev->acl_cnt;
1082 (ci + n)->pkts = hdev->acl_pkts;
1083 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 if (++n >= req.conn_num)
1085 break;
1086 }
1087 hci_dev_unlock_bh(hdev);
1088
1089 cl->dev_id = hdev->id;
1090 cl->conn_num = n;
1091 size = sizeof(req) + n * sizeof(*ci);
1092
1093 hci_dev_put(hdev);
1094
1095 err = copy_to_user(arg, cl, size);
1096 kfree(cl);
1097
1098 return err ? -EFAULT : 0;
1099}
1100
1101int hci_get_conn_info(struct hci_dev *hdev, void __user *arg)
1102{
1103 struct hci_conn_info_req req;
1104 struct hci_conn_info ci;
1105 struct hci_conn *conn;
1106 char __user *ptr = arg + sizeof(req);
1107
1108 if (copy_from_user(&req, arg, sizeof(req)))
1109 return -EFAULT;
1110
1111 hci_dev_lock_bh(hdev);
1112 conn = hci_conn_hash_lookup_ba(hdev, req.type, &req.bdaddr);
1113 if (conn) {
1114 bacpy(&ci.bdaddr, &conn->dst);
1115 ci.handle = conn->handle;
1116 ci.type = conn->type;
1117 ci.out = conn->out;
1118 ci.state = conn->state;
1119 ci.link_mode = conn->link_mode;
Nick Pellyc1728492009-12-09 00:15:41 -08001120 if (req.type == SCO_LINK) {
1121 ci.mtu = hdev->sco_mtu;
1122 ci.cnt = hdev->sco_cnt;
1123 ci.pkts = hdev->sco_pkts;
1124 } else {
1125 ci.mtu = hdev->acl_mtu;
1126 ci.cnt = hdev->acl_cnt;
1127 ci.pkts = hdev->acl_pkts;
1128 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001129 ci.pending_sec_level = conn->pending_sec_level;
1130 ci.ssp_mode = conn->ssp_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 }
1132 hci_dev_unlock_bh(hdev);
1133
1134 if (!conn)
1135 return -ENOENT;
1136
1137 return copy_to_user(ptr, &ci, sizeof(ci)) ? -EFAULT : 0;
1138}
Marcel Holtmann40be4922008-07-14 20:13:50 +02001139
1140int hci_get_auth_info(struct hci_dev *hdev, void __user *arg)
1141{
1142 struct hci_auth_info_req req;
1143 struct hci_conn *conn;
1144
1145 if (copy_from_user(&req, arg, sizeof(req)))
1146 return -EFAULT;
1147
1148 hci_dev_lock_bh(hdev);
1149 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &req.bdaddr);
1150 if (conn)
1151 req.type = conn->auth_type;
1152 hci_dev_unlock_bh(hdev);
1153
1154 if (!conn)
1155 return -ENOENT;
1156
1157 return copy_to_user(arg, &req, sizeof(req)) ? -EFAULT : 0;
1158}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001159
1160int hci_set_auth_info(struct hci_dev *hdev, void __user *arg)
1161{
1162 struct hci_auth_info_req req;
1163 struct hci_conn *conn;
1164
1165 if (copy_from_user(&req, arg, sizeof(req)))
1166 return -EFAULT;
1167
1168 hci_dev_lock_bh(hdev);
1169 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &req.bdaddr);
1170 if (conn)
1171 conn->auth_type = req.type;
1172 hci_dev_unlock_bh(hdev);
1173
1174 if (!conn)
1175 return -ENOENT;
1176
1177 return copy_to_user(arg, &req, sizeof(req)) ? -EFAULT : 0;
1178}