2 BlueZ - Bluetooth protocol stack for Linux
3 Copyright (C) 2000-2001 Qualcomm Incorporated
5 Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
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;
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.
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.
26 * HCI Connection handling.
28 * $Id: hci_conn.c,v 1.1.1.1 2005/04/11 02:51:12 jack Exp $
31 #include <linux/config.h>
32 #include <linux/module.h>
34 #include <linux/types.h>
35 #include <linux/errno.h>
36 #include <linux/kernel.h>
37 #include <linux/major.h>
38 #include <linux/sched.h>
39 #include <linux/slab.h>
40 #include <linux/poll.h>
41 #include <linux/fcntl.h>
42 #include <linux/init.h>
43 #include <linux/skbuff.h>
44 #include <linux/interrupt.h>
45 #include <linux/notifier.h>
48 #include <asm/system.h>
49 #include <asm/uaccess.h>
50 #include <asm/unaligned.h>
52 #include <net/bluetooth/bluetooth.h>
53 #include <net/bluetooth/hci_core.h>
55 #ifndef HCI_CORE_DEBUG
57 #define BT_DBG( A... )
60 void hci_acl_connect(struct hci_conn *conn)
62 struct hci_dev *hdev = conn->hdev;
63 struct inquiry_entry *ie;
68 conn->state = BT_CONNECT;
70 conn->link_mode = HCI_LM_MASTER;
72 memset(&cp, 0, sizeof(cp));
73 bacpy(&cp.bdaddr, &conn->dst);
75 if ((ie = inquiry_cache_lookup(hdev, &conn->dst)) &&
76 inquiry_entry_age(ie) <= INQUIRY_ENTRY_AGE_MAX) {
77 cp.pscan_rep_mode = ie->info.pscan_rep_mode;
78 cp.pscan_mode = ie->info.pscan_mode;
79 cp.clock_offset = ie->info.clock_offset | __cpu_to_le16(0x8000);
82 cp.pkt_type = __cpu_to_le16(hdev->pkt_type & ACL_PTYPE_MASK);
83 if (lmp_rswitch_capable(hdev) && !(hdev->link_mode & HCI_LM_MASTER))
84 cp.role_switch = 0x01;
86 cp.role_switch = 0x00;
88 hci_send_cmd(hdev, OGF_LINK_CTL, OCF_CREATE_CONN,
89 CREATE_CONN_CP_SIZE, &cp);
92 void hci_acl_disconn(struct hci_conn *conn, __u8 reason)
98 conn->state = BT_DISCONN;
100 cp.handle = __cpu_to_le16(conn->handle);
102 hci_send_cmd(conn->hdev, OGF_LINK_CTL, OCF_DISCONNECT,
103 DISCONNECT_CP_SIZE, &cp);
106 void hci_add_sco(struct hci_conn *conn, __u16 handle)
108 struct hci_dev *hdev = conn->hdev;
113 conn->state = BT_CONNECT;
116 cp.pkt_type = __cpu_to_le16(hdev->pkt_type & SCO_PTYPE_MASK);
117 cp.handle = __cpu_to_le16(handle);
119 hci_send_cmd(hdev, OGF_LINK_CTL, OCF_ADD_SCO, ADD_SCO_CP_SIZE, &cp);
122 static void hci_conn_timeout(unsigned long arg)
124 struct hci_conn *conn = (void *)arg;
125 struct hci_dev *hdev = conn->hdev;
127 BT_DBG("conn %p state %d", conn, conn->state);
129 if (atomic_read(&conn->refcnt))
133 if (conn->state == BT_CONNECTED)
134 hci_acl_disconn(conn, 0x13);
136 conn->state = BT_CLOSED;
137 hci_dev_unlock(hdev);
141 static void hci_conn_init_timer(struct hci_conn *conn)
143 init_timer(&conn->timer);
144 conn->timer.function = hci_conn_timeout;
145 conn->timer.data = (unsigned long)conn;
148 struct hci_conn *hci_conn_add(struct hci_dev *hdev, int type, bdaddr_t *dst)
150 struct hci_conn *conn;
152 BT_DBG("%s dst %s", hdev->name, batostr(dst));
154 if (!(conn = kmalloc(sizeof(struct hci_conn), GFP_ATOMIC)))
156 memset(conn, 0, sizeof(struct hci_conn));
158 bacpy(&conn->dst, dst);
161 conn->state = BT_OPEN;
163 skb_queue_head_init(&conn->data_q);
164 hci_conn_init_timer(conn);
166 atomic_set(&conn->refcnt, 0);
170 tasklet_disable(&hdev->tx_task);
171 conn_hash_add(hdev, conn);
172 tasklet_enable(&hdev->tx_task);
177 int hci_conn_del(struct hci_conn *conn)
179 struct hci_dev *hdev = conn->hdev;
181 BT_DBG("%s conn %p handle %d", hdev->name, conn, conn->handle);
183 hci_conn_del_timer(conn);
185 if (conn->type == SCO_LINK) {
186 struct hci_conn *acl = conn->link;
192 struct hci_conn *sco = conn->link;
197 hdev->acl_cnt += conn->sent;
200 tasklet_disable(&hdev->tx_task);
201 conn_hash_del(hdev, conn);
202 tasklet_enable(&hdev->tx_task);
204 skb_queue_purge(&conn->data_q);
212 struct hci_dev *hci_get_route(bdaddr_t *dst, bdaddr_t *src)
214 int use_src = bacmp(src, BDADDR_ANY);
215 struct hci_dev *hdev = NULL;
218 BT_DBG("%s -> %s", batostr(src), batostr(dst));
220 read_lock_bh(&hdev_list_lock);
222 list_for_each(p, &hdev_list) {
224 d = list_entry(p, struct hci_dev, list);
226 if (!test_bit(HCI_UP, &d->flags))
230 * No source address - find interface with bdaddr != dst
231 * Source address - find interface with bdaddr == src
235 if (!bacmp(&d->bdaddr, src)) {
239 if (bacmp(&d->bdaddr, dst)) {
248 read_unlock_bh(&hdev_list_lock);
252 /* Create SCO or ACL connection.
253 * Device _must_ be locked */
254 struct hci_conn * hci_connect(struct hci_dev *hdev, int type, bdaddr_t *dst)
256 struct hci_conn *acl;
258 BT_DBG("%s dst %s", hdev->name, batostr(dst));
260 if (!(acl = conn_hash_lookup_ba(hdev, ACL_LINK, dst))) {
261 if (!(acl = hci_conn_add(hdev, ACL_LINK, dst)))
267 if (acl->state == BT_OPEN || acl->state == BT_CLOSED)
268 hci_acl_connect(acl);
270 if (type == SCO_LINK) {
271 struct hci_conn *sco;
273 if (!(sco = conn_hash_lookup_ba(hdev, SCO_LINK, dst))) {
274 if (!(sco = hci_conn_add(hdev, SCO_LINK, dst))) {
284 if (acl->state == BT_CONNECTED &&
285 (sco->state == BT_OPEN || sco->state == BT_CLOSED))
286 hci_add_sco(sco, acl->handle);
294 /* Authenticate remote device */
295 int hci_conn_auth(struct hci_conn *conn)
297 BT_DBG("conn %p", conn);
299 if (conn->link_mode & HCI_LM_AUTH)
302 if (!test_and_set_bit(HCI_CONN_AUTH_PEND, &conn->pend)) {
303 auth_requested_cp ar;
304 ar.handle = __cpu_to_le16(conn->handle);
305 hci_send_cmd(conn->hdev, OGF_LINK_CTL, OCF_AUTH_REQUESTED,
306 AUTH_REQUESTED_CP_SIZE, &ar);
311 /* Enable encryption */
312 int hci_conn_encrypt(struct hci_conn *conn)
314 BT_DBG("conn %p", conn);
316 if (conn->link_mode & HCI_LM_ENCRYPT)
319 if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &conn->pend))
322 if (hci_conn_auth(conn)) {
323 set_conn_encrypt_cp ce;
324 ce.handle = __cpu_to_le16(conn->handle);
326 hci_send_cmd(conn->hdev, OGF_LINK_CTL, OCF_SET_CONN_ENCRYPT,
327 SET_CONN_ENCRYPT_CP_SIZE, &ce);
332 /* Drop all connection on the device */
333 void hci_conn_hash_flush(struct hci_dev *hdev)
335 struct conn_hash *h = &hdev->conn_hash;
338 BT_DBG("hdev %s", hdev->name);
341 while (p != &h->list) {
344 c = list_entry(p, struct hci_conn, list);
347 c->state = BT_CLOSED;
349 hci_proto_disconn_ind(c, 0x16);
354 int hci_get_conn_list(unsigned long arg)
356 struct hci_conn_list_req req, *cl;
357 struct hci_conn_info *ci;
358 struct hci_dev *hdev;
362 if (copy_from_user(&req, (void *) arg, sizeof(req)))
365 if (!(hdev = hci_dev_get(req.dev_id)))
368 size = req.conn_num * sizeof(struct hci_conn_info) + sizeof(req);
370 if (verify_area(VERIFY_WRITE, (void *)arg, size))
373 if (!(cl = (void *) kmalloc(size, GFP_KERNEL)))
377 hci_dev_lock_bh(hdev);
378 list_for_each(p, &hdev->conn_hash.list) {
379 register struct hci_conn *c;
380 c = list_entry(p, struct hci_conn, list);
382 bacpy(&(ci + n)->bdaddr, &c->dst);
383 (ci + n)->handle = c->handle;
384 (ci + n)->type = c->type;
385 (ci + n)->out = c->out;
386 (ci + n)->state = c->state;
387 (ci + n)->link_mode = c->link_mode;
390 hci_dev_unlock_bh(hdev);
392 cl->dev_id = hdev->id;
394 size = n * sizeof(struct hci_conn_info) + sizeof(req);
398 copy_to_user((void *) arg, cl, size);
404 int hci_get_conn_info(struct hci_dev *hdev, unsigned long arg)
406 struct hci_conn_info_req req;
407 struct hci_conn_info ci;
408 struct hci_conn *conn;
409 char *ptr = (void *) arg + sizeof(req);
411 if (copy_from_user(&req, (void *) arg, sizeof(req)))
414 if (verify_area(VERIFY_WRITE, ptr, sizeof(ci)))
417 hci_dev_lock_bh(hdev);
418 conn = conn_hash_lookup_ba(hdev, req.type, &req.bdaddr);
420 bacpy(&ci.bdaddr, &conn->dst);
421 ci.handle = conn->handle;
422 ci.type = conn->type;
424 ci.state = conn->state;
425 ci.link_mode = conn->link_mode;
427 hci_dev_unlock_bh(hdev);
432 copy_to_user(ptr, &ci, sizeof(ci));