[Bluetooth] Track connection packet type changes
[pandora-kernel.git] / net / bluetooth / hci_conn.c
1 /*
2    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
15    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
18    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19
20    ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
21    COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
22    SOFTWARE IS DISCLAIMED.
23 */
24
25 /* Bluetooth HCI connection handling. */
26
27 #include <linux/module.h>
28
29 #include <linux/types.h>
30 #include <linux/errno.h>
31 #include <linux/kernel.h>
32 #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
53 void hci_acl_connect(struct hci_conn *conn)
54 {
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;
62         conn->out = 1;
63
64         conn->link_mode = HCI_LM_MASTER;
65
66         conn->attempt++;
67
68         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;
76                 cp.clock_offset   = ie->data.clock_offset | cpu_to_le16(0x8000);
77                 memcpy(conn->dev_class, ie->data.dev_class, 3);
78         }
79
80         cp.pkt_type = cpu_to_le16(conn->pkt_type);
81         if (lmp_rswitch_capable(hdev) && !(hdev->link_mode & HCI_LM_MASTER))
82                 cp.role_switch = 0x01;
83         else
84                 cp.role_switch = 0x00;
85
86         hci_send_cmd(hdev, HCI_OP_CREATE_CONN, sizeof(cp), &cp);
87 }
88
89 static 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);
99         hci_send_cmd(conn->hdev, HCI_OP_CREATE_CONN_CANCEL, sizeof(cp), &cp);
100 }
101
102 void 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
110         cp.handle = cpu_to_le16(conn->handle);
111         cp.reason = reason;
112         hci_send_cmd(conn->hdev, HCI_OP_DISCONNECT, sizeof(cp), &cp);
113 }
114
115 void 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
125         cp.handle   = cpu_to_le16(handle);
126         cp.pkt_type = cpu_to_le16(conn->pkt_type);
127
128         hci_send_cmd(hdev, HCI_OP_ADD_SCO, sizeof(cp), &cp);
129 }
130
131 void 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);
142         cp.pkt_type = cpu_to_le16(conn->pkt_type);
143
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
153 static void hci_conn_timeout(unsigned long arg)
154 {
155         struct hci_conn *conn = (void *) arg;
156         struct hci_dev *hdev = conn->hdev;
157
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);
164
165         switch (conn->state) {
166         case BT_CONNECT:
167                 if (conn->type == ACL_LINK)
168                         hci_acl_connect_cancel(conn);
169                 else
170                         hci_acl_disconn(conn, 0x13);
171                 break;
172         case BT_CONNECTED:
173                 hci_acl_disconn(conn, 0x13);
174                 break;
175         default:
176                 conn->state = BT_CLOSED;
177                 break;
178         }
179
180         hci_dev_unlock(hdev);
181 }
182
183 static void hci_conn_idle(unsigned long arg)
184 {
185         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);
190 }
191
192 struct 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
198         conn = kzalloc(sizeof(struct hci_conn), GFP_ATOMIC);
199         if (!conn)
200                 return NULL;
201
202         bacpy(&conn->dst, dst);
203         conn->hdev  = hdev;
204         conn->type  = type;
205         conn->mode  = HCI_CM_ACTIVE;
206         conn->state = BT_OPEN;
207
208         conn->power_save = 1;
209
210         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
225         skb_queue_head_init(&conn->data_q);
226
227         setup_timer(&conn->disc_timer, hci_conn_timeout, (unsigned long)conn);
228         setup_timer(&conn->idle_timer, hci_conn_idle, (unsigned long)conn);
229
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
240         hci_conn_add_sysfs(conn);
241
242         tasklet_enable(&hdev->tx_task);
243
244         return conn;
245 }
246
247 int 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
253         del_timer(&conn->idle_timer);
254
255         del_timer(&conn->disc_timer);
256
257         if (conn->type == ACL_LINK) {
258                 struct hci_conn *sco = conn->link;
259                 if (sco)
260                         sco->link = NULL;
261
262                 /* Unacked frames */
263                 hdev->acl_cnt += conn->sent;
264         } else {
265                 struct hci_conn *acl = conn->link;
266                 if (acl) {
267                         acl->link = NULL;
268                         hci_conn_put(acl);
269                 }
270         }
271
272         tasklet_disable(&hdev->tx_task);
273         hci_conn_hash_del(hdev, conn);
274         if (hdev->notify)
275                 hdev->notify(hdev, HCI_NOTIFY_CONN_DEL);
276         tasklet_enable(&hdev->tx_task);
277         skb_queue_purge(&conn->data_q);
278         hci_conn_del_sysfs(conn);
279
280         return 0;
281 }
282
283 struct 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
299                 /* Simple routing:
300                  *   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 }
321 EXPORT_SYMBOL(hci_get_route);
322
323 /* Create SCO or ACL connection.
324  * Device _must_ be locked */
325 struct hci_conn *hci_connect(struct hci_dev *hdev, int type, bdaddr_t *dst)
326 {
327         struct hci_conn *acl;
328         struct hci_conn *sco;
329
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
342         if (type == ACL_LINK)
343                 return acl;
344
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                 }
350         }
351
352         acl->link = sco;
353         sco->link = acl;
354
355         hci_conn_hold(sco);
356
357         if (acl->state == BT_CONNECTED &&
358                         (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         }
364
365         return sco;
366 }
367 EXPORT_SYMBOL(hci_connect);
368
369 /* Authenticate remote device */
370 int 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;
379                 cp.handle = cpu_to_le16(conn->handle);
380                 hci_send_cmd(conn->hdev, HCI_OP_AUTH_REQUESTED, sizeof(cp), &cp);
381         }
382         return 0;
383 }
384 EXPORT_SYMBOL(hci_conn_auth);
385
386 /* Enable encryption */
387 int 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;
399                 cp.handle  = cpu_to_le16(conn->handle);
400                 cp.encrypt = 1;
401                 hci_send_cmd(conn->hdev, HCI_OP_SET_CONN_ENCRYPT, sizeof(cp), &cp);
402         }
403         return 0;
404 }
405 EXPORT_SYMBOL(hci_conn_encrypt);
406
407 /* Change link key */
408 int 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;
414                 cp.handle = cpu_to_le16(conn->handle);
415                 hci_send_cmd(conn->hdev, HCI_OP_CHANGE_CONN_LINK_KEY, sizeof(cp), &cp);
416         }
417         return 0;
418 }
419 EXPORT_SYMBOL(hci_conn_change_link_key);
420
421 /* Switch role */
422 int 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;
433                 hci_send_cmd(conn->hdev, HCI_OP_SWITCH_ROLE, sizeof(cp), &cp);
434         }
435         return 0;
436 }
437 EXPORT_SYMBOL(hci_conn_switch_role);
438
439 /* Enter active mode */
440 void 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;
454                 cp.handle = cpu_to_le16(conn->handle);
455                 hci_send_cmd(hdev, HCI_OP_EXIT_SNIFF_MODE, sizeof(cp), &cp);
456         }
457
458 timer:
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 */
465 void 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;
482                 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);
486                 hci_send_cmd(hdev, HCI_OP_SNIFF_SUBRATE, sizeof(cp), &cp);
487         }
488
489         if (!test_and_set_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->pend)) {
490                 struct hci_cp_sniff_mode cp;
491                 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);
496                 hci_send_cmd(hdev, HCI_OP_SNIFF_MODE, sizeof(cp), &cp);
497         }
498 }
499
500 /* Drop all connection on the device */
501 void 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
522 /* Check pending connect attempts */
523 void 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
538 int 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
554         if (!(cl = kmalloc(size, GFP_KERNEL)))
555                 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
592 int 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 }