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.5 2002/07/17 18:46:25 maxk 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);
74 cp.pscan_rep_mode = 0x02;
76 if ((ie = inquiry_cache_lookup(hdev, &conn->dst)) &&
77 inquiry_entry_age(ie) <= INQUIRY_ENTRY_AGE_MAX) {
78 cp.pscan_rep_mode = ie->info.pscan_rep_mode;
79 cp.pscan_mode = ie->info.pscan_mode;
80 cp.clock_offset = ie->info.clock_offset | __cpu_to_le16(0x8000);
83 cp.pkt_type = __cpu_to_le16(hdev->pkt_type & ACL_PTYPE_MASK);
84 if (lmp_rswitch_capable(hdev) && !(hdev->link_mode & HCI_LM_MASTER))
85 cp.role_switch = 0x01;
87 cp.role_switch = 0x00;
89 hci_send_cmd(hdev, OGF_LINK_CTL, OCF_CREATE_CONN,
90 CREATE_CONN_CP_SIZE, &cp);
93 void hci_acl_disconn(struct hci_conn *conn, __u8 reason)
99 conn->state = BT_DISCONN;
101 cp.handle = __cpu_to_le16(conn->handle);
103 hci_send_cmd(conn->hdev, OGF_LINK_CTL, OCF_DISCONNECT,
104 DISCONNECT_CP_SIZE, &cp);
107 void hci_add_sco(struct hci_conn *conn, __u16 handle)
109 struct hci_dev *hdev = conn->hdev;
114 conn->state = BT_CONNECT;
117 cp.pkt_type = __cpu_to_le16(hdev->pkt_type & SCO_PTYPE_MASK);
118 cp.handle = __cpu_to_le16(handle);
120 hci_send_cmd(hdev, OGF_LINK_CTL, OCF_ADD_SCO, ADD_SCO_CP_SIZE, &cp);
123 static void hci_conn_timeout(unsigned long arg)
125 struct hci_conn *conn = (void *)arg;
126 struct hci_dev *hdev = conn->hdev;
128 BT_DBG("conn %p state %d", conn, conn->state);
130 if (atomic_read(&conn->refcnt))
134 if (conn->state == BT_CONNECTED)
135 hci_acl_disconn(conn, 0x13);
137 conn->state = BT_CLOSED;
138 hci_dev_unlock(hdev);
142 static void hci_conn_init_timer(struct hci_conn *conn)
144 init_timer(&conn->timer);
145 conn->timer.function = hci_conn_timeout;
146 conn->timer.data = (unsigned long)conn;
149 struct hci_conn *hci_conn_add(struct hci_dev *hdev, int type, bdaddr_t *dst)
151 struct hci_conn *conn;
153 BT_DBG("%s dst %s", hdev->name, batostr(dst));
155 if (!(conn = kmalloc(sizeof(struct hci_conn), GFP_ATOMIC)))
157 memset(conn, 0, sizeof(struct hci_conn));
159 bacpy(&conn->dst, dst);
162 conn->state = BT_OPEN;
164 skb_queue_head_init(&conn->data_q);
165 hci_conn_init_timer(conn);
167 atomic_set(&conn->refcnt, 0);
171 tasklet_disable(&hdev->tx_task);
172 conn_hash_add(hdev, conn);
173 tasklet_enable(&hdev->tx_task);
178 int hci_conn_del(struct hci_conn *conn)
180 struct hci_dev *hdev = conn->hdev;
182 BT_DBG("%s conn %p handle %d", hdev->name, conn, conn->handle);
184 hci_conn_del_timer(conn);
186 if (conn->type == SCO_LINK) {
187 struct hci_conn *acl = conn->link;
193 struct hci_conn *sco = conn->link;
198 hdev->acl_cnt += conn->sent;
201 tasklet_disable(&hdev->tx_task);
202 conn_hash_del(hdev, conn);
203 tasklet_enable(&hdev->tx_task);
205 skb_queue_purge(&conn->data_q);
213 struct hci_dev *hci_get_route(bdaddr_t *dst, bdaddr_t *src)
215 int use_src = bacmp(src, BDADDR_ANY);
216 struct hci_dev *hdev = NULL;
219 BT_DBG("%s -> %s", batostr(src), batostr(dst));
221 read_lock_bh(&hdev_list_lock);
223 list_for_each(p, &hdev_list) {
225 d = list_entry(p, struct hci_dev, list);
227 if (!test_bit(HCI_UP, &d->flags))
231 * No source address - find interface with bdaddr != dst
232 * Source address - find interface with bdaddr == src
236 if (!bacmp(&d->bdaddr, src)) {
240 if (bacmp(&d->bdaddr, dst)) {
249 read_unlock_bh(&hdev_list_lock);
253 /* Create SCO or ACL connection.
254 * Device _must_ be locked */
255 struct hci_conn * hci_connect(struct hci_dev *hdev, int type, bdaddr_t *dst)
257 struct hci_conn *acl;
259 BT_DBG("%s dst %s", hdev->name, batostr(dst));
261 if (!(acl = conn_hash_lookup_ba(hdev, ACL_LINK, dst))) {
262 if (!(acl = hci_conn_add(hdev, ACL_LINK, dst)))
268 if (acl->state == BT_OPEN || acl->state == BT_CLOSED)
269 hci_acl_connect(acl);
271 if (type == SCO_LINK) {
272 struct hci_conn *sco;
274 if (!(sco = conn_hash_lookup_ba(hdev, SCO_LINK, dst))) {
275 if (!(sco = hci_conn_add(hdev, SCO_LINK, dst))) {
285 if (acl->state == BT_CONNECTED &&
286 (sco->state == BT_OPEN || sco->state == BT_CLOSED))
287 hci_add_sco(sco, acl->handle);
295 /* Authenticate remote device */
296 int hci_conn_auth(struct hci_conn *conn)
298 BT_DBG("conn %p", conn);
300 if (conn->link_mode & HCI_LM_AUTH)
303 if (!test_and_set_bit(HCI_CONN_AUTH_PEND, &conn->pend)) {
304 auth_requested_cp ar;
305 ar.handle = __cpu_to_le16(conn->handle);
306 hci_send_cmd(conn->hdev, OGF_LINK_CTL, OCF_AUTH_REQUESTED,
307 AUTH_REQUESTED_CP_SIZE, &ar);
312 /* Enable encryption */
313 int hci_conn_encrypt(struct hci_conn *conn)
315 BT_DBG("conn %p", conn);
317 if (conn->link_mode & HCI_LM_ENCRYPT)
320 if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &conn->pend))
323 if (hci_conn_auth(conn)) {
324 set_conn_encrypt_cp ce;
325 ce.handle = __cpu_to_le16(conn->handle);
327 hci_send_cmd(conn->hdev, OGF_LINK_CTL, OCF_SET_CONN_ENCRYPT,
328 SET_CONN_ENCRYPT_CP_SIZE, &ce);
333 /* Drop all connection on the device */
334 void hci_conn_hash_flush(struct hci_dev *hdev)
336 struct conn_hash *h = &hdev->conn_hash;
339 BT_DBG("hdev %s", hdev->name);
342 while (p != &h->list) {
345 c = list_entry(p, struct hci_conn, list);
348 c->state = BT_CLOSED;
350 hci_proto_disconn_ind(c, 0x16);
355 int hci_get_conn_list(unsigned long arg)
357 struct hci_conn_list_req req, *cl;
358 struct hci_conn_info *ci;
359 struct hci_dev *hdev;
361 int n = 0, size, err;
363 if (copy_from_user(&req, (void *) arg, sizeof(req)))
366 if (!req.conn_num || req.conn_num > (PAGE_SIZE * 2) / sizeof(*ci))
369 size = sizeof(req) + req.conn_num * sizeof(*ci);
371 if (!(cl = (void *) kmalloc(size, GFP_KERNEL)))
374 if (!(hdev = hci_dev_get(req.dev_id))) {
381 hci_dev_lock_bh(hdev);
382 list_for_each(p, &hdev->conn_hash.list) {
383 register struct hci_conn *c;
384 c = list_entry(p, struct hci_conn, list);
386 bacpy(&(ci + n)->bdaddr, &c->dst);
387 (ci + n)->handle = c->handle;
388 (ci + n)->type = c->type;
389 (ci + n)->out = c->out;
390 (ci + n)->state = c->state;
391 (ci + n)->link_mode = c->link_mode;
392 if (++n >= req.conn_num)
395 hci_dev_unlock_bh(hdev);
397 cl->dev_id = hdev->id;
399 size = sizeof(req) + n * sizeof(*ci);
403 err = copy_to_user((void *) arg, cl, size);
406 return err ? -EFAULT : 0;
409 int hci_get_conn_info(struct hci_dev *hdev, unsigned long arg)
411 struct hci_conn_info_req req;
412 struct hci_conn_info ci;
413 struct hci_conn *conn;
414 char *ptr = (void *) arg + sizeof(req);
416 if (copy_from_user(&req, (void *) arg, sizeof(req)))
419 hci_dev_lock_bh(hdev);
420 conn = conn_hash_lookup_ba(hdev, req.type, &req.bdaddr);
422 bacpy(&ci.bdaddr, &conn->dst);
423 ci.handle = conn->handle;
424 ci.type = conn->type;
426 ci.state = conn->state;
427 ci.link_mode = conn->link_mode;
429 hci_dev_unlock_bh(hdev);
434 return copy_to_user(ptr, &ci, sizeof(ci)) ? -EFAULT : 0;