2 BlueZ - Bluetooth protocol stack for Linux
3 Copyright (c) 2000-2001, 2010, Code Aurora Forum. All rights reserved.
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/module.h>
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>
41 #include <asm/system.h>
42 #include <linux/uaccess.h>
43 #include <asm/unaligned.h>
45 #include <net/bluetooth/bluetooth.h>
46 #include <net/bluetooth/hci_core.h>
48 static void hci_le_connect(struct hci_conn
*conn
)
50 struct hci_dev
*hdev
= conn
->hdev
;
51 struct hci_cp_le_create_conn cp
;
53 conn
->state
= BT_CONNECT
;
55 conn
->link_mode
|= HCI_LM_MASTER
;
57 memset(&cp
, 0, sizeof(cp
));
58 cp
.scan_interval
= cpu_to_le16(0x0004);
59 cp
.scan_window
= cpu_to_le16(0x0004);
60 bacpy(&cp
.peer_addr
, &conn
->dst
);
61 cp
.peer_addr_type
= conn
->dst_type
;
62 cp
.conn_interval_min
= cpu_to_le16(0x0008);
63 cp
.conn_interval_max
= cpu_to_le16(0x0100);
64 cp
.supervision_timeout
= cpu_to_le16(0x0064);
65 cp
.min_ce_len
= cpu_to_le16(0x0001);
66 cp
.max_ce_len
= cpu_to_le16(0x0001);
68 hci_send_cmd(hdev
, HCI_OP_LE_CREATE_CONN
, sizeof(cp
), &cp
);
71 static void hci_le_connect_cancel(struct hci_conn
*conn
)
73 hci_send_cmd(conn
->hdev
, HCI_OP_LE_CREATE_CONN_CANCEL
, 0, NULL
);
76 void hci_acl_connect(struct hci_conn
*conn
)
78 struct hci_dev
*hdev
= conn
->hdev
;
79 struct inquiry_entry
*ie
;
80 struct hci_cp_create_conn cp
;
84 conn
->state
= BT_CONNECT
;
87 conn
->link_mode
= HCI_LM_MASTER
;
91 conn
->link_policy
= hdev
->link_policy
;
93 memset(&cp
, 0, sizeof(cp
));
94 bacpy(&cp
.bdaddr
, &conn
->dst
);
95 cp
.pscan_rep_mode
= 0x02;
97 ie
= hci_inquiry_cache_lookup(hdev
, &conn
->dst
);
99 if (inquiry_entry_age(ie
) <= INQUIRY_ENTRY_AGE_MAX
) {
100 cp
.pscan_rep_mode
= ie
->data
.pscan_rep_mode
;
101 cp
.pscan_mode
= ie
->data
.pscan_mode
;
102 cp
.clock_offset
= ie
->data
.clock_offset
|
106 memcpy(conn
->dev_class
, ie
->data
.dev_class
, 3);
107 conn
->ssp_mode
= ie
->data
.ssp_mode
;
110 cp
.pkt_type
= cpu_to_le16(conn
->pkt_type
);
111 if (lmp_rswitch_capable(hdev
) && !(hdev
->link_mode
& HCI_LM_MASTER
))
112 cp
.role_switch
= 0x01;
114 cp
.role_switch
= 0x00;
116 hci_send_cmd(hdev
, HCI_OP_CREATE_CONN
, sizeof(cp
), &cp
);
119 static void hci_acl_connect_cancel(struct hci_conn
*conn
)
121 struct hci_cp_create_conn_cancel cp
;
125 if (conn
->hdev
->hci_ver
< 2)
128 bacpy(&cp
.bdaddr
, &conn
->dst
);
129 hci_send_cmd(conn
->hdev
, HCI_OP_CREATE_CONN_CANCEL
, sizeof(cp
), &cp
);
132 void hci_acl_disconn(struct hci_conn
*conn
, __u8 reason
)
134 struct hci_cp_disconnect cp
;
138 conn
->state
= BT_DISCONN
;
140 cp
.handle
= cpu_to_le16(conn
->handle
);
142 hci_send_cmd(conn
->hdev
, HCI_OP_DISCONNECT
, sizeof(cp
), &cp
);
145 void hci_add_sco(struct hci_conn
*conn
, __u16 handle
)
147 struct hci_dev
*hdev
= conn
->hdev
;
148 struct hci_cp_add_sco cp
;
152 conn
->state
= BT_CONNECT
;
157 cp
.handle
= cpu_to_le16(handle
);
158 cp
.pkt_type
= cpu_to_le16(conn
->pkt_type
);
160 hci_send_cmd(hdev
, HCI_OP_ADD_SCO
, sizeof(cp
), &cp
);
163 void hci_setup_sync(struct hci_conn
*conn
, __u16 handle
)
165 struct hci_dev
*hdev
= conn
->hdev
;
166 struct hci_cp_setup_sync_conn cp
;
170 conn
->state
= BT_CONNECT
;
175 cp
.handle
= cpu_to_le16(handle
);
176 cp
.pkt_type
= cpu_to_le16(conn
->pkt_type
);
178 cp
.tx_bandwidth
= cpu_to_le32(0x00001f40);
179 cp
.rx_bandwidth
= cpu_to_le32(0x00001f40);
180 cp
.max_latency
= cpu_to_le16(0xffff);
181 cp
.voice_setting
= cpu_to_le16(hdev
->voice_setting
);
182 cp
.retrans_effort
= 0xff;
184 hci_send_cmd(hdev
, HCI_OP_SETUP_SYNC_CONN
, sizeof(cp
), &cp
);
187 void hci_le_conn_update(struct hci_conn
*conn
, u16 min
, u16 max
,
188 u16 latency
, u16 to_multiplier
)
190 struct hci_cp_le_conn_update cp
;
191 struct hci_dev
*hdev
= conn
->hdev
;
193 memset(&cp
, 0, sizeof(cp
));
195 cp
.handle
= cpu_to_le16(conn
->handle
);
196 cp
.conn_interval_min
= cpu_to_le16(min
);
197 cp
.conn_interval_max
= cpu_to_le16(max
);
198 cp
.conn_latency
= cpu_to_le16(latency
);
199 cp
.supervision_timeout
= cpu_to_le16(to_multiplier
);
200 cp
.min_ce_len
= cpu_to_le16(0x0001);
201 cp
.max_ce_len
= cpu_to_le16(0x0001);
203 hci_send_cmd(hdev
, HCI_OP_LE_CONN_UPDATE
, sizeof(cp
), &cp
);
205 EXPORT_SYMBOL(hci_le_conn_update
);
207 /* Device _must_ be locked */
208 void hci_sco_setup(struct hci_conn
*conn
, __u8 status
)
210 struct hci_conn
*sco
= conn
->link
;
218 if (lmp_esco_capable(conn
->hdev
))
219 hci_setup_sync(sco
, conn
->handle
);
221 hci_add_sco(sco
, conn
->handle
);
223 hci_proto_connect_cfm(sco
, status
);
228 static void hci_conn_timeout(unsigned long arg
)
230 struct hci_conn
*conn
= (void *) arg
;
231 struct hci_dev
*hdev
= conn
->hdev
;
234 BT_DBG("conn %p state %d", conn
, conn
->state
);
236 if (atomic_read(&conn
->refcnt
))
241 switch (conn
->state
) {
245 if (conn
->type
== ACL_LINK
)
246 hci_acl_connect_cancel(conn
);
247 else if (conn
->type
== LE_LINK
)
248 hci_le_connect_cancel(conn
);
253 reason
= hci_proto_disconn_ind(conn
);
254 hci_acl_disconn(conn
, reason
);
257 conn
->state
= BT_CLOSED
;
261 hci_dev_unlock(hdev
);
264 static void hci_conn_idle(unsigned long arg
)
266 struct hci_conn
*conn
= (void *) arg
;
268 BT_DBG("conn %p mode %d", conn
, conn
->mode
);
270 hci_conn_enter_sniff_mode(conn
);
273 static void hci_conn_auto_accept(unsigned long arg
)
275 struct hci_conn
*conn
= (void *) arg
;
276 struct hci_dev
*hdev
= conn
->hdev
;
280 hci_send_cmd(hdev
, HCI_OP_USER_CONFIRM_REPLY
, sizeof(conn
->dst
),
283 hci_dev_unlock(hdev
);
286 struct hci_conn
*hci_conn_add(struct hci_dev
*hdev
, int type
, bdaddr_t
*dst
)
288 struct hci_conn
*conn
;
290 BT_DBG("%s dst %s", hdev
->name
, batostr(dst
));
292 conn
= kzalloc(sizeof(struct hci_conn
), GFP_ATOMIC
);
296 bacpy(&conn
->dst
, dst
);
299 conn
->mode
= HCI_CM_ACTIVE
;
300 conn
->state
= BT_OPEN
;
301 conn
->auth_type
= HCI_AT_GENERAL_BONDING
;
302 conn
->io_capability
= hdev
->io_capability
;
303 conn
->remote_auth
= 0xff;
304 conn
->key_type
= 0xff;
306 conn
->power_save
= 1;
307 conn
->disc_timeout
= HCI_DISCONN_TIMEOUT
;
311 conn
->pkt_type
= hdev
->pkt_type
& ACL_PTYPE_MASK
;
314 if (lmp_esco_capable(hdev
))
315 conn
->pkt_type
= (hdev
->esco_type
& SCO_ESCO_MASK
) |
316 (hdev
->esco_type
& EDR_ESCO_MASK
);
318 conn
->pkt_type
= hdev
->pkt_type
& SCO_PTYPE_MASK
;
321 conn
->pkt_type
= hdev
->esco_type
& ~EDR_ESCO_MASK
;
325 skb_queue_head_init(&conn
->data_q
);
327 setup_timer(&conn
->disc_timer
, hci_conn_timeout
, (unsigned long)conn
);
328 setup_timer(&conn
->idle_timer
, hci_conn_idle
, (unsigned long)conn
);
329 setup_timer(&conn
->auto_accept_timer
, hci_conn_auto_accept
,
330 (unsigned long) conn
);
332 atomic_set(&conn
->refcnt
, 0);
336 tasklet_disable(&hdev
->tx_task
);
338 hci_conn_hash_add(hdev
, conn
);
340 hdev
->notify(hdev
, HCI_NOTIFY_CONN_ADD
);
342 atomic_set(&conn
->devref
, 0);
344 hci_conn_init_sysfs(conn
);
346 tasklet_enable(&hdev
->tx_task
);
351 int hci_conn_del(struct hci_conn
*conn
)
353 struct hci_dev
*hdev
= conn
->hdev
;
355 BT_DBG("%s conn %p handle %d", hdev
->name
, conn
, conn
->handle
);
357 del_timer(&conn
->idle_timer
);
359 del_timer(&conn
->disc_timer
);
361 del_timer(&conn
->auto_accept_timer
);
363 if (conn
->type
== ACL_LINK
) {
364 struct hci_conn
*sco
= conn
->link
;
369 hdev
->acl_cnt
+= conn
->sent
;
370 } else if (conn
->type
== LE_LINK
) {
372 hdev
->le_cnt
+= conn
->sent
;
374 hdev
->acl_cnt
+= conn
->sent
;
376 struct hci_conn
*acl
= conn
->link
;
383 tasklet_disable(&hdev
->tx_task
);
385 hci_conn_hash_del(hdev
, conn
);
387 hdev
->notify(hdev
, HCI_NOTIFY_CONN_DEL
);
389 tasklet_enable(&hdev
->tx_task
);
391 skb_queue_purge(&conn
->data_q
);
393 hci_conn_put_device(conn
);
400 struct hci_dev
*hci_get_route(bdaddr_t
*dst
, bdaddr_t
*src
)
402 int use_src
= bacmp(src
, BDADDR_ANY
);
403 struct hci_dev
*hdev
= NULL
;
406 BT_DBG("%s -> %s", batostr(src
), batostr(dst
));
408 read_lock_bh(&hci_dev_list_lock
);
410 list_for_each(p
, &hci_dev_list
) {
411 struct hci_dev
*d
= list_entry(p
, struct hci_dev
, list
);
413 if (!test_bit(HCI_UP
, &d
->flags
) || test_bit(HCI_RAW
, &d
->flags
))
417 * No source address - find interface with bdaddr != dst
418 * Source address - find interface with bdaddr == src
422 if (!bacmp(&d
->bdaddr
, src
)) {
426 if (bacmp(&d
->bdaddr
, dst
)) {
433 hdev
= hci_dev_hold(hdev
);
435 read_unlock_bh(&hci_dev_list_lock
);
438 EXPORT_SYMBOL(hci_get_route
);
440 /* Create SCO, ACL or LE connection.
441 * Device _must_ be locked */
442 struct hci_conn
*hci_connect(struct hci_dev
*hdev
, int type
, bdaddr_t
*dst
, __u8 sec_level
, __u8 auth_type
)
444 struct hci_conn
*acl
;
445 struct hci_conn
*sco
;
448 BT_DBG("%s dst %s", hdev
->name
, batostr(dst
));
450 if (type
== LE_LINK
) {
451 struct adv_entry
*entry
;
453 le
= hci_conn_hash_lookup_ba(hdev
, LE_LINK
, dst
);
455 return ERR_PTR(-EBUSY
);
457 entry
= hci_find_adv_entry(hdev
, dst
);
459 return ERR_PTR(-EHOSTUNREACH
);
461 le
= hci_conn_add(hdev
, LE_LINK
, dst
);
463 return ERR_PTR(-ENOMEM
);
465 le
->dst_type
= entry
->bdaddr_type
;
474 acl
= hci_conn_hash_lookup_ba(hdev
, ACL_LINK
, dst
);
476 acl
= hci_conn_add(hdev
, ACL_LINK
, dst
);
483 if (acl
->state
== BT_OPEN
|| acl
->state
== BT_CLOSED
) {
484 acl
->sec_level
= BT_SECURITY_LOW
;
485 acl
->pending_sec_level
= sec_level
;
486 acl
->auth_type
= auth_type
;
487 hci_acl_connect(acl
);
490 if (type
== ACL_LINK
)
493 sco
= hci_conn_hash_lookup_ba(hdev
, type
, dst
);
495 sco
= hci_conn_add(hdev
, type
, dst
);
507 if (acl
->state
== BT_CONNECTED
&&
508 (sco
->state
== BT_OPEN
|| sco
->state
== BT_CLOSED
)) {
510 hci_conn_enter_active_mode(acl
);
512 if (test_bit(HCI_CONN_MODE_CHANGE_PEND
, &acl
->pend
)) {
513 /* defer SCO setup until mode change completed */
514 set_bit(HCI_CONN_SCO_SETUP_PEND
, &acl
->pend
);
518 hci_sco_setup(acl
, 0x00);
523 EXPORT_SYMBOL(hci_connect
);
525 /* Check link security requirement */
526 int hci_conn_check_link_mode(struct hci_conn
*conn
)
528 BT_DBG("conn %p", conn
);
530 if (conn
->ssp_mode
> 0 && conn
->hdev
->ssp_mode
> 0 &&
531 !(conn
->link_mode
& HCI_LM_ENCRYPT
))
536 EXPORT_SYMBOL(hci_conn_check_link_mode
);
538 /* Authenticate remote device */
539 static int hci_conn_auth(struct hci_conn
*conn
, __u8 sec_level
, __u8 auth_type
)
541 BT_DBG("conn %p", conn
);
543 if (conn
->pending_sec_level
> sec_level
)
544 sec_level
= conn
->pending_sec_level
;
546 if (sec_level
> conn
->sec_level
)
547 conn
->pending_sec_level
= sec_level
;
548 else if (conn
->link_mode
& HCI_LM_AUTH
)
551 /* Make sure we preserve an existing MITM requirement*/
552 auth_type
|= (conn
->auth_type
& 0x01);
554 conn
->auth_type
= auth_type
;
556 if (!test_and_set_bit(HCI_CONN_AUTH_PEND
, &conn
->pend
)) {
557 struct hci_cp_auth_requested cp
;
558 cp
.handle
= cpu_to_le16(conn
->handle
);
559 hci_send_cmd(conn
->hdev
, HCI_OP_AUTH_REQUESTED
,
561 if (conn
->key_type
!= 0xff)
562 set_bit(HCI_CONN_REAUTH_PEND
, &conn
->pend
);
568 /* Encrypt the the link */
569 static void hci_conn_encrypt(struct hci_conn
*conn
)
571 BT_DBG("conn %p", conn
);
573 if (!test_and_set_bit(HCI_CONN_ENCRYPT_PEND
, &conn
->pend
)) {
574 struct hci_cp_set_conn_encrypt cp
;
575 cp
.handle
= cpu_to_le16(conn
->handle
);
577 hci_send_cmd(conn
->hdev
, HCI_OP_SET_CONN_ENCRYPT
, sizeof(cp
),
582 /* Enable security */
583 int hci_conn_security(struct hci_conn
*conn
, __u8 sec_level
, __u8 auth_type
)
585 BT_DBG("conn %p", conn
);
587 /* For sdp we don't need the link key. */
588 if (sec_level
== BT_SECURITY_SDP
)
591 /* For non 2.1 devices and low security level we don't need the link
593 if (sec_level
== BT_SECURITY_LOW
&&
594 (!conn
->ssp_mode
|| !conn
->hdev
->ssp_mode
))
597 /* For other security levels we need the link key. */
598 if (!(conn
->link_mode
& HCI_LM_AUTH
))
601 /* An authenticated combination key has sufficient security for any
603 if (conn
->key_type
== HCI_LK_AUTH_COMBINATION
)
606 /* An unauthenticated combination key has sufficient security for
607 security level 1 and 2. */
608 if (conn
->key_type
== HCI_LK_UNAUTH_COMBINATION
&&
609 (sec_level
== BT_SECURITY_MEDIUM
||
610 sec_level
== BT_SECURITY_LOW
))
613 /* A combination key has always sufficient security for the security
614 levels 1 or 2. High security level requires the combination key
615 is generated using maximum PIN code length (16).
616 For pre 2.1 units. */
617 if (conn
->key_type
== HCI_LK_COMBINATION
&&
618 (sec_level
!= BT_SECURITY_HIGH
||
619 conn
->pin_length
== 16))
623 if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND
, &conn
->pend
))
626 hci_conn_auth(conn
, sec_level
, auth_type
);
630 if (conn
->link_mode
& HCI_LM_ENCRYPT
)
633 hci_conn_encrypt(conn
);
636 EXPORT_SYMBOL(hci_conn_security
);
638 /* Check secure link requirement */
639 int hci_conn_check_secure(struct hci_conn
*conn
, __u8 sec_level
)
641 BT_DBG("conn %p", conn
);
643 if (sec_level
!= BT_SECURITY_HIGH
)
644 return 1; /* Accept if non-secure is required */
646 if (conn
->key_type
== HCI_LK_AUTH_COMBINATION
||
647 (conn
->key_type
== HCI_LK_COMBINATION
&&
648 conn
->pin_length
== 16))
651 return 0; /* Reject not secure link */
653 EXPORT_SYMBOL(hci_conn_check_secure
);
655 /* Change link key */
656 int hci_conn_change_link_key(struct hci_conn
*conn
)
658 BT_DBG("conn %p", conn
);
660 if (!test_and_set_bit(HCI_CONN_AUTH_PEND
, &conn
->pend
)) {
661 struct hci_cp_change_conn_link_key cp
;
662 cp
.handle
= cpu_to_le16(conn
->handle
);
663 hci_send_cmd(conn
->hdev
, HCI_OP_CHANGE_CONN_LINK_KEY
,
669 EXPORT_SYMBOL(hci_conn_change_link_key
);
672 int hci_conn_switch_role(struct hci_conn
*conn
, __u8 role
)
674 BT_DBG("conn %p", conn
);
676 if (!role
&& conn
->link_mode
& HCI_LM_MASTER
)
679 if (!test_and_set_bit(HCI_CONN_RSWITCH_PEND
, &conn
->pend
)) {
680 struct hci_cp_switch_role cp
;
681 bacpy(&cp
.bdaddr
, &conn
->dst
);
683 hci_send_cmd(conn
->hdev
, HCI_OP_SWITCH_ROLE
, sizeof(cp
), &cp
);
688 EXPORT_SYMBOL(hci_conn_switch_role
);
690 /* Enter active mode */
691 void hci_conn_enter_active_mode(struct hci_conn
*conn
)
693 struct hci_dev
*hdev
= conn
->hdev
;
695 BT_DBG("conn %p mode %d", conn
, conn
->mode
);
697 if (test_bit(HCI_RAW
, &hdev
->flags
))
700 if (conn
->mode
!= HCI_CM_SNIFF
|| !conn
->power_save
)
703 if (!test_and_set_bit(HCI_CONN_MODE_CHANGE_PEND
, &conn
->pend
)) {
704 struct hci_cp_exit_sniff_mode cp
;
705 cp
.handle
= cpu_to_le16(conn
->handle
);
706 hci_send_cmd(hdev
, HCI_OP_EXIT_SNIFF_MODE
, sizeof(cp
), &cp
);
710 if (hdev
->idle_timeout
> 0)
711 mod_timer(&conn
->idle_timer
,
712 jiffies
+ msecs_to_jiffies(hdev
->idle_timeout
));
715 /* Enter sniff mode */
716 void hci_conn_enter_sniff_mode(struct hci_conn
*conn
)
718 struct hci_dev
*hdev
= conn
->hdev
;
720 BT_DBG("conn %p mode %d", conn
, conn
->mode
);
722 if (test_bit(HCI_RAW
, &hdev
->flags
))
725 if (!lmp_sniff_capable(hdev
) || !lmp_sniff_capable(conn
))
728 if (conn
->mode
!= HCI_CM_ACTIVE
|| !(conn
->link_policy
& HCI_LP_SNIFF
))
731 if (lmp_sniffsubr_capable(hdev
) && lmp_sniffsubr_capable(conn
)) {
732 struct hci_cp_sniff_subrate cp
;
733 cp
.handle
= cpu_to_le16(conn
->handle
);
734 cp
.max_latency
= cpu_to_le16(0);
735 cp
.min_remote_timeout
= cpu_to_le16(0);
736 cp
.min_local_timeout
= cpu_to_le16(0);
737 hci_send_cmd(hdev
, HCI_OP_SNIFF_SUBRATE
, sizeof(cp
), &cp
);
740 if (!test_and_set_bit(HCI_CONN_MODE_CHANGE_PEND
, &conn
->pend
)) {
741 struct hci_cp_sniff_mode cp
;
742 cp
.handle
= cpu_to_le16(conn
->handle
);
743 cp
.max_interval
= cpu_to_le16(hdev
->sniff_max_interval
);
744 cp
.min_interval
= cpu_to_le16(hdev
->sniff_min_interval
);
745 cp
.attempt
= cpu_to_le16(4);
746 cp
.timeout
= cpu_to_le16(1);
747 hci_send_cmd(hdev
, HCI_OP_SNIFF_MODE
, sizeof(cp
), &cp
);
751 /* Drop all connection on the device */
752 void hci_conn_hash_flush(struct hci_dev
*hdev
)
754 struct hci_conn_hash
*h
= &hdev
->conn_hash
;
757 BT_DBG("hdev %s", hdev
->name
);
760 while (p
!= &h
->list
) {
763 c
= list_entry(p
, struct hci_conn
, list
);
766 c
->state
= BT_CLOSED
;
768 hci_proto_disconn_cfm(c
, 0x16);
773 /* Check pending connect attempts */
774 void hci_conn_check_pending(struct hci_dev
*hdev
)
776 struct hci_conn
*conn
;
778 BT_DBG("hdev %s", hdev
->name
);
782 conn
= hci_conn_hash_lookup_state(hdev
, ACL_LINK
, BT_CONNECT2
);
784 hci_acl_connect(conn
);
786 hci_dev_unlock(hdev
);
789 void hci_conn_hold_device(struct hci_conn
*conn
)
791 atomic_inc(&conn
->devref
);
793 EXPORT_SYMBOL(hci_conn_hold_device
);
795 void hci_conn_put_device(struct hci_conn
*conn
)
797 if (atomic_dec_and_test(&conn
->devref
))
798 hci_conn_del_sysfs(conn
);
800 EXPORT_SYMBOL(hci_conn_put_device
);
802 int hci_get_conn_list(void __user
*arg
)
804 struct hci_conn_list_req req
, *cl
;
805 struct hci_conn_info
*ci
;
806 struct hci_dev
*hdev
;
808 int n
= 0, size
, err
;
810 if (copy_from_user(&req
, arg
, sizeof(req
)))
813 if (!req
.conn_num
|| req
.conn_num
> (PAGE_SIZE
* 2) / sizeof(*ci
))
816 size
= sizeof(req
) + req
.conn_num
* sizeof(*ci
);
818 cl
= kmalloc(size
, GFP_KERNEL
);
822 hdev
= hci_dev_get(req
.dev_id
);
830 hci_dev_lock_bh(hdev
);
831 list_for_each(p
, &hdev
->conn_hash
.list
) {
832 register struct hci_conn
*c
;
833 c
= list_entry(p
, struct hci_conn
, list
);
835 bacpy(&(ci
+ n
)->bdaddr
, &c
->dst
);
836 (ci
+ n
)->handle
= c
->handle
;
837 (ci
+ n
)->type
= c
->type
;
838 (ci
+ n
)->out
= c
->out
;
839 (ci
+ n
)->state
= c
->state
;
840 (ci
+ n
)->link_mode
= c
->link_mode
;
841 if (++n
>= req
.conn_num
)
844 hci_dev_unlock_bh(hdev
);
846 cl
->dev_id
= hdev
->id
;
848 size
= sizeof(req
) + n
* sizeof(*ci
);
852 err
= copy_to_user(arg
, cl
, size
);
855 return err
? -EFAULT
: 0;
858 int hci_get_conn_info(struct hci_dev
*hdev
, void __user
*arg
)
860 struct hci_conn_info_req req
;
861 struct hci_conn_info ci
;
862 struct hci_conn
*conn
;
863 char __user
*ptr
= arg
+ sizeof(req
);
865 if (copy_from_user(&req
, arg
, sizeof(req
)))
868 hci_dev_lock_bh(hdev
);
869 conn
= hci_conn_hash_lookup_ba(hdev
, req
.type
, &req
.bdaddr
);
871 bacpy(&ci
.bdaddr
, &conn
->dst
);
872 ci
.handle
= conn
->handle
;
873 ci
.type
= conn
->type
;
875 ci
.state
= conn
->state
;
876 ci
.link_mode
= conn
->link_mode
;
878 hci_dev_unlock_bh(hdev
);
883 return copy_to_user(ptr
, &ci
, sizeof(ci
)) ? -EFAULT
: 0;
886 int hci_get_auth_info(struct hci_dev
*hdev
, void __user
*arg
)
888 struct hci_auth_info_req req
;
889 struct hci_conn
*conn
;
891 if (copy_from_user(&req
, arg
, sizeof(req
)))
894 hci_dev_lock_bh(hdev
);
895 conn
= hci_conn_hash_lookup_ba(hdev
, ACL_LINK
, &req
.bdaddr
);
897 req
.type
= conn
->auth_type
;
898 hci_dev_unlock_bh(hdev
);
903 return copy_to_user(arg
, &req
, sizeof(req
)) ? -EFAULT
: 0;