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.
25 /* Bluetooth HCI connection handling. */
27 #include <linux/config.h>
28 #include <linux/module.h>
30 #include <linux/types.h>
31 #include <linux/errno.h>
32 #include <linux/kernel.h>
33 #include <linux/sched.h>
34 #include <linux/slab.h>
35 #include <linux/poll.h>
36 #include <linux/fcntl.h>
37 #include <linux/init.h>
38 #include <linux/skbuff.h>
39 #include <linux/interrupt.h>
40 #include <linux/notifier.h>
43 #include <asm/system.h>
44 #include <asm/uaccess.h>
45 #include <asm/unaligned.h>
47 #include <net/bluetooth/bluetooth.h>
48 #include <net/bluetooth/hci_core.h>
50 #ifndef CONFIG_BT_HCI_CORE_DEBUG
55 static void hci_acl_connect(struct hci_conn
*conn
)
57 struct hci_dev
*hdev
= conn
->hdev
;
58 struct inquiry_entry
*ie
;
59 struct hci_cp_create_conn cp
;
63 conn
->state
= BT_CONNECT
;
65 conn
->link_mode
= HCI_LM_MASTER
;
67 memset(&cp
, 0, sizeof(cp
));
68 bacpy(&cp
.bdaddr
, &conn
->dst
);
69 cp
.pscan_rep_mode
= 0x02;
71 if ((ie
= hci_inquiry_cache_lookup(hdev
, &conn
->dst
)) &&
72 inquiry_entry_age(ie
) <= INQUIRY_ENTRY_AGE_MAX
) {
73 cp
.pscan_rep_mode
= ie
->data
.pscan_rep_mode
;
74 cp
.pscan_mode
= ie
->data
.pscan_mode
;
75 cp
.clock_offset
= ie
->data
.clock_offset
| __cpu_to_le16(0x8000);
76 memcpy(conn
->dev_class
, ie
->data
.dev_class
, 3);
79 cp
.pkt_type
= __cpu_to_le16(hdev
->pkt_type
& ACL_PTYPE_MASK
);
80 if (lmp_rswitch_capable(hdev
) && !(hdev
->link_mode
& HCI_LM_MASTER
))
81 cp
.role_switch
= 0x01;
83 cp
.role_switch
= 0x00;
85 hci_send_cmd(hdev
, OGF_LINK_CTL
, OCF_CREATE_CONN
, sizeof(cp
), &cp
);
88 void hci_acl_disconn(struct hci_conn
*conn
, __u8 reason
)
90 struct hci_cp_disconnect cp
;
94 conn
->state
= BT_DISCONN
;
96 cp
.handle
= __cpu_to_le16(conn
->handle
);
98 hci_send_cmd(conn
->hdev
, OGF_LINK_CTL
, OCF_DISCONNECT
, sizeof(cp
), &cp
);
101 void hci_add_sco(struct hci_conn
*conn
, __u16 handle
)
103 struct hci_dev
*hdev
= conn
->hdev
;
104 struct hci_cp_add_sco cp
;
108 conn
->state
= BT_CONNECT
;
111 cp
.pkt_type
= __cpu_to_le16(hdev
->pkt_type
& SCO_PTYPE_MASK
);
112 cp
.handle
= __cpu_to_le16(handle
);
114 hci_send_cmd(hdev
, OGF_LINK_CTL
, OCF_ADD_SCO
, sizeof(cp
), &cp
);
117 static void hci_conn_timeout(unsigned long arg
)
119 struct hci_conn
*conn
= (void *)arg
;
120 struct hci_dev
*hdev
= conn
->hdev
;
122 BT_DBG("conn %p state %d", conn
, conn
->state
);
124 if (atomic_read(&conn
->refcnt
))
128 if (conn
->state
== BT_CONNECTED
)
129 hci_acl_disconn(conn
, 0x13);
131 conn
->state
= BT_CLOSED
;
132 hci_dev_unlock(hdev
);
136 static void hci_conn_init_timer(struct hci_conn
*conn
)
138 init_timer(&conn
->timer
);
139 conn
->timer
.function
= hci_conn_timeout
;
140 conn
->timer
.data
= (unsigned long)conn
;
143 struct hci_conn
*hci_conn_add(struct hci_dev
*hdev
, int type
, bdaddr_t
*dst
)
145 struct hci_conn
*conn
;
147 BT_DBG("%s dst %s", hdev
->name
, batostr(dst
));
149 if (!(conn
= kmalloc(sizeof(struct hci_conn
), GFP_ATOMIC
)))
151 memset(conn
, 0, sizeof(struct hci_conn
));
153 bacpy(&conn
->dst
, dst
);
156 conn
->state
= BT_OPEN
;
158 skb_queue_head_init(&conn
->data_q
);
159 hci_conn_init_timer(conn
);
161 atomic_set(&conn
->refcnt
, 0);
165 tasklet_disable(&hdev
->tx_task
);
167 hci_conn_hash_add(hdev
, conn
);
169 hdev
->notify(hdev
, HCI_NOTIFY_CONN_ADD
);
171 tasklet_enable(&hdev
->tx_task
);
176 int hci_conn_del(struct hci_conn
*conn
)
178 struct hci_dev
*hdev
= conn
->hdev
;
180 BT_DBG("%s conn %p handle %d", hdev
->name
, conn
, conn
->handle
);
182 hci_conn_del_timer(conn
);
184 if (conn
->type
== SCO_LINK
) {
185 struct hci_conn
*acl
= conn
->link
;
191 struct hci_conn
*sco
= conn
->link
;
196 hdev
->acl_cnt
+= conn
->sent
;
199 tasklet_disable(&hdev
->tx_task
);
201 hci_conn_hash_del(hdev
, conn
);
203 hdev
->notify(hdev
, HCI_NOTIFY_CONN_DEL
);
205 tasklet_enable(&hdev
->tx_task
);
207 skb_queue_purge(&conn
->data_q
);
215 struct hci_dev
*hci_get_route(bdaddr_t
*dst
, bdaddr_t
*src
)
217 int use_src
= bacmp(src
, BDADDR_ANY
);
218 struct hci_dev
*hdev
= NULL
;
221 BT_DBG("%s -> %s", batostr(src
), batostr(dst
));
223 read_lock_bh(&hci_dev_list_lock
);
225 list_for_each(p
, &hci_dev_list
) {
226 struct hci_dev
*d
= list_entry(p
, struct hci_dev
, list
);
228 if (!test_bit(HCI_UP
, &d
->flags
) || test_bit(HCI_RAW
, &d
->flags
))
232 * No source address - find interface with bdaddr != dst
233 * Source address - find interface with bdaddr == src
237 if (!bacmp(&d
->bdaddr
, src
)) {
241 if (bacmp(&d
->bdaddr
, dst
)) {
248 hdev
= hci_dev_hold(hdev
);
250 read_unlock_bh(&hci_dev_list_lock
);
253 EXPORT_SYMBOL(hci_get_route
);
255 /* Create SCO or ACL connection.
256 * Device _must_ be locked */
257 struct hci_conn
* hci_connect(struct hci_dev
*hdev
, int type
, bdaddr_t
*dst
)
259 struct hci_conn
*acl
;
261 BT_DBG("%s dst %s", hdev
->name
, batostr(dst
));
263 if (!(acl
= hci_conn_hash_lookup_ba(hdev
, ACL_LINK
, dst
))) {
264 if (!(acl
= hci_conn_add(hdev
, ACL_LINK
, dst
)))
270 if (acl
->state
== BT_OPEN
|| acl
->state
== BT_CLOSED
)
271 hci_acl_connect(acl
);
273 if (type
== SCO_LINK
) {
274 struct hci_conn
*sco
;
276 if (!(sco
= hci_conn_hash_lookup_ba(hdev
, SCO_LINK
, dst
))) {
277 if (!(sco
= hci_conn_add(hdev
, SCO_LINK
, dst
))) {
287 if (acl
->state
== BT_CONNECTED
&&
288 (sco
->state
== BT_OPEN
|| sco
->state
== BT_CLOSED
))
289 hci_add_sco(sco
, acl
->handle
);
296 EXPORT_SYMBOL(hci_connect
);
298 /* Authenticate remote device */
299 int hci_conn_auth(struct hci_conn
*conn
)
301 BT_DBG("conn %p", conn
);
303 if (conn
->link_mode
& HCI_LM_AUTH
)
306 if (!test_and_set_bit(HCI_CONN_AUTH_PEND
, &conn
->pend
)) {
307 struct hci_cp_auth_requested cp
;
308 cp
.handle
= __cpu_to_le16(conn
->handle
);
309 hci_send_cmd(conn
->hdev
, OGF_LINK_CTL
, OCF_AUTH_REQUESTED
, sizeof(cp
), &cp
);
313 EXPORT_SYMBOL(hci_conn_auth
);
315 /* Enable encryption */
316 int hci_conn_encrypt(struct hci_conn
*conn
)
318 BT_DBG("conn %p", conn
);
320 if (conn
->link_mode
& HCI_LM_ENCRYPT
)
323 if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND
, &conn
->pend
))
326 if (hci_conn_auth(conn
)) {
327 struct hci_cp_set_conn_encrypt cp
;
328 cp
.handle
= __cpu_to_le16(conn
->handle
);
330 hci_send_cmd(conn
->hdev
, OGF_LINK_CTL
, OCF_SET_CONN_ENCRYPT
, sizeof(cp
), &cp
);
334 EXPORT_SYMBOL(hci_conn_encrypt
);
336 /* Change link key */
337 int hci_conn_change_link_key(struct hci_conn
*conn
)
339 BT_DBG("conn %p", conn
);
341 if (!test_and_set_bit(HCI_CONN_AUTH_PEND
, &conn
->pend
)) {
342 struct hci_cp_change_conn_link_key cp
;
343 cp
.handle
= __cpu_to_le16(conn
->handle
);
344 hci_send_cmd(conn
->hdev
, OGF_LINK_CTL
, OCF_CHANGE_CONN_LINK_KEY
, sizeof(cp
), &cp
);
348 EXPORT_SYMBOL(hci_conn_change_link_key
);
351 int hci_conn_switch_role(struct hci_conn
*conn
, uint8_t role
)
353 BT_DBG("conn %p", conn
);
355 if (!role
&& conn
->link_mode
& HCI_LM_MASTER
)
358 if (!test_and_set_bit(HCI_CONN_RSWITCH_PEND
, &conn
->pend
)) {
359 struct hci_cp_switch_role cp
;
360 bacpy(&cp
.bdaddr
, &conn
->dst
);
362 hci_send_cmd(conn
->hdev
, OGF_LINK_POLICY
, OCF_SWITCH_ROLE
, sizeof(cp
), &cp
);
366 EXPORT_SYMBOL(hci_conn_switch_role
);
368 /* Drop all connection on the device */
369 void hci_conn_hash_flush(struct hci_dev
*hdev
)
371 struct hci_conn_hash
*h
= &hdev
->conn_hash
;
374 BT_DBG("hdev %s", hdev
->name
);
377 while (p
!= &h
->list
) {
380 c
= list_entry(p
, struct hci_conn
, list
);
383 c
->state
= BT_CLOSED
;
385 hci_proto_disconn_ind(c
, 0x16);
390 int hci_get_conn_list(void __user
*arg
)
392 struct hci_conn_list_req req
, *cl
;
393 struct hci_conn_info
*ci
;
394 struct hci_dev
*hdev
;
396 int n
= 0, size
, err
;
398 if (copy_from_user(&req
, arg
, sizeof(req
)))
401 if (!req
.conn_num
|| req
.conn_num
> (PAGE_SIZE
* 2) / sizeof(*ci
))
404 size
= sizeof(req
) + req
.conn_num
* sizeof(*ci
);
406 if (!(cl
= (void *) kmalloc(size
, GFP_KERNEL
)))
409 if (!(hdev
= hci_dev_get(req
.dev_id
))) {
416 hci_dev_lock_bh(hdev
);
417 list_for_each(p
, &hdev
->conn_hash
.list
) {
418 register struct hci_conn
*c
;
419 c
= list_entry(p
, struct hci_conn
, list
);
421 bacpy(&(ci
+ n
)->bdaddr
, &c
->dst
);
422 (ci
+ n
)->handle
= c
->handle
;
423 (ci
+ n
)->type
= c
->type
;
424 (ci
+ n
)->out
= c
->out
;
425 (ci
+ n
)->state
= c
->state
;
426 (ci
+ n
)->link_mode
= c
->link_mode
;
427 if (++n
>= req
.conn_num
)
430 hci_dev_unlock_bh(hdev
);
432 cl
->dev_id
= hdev
->id
;
434 size
= sizeof(req
) + n
* sizeof(*ci
);
438 err
= copy_to_user(arg
, cl
, size
);
441 return err
? -EFAULT
: 0;
444 int hci_get_conn_info(struct hci_dev
*hdev
, void __user
*arg
)
446 struct hci_conn_info_req req
;
447 struct hci_conn_info ci
;
448 struct hci_conn
*conn
;
449 char __user
*ptr
= arg
+ sizeof(req
);
451 if (copy_from_user(&req
, arg
, sizeof(req
)))
454 hci_dev_lock_bh(hdev
);
455 conn
= hci_conn_hash_lookup_ba(hdev
, req
.type
, &req
.bdaddr
);
457 bacpy(&ci
.bdaddr
, &conn
->dst
);
458 ci
.handle
= conn
->handle
;
459 ci
.type
= conn
->type
;
461 ci
.state
= conn
->state
;
462 ci
.link_mode
= conn
->link_mode
;
464 hci_dev_unlock_bh(hdev
);
469 return copy_to_user(ptr
, &ci
, sizeof(ci
)) ? -EFAULT
: 0;