blob: 69c64ce054fbce226245386e0ad056df7577d17c [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
3 Copyright (C) 2000-2001 Qualcomm Incorporated
4
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>
42#include <asm/uaccess.h>
43#include <asm/unaligned.h>
44
45#include <net/bluetooth/bluetooth.h>
46#include <net/bluetooth/hci_core.h>
47
48#ifndef CONFIG_BT_HCI_CORE_DEBUG
49#undef BT_DBG
50#define BT_DBG(D...)
51#endif
52
Marcel Holtmann4c67bc72006-10-15 17:30:56 +020053void hci_acl_connect(struct hci_conn *conn)
Linus Torvalds1da177e2005-04-16 15:20:36 -070054{
55 struct hci_dev *hdev = conn->hdev;
56 struct inquiry_entry *ie;
57 struct hci_cp_create_conn cp;
58
59 BT_DBG("%p", conn);
60
61 conn->state = BT_CONNECT;
Marcel Holtmanna8746412008-07-14 20:13:46 +020062 conn->out = 1;
63
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 conn->link_mode = HCI_LM_MASTER;
65
Marcel Holtmann4c67bc72006-10-15 17:30:56 +020066 conn->attempt++;
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 memset(&cp, 0, sizeof(cp));
69 bacpy(&cp.bdaddr, &conn->dst);
70 cp.pscan_rep_mode = 0x02;
71
72 if ((ie = hci_inquiry_cache_lookup(hdev, &conn->dst)) &&
73 inquiry_entry_age(ie) <= INQUIRY_ENTRY_AGE_MAX) {
74 cp.pscan_rep_mode = ie->data.pscan_rep_mode;
75 cp.pscan_mode = ie->data.pscan_mode;
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -070076 cp.clock_offset = ie->data.clock_offset | cpu_to_le16(0x8000);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 memcpy(conn->dev_class, ie->data.dev_class, 3);
78 }
79
Marcel Holtmanna8746412008-07-14 20:13:46 +020080 cp.pkt_type = cpu_to_le16(conn->pkt_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 if (lmp_rswitch_capable(hdev) && !(hdev->link_mode & HCI_LM_MASTER))
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +020082 cp.role_switch = 0x01;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 else
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +020084 cp.role_switch = 0x00;
Marcel Holtmann4c67bc72006-10-15 17:30:56 +020085
Marcel Holtmanna9de9242007-10-20 13:33:56 +020086 hci_send_cmd(hdev, HCI_OP_CREATE_CONN, sizeof(cp), &cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087}
88
Marcel Holtmann6ac59342006-09-26 09:43:48 +020089static void hci_acl_connect_cancel(struct hci_conn *conn)
90{
91 struct hci_cp_create_conn_cancel cp;
92
93 BT_DBG("%p", conn);
94
95 if (conn->hdev->hci_ver < 2)
96 return;
97
98 bacpy(&cp.bdaddr, &conn->dst);
Marcel Holtmanna9de9242007-10-20 13:33:56 +020099 hci_send_cmd(conn->hdev, HCI_OP_CREATE_CONN_CANCEL, sizeof(cp), &cp);
Marcel Holtmann6ac59342006-09-26 09:43:48 +0200100}
101
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102void hci_acl_disconn(struct hci_conn *conn, __u8 reason)
103{
104 struct hci_cp_disconnect cp;
105
106 BT_DBG("%p", conn);
107
108 conn->state = BT_DISCONN;
109
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -0700110 cp.handle = cpu_to_le16(conn->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 cp.reason = reason;
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200112 hci_send_cmd(conn->hdev, HCI_OP_DISCONNECT, sizeof(cp), &cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113}
114
115void hci_add_sco(struct hci_conn *conn, __u16 handle)
116{
117 struct hci_dev *hdev = conn->hdev;
118 struct hci_cp_add_sco cp;
119
120 BT_DBG("%p", conn);
121
122 conn->state = BT_CONNECT;
123 conn->out = 1;
124
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -0700125 cp.handle = cpu_to_le16(handle);
Marcel Holtmanna8746412008-07-14 20:13:46 +0200126 cp.pkt_type = cpu_to_le16(conn->pkt_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200128 hci_send_cmd(hdev, HCI_OP_ADD_SCO, sizeof(cp), &cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129}
130
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200131void hci_setup_sync(struct hci_conn *conn, __u16 handle)
132{
133 struct hci_dev *hdev = conn->hdev;
134 struct hci_cp_setup_sync_conn cp;
135
136 BT_DBG("%p", conn);
137
138 conn->state = BT_CONNECT;
139 conn->out = 1;
140
141 cp.handle = cpu_to_le16(handle);
Marcel Holtmanna8746412008-07-14 20:13:46 +0200142 cp.pkt_type = cpu_to_le16(conn->pkt_type);
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200143
144 cp.tx_bandwidth = cpu_to_le32(0x00001f40);
145 cp.rx_bandwidth = cpu_to_le32(0x00001f40);
146 cp.max_latency = cpu_to_le16(0xffff);
147 cp.voice_setting = cpu_to_le16(hdev->voice_setting);
148 cp.retrans_effort = 0xff;
149
150 hci_send_cmd(hdev, HCI_OP_SETUP_SYNC_CONN, sizeof(cp), &cp);
151}
152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153static void hci_conn_timeout(unsigned long arg)
154{
Marcel Holtmann04837f62006-07-03 10:02:33 +0200155 struct hci_conn *conn = (void *) arg;
156 struct hci_dev *hdev = conn->hdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
158 BT_DBG("conn %p state %d", conn, conn->state);
159
160 if (atomic_read(&conn->refcnt))
161 return;
162
163 hci_dev_lock(hdev);
Marcel Holtmann6ac59342006-09-26 09:43:48 +0200164
165 switch (conn->state) {
166 case BT_CONNECT:
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200167 if (conn->type == ACL_LINK)
168 hci_acl_connect_cancel(conn);
169 else
170 hci_acl_disconn(conn, 0x13);
Marcel Holtmann6ac59342006-09-26 09:43:48 +0200171 break;
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900172 case BT_CONNECTED:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 hci_acl_disconn(conn, 0x13);
Marcel Holtmann6ac59342006-09-26 09:43:48 +0200174 break;
175 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 conn->state = BT_CLOSED;
Marcel Holtmann6ac59342006-09-26 09:43:48 +0200177 break;
178 }
179
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181}
182
Marcel Holtmann04837f62006-07-03 10:02:33 +0200183static void hci_conn_idle(unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184{
Marcel Holtmann04837f62006-07-03 10:02:33 +0200185 struct hci_conn *conn = (void *) arg;
186
187 BT_DBG("conn %p mode %d", conn, conn->mode);
188
189 hci_conn_enter_sniff_mode(conn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190}
191
192struct hci_conn *hci_conn_add(struct hci_dev *hdev, int type, bdaddr_t *dst)
193{
194 struct hci_conn *conn;
195
196 BT_DBG("%s dst %s", hdev->name, batostr(dst));
197
Marcel Holtmann04837f62006-07-03 10:02:33 +0200198 conn = kzalloc(sizeof(struct hci_conn), GFP_ATOMIC);
199 if (!conn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
202 bacpy(&conn->dst, dst);
Marcel Holtmanna8746412008-07-14 20:13:46 +0200203 conn->hdev = hdev;
204 conn->type = type;
205 conn->mode = HCI_CM_ACTIVE;
206 conn->state = BT_OPEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Marcel Holtmann04837f62006-07-03 10:02:33 +0200208 conn->power_save = 1;
209
Marcel Holtmanna8746412008-07-14 20:13:46 +0200210 switch (type) {
211 case ACL_LINK:
212 conn->pkt_type = hdev->pkt_type & ACL_PTYPE_MASK;
213 break;
214 case SCO_LINK:
215 if (lmp_esco_capable(hdev))
216 conn->pkt_type = hdev->esco_type & SCO_ESCO_MASK;
217 else
218 conn->pkt_type = hdev->pkt_type & SCO_PTYPE_MASK;
219 break;
220 case ESCO_LINK:
221 conn->pkt_type = hdev->esco_type;
222 break;
223 }
224
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 skb_queue_head_init(&conn->data_q);
Marcel Holtmann04837f62006-07-03 10:02:33 +0200226
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800227 setup_timer(&conn->disc_timer, hci_conn_timeout, (unsigned long)conn);
228 setup_timer(&conn->idle_timer, hci_conn_idle, (unsigned long)conn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
230 atomic_set(&conn->refcnt, 0);
231
232 hci_dev_hold(hdev);
233
234 tasklet_disable(&hdev->tx_task);
235
236 hci_conn_hash_add(hdev, conn);
237 if (hdev->notify)
238 hdev->notify(hdev, HCI_NOTIFY_CONN_ADD);
239
Marcel Holtmannb219e3a2006-07-06 12:38:46 +0200240 hci_conn_add_sysfs(conn);
241
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 tasklet_enable(&hdev->tx_task);
243
244 return conn;
245}
246
247int hci_conn_del(struct hci_conn *conn)
248{
249 struct hci_dev *hdev = conn->hdev;
250
251 BT_DBG("%s conn %p handle %d", hdev->name, conn, conn->handle);
252
Marcel Holtmann04837f62006-07-03 10:02:33 +0200253 del_timer(&conn->idle_timer);
254
255 del_timer(&conn->disc_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200257 if (conn->type == ACL_LINK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 struct hci_conn *sco = conn->link;
259 if (sco)
260 sco->link = NULL;
261
262 /* Unacked frames */
263 hdev->acl_cnt += conn->sent;
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200264 } else {
265 struct hci_conn *acl = conn->link;
266 if (acl) {
267 acl->link = NULL;
268 hci_conn_put(acl);
269 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 }
271
272 tasklet_disable(&hdev->tx_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 hci_conn_hash_del(hdev, conn);
274 if (hdev->notify)
275 hdev->notify(hdev, HCI_NOTIFY_CONN_DEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 tasklet_enable(&hdev->tx_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 skb_queue_purge(&conn->data_q);
Dave Young38b7da02007-12-29 19:17:47 -0800278 hci_conn_del_sysfs(conn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 return 0;
281}
282
283struct hci_dev *hci_get_route(bdaddr_t *dst, bdaddr_t *src)
284{
285 int use_src = bacmp(src, BDADDR_ANY);
286 struct hci_dev *hdev = NULL;
287 struct list_head *p;
288
289 BT_DBG("%s -> %s", batostr(src), batostr(dst));
290
291 read_lock_bh(&hci_dev_list_lock);
292
293 list_for_each(p, &hci_dev_list) {
294 struct hci_dev *d = list_entry(p, struct hci_dev, list);
295
296 if (!test_bit(HCI_UP, &d->flags) || test_bit(HCI_RAW, &d->flags))
297 continue;
298
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900299 /* Simple routing:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 * No source address - find interface with bdaddr != dst
301 * Source address - find interface with bdaddr == src
302 */
303
304 if (use_src) {
305 if (!bacmp(&d->bdaddr, src)) {
306 hdev = d; break;
307 }
308 } else {
309 if (bacmp(&d->bdaddr, dst)) {
310 hdev = d; break;
311 }
312 }
313 }
314
315 if (hdev)
316 hdev = hci_dev_hold(hdev);
317
318 read_unlock_bh(&hci_dev_list_lock);
319 return hdev;
320}
321EXPORT_SYMBOL(hci_get_route);
322
323/* Create SCO or ACL connection.
324 * Device _must_ be locked */
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200325struct hci_conn *hci_connect(struct hci_dev *hdev, int type, bdaddr_t *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326{
327 struct hci_conn *acl;
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200328 struct hci_conn *sco;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
330 BT_DBG("%s dst %s", hdev->name, batostr(dst));
331
332 if (!(acl = hci_conn_hash_lookup_ba(hdev, ACL_LINK, dst))) {
333 if (!(acl = hci_conn_add(hdev, ACL_LINK, dst)))
334 return NULL;
335 }
336
337 hci_conn_hold(acl);
338
339 if (acl->state == BT_OPEN || acl->state == BT_CLOSED)
340 hci_acl_connect(acl);
341
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200342 if (type == ACL_LINK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 return acl;
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200344
345 if (!(sco = hci_conn_hash_lookup_ba(hdev, type, dst))) {
346 if (!(sco = hci_conn_add(hdev, type, dst))) {
347 hci_conn_put(acl);
348 return NULL;
349 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 }
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200351
352 acl->link = sco;
353 sco->link = acl;
354
355 hci_conn_hold(sco);
356
357 if (acl->state == BT_CONNECTED &&
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200358 (sco->state == BT_OPEN || sco->state == BT_CLOSED)) {
359 if (lmp_esco_capable(hdev))
360 hci_setup_sync(sco, acl->handle);
361 else
362 hci_add_sco(sco, acl->handle);
363 }
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200364
365 return sco;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366}
367EXPORT_SYMBOL(hci_connect);
368
369/* Authenticate remote device */
370int hci_conn_auth(struct hci_conn *conn)
371{
372 BT_DBG("conn %p", conn);
373
374 if (conn->link_mode & HCI_LM_AUTH)
375 return 1;
376
377 if (!test_and_set_bit(HCI_CONN_AUTH_PEND, &conn->pend)) {
378 struct hci_cp_auth_requested cp;
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -0700379 cp.handle = cpu_to_le16(conn->handle);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200380 hci_send_cmd(conn->hdev, HCI_OP_AUTH_REQUESTED, sizeof(cp), &cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 }
382 return 0;
383}
384EXPORT_SYMBOL(hci_conn_auth);
385
386/* Enable encryption */
387int hci_conn_encrypt(struct hci_conn *conn)
388{
389 BT_DBG("conn %p", conn);
390
391 if (conn->link_mode & HCI_LM_ENCRYPT)
392 return 1;
393
394 if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &conn->pend))
395 return 0;
396
397 if (hci_conn_auth(conn)) {
398 struct hci_cp_set_conn_encrypt cp;
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -0700399 cp.handle = cpu_to_le16(conn->handle);
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900400 cp.encrypt = 1;
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200401 hci_send_cmd(conn->hdev, HCI_OP_SET_CONN_ENCRYPT, sizeof(cp), &cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 }
403 return 0;
404}
405EXPORT_SYMBOL(hci_conn_encrypt);
406
407/* Change link key */
408int hci_conn_change_link_key(struct hci_conn *conn)
409{
410 BT_DBG("conn %p", conn);
411
412 if (!test_and_set_bit(HCI_CONN_AUTH_PEND, &conn->pend)) {
413 struct hci_cp_change_conn_link_key cp;
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -0700414 cp.handle = cpu_to_le16(conn->handle);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200415 hci_send_cmd(conn->hdev, HCI_OP_CHANGE_CONN_LINK_KEY, sizeof(cp), &cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 }
417 return 0;
418}
419EXPORT_SYMBOL(hci_conn_change_link_key);
420
421/* Switch role */
422int hci_conn_switch_role(struct hci_conn *conn, uint8_t role)
423{
424 BT_DBG("conn %p", conn);
425
426 if (!role && conn->link_mode & HCI_LM_MASTER)
427 return 1;
428
429 if (!test_and_set_bit(HCI_CONN_RSWITCH_PEND, &conn->pend)) {
430 struct hci_cp_switch_role cp;
431 bacpy(&cp.bdaddr, &conn->dst);
432 cp.role = role;
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200433 hci_send_cmd(conn->hdev, HCI_OP_SWITCH_ROLE, sizeof(cp), &cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 }
435 return 0;
436}
437EXPORT_SYMBOL(hci_conn_switch_role);
438
Marcel Holtmann04837f62006-07-03 10:02:33 +0200439/* Enter active mode */
440void hci_conn_enter_active_mode(struct hci_conn *conn)
441{
442 struct hci_dev *hdev = conn->hdev;
443
444 BT_DBG("conn %p mode %d", conn, conn->mode);
445
446 if (test_bit(HCI_RAW, &hdev->flags))
447 return;
448
449 if (conn->mode != HCI_CM_SNIFF || !conn->power_save)
450 goto timer;
451
452 if (!test_and_set_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->pend)) {
453 struct hci_cp_exit_sniff_mode cp;
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -0700454 cp.handle = cpu_to_le16(conn->handle);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200455 hci_send_cmd(hdev, HCI_OP_EXIT_SNIFF_MODE, sizeof(cp), &cp);
Marcel Holtmann04837f62006-07-03 10:02:33 +0200456 }
457
458timer:
459 if (hdev->idle_timeout > 0)
460 mod_timer(&conn->idle_timer,
461 jiffies + msecs_to_jiffies(hdev->idle_timeout));
462}
463
464/* Enter sniff mode */
465void hci_conn_enter_sniff_mode(struct hci_conn *conn)
466{
467 struct hci_dev *hdev = conn->hdev;
468
469 BT_DBG("conn %p mode %d", conn, conn->mode);
470
471 if (test_bit(HCI_RAW, &hdev->flags))
472 return;
473
474 if (!lmp_sniff_capable(hdev) || !lmp_sniff_capable(conn))
475 return;
476
477 if (conn->mode != HCI_CM_ACTIVE || !(conn->link_policy & HCI_LP_SNIFF))
478 return;
479
480 if (lmp_sniffsubr_capable(hdev) && lmp_sniffsubr_capable(conn)) {
481 struct hci_cp_sniff_subrate cp;
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -0700482 cp.handle = cpu_to_le16(conn->handle);
483 cp.max_latency = cpu_to_le16(0);
484 cp.min_remote_timeout = cpu_to_le16(0);
485 cp.min_local_timeout = cpu_to_le16(0);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200486 hci_send_cmd(hdev, HCI_OP_SNIFF_SUBRATE, sizeof(cp), &cp);
Marcel Holtmann04837f62006-07-03 10:02:33 +0200487 }
488
489 if (!test_and_set_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->pend)) {
490 struct hci_cp_sniff_mode cp;
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -0700491 cp.handle = cpu_to_le16(conn->handle);
492 cp.max_interval = cpu_to_le16(hdev->sniff_max_interval);
493 cp.min_interval = cpu_to_le16(hdev->sniff_min_interval);
494 cp.attempt = cpu_to_le16(4);
495 cp.timeout = cpu_to_le16(1);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200496 hci_send_cmd(hdev, HCI_OP_SNIFF_MODE, sizeof(cp), &cp);
Marcel Holtmann04837f62006-07-03 10:02:33 +0200497 }
498}
499
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500/* Drop all connection on the device */
501void hci_conn_hash_flush(struct hci_dev *hdev)
502{
503 struct hci_conn_hash *h = &hdev->conn_hash;
504 struct list_head *p;
505
506 BT_DBG("hdev %s", hdev->name);
507
508 p = h->list.next;
509 while (p != &h->list) {
510 struct hci_conn *c;
511
512 c = list_entry(p, struct hci_conn, list);
513 p = p->next;
514
515 c->state = BT_CLOSED;
516
517 hci_proto_disconn_ind(c, 0x16);
518 hci_conn_del(c);
519 }
520}
521
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200522/* Check pending connect attempts */
523void hci_conn_check_pending(struct hci_dev *hdev)
524{
525 struct hci_conn *conn;
526
527 BT_DBG("hdev %s", hdev->name);
528
529 hci_dev_lock(hdev);
530
531 conn = hci_conn_hash_lookup_state(hdev, ACL_LINK, BT_CONNECT2);
532 if (conn)
533 hci_acl_connect(conn);
534
535 hci_dev_unlock(hdev);
536}
537
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538int hci_get_conn_list(void __user *arg)
539{
540 struct hci_conn_list_req req, *cl;
541 struct hci_conn_info *ci;
542 struct hci_dev *hdev;
543 struct list_head *p;
544 int n = 0, size, err;
545
546 if (copy_from_user(&req, arg, sizeof(req)))
547 return -EFAULT;
548
549 if (!req.conn_num || req.conn_num > (PAGE_SIZE * 2) / sizeof(*ci))
550 return -EINVAL;
551
552 size = sizeof(req) + req.conn_num * sizeof(*ci);
553
Jesper Juhl12fe2c52006-01-10 13:08:21 -0800554 if (!(cl = kmalloc(size, GFP_KERNEL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 return -ENOMEM;
556
557 if (!(hdev = hci_dev_get(req.dev_id))) {
558 kfree(cl);
559 return -ENODEV;
560 }
561
562 ci = cl->conn_info;
563
564 hci_dev_lock_bh(hdev);
565 list_for_each(p, &hdev->conn_hash.list) {
566 register struct hci_conn *c;
567 c = list_entry(p, struct hci_conn, list);
568
569 bacpy(&(ci + n)->bdaddr, &c->dst);
570 (ci + n)->handle = c->handle;
571 (ci + n)->type = c->type;
572 (ci + n)->out = c->out;
573 (ci + n)->state = c->state;
574 (ci + n)->link_mode = c->link_mode;
575 if (++n >= req.conn_num)
576 break;
577 }
578 hci_dev_unlock_bh(hdev);
579
580 cl->dev_id = hdev->id;
581 cl->conn_num = n;
582 size = sizeof(req) + n * sizeof(*ci);
583
584 hci_dev_put(hdev);
585
586 err = copy_to_user(arg, cl, size);
587 kfree(cl);
588
589 return err ? -EFAULT : 0;
590}
591
592int hci_get_conn_info(struct hci_dev *hdev, void __user *arg)
593{
594 struct hci_conn_info_req req;
595 struct hci_conn_info ci;
596 struct hci_conn *conn;
597 char __user *ptr = arg + sizeof(req);
598
599 if (copy_from_user(&req, arg, sizeof(req)))
600 return -EFAULT;
601
602 hci_dev_lock_bh(hdev);
603 conn = hci_conn_hash_lookup_ba(hdev, req.type, &req.bdaddr);
604 if (conn) {
605 bacpy(&ci.bdaddr, &conn->dst);
606 ci.handle = conn->handle;
607 ci.type = conn->type;
608 ci.out = conn->out;
609 ci.state = conn->state;
610 ci.link_mode = conn->link_mode;
611 }
612 hci_dev_unlock_bh(hdev);
613
614 if (!conn)
615 return -ENOENT;
616
617 return copy_to_user(ptr, &ci, sizeof(ci)) ? -EFAULT : 0;
618}