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 event 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 /* Handle HCI Event packets */
50 static void hci_cc_inquiry_cancel(struct hci_dev
*hdev
, struct sk_buff
*skb
)
52 __u8 status
= *((__u8
*) skb
->data
);
54 BT_DBG("%s status 0x%x", hdev
->name
, status
);
59 clear_bit(HCI_INQUIRY
, &hdev
->flags
);
61 hci_req_complete(hdev
, HCI_OP_INQUIRY_CANCEL
, status
);
63 hci_conn_check_pending(hdev
);
66 static void hci_cc_exit_periodic_inq(struct hci_dev
*hdev
, struct sk_buff
*skb
)
68 __u8 status
= *((__u8
*) skb
->data
);
70 BT_DBG("%s status 0x%x", hdev
->name
, status
);
75 clear_bit(HCI_INQUIRY
, &hdev
->flags
);
77 hci_conn_check_pending(hdev
);
80 static void hci_cc_remote_name_req_cancel(struct hci_dev
*hdev
, struct sk_buff
*skb
)
82 BT_DBG("%s", hdev
->name
);
85 static void hci_cc_role_discovery(struct hci_dev
*hdev
, struct sk_buff
*skb
)
87 struct hci_rp_role_discovery
*rp
= (void *) skb
->data
;
88 struct hci_conn
*conn
;
90 BT_DBG("%s status 0x%x", hdev
->name
, rp
->status
);
97 conn
= hci_conn_hash_lookup_handle(hdev
, __le16_to_cpu(rp
->handle
));
100 conn
->link_mode
&= ~HCI_LM_MASTER
;
102 conn
->link_mode
|= HCI_LM_MASTER
;
105 hci_dev_unlock(hdev
);
108 static void hci_cc_read_link_policy(struct hci_dev
*hdev
, struct sk_buff
*skb
)
110 struct hci_rp_read_link_policy
*rp
= (void *) skb
->data
;
111 struct hci_conn
*conn
;
113 BT_DBG("%s status 0x%x", hdev
->name
, rp
->status
);
120 conn
= hci_conn_hash_lookup_handle(hdev
, __le16_to_cpu(rp
->handle
));
122 conn
->link_policy
= __le16_to_cpu(rp
->policy
);
124 hci_dev_unlock(hdev
);
127 static void hci_cc_write_link_policy(struct hci_dev
*hdev
, struct sk_buff
*skb
)
129 struct hci_rp_write_link_policy
*rp
= (void *) skb
->data
;
130 struct hci_conn
*conn
;
133 BT_DBG("%s status 0x%x", hdev
->name
, rp
->status
);
138 sent
= hci_sent_cmd_data(hdev
, HCI_OP_WRITE_LINK_POLICY
);
144 conn
= hci_conn_hash_lookup_handle(hdev
, __le16_to_cpu(rp
->handle
));
146 conn
->link_policy
= get_unaligned_le16(sent
+ 2);
148 hci_dev_unlock(hdev
);
151 static void hci_cc_read_def_link_policy(struct hci_dev
*hdev
, struct sk_buff
*skb
)
153 struct hci_rp_read_def_link_policy
*rp
= (void *) skb
->data
;
155 BT_DBG("%s status 0x%x", hdev
->name
, rp
->status
);
160 hdev
->link_policy
= __le16_to_cpu(rp
->policy
);
163 static void hci_cc_write_def_link_policy(struct hci_dev
*hdev
, struct sk_buff
*skb
)
165 __u8 status
= *((__u8
*) skb
->data
);
168 BT_DBG("%s status 0x%x", hdev
->name
, status
);
170 sent
= hci_sent_cmd_data(hdev
, HCI_OP_WRITE_DEF_LINK_POLICY
);
175 hdev
->link_policy
= get_unaligned_le16(sent
);
177 hci_req_complete(hdev
, HCI_OP_WRITE_DEF_LINK_POLICY
, status
);
180 static void hci_cc_reset(struct hci_dev
*hdev
, struct sk_buff
*skb
)
182 __u8 status
= *((__u8
*) skb
->data
);
184 BT_DBG("%s status 0x%x", hdev
->name
, status
);
186 hci_req_complete(hdev
, HCI_OP_RESET
, status
);
189 static void hci_cc_write_local_name(struct hci_dev
*hdev
, struct sk_buff
*skb
)
191 __u8 status
= *((__u8
*) skb
->data
);
194 BT_DBG("%s status 0x%x", hdev
->name
, status
);
199 sent
= hci_sent_cmd_data(hdev
, HCI_OP_WRITE_LOCAL_NAME
);
203 memcpy(hdev
->dev_name
, sent
, 248);
206 static void hci_cc_read_local_name(struct hci_dev
*hdev
, struct sk_buff
*skb
)
208 struct hci_rp_read_local_name
*rp
= (void *) skb
->data
;
210 BT_DBG("%s status 0x%x", hdev
->name
, rp
->status
);
215 memcpy(hdev
->dev_name
, rp
->name
, 248);
218 static void hci_cc_write_auth_enable(struct hci_dev
*hdev
, struct sk_buff
*skb
)
220 __u8 status
= *((__u8
*) skb
->data
);
223 BT_DBG("%s status 0x%x", hdev
->name
, status
);
225 sent
= hci_sent_cmd_data(hdev
, HCI_OP_WRITE_AUTH_ENABLE
);
230 __u8 param
= *((__u8
*) sent
);
232 if (param
== AUTH_ENABLED
)
233 set_bit(HCI_AUTH
, &hdev
->flags
);
235 clear_bit(HCI_AUTH
, &hdev
->flags
);
238 hci_req_complete(hdev
, HCI_OP_WRITE_AUTH_ENABLE
, status
);
241 static void hci_cc_write_encrypt_mode(struct hci_dev
*hdev
, struct sk_buff
*skb
)
243 __u8 status
= *((__u8
*) skb
->data
);
246 BT_DBG("%s status 0x%x", hdev
->name
, status
);
248 sent
= hci_sent_cmd_data(hdev
, HCI_OP_WRITE_ENCRYPT_MODE
);
253 __u8 param
= *((__u8
*) sent
);
256 set_bit(HCI_ENCRYPT
, &hdev
->flags
);
258 clear_bit(HCI_ENCRYPT
, &hdev
->flags
);
261 hci_req_complete(hdev
, HCI_OP_WRITE_ENCRYPT_MODE
, status
);
264 static void hci_cc_write_scan_enable(struct hci_dev
*hdev
, struct sk_buff
*skb
)
266 __u8 status
= *((__u8
*) skb
->data
);
269 BT_DBG("%s status 0x%x", hdev
->name
, status
);
271 sent
= hci_sent_cmd_data(hdev
, HCI_OP_WRITE_SCAN_ENABLE
);
276 __u8 param
= *((__u8
*) sent
);
277 int old_pscan
, old_iscan
;
279 old_pscan
= test_and_clear_bit(HCI_PSCAN
, &hdev
->flags
);
280 old_iscan
= test_and_clear_bit(HCI_ISCAN
, &hdev
->flags
);
282 if (param
& SCAN_INQUIRY
) {
283 set_bit(HCI_ISCAN
, &hdev
->flags
);
285 mgmt_discoverable(hdev
->id
, 1);
286 } else if (old_iscan
)
287 mgmt_discoverable(hdev
->id
, 0);
289 if (param
& SCAN_PAGE
) {
290 set_bit(HCI_PSCAN
, &hdev
->flags
);
292 mgmt_connectable(hdev
->id
, 1);
293 } else if (old_pscan
)
294 mgmt_connectable(hdev
->id
, 0);
297 hci_req_complete(hdev
, HCI_OP_WRITE_SCAN_ENABLE
, status
);
300 static void hci_cc_read_class_of_dev(struct hci_dev
*hdev
, struct sk_buff
*skb
)
302 struct hci_rp_read_class_of_dev
*rp
= (void *) skb
->data
;
304 BT_DBG("%s status 0x%x", hdev
->name
, rp
->status
);
309 memcpy(hdev
->dev_class
, rp
->dev_class
, 3);
311 BT_DBG("%s class 0x%.2x%.2x%.2x", hdev
->name
,
312 hdev
->dev_class
[2], hdev
->dev_class
[1], hdev
->dev_class
[0]);
315 static void hci_cc_write_class_of_dev(struct hci_dev
*hdev
, struct sk_buff
*skb
)
317 __u8 status
= *((__u8
*) skb
->data
);
320 BT_DBG("%s status 0x%x", hdev
->name
, status
);
325 sent
= hci_sent_cmd_data(hdev
, HCI_OP_WRITE_CLASS_OF_DEV
);
329 memcpy(hdev
->dev_class
, sent
, 3);
332 static void hci_cc_read_voice_setting(struct hci_dev
*hdev
, struct sk_buff
*skb
)
334 struct hci_rp_read_voice_setting
*rp
= (void *) skb
->data
;
337 BT_DBG("%s status 0x%x", hdev
->name
, rp
->status
);
342 setting
= __le16_to_cpu(rp
->voice_setting
);
344 if (hdev
->voice_setting
== setting
)
347 hdev
->voice_setting
= setting
;
349 BT_DBG("%s voice setting 0x%04x", hdev
->name
, setting
);
352 tasklet_disable(&hdev
->tx_task
);
353 hdev
->notify(hdev
, HCI_NOTIFY_VOICE_SETTING
);
354 tasklet_enable(&hdev
->tx_task
);
358 static void hci_cc_write_voice_setting(struct hci_dev
*hdev
, struct sk_buff
*skb
)
360 __u8 status
= *((__u8
*) skb
->data
);
364 BT_DBG("%s status 0x%x", hdev
->name
, status
);
369 sent
= hci_sent_cmd_data(hdev
, HCI_OP_WRITE_VOICE_SETTING
);
373 setting
= get_unaligned_le16(sent
);
375 if (hdev
->voice_setting
== setting
)
378 hdev
->voice_setting
= setting
;
380 BT_DBG("%s voice setting 0x%04x", hdev
->name
, setting
);
383 tasklet_disable(&hdev
->tx_task
);
384 hdev
->notify(hdev
, HCI_NOTIFY_VOICE_SETTING
);
385 tasklet_enable(&hdev
->tx_task
);
389 static void hci_cc_host_buffer_size(struct hci_dev
*hdev
, struct sk_buff
*skb
)
391 __u8 status
= *((__u8
*) skb
->data
);
393 BT_DBG("%s status 0x%x", hdev
->name
, status
);
395 hci_req_complete(hdev
, HCI_OP_HOST_BUFFER_SIZE
, status
);
398 static void hci_cc_read_ssp_mode(struct hci_dev
*hdev
, struct sk_buff
*skb
)
400 struct hci_rp_read_ssp_mode
*rp
= (void *) skb
->data
;
402 BT_DBG("%s status 0x%x", hdev
->name
, rp
->status
);
407 hdev
->ssp_mode
= rp
->mode
;
410 static void hci_cc_write_ssp_mode(struct hci_dev
*hdev
, struct sk_buff
*skb
)
412 __u8 status
= *((__u8
*) skb
->data
);
415 BT_DBG("%s status 0x%x", hdev
->name
, status
);
420 sent
= hci_sent_cmd_data(hdev
, HCI_OP_WRITE_SSP_MODE
);
424 hdev
->ssp_mode
= *((__u8
*) sent
);
427 static void hci_cc_read_local_version(struct hci_dev
*hdev
, struct sk_buff
*skb
)
429 struct hci_rp_read_local_version
*rp
= (void *) skb
->data
;
431 BT_DBG("%s status 0x%x", hdev
->name
, rp
->status
);
436 hdev
->hci_ver
= rp
->hci_ver
;
437 hdev
->hci_rev
= __le16_to_cpu(rp
->hci_rev
);
438 hdev
->manufacturer
= __le16_to_cpu(rp
->manufacturer
);
440 BT_DBG("%s manufacturer %d hci ver %d:%d", hdev
->name
,
442 hdev
->hci_ver
, hdev
->hci_rev
);
445 static void hci_cc_read_local_commands(struct hci_dev
*hdev
, struct sk_buff
*skb
)
447 struct hci_rp_read_local_commands
*rp
= (void *) skb
->data
;
449 BT_DBG("%s status 0x%x", hdev
->name
, rp
->status
);
454 memcpy(hdev
->commands
, rp
->commands
, sizeof(hdev
->commands
));
457 static void hci_cc_read_local_features(struct hci_dev
*hdev
, struct sk_buff
*skb
)
459 struct hci_rp_read_local_features
*rp
= (void *) skb
->data
;
461 BT_DBG("%s status 0x%x", hdev
->name
, rp
->status
);
466 memcpy(hdev
->features
, rp
->features
, 8);
468 /* Adjust default settings according to features
469 * supported by device. */
471 if (hdev
->features
[0] & LMP_3SLOT
)
472 hdev
->pkt_type
|= (HCI_DM3
| HCI_DH3
);
474 if (hdev
->features
[0] & LMP_5SLOT
)
475 hdev
->pkt_type
|= (HCI_DM5
| HCI_DH5
);
477 if (hdev
->features
[1] & LMP_HV2
) {
478 hdev
->pkt_type
|= (HCI_HV2
);
479 hdev
->esco_type
|= (ESCO_HV2
);
482 if (hdev
->features
[1] & LMP_HV3
) {
483 hdev
->pkt_type
|= (HCI_HV3
);
484 hdev
->esco_type
|= (ESCO_HV3
);
487 if (hdev
->features
[3] & LMP_ESCO
)
488 hdev
->esco_type
|= (ESCO_EV3
);
490 if (hdev
->features
[4] & LMP_EV4
)
491 hdev
->esco_type
|= (ESCO_EV4
);
493 if (hdev
->features
[4] & LMP_EV5
)
494 hdev
->esco_type
|= (ESCO_EV5
);
496 if (hdev
->features
[5] & LMP_EDR_ESCO_2M
)
497 hdev
->esco_type
|= (ESCO_2EV3
);
499 if (hdev
->features
[5] & LMP_EDR_ESCO_3M
)
500 hdev
->esco_type
|= (ESCO_3EV3
);
502 if (hdev
->features
[5] & LMP_EDR_3S_ESCO
)
503 hdev
->esco_type
|= (ESCO_2EV5
| ESCO_3EV5
);
505 BT_DBG("%s features 0x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x", hdev
->name
,
506 hdev
->features
[0], hdev
->features
[1],
507 hdev
->features
[2], hdev
->features
[3],
508 hdev
->features
[4], hdev
->features
[5],
509 hdev
->features
[6], hdev
->features
[7]);
512 static void hci_cc_read_buffer_size(struct hci_dev
*hdev
, struct sk_buff
*skb
)
514 struct hci_rp_read_buffer_size
*rp
= (void *) skb
->data
;
516 BT_DBG("%s status 0x%x", hdev
->name
, rp
->status
);
521 hdev
->acl_mtu
= __le16_to_cpu(rp
->acl_mtu
);
522 hdev
->sco_mtu
= rp
->sco_mtu
;
523 hdev
->acl_pkts
= __le16_to_cpu(rp
->acl_max_pkt
);
524 hdev
->sco_pkts
= __le16_to_cpu(rp
->sco_max_pkt
);
526 if (test_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE
, &hdev
->quirks
)) {
531 hdev
->acl_cnt
= hdev
->acl_pkts
;
532 hdev
->sco_cnt
= hdev
->sco_pkts
;
534 BT_DBG("%s acl mtu %d:%d sco mtu %d:%d", hdev
->name
,
535 hdev
->acl_mtu
, hdev
->acl_pkts
,
536 hdev
->sco_mtu
, hdev
->sco_pkts
);
539 static void hci_cc_read_bd_addr(struct hci_dev
*hdev
, struct sk_buff
*skb
)
541 struct hci_rp_read_bd_addr
*rp
= (void *) skb
->data
;
543 BT_DBG("%s status 0x%x", hdev
->name
, rp
->status
);
546 bacpy(&hdev
->bdaddr
, &rp
->bdaddr
);
548 hci_req_complete(hdev
, HCI_OP_READ_BD_ADDR
, rp
->status
);
551 static void hci_cc_write_ca_timeout(struct hci_dev
*hdev
, struct sk_buff
*skb
)
553 __u8 status
= *((__u8
*) skb
->data
);
555 BT_DBG("%s status 0x%x", hdev
->name
, status
);
557 hci_req_complete(hdev
, HCI_OP_WRITE_CA_TIMEOUT
, status
);
560 static void hci_cc_delete_stored_link_key(struct hci_dev
*hdev
,
563 __u8 status
= *((__u8
*) skb
->data
);
565 BT_DBG("%s status 0x%x", hdev
->name
, status
);
567 hci_req_complete(hdev
, HCI_OP_DELETE_STORED_LINK_KEY
, status
);
570 static inline void hci_cs_inquiry(struct hci_dev
*hdev
, __u8 status
)
572 BT_DBG("%s status 0x%x", hdev
->name
, status
);
575 hci_req_complete(hdev
, HCI_OP_INQUIRY
, status
);
577 hci_conn_check_pending(hdev
);
579 set_bit(HCI_INQUIRY
, &hdev
->flags
);
582 static inline void hci_cs_create_conn(struct hci_dev
*hdev
, __u8 status
)
584 struct hci_cp_create_conn
*cp
;
585 struct hci_conn
*conn
;
587 BT_DBG("%s status 0x%x", hdev
->name
, status
);
589 cp
= hci_sent_cmd_data(hdev
, HCI_OP_CREATE_CONN
);
595 conn
= hci_conn_hash_lookup_ba(hdev
, ACL_LINK
, &cp
->bdaddr
);
597 BT_DBG("%s bdaddr %s conn %p", hdev
->name
, batostr(&cp
->bdaddr
), conn
);
600 if (conn
&& conn
->state
== BT_CONNECT
) {
601 if (status
!= 0x0c || conn
->attempt
> 2) {
602 conn
->state
= BT_CLOSED
;
603 hci_proto_connect_cfm(conn
, status
);
606 conn
->state
= BT_CONNECT2
;
610 conn
= hci_conn_add(hdev
, ACL_LINK
, &cp
->bdaddr
);
613 conn
->link_mode
|= HCI_LM_MASTER
;
615 BT_ERR("No memory for new connection");
619 hci_dev_unlock(hdev
);
622 static void hci_cs_add_sco(struct hci_dev
*hdev
, __u8 status
)
624 struct hci_cp_add_sco
*cp
;
625 struct hci_conn
*acl
, *sco
;
628 BT_DBG("%s status 0x%x", hdev
->name
, status
);
633 cp
= hci_sent_cmd_data(hdev
, HCI_OP_ADD_SCO
);
637 handle
= __le16_to_cpu(cp
->handle
);
639 BT_DBG("%s handle %d", hdev
->name
, handle
);
643 acl
= hci_conn_hash_lookup_handle(hdev
, handle
);
644 if (acl
&& (sco
= acl
->link
)) {
645 sco
->state
= BT_CLOSED
;
647 hci_proto_connect_cfm(sco
, status
);
651 hci_dev_unlock(hdev
);
654 static void hci_cs_auth_requested(struct hci_dev
*hdev
, __u8 status
)
656 struct hci_cp_auth_requested
*cp
;
657 struct hci_conn
*conn
;
659 BT_DBG("%s status 0x%x", hdev
->name
, status
);
664 cp
= hci_sent_cmd_data(hdev
, HCI_OP_AUTH_REQUESTED
);
670 conn
= hci_conn_hash_lookup_handle(hdev
, __le16_to_cpu(cp
->handle
));
672 if (conn
->state
== BT_CONFIG
) {
673 hci_proto_connect_cfm(conn
, status
);
678 hci_dev_unlock(hdev
);
681 static void hci_cs_set_conn_encrypt(struct hci_dev
*hdev
, __u8 status
)
683 struct hci_cp_set_conn_encrypt
*cp
;
684 struct hci_conn
*conn
;
686 BT_DBG("%s status 0x%x", hdev
->name
, status
);
691 cp
= hci_sent_cmd_data(hdev
, HCI_OP_SET_CONN_ENCRYPT
);
697 conn
= hci_conn_hash_lookup_handle(hdev
, __le16_to_cpu(cp
->handle
));
699 if (conn
->state
== BT_CONFIG
) {
700 hci_proto_connect_cfm(conn
, status
);
705 hci_dev_unlock(hdev
);
708 static int hci_outgoing_auth_needed(struct hci_dev
*hdev
,
709 struct hci_conn
*conn
)
711 if (conn
->state
!= BT_CONFIG
|| !conn
->out
)
714 if (conn
->pending_sec_level
== BT_SECURITY_SDP
)
717 /* Only request authentication for SSP connections or non-SSP
718 * devices with sec_level HIGH */
719 if (!(hdev
->ssp_mode
> 0 && conn
->ssp_mode
> 0) &&
720 conn
->pending_sec_level
!= BT_SECURITY_HIGH
)
726 static void hci_cs_remote_name_req(struct hci_dev
*hdev
, __u8 status
)
728 struct hci_cp_remote_name_req
*cp
;
729 struct hci_conn
*conn
;
731 BT_DBG("%s status 0x%x", hdev
->name
, status
);
733 /* If successful wait for the name req complete event before
734 * checking for the need to do authentication */
738 cp
= hci_sent_cmd_data(hdev
, HCI_OP_REMOTE_NAME_REQ
);
744 conn
= hci_conn_hash_lookup_ba(hdev
, ACL_LINK
, &cp
->bdaddr
);
745 if (conn
&& hci_outgoing_auth_needed(hdev
, conn
)) {
746 struct hci_cp_auth_requested cp
;
747 cp
.handle
= __cpu_to_le16(conn
->handle
);
748 hci_send_cmd(hdev
, HCI_OP_AUTH_REQUESTED
, sizeof(cp
), &cp
);
751 hci_dev_unlock(hdev
);
754 static void hci_cs_read_remote_features(struct hci_dev
*hdev
, __u8 status
)
756 struct hci_cp_read_remote_features
*cp
;
757 struct hci_conn
*conn
;
759 BT_DBG("%s status 0x%x", hdev
->name
, status
);
764 cp
= hci_sent_cmd_data(hdev
, HCI_OP_READ_REMOTE_FEATURES
);
770 conn
= hci_conn_hash_lookup_handle(hdev
, __le16_to_cpu(cp
->handle
));
772 if (conn
->state
== BT_CONFIG
) {
773 hci_proto_connect_cfm(conn
, status
);
778 hci_dev_unlock(hdev
);
781 static void hci_cs_read_remote_ext_features(struct hci_dev
*hdev
, __u8 status
)
783 struct hci_cp_read_remote_ext_features
*cp
;
784 struct hci_conn
*conn
;
786 BT_DBG("%s status 0x%x", hdev
->name
, status
);
791 cp
= hci_sent_cmd_data(hdev
, HCI_OP_READ_REMOTE_EXT_FEATURES
);
797 conn
= hci_conn_hash_lookup_handle(hdev
, __le16_to_cpu(cp
->handle
));
799 if (conn
->state
== BT_CONFIG
) {
800 hci_proto_connect_cfm(conn
, status
);
805 hci_dev_unlock(hdev
);
808 static void hci_cs_setup_sync_conn(struct hci_dev
*hdev
, __u8 status
)
810 struct hci_cp_setup_sync_conn
*cp
;
811 struct hci_conn
*acl
, *sco
;
814 BT_DBG("%s status 0x%x", hdev
->name
, status
);
819 cp
= hci_sent_cmd_data(hdev
, HCI_OP_SETUP_SYNC_CONN
);
823 handle
= __le16_to_cpu(cp
->handle
);
825 BT_DBG("%s handle %d", hdev
->name
, handle
);
829 acl
= hci_conn_hash_lookup_handle(hdev
, handle
);
830 if (acl
&& (sco
= acl
->link
)) {
831 sco
->state
= BT_CLOSED
;
833 hci_proto_connect_cfm(sco
, status
);
837 hci_dev_unlock(hdev
);
840 static void hci_cs_sniff_mode(struct hci_dev
*hdev
, __u8 status
)
842 struct hci_cp_sniff_mode
*cp
;
843 struct hci_conn
*conn
;
845 BT_DBG("%s status 0x%x", hdev
->name
, status
);
850 cp
= hci_sent_cmd_data(hdev
, HCI_OP_SNIFF_MODE
);
856 conn
= hci_conn_hash_lookup_handle(hdev
, __le16_to_cpu(cp
->handle
));
858 clear_bit(HCI_CONN_MODE_CHANGE_PEND
, &conn
->pend
);
860 if (test_and_clear_bit(HCI_CONN_SCO_SETUP_PEND
, &conn
->pend
))
861 hci_sco_setup(conn
, status
);
864 hci_dev_unlock(hdev
);
867 static void hci_cs_exit_sniff_mode(struct hci_dev
*hdev
, __u8 status
)
869 struct hci_cp_exit_sniff_mode
*cp
;
870 struct hci_conn
*conn
;
872 BT_DBG("%s status 0x%x", hdev
->name
, status
);
877 cp
= hci_sent_cmd_data(hdev
, HCI_OP_EXIT_SNIFF_MODE
);
883 conn
= hci_conn_hash_lookup_handle(hdev
, __le16_to_cpu(cp
->handle
));
885 clear_bit(HCI_CONN_MODE_CHANGE_PEND
, &conn
->pend
);
887 if (test_and_clear_bit(HCI_CONN_SCO_SETUP_PEND
, &conn
->pend
))
888 hci_sco_setup(conn
, status
);
891 hci_dev_unlock(hdev
);
894 static inline void hci_inquiry_complete_evt(struct hci_dev
*hdev
, struct sk_buff
*skb
)
896 __u8 status
= *((__u8
*) skb
->data
);
898 BT_DBG("%s status %d", hdev
->name
, status
);
900 clear_bit(HCI_INQUIRY
, &hdev
->flags
);
902 hci_req_complete(hdev
, HCI_OP_INQUIRY
, status
);
904 hci_conn_check_pending(hdev
);
907 static inline void hci_inquiry_result_evt(struct hci_dev
*hdev
, struct sk_buff
*skb
)
909 struct inquiry_data data
;
910 struct inquiry_info
*info
= (void *) (skb
->data
+ 1);
911 int num_rsp
= *((__u8
*) skb
->data
);
913 BT_DBG("%s num_rsp %d", hdev
->name
, num_rsp
);
920 for (; num_rsp
; num_rsp
--) {
921 bacpy(&data
.bdaddr
, &info
->bdaddr
);
922 data
.pscan_rep_mode
= info
->pscan_rep_mode
;
923 data
.pscan_period_mode
= info
->pscan_period_mode
;
924 data
.pscan_mode
= info
->pscan_mode
;
925 memcpy(data
.dev_class
, info
->dev_class
, 3);
926 data
.clock_offset
= info
->clock_offset
;
928 data
.ssp_mode
= 0x00;
930 hci_inquiry_cache_update(hdev
, &data
);
933 hci_dev_unlock(hdev
);
936 static inline void hci_conn_complete_evt(struct hci_dev
*hdev
, struct sk_buff
*skb
)
938 struct hci_ev_conn_complete
*ev
= (void *) skb
->data
;
939 struct hci_conn
*conn
;
941 BT_DBG("%s", hdev
->name
);
945 conn
= hci_conn_hash_lookup_ba(hdev
, ev
->link_type
, &ev
->bdaddr
);
947 if (ev
->link_type
!= SCO_LINK
)
950 conn
= hci_conn_hash_lookup_ba(hdev
, ESCO_LINK
, &ev
->bdaddr
);
954 conn
->type
= SCO_LINK
;
958 conn
->handle
= __le16_to_cpu(ev
->handle
);
960 if (conn
->type
== ACL_LINK
) {
961 conn
->state
= BT_CONFIG
;
963 conn
->disc_timeout
= HCI_DISCONN_TIMEOUT
;
965 conn
->state
= BT_CONNECTED
;
967 hci_conn_hold_device(conn
);
968 hci_conn_add_sysfs(conn
);
970 if (test_bit(HCI_AUTH
, &hdev
->flags
))
971 conn
->link_mode
|= HCI_LM_AUTH
;
973 if (test_bit(HCI_ENCRYPT
, &hdev
->flags
))
974 conn
->link_mode
|= HCI_LM_ENCRYPT
;
976 /* Get remote features */
977 if (conn
->type
== ACL_LINK
) {
978 struct hci_cp_read_remote_features cp
;
979 cp
.handle
= ev
->handle
;
980 hci_send_cmd(hdev
, HCI_OP_READ_REMOTE_FEATURES
,
984 /* Set packet type for incoming connection */
985 if (!conn
->out
&& hdev
->hci_ver
< 3) {
986 struct hci_cp_change_conn_ptype cp
;
987 cp
.handle
= ev
->handle
;
988 cp
.pkt_type
= cpu_to_le16(conn
->pkt_type
);
989 hci_send_cmd(hdev
, HCI_OP_CHANGE_CONN_PTYPE
,
993 conn
->state
= BT_CLOSED
;
995 if (conn
->type
== ACL_LINK
)
996 hci_sco_setup(conn
, ev
->status
);
999 hci_proto_connect_cfm(conn
, ev
->status
);
1001 } else if (ev
->link_type
!= ACL_LINK
)
1002 hci_proto_connect_cfm(conn
, ev
->status
);
1005 hci_dev_unlock(hdev
);
1007 hci_conn_check_pending(hdev
);
1010 static inline void hci_conn_request_evt(struct hci_dev
*hdev
, struct sk_buff
*skb
)
1012 struct hci_ev_conn_request
*ev
= (void *) skb
->data
;
1013 int mask
= hdev
->link_mode
;
1015 BT_DBG("%s bdaddr %s type 0x%x", hdev
->name
,
1016 batostr(&ev
->bdaddr
), ev
->link_type
);
1018 mask
|= hci_proto_connect_ind(hdev
, &ev
->bdaddr
, ev
->link_type
);
1020 if ((mask
& HCI_LM_ACCEPT
) && !hci_blacklist_lookup(hdev
, &ev
->bdaddr
)) {
1021 /* Connection accepted */
1022 struct inquiry_entry
*ie
;
1023 struct hci_conn
*conn
;
1027 ie
= hci_inquiry_cache_lookup(hdev
, &ev
->bdaddr
);
1029 memcpy(ie
->data
.dev_class
, ev
->dev_class
, 3);
1031 conn
= hci_conn_hash_lookup_ba(hdev
, ev
->link_type
, &ev
->bdaddr
);
1033 conn
= hci_conn_add(hdev
, ev
->link_type
, &ev
->bdaddr
);
1035 BT_ERR("No memory for new connection");
1036 hci_dev_unlock(hdev
);
1041 memcpy(conn
->dev_class
, ev
->dev_class
, 3);
1042 conn
->state
= BT_CONNECT
;
1044 hci_dev_unlock(hdev
);
1046 if (ev
->link_type
== ACL_LINK
|| !lmp_esco_capable(hdev
)) {
1047 struct hci_cp_accept_conn_req cp
;
1049 bacpy(&cp
.bdaddr
, &ev
->bdaddr
);
1051 if (lmp_rswitch_capable(hdev
) && (mask
& HCI_LM_MASTER
))
1052 cp
.role
= 0x00; /* Become master */
1054 cp
.role
= 0x01; /* Remain slave */
1056 hci_send_cmd(hdev
, HCI_OP_ACCEPT_CONN_REQ
,
1059 struct hci_cp_accept_sync_conn_req cp
;
1061 bacpy(&cp
.bdaddr
, &ev
->bdaddr
);
1062 cp
.pkt_type
= cpu_to_le16(conn
->pkt_type
);
1064 cp
.tx_bandwidth
= cpu_to_le32(0x00001f40);
1065 cp
.rx_bandwidth
= cpu_to_le32(0x00001f40);
1066 cp
.max_latency
= cpu_to_le16(0xffff);
1067 cp
.content_format
= cpu_to_le16(hdev
->voice_setting
);
1068 cp
.retrans_effort
= 0xff;
1070 hci_send_cmd(hdev
, HCI_OP_ACCEPT_SYNC_CONN_REQ
,
1074 /* Connection rejected */
1075 struct hci_cp_reject_conn_req cp
;
1077 bacpy(&cp
.bdaddr
, &ev
->bdaddr
);
1079 hci_send_cmd(hdev
, HCI_OP_REJECT_CONN_REQ
, sizeof(cp
), &cp
);
1083 static inline void hci_disconn_complete_evt(struct hci_dev
*hdev
, struct sk_buff
*skb
)
1085 struct hci_ev_disconn_complete
*ev
= (void *) skb
->data
;
1086 struct hci_conn
*conn
;
1088 BT_DBG("%s status %d", hdev
->name
, ev
->status
);
1095 conn
= hci_conn_hash_lookup_handle(hdev
, __le16_to_cpu(ev
->handle
));
1097 conn
->state
= BT_CLOSED
;
1099 hci_proto_disconn_cfm(conn
, ev
->reason
);
1103 hci_dev_unlock(hdev
);
1106 static inline void hci_auth_complete_evt(struct hci_dev
*hdev
, struct sk_buff
*skb
)
1108 struct hci_ev_auth_complete
*ev
= (void *) skb
->data
;
1109 struct hci_conn
*conn
;
1111 BT_DBG("%s status %d", hdev
->name
, ev
->status
);
1115 conn
= hci_conn_hash_lookup_handle(hdev
, __le16_to_cpu(ev
->handle
));
1118 conn
->link_mode
|= HCI_LM_AUTH
;
1119 conn
->sec_level
= conn
->pending_sec_level
;
1121 conn
->sec_level
= BT_SECURITY_LOW
;
1123 clear_bit(HCI_CONN_AUTH_PEND
, &conn
->pend
);
1125 if (conn
->state
== BT_CONFIG
) {
1126 if (!ev
->status
&& hdev
->ssp_mode
> 0 &&
1127 conn
->ssp_mode
> 0) {
1128 struct hci_cp_set_conn_encrypt cp
;
1129 cp
.handle
= ev
->handle
;
1131 hci_send_cmd(hdev
, HCI_OP_SET_CONN_ENCRYPT
,
1134 conn
->state
= BT_CONNECTED
;
1135 hci_proto_connect_cfm(conn
, ev
->status
);
1139 hci_auth_cfm(conn
, ev
->status
);
1141 hci_conn_hold(conn
);
1142 conn
->disc_timeout
= HCI_DISCONN_TIMEOUT
;
1146 if (test_bit(HCI_CONN_ENCRYPT_PEND
, &conn
->pend
)) {
1148 struct hci_cp_set_conn_encrypt cp
;
1149 cp
.handle
= ev
->handle
;
1151 hci_send_cmd(hdev
, HCI_OP_SET_CONN_ENCRYPT
,
1154 clear_bit(HCI_CONN_ENCRYPT_PEND
, &conn
->pend
);
1155 hci_encrypt_cfm(conn
, ev
->status
, 0x00);
1160 hci_dev_unlock(hdev
);
1163 static inline void hci_remote_name_evt(struct hci_dev
*hdev
, struct sk_buff
*skb
)
1165 struct hci_ev_remote_name
*ev
= (void *) skb
->data
;
1166 struct hci_conn
*conn
;
1168 BT_DBG("%s", hdev
->name
);
1170 hci_conn_check_pending(hdev
);
1174 conn
= hci_conn_hash_lookup_ba(hdev
, ACL_LINK
, &ev
->bdaddr
);
1175 if (conn
&& hci_outgoing_auth_needed(hdev
, conn
)) {
1176 struct hci_cp_auth_requested cp
;
1177 cp
.handle
= __cpu_to_le16(conn
->handle
);
1178 hci_send_cmd(hdev
, HCI_OP_AUTH_REQUESTED
, sizeof(cp
), &cp
);
1181 hci_dev_unlock(hdev
);
1184 static inline void hci_encrypt_change_evt(struct hci_dev
*hdev
, struct sk_buff
*skb
)
1186 struct hci_ev_encrypt_change
*ev
= (void *) skb
->data
;
1187 struct hci_conn
*conn
;
1189 BT_DBG("%s status %d", hdev
->name
, ev
->status
);
1193 conn
= hci_conn_hash_lookup_handle(hdev
, __le16_to_cpu(ev
->handle
));
1197 /* Encryption implies authentication */
1198 conn
->link_mode
|= HCI_LM_AUTH
;
1199 conn
->link_mode
|= HCI_LM_ENCRYPT
;
1201 conn
->link_mode
&= ~HCI_LM_ENCRYPT
;
1204 clear_bit(HCI_CONN_ENCRYPT_PEND
, &conn
->pend
);
1206 if (conn
->state
== BT_CONFIG
) {
1208 conn
->state
= BT_CONNECTED
;
1210 hci_proto_connect_cfm(conn
, ev
->status
);
1213 hci_encrypt_cfm(conn
, ev
->status
, ev
->encrypt
);
1216 hci_dev_unlock(hdev
);
1219 static inline void hci_change_link_key_complete_evt(struct hci_dev
*hdev
, struct sk_buff
*skb
)
1221 struct hci_ev_change_link_key_complete
*ev
= (void *) skb
->data
;
1222 struct hci_conn
*conn
;
1224 BT_DBG("%s status %d", hdev
->name
, ev
->status
);
1228 conn
= hci_conn_hash_lookup_handle(hdev
, __le16_to_cpu(ev
->handle
));
1231 conn
->link_mode
|= HCI_LM_SECURE
;
1233 clear_bit(HCI_CONN_AUTH_PEND
, &conn
->pend
);
1235 hci_key_change_cfm(conn
, ev
->status
);
1238 hci_dev_unlock(hdev
);
1241 static inline void hci_remote_features_evt(struct hci_dev
*hdev
, struct sk_buff
*skb
)
1243 struct hci_ev_remote_features
*ev
= (void *) skb
->data
;
1244 struct hci_conn
*conn
;
1246 BT_DBG("%s status %d", hdev
->name
, ev
->status
);
1250 conn
= hci_conn_hash_lookup_handle(hdev
, __le16_to_cpu(ev
->handle
));
1255 memcpy(conn
->features
, ev
->features
, 8);
1257 if (conn
->state
!= BT_CONFIG
)
1260 if (!ev
->status
&& lmp_ssp_capable(hdev
) && lmp_ssp_capable(conn
)) {
1261 struct hci_cp_read_remote_ext_features cp
;
1262 cp
.handle
= ev
->handle
;
1264 hci_send_cmd(hdev
, HCI_OP_READ_REMOTE_EXT_FEATURES
,
1270 struct hci_cp_remote_name_req cp
;
1271 memset(&cp
, 0, sizeof(cp
));
1272 bacpy(&cp
.bdaddr
, &conn
->dst
);
1273 cp
.pscan_rep_mode
= 0x02;
1274 hci_send_cmd(hdev
, HCI_OP_REMOTE_NAME_REQ
, sizeof(cp
), &cp
);
1277 if (!hci_outgoing_auth_needed(hdev
, conn
)) {
1278 conn
->state
= BT_CONNECTED
;
1279 hci_proto_connect_cfm(conn
, ev
->status
);
1284 hci_dev_unlock(hdev
);
1287 static inline void hci_remote_version_evt(struct hci_dev
*hdev
, struct sk_buff
*skb
)
1289 BT_DBG("%s", hdev
->name
);
1292 static inline void hci_qos_setup_complete_evt(struct hci_dev
*hdev
, struct sk_buff
*skb
)
1294 BT_DBG("%s", hdev
->name
);
1297 static inline void hci_cmd_complete_evt(struct hci_dev
*hdev
, struct sk_buff
*skb
)
1299 struct hci_ev_cmd_complete
*ev
= (void *) skb
->data
;
1302 skb_pull(skb
, sizeof(*ev
));
1304 opcode
= __le16_to_cpu(ev
->opcode
);
1307 case HCI_OP_INQUIRY_CANCEL
:
1308 hci_cc_inquiry_cancel(hdev
, skb
);
1311 case HCI_OP_EXIT_PERIODIC_INQ
:
1312 hci_cc_exit_periodic_inq(hdev
, skb
);
1315 case HCI_OP_REMOTE_NAME_REQ_CANCEL
:
1316 hci_cc_remote_name_req_cancel(hdev
, skb
);
1319 case HCI_OP_ROLE_DISCOVERY
:
1320 hci_cc_role_discovery(hdev
, skb
);
1323 case HCI_OP_READ_LINK_POLICY
:
1324 hci_cc_read_link_policy(hdev
, skb
);
1327 case HCI_OP_WRITE_LINK_POLICY
:
1328 hci_cc_write_link_policy(hdev
, skb
);
1331 case HCI_OP_READ_DEF_LINK_POLICY
:
1332 hci_cc_read_def_link_policy(hdev
, skb
);
1335 case HCI_OP_WRITE_DEF_LINK_POLICY
:
1336 hci_cc_write_def_link_policy(hdev
, skb
);
1340 hci_cc_reset(hdev
, skb
);
1343 case HCI_OP_WRITE_LOCAL_NAME
:
1344 hci_cc_write_local_name(hdev
, skb
);
1347 case HCI_OP_READ_LOCAL_NAME
:
1348 hci_cc_read_local_name(hdev
, skb
);
1351 case HCI_OP_WRITE_AUTH_ENABLE
:
1352 hci_cc_write_auth_enable(hdev
, skb
);
1355 case HCI_OP_WRITE_ENCRYPT_MODE
:
1356 hci_cc_write_encrypt_mode(hdev
, skb
);
1359 case HCI_OP_WRITE_SCAN_ENABLE
:
1360 hci_cc_write_scan_enable(hdev
, skb
);
1363 case HCI_OP_READ_CLASS_OF_DEV
:
1364 hci_cc_read_class_of_dev(hdev
, skb
);
1367 case HCI_OP_WRITE_CLASS_OF_DEV
:
1368 hci_cc_write_class_of_dev(hdev
, skb
);
1371 case HCI_OP_READ_VOICE_SETTING
:
1372 hci_cc_read_voice_setting(hdev
, skb
);
1375 case HCI_OP_WRITE_VOICE_SETTING
:
1376 hci_cc_write_voice_setting(hdev
, skb
);
1379 case HCI_OP_HOST_BUFFER_SIZE
:
1380 hci_cc_host_buffer_size(hdev
, skb
);
1383 case HCI_OP_READ_SSP_MODE
:
1384 hci_cc_read_ssp_mode(hdev
, skb
);
1387 case HCI_OP_WRITE_SSP_MODE
:
1388 hci_cc_write_ssp_mode(hdev
, skb
);
1391 case HCI_OP_READ_LOCAL_VERSION
:
1392 hci_cc_read_local_version(hdev
, skb
);
1395 case HCI_OP_READ_LOCAL_COMMANDS
:
1396 hci_cc_read_local_commands(hdev
, skb
);
1399 case HCI_OP_READ_LOCAL_FEATURES
:
1400 hci_cc_read_local_features(hdev
, skb
);
1403 case HCI_OP_READ_BUFFER_SIZE
:
1404 hci_cc_read_buffer_size(hdev
, skb
);
1407 case HCI_OP_READ_BD_ADDR
:
1408 hci_cc_read_bd_addr(hdev
, skb
);
1411 case HCI_OP_WRITE_CA_TIMEOUT
:
1412 hci_cc_write_ca_timeout(hdev
, skb
);
1415 case HCI_OP_DELETE_STORED_LINK_KEY
:
1416 hci_cc_delete_stored_link_key(hdev
, skb
);
1420 BT_DBG("%s opcode 0x%x", hdev
->name
, opcode
);
1425 atomic_set(&hdev
->cmd_cnt
, 1);
1426 if (!skb_queue_empty(&hdev
->cmd_q
))
1427 tasklet_schedule(&hdev
->cmd_task
);
1431 static inline void hci_cmd_status_evt(struct hci_dev
*hdev
, struct sk_buff
*skb
)
1433 struct hci_ev_cmd_status
*ev
= (void *) skb
->data
;
1436 skb_pull(skb
, sizeof(*ev
));
1438 opcode
= __le16_to_cpu(ev
->opcode
);
1441 case HCI_OP_INQUIRY
:
1442 hci_cs_inquiry(hdev
, ev
->status
);
1445 case HCI_OP_CREATE_CONN
:
1446 hci_cs_create_conn(hdev
, ev
->status
);
1449 case HCI_OP_ADD_SCO
:
1450 hci_cs_add_sco(hdev
, ev
->status
);
1453 case HCI_OP_AUTH_REQUESTED
:
1454 hci_cs_auth_requested(hdev
, ev
->status
);
1457 case HCI_OP_SET_CONN_ENCRYPT
:
1458 hci_cs_set_conn_encrypt(hdev
, ev
->status
);
1461 case HCI_OP_REMOTE_NAME_REQ
:
1462 hci_cs_remote_name_req(hdev
, ev
->status
);
1465 case HCI_OP_READ_REMOTE_FEATURES
:
1466 hci_cs_read_remote_features(hdev
, ev
->status
);
1469 case HCI_OP_READ_REMOTE_EXT_FEATURES
:
1470 hci_cs_read_remote_ext_features(hdev
, ev
->status
);
1473 case HCI_OP_SETUP_SYNC_CONN
:
1474 hci_cs_setup_sync_conn(hdev
, ev
->status
);
1477 case HCI_OP_SNIFF_MODE
:
1478 hci_cs_sniff_mode(hdev
, ev
->status
);
1481 case HCI_OP_EXIT_SNIFF_MODE
:
1482 hci_cs_exit_sniff_mode(hdev
, ev
->status
);
1486 BT_DBG("%s opcode 0x%x", hdev
->name
, opcode
);
1491 atomic_set(&hdev
->cmd_cnt
, 1);
1492 if (!skb_queue_empty(&hdev
->cmd_q
))
1493 tasklet_schedule(&hdev
->cmd_task
);
1497 static inline void hci_role_change_evt(struct hci_dev
*hdev
, struct sk_buff
*skb
)
1499 struct hci_ev_role_change
*ev
= (void *) skb
->data
;
1500 struct hci_conn
*conn
;
1502 BT_DBG("%s status %d", hdev
->name
, ev
->status
);
1506 conn
= hci_conn_hash_lookup_ba(hdev
, ACL_LINK
, &ev
->bdaddr
);
1510 conn
->link_mode
&= ~HCI_LM_MASTER
;
1512 conn
->link_mode
|= HCI_LM_MASTER
;
1515 clear_bit(HCI_CONN_RSWITCH_PEND
, &conn
->pend
);
1517 hci_role_switch_cfm(conn
, ev
->status
, ev
->role
);
1520 hci_dev_unlock(hdev
);
1523 static inline void hci_num_comp_pkts_evt(struct hci_dev
*hdev
, struct sk_buff
*skb
)
1525 struct hci_ev_num_comp_pkts
*ev
= (void *) skb
->data
;
1529 skb_pull(skb
, sizeof(*ev
));
1531 BT_DBG("%s num_hndl %d", hdev
->name
, ev
->num_hndl
);
1533 if (skb
->len
< ev
->num_hndl
* 4) {
1534 BT_DBG("%s bad parameters", hdev
->name
);
1538 tasklet_disable(&hdev
->tx_task
);
1540 for (i
= 0, ptr
= (__le16
*) skb
->data
; i
< ev
->num_hndl
; i
++) {
1541 struct hci_conn
*conn
;
1542 __u16 handle
, count
;
1544 handle
= get_unaligned_le16(ptr
++);
1545 count
= get_unaligned_le16(ptr
++);
1547 conn
= hci_conn_hash_lookup_handle(hdev
, handle
);
1549 conn
->sent
-= count
;
1551 if (conn
->type
== ACL_LINK
) {
1552 hdev
->acl_cnt
+= count
;
1553 if (hdev
->acl_cnt
> hdev
->acl_pkts
)
1554 hdev
->acl_cnt
= hdev
->acl_pkts
;
1556 hdev
->sco_cnt
+= count
;
1557 if (hdev
->sco_cnt
> hdev
->sco_pkts
)
1558 hdev
->sco_cnt
= hdev
->sco_pkts
;
1563 tasklet_schedule(&hdev
->tx_task
);
1565 tasklet_enable(&hdev
->tx_task
);
1568 static inline void hci_mode_change_evt(struct hci_dev
*hdev
, struct sk_buff
*skb
)
1570 struct hci_ev_mode_change
*ev
= (void *) skb
->data
;
1571 struct hci_conn
*conn
;
1573 BT_DBG("%s status %d", hdev
->name
, ev
->status
);
1577 conn
= hci_conn_hash_lookup_handle(hdev
, __le16_to_cpu(ev
->handle
));
1579 conn
->mode
= ev
->mode
;
1580 conn
->interval
= __le16_to_cpu(ev
->interval
);
1582 if (!test_and_clear_bit(HCI_CONN_MODE_CHANGE_PEND
, &conn
->pend
)) {
1583 if (conn
->mode
== HCI_CM_ACTIVE
)
1584 conn
->power_save
= 1;
1586 conn
->power_save
= 0;
1589 if (test_and_clear_bit(HCI_CONN_SCO_SETUP_PEND
, &conn
->pend
))
1590 hci_sco_setup(conn
, ev
->status
);
1593 hci_dev_unlock(hdev
);
1596 static inline void hci_pin_code_request_evt(struct hci_dev
*hdev
, struct sk_buff
*skb
)
1598 struct hci_ev_pin_code_req
*ev
= (void *) skb
->data
;
1599 struct hci_conn
*conn
;
1601 BT_DBG("%s", hdev
->name
);
1605 conn
= hci_conn_hash_lookup_ba(hdev
, ACL_LINK
, &ev
->bdaddr
);
1606 if (conn
&& conn
->state
== BT_CONNECTED
) {
1607 hci_conn_hold(conn
);
1608 conn
->disc_timeout
= HCI_PAIRING_TIMEOUT
;
1612 if (!test_bit(HCI_PAIRABLE
, &hdev
->flags
))
1613 hci_send_cmd(hdev
, HCI_OP_PIN_CODE_NEG_REPLY
,
1614 sizeof(ev
->bdaddr
), &ev
->bdaddr
);
1616 hci_dev_unlock(hdev
);
1619 static inline void hci_link_key_request_evt(struct hci_dev
*hdev
, struct sk_buff
*skb
)
1621 BT_DBG("%s", hdev
->name
);
1624 static inline void hci_link_key_notify_evt(struct hci_dev
*hdev
, struct sk_buff
*skb
)
1626 struct hci_ev_link_key_notify
*ev
= (void *) skb
->data
;
1627 struct hci_conn
*conn
;
1629 BT_DBG("%s", hdev
->name
);
1633 conn
= hci_conn_hash_lookup_ba(hdev
, ACL_LINK
, &ev
->bdaddr
);
1635 hci_conn_hold(conn
);
1636 conn
->disc_timeout
= HCI_DISCONN_TIMEOUT
;
1640 hci_dev_unlock(hdev
);
1643 static inline void hci_clock_offset_evt(struct hci_dev
*hdev
, struct sk_buff
*skb
)
1645 struct hci_ev_clock_offset
*ev
= (void *) skb
->data
;
1646 struct hci_conn
*conn
;
1648 BT_DBG("%s status %d", hdev
->name
, ev
->status
);
1652 conn
= hci_conn_hash_lookup_handle(hdev
, __le16_to_cpu(ev
->handle
));
1653 if (conn
&& !ev
->status
) {
1654 struct inquiry_entry
*ie
;
1656 ie
= hci_inquiry_cache_lookup(hdev
, &conn
->dst
);
1658 ie
->data
.clock_offset
= ev
->clock_offset
;
1659 ie
->timestamp
= jiffies
;
1663 hci_dev_unlock(hdev
);
1666 static inline void hci_pkt_type_change_evt(struct hci_dev
*hdev
, struct sk_buff
*skb
)
1668 struct hci_ev_pkt_type_change
*ev
= (void *) skb
->data
;
1669 struct hci_conn
*conn
;
1671 BT_DBG("%s status %d", hdev
->name
, ev
->status
);
1675 conn
= hci_conn_hash_lookup_handle(hdev
, __le16_to_cpu(ev
->handle
));
1676 if (conn
&& !ev
->status
)
1677 conn
->pkt_type
= __le16_to_cpu(ev
->pkt_type
);
1679 hci_dev_unlock(hdev
);
1682 static inline void hci_pscan_rep_mode_evt(struct hci_dev
*hdev
, struct sk_buff
*skb
)
1684 struct hci_ev_pscan_rep_mode
*ev
= (void *) skb
->data
;
1685 struct inquiry_entry
*ie
;
1687 BT_DBG("%s", hdev
->name
);
1691 ie
= hci_inquiry_cache_lookup(hdev
, &ev
->bdaddr
);
1693 ie
->data
.pscan_rep_mode
= ev
->pscan_rep_mode
;
1694 ie
->timestamp
= jiffies
;
1697 hci_dev_unlock(hdev
);
1700 static inline void hci_inquiry_result_with_rssi_evt(struct hci_dev
*hdev
, struct sk_buff
*skb
)
1702 struct inquiry_data data
;
1703 int num_rsp
= *((__u8
*) skb
->data
);
1705 BT_DBG("%s num_rsp %d", hdev
->name
, num_rsp
);
1712 if ((skb
->len
- 1) / num_rsp
!= sizeof(struct inquiry_info_with_rssi
)) {
1713 struct inquiry_info_with_rssi_and_pscan_mode
*info
= (void *) (skb
->data
+ 1);
1715 for (; num_rsp
; num_rsp
--) {
1716 bacpy(&data
.bdaddr
, &info
->bdaddr
);
1717 data
.pscan_rep_mode
= info
->pscan_rep_mode
;
1718 data
.pscan_period_mode
= info
->pscan_period_mode
;
1719 data
.pscan_mode
= info
->pscan_mode
;
1720 memcpy(data
.dev_class
, info
->dev_class
, 3);
1721 data
.clock_offset
= info
->clock_offset
;
1722 data
.rssi
= info
->rssi
;
1723 data
.ssp_mode
= 0x00;
1725 hci_inquiry_cache_update(hdev
, &data
);
1728 struct inquiry_info_with_rssi
*info
= (void *) (skb
->data
+ 1);
1730 for (; num_rsp
; num_rsp
--) {
1731 bacpy(&data
.bdaddr
, &info
->bdaddr
);
1732 data
.pscan_rep_mode
= info
->pscan_rep_mode
;
1733 data
.pscan_period_mode
= info
->pscan_period_mode
;
1734 data
.pscan_mode
= 0x00;
1735 memcpy(data
.dev_class
, info
->dev_class
, 3);
1736 data
.clock_offset
= info
->clock_offset
;
1737 data
.rssi
= info
->rssi
;
1738 data
.ssp_mode
= 0x00;
1740 hci_inquiry_cache_update(hdev
, &data
);
1744 hci_dev_unlock(hdev
);
1747 static inline void hci_remote_ext_features_evt(struct hci_dev
*hdev
, struct sk_buff
*skb
)
1749 struct hci_ev_remote_ext_features
*ev
= (void *) skb
->data
;
1750 struct hci_conn
*conn
;
1752 BT_DBG("%s", hdev
->name
);
1756 conn
= hci_conn_hash_lookup_handle(hdev
, __le16_to_cpu(ev
->handle
));
1760 if (!ev
->status
&& ev
->page
== 0x01) {
1761 struct inquiry_entry
*ie
;
1763 ie
= hci_inquiry_cache_lookup(hdev
, &conn
->dst
);
1765 ie
->data
.ssp_mode
= (ev
->features
[0] & 0x01);
1767 conn
->ssp_mode
= (ev
->features
[0] & 0x01);
1770 if (conn
->state
!= BT_CONFIG
)
1774 struct hci_cp_remote_name_req cp
;
1775 memset(&cp
, 0, sizeof(cp
));
1776 bacpy(&cp
.bdaddr
, &conn
->dst
);
1777 cp
.pscan_rep_mode
= 0x02;
1778 hci_send_cmd(hdev
, HCI_OP_REMOTE_NAME_REQ
, sizeof(cp
), &cp
);
1781 if (!hci_outgoing_auth_needed(hdev
, conn
)) {
1782 conn
->state
= BT_CONNECTED
;
1783 hci_proto_connect_cfm(conn
, ev
->status
);
1788 hci_dev_unlock(hdev
);
1791 static inline void hci_sync_conn_complete_evt(struct hci_dev
*hdev
, struct sk_buff
*skb
)
1793 struct hci_ev_sync_conn_complete
*ev
= (void *) skb
->data
;
1794 struct hci_conn
*conn
;
1796 BT_DBG("%s status %d", hdev
->name
, ev
->status
);
1800 conn
= hci_conn_hash_lookup_ba(hdev
, ev
->link_type
, &ev
->bdaddr
);
1802 if (ev
->link_type
== ESCO_LINK
)
1805 conn
= hci_conn_hash_lookup_ba(hdev
, ESCO_LINK
, &ev
->bdaddr
);
1809 conn
->type
= SCO_LINK
;
1812 switch (ev
->status
) {
1814 conn
->handle
= __le16_to_cpu(ev
->handle
);
1815 conn
->state
= BT_CONNECTED
;
1817 hci_conn_hold_device(conn
);
1818 hci_conn_add_sysfs(conn
);
1821 case 0x11: /* Unsupported Feature or Parameter Value */
1822 case 0x1c: /* SCO interval rejected */
1823 case 0x1a: /* Unsupported Remote Feature */
1824 case 0x1f: /* Unspecified error */
1825 if (conn
->out
&& conn
->attempt
< 2) {
1826 conn
->pkt_type
= (hdev
->esco_type
& SCO_ESCO_MASK
) |
1827 (hdev
->esco_type
& EDR_ESCO_MASK
);
1828 hci_setup_sync(conn
, conn
->link
->handle
);
1834 conn
->state
= BT_CLOSED
;
1838 hci_proto_connect_cfm(conn
, ev
->status
);
1843 hci_dev_unlock(hdev
);
1846 static inline void hci_sync_conn_changed_evt(struct hci_dev
*hdev
, struct sk_buff
*skb
)
1848 BT_DBG("%s", hdev
->name
);
1851 static inline void hci_sniff_subrate_evt(struct hci_dev
*hdev
, struct sk_buff
*skb
)
1853 struct hci_ev_sniff_subrate
*ev
= (void *) skb
->data
;
1854 struct hci_conn
*conn
;
1856 BT_DBG("%s status %d", hdev
->name
, ev
->status
);
1860 conn
= hci_conn_hash_lookup_handle(hdev
, __le16_to_cpu(ev
->handle
));
1864 hci_dev_unlock(hdev
);
1867 static inline void hci_extended_inquiry_result_evt(struct hci_dev
*hdev
, struct sk_buff
*skb
)
1869 struct inquiry_data data
;
1870 struct extended_inquiry_info
*info
= (void *) (skb
->data
+ 1);
1871 int num_rsp
= *((__u8
*) skb
->data
);
1873 BT_DBG("%s num_rsp %d", hdev
->name
, num_rsp
);
1880 for (; num_rsp
; num_rsp
--) {
1881 bacpy(&data
.bdaddr
, &info
->bdaddr
);
1882 data
.pscan_rep_mode
= info
->pscan_rep_mode
;
1883 data
.pscan_period_mode
= info
->pscan_period_mode
;
1884 data
.pscan_mode
= 0x00;
1885 memcpy(data
.dev_class
, info
->dev_class
, 3);
1886 data
.clock_offset
= info
->clock_offset
;
1887 data
.rssi
= info
->rssi
;
1888 data
.ssp_mode
= 0x01;
1890 hci_inquiry_cache_update(hdev
, &data
);
1893 hci_dev_unlock(hdev
);
1896 static inline void hci_io_capa_request_evt(struct hci_dev
*hdev
, struct sk_buff
*skb
)
1898 struct hci_ev_io_capa_request
*ev
= (void *) skb
->data
;
1899 struct hci_conn
*conn
;
1901 BT_DBG("%s", hdev
->name
);
1905 conn
= hci_conn_hash_lookup_ba(hdev
, ACL_LINK
, &ev
->bdaddr
);
1909 hci_conn_hold(conn
);
1911 if (!test_bit(HCI_MGMT
, &hdev
->flags
))
1914 if (test_bit(HCI_PAIRABLE
, &hdev
->flags
) ||
1915 (conn
->remote_auth
& ~0x01) == HCI_AT_NO_BONDING
) {
1916 /* FIXME: Do IO capa response based on information
1917 * provided through the management interface */
1919 struct hci_cp_io_capability_neg_reply cp
;
1921 bacpy(&cp
.bdaddr
, &ev
->bdaddr
);
1922 cp
.reason
= 0x16; /* Pairing not allowed */
1924 hci_send_cmd(hdev
, HCI_OP_IO_CAPABILITY_NEG_REPLY
,
1929 hci_dev_unlock(hdev
);
1932 static inline void hci_io_capa_reply_evt(struct hci_dev
*hdev
, struct sk_buff
*skb
)
1934 struct hci_ev_io_capa_reply
*ev
= (void *) skb
->data
;
1935 struct hci_conn
*conn
;
1937 BT_DBG("%s", hdev
->name
);
1941 conn
= hci_conn_hash_lookup_ba(hdev
, ACL_LINK
, &ev
->bdaddr
);
1945 hci_conn_hold(conn
);
1947 conn
->remote_cap
= ev
->capability
;
1948 conn
->remote_oob
= ev
->oob_data
;
1949 conn
->remote_auth
= ev
->authentication
;
1952 hci_dev_unlock(hdev
);
1955 static inline void hci_simple_pair_complete_evt(struct hci_dev
*hdev
, struct sk_buff
*skb
)
1957 struct hci_ev_simple_pair_complete
*ev
= (void *) skb
->data
;
1958 struct hci_conn
*conn
;
1960 BT_DBG("%s", hdev
->name
);
1964 conn
= hci_conn_hash_lookup_ba(hdev
, ACL_LINK
, &ev
->bdaddr
);
1968 hci_dev_unlock(hdev
);
1971 static inline void hci_remote_host_features_evt(struct hci_dev
*hdev
, struct sk_buff
*skb
)
1973 struct hci_ev_remote_host_features
*ev
= (void *) skb
->data
;
1974 struct inquiry_entry
*ie
;
1976 BT_DBG("%s", hdev
->name
);
1980 ie
= hci_inquiry_cache_lookup(hdev
, &ev
->bdaddr
);
1982 ie
->data
.ssp_mode
= (ev
->features
[0] & 0x01);
1984 hci_dev_unlock(hdev
);
1987 void hci_event_packet(struct hci_dev
*hdev
, struct sk_buff
*skb
)
1989 struct hci_event_hdr
*hdr
= (void *) skb
->data
;
1990 __u8 event
= hdr
->evt
;
1992 skb_pull(skb
, HCI_EVENT_HDR_SIZE
);
1995 case HCI_EV_INQUIRY_COMPLETE
:
1996 hci_inquiry_complete_evt(hdev
, skb
);
1999 case HCI_EV_INQUIRY_RESULT
:
2000 hci_inquiry_result_evt(hdev
, skb
);
2003 case HCI_EV_CONN_COMPLETE
:
2004 hci_conn_complete_evt(hdev
, skb
);
2007 case HCI_EV_CONN_REQUEST
:
2008 hci_conn_request_evt(hdev
, skb
);
2011 case HCI_EV_DISCONN_COMPLETE
:
2012 hci_disconn_complete_evt(hdev
, skb
);
2015 case HCI_EV_AUTH_COMPLETE
:
2016 hci_auth_complete_evt(hdev
, skb
);
2019 case HCI_EV_REMOTE_NAME
:
2020 hci_remote_name_evt(hdev
, skb
);
2023 case HCI_EV_ENCRYPT_CHANGE
:
2024 hci_encrypt_change_evt(hdev
, skb
);
2027 case HCI_EV_CHANGE_LINK_KEY_COMPLETE
:
2028 hci_change_link_key_complete_evt(hdev
, skb
);
2031 case HCI_EV_REMOTE_FEATURES
:
2032 hci_remote_features_evt(hdev
, skb
);
2035 case HCI_EV_REMOTE_VERSION
:
2036 hci_remote_version_evt(hdev
, skb
);
2039 case HCI_EV_QOS_SETUP_COMPLETE
:
2040 hci_qos_setup_complete_evt(hdev
, skb
);
2043 case HCI_EV_CMD_COMPLETE
:
2044 hci_cmd_complete_evt(hdev
, skb
);
2047 case HCI_EV_CMD_STATUS
:
2048 hci_cmd_status_evt(hdev
, skb
);
2051 case HCI_EV_ROLE_CHANGE
:
2052 hci_role_change_evt(hdev
, skb
);
2055 case HCI_EV_NUM_COMP_PKTS
:
2056 hci_num_comp_pkts_evt(hdev
, skb
);
2059 case HCI_EV_MODE_CHANGE
:
2060 hci_mode_change_evt(hdev
, skb
);
2063 case HCI_EV_PIN_CODE_REQ
:
2064 hci_pin_code_request_evt(hdev
, skb
);
2067 case HCI_EV_LINK_KEY_REQ
:
2068 hci_link_key_request_evt(hdev
, skb
);
2071 case HCI_EV_LINK_KEY_NOTIFY
:
2072 hci_link_key_notify_evt(hdev
, skb
);
2075 case HCI_EV_CLOCK_OFFSET
:
2076 hci_clock_offset_evt(hdev
, skb
);
2079 case HCI_EV_PKT_TYPE_CHANGE
:
2080 hci_pkt_type_change_evt(hdev
, skb
);
2083 case HCI_EV_PSCAN_REP_MODE
:
2084 hci_pscan_rep_mode_evt(hdev
, skb
);
2087 case HCI_EV_INQUIRY_RESULT_WITH_RSSI
:
2088 hci_inquiry_result_with_rssi_evt(hdev
, skb
);
2091 case HCI_EV_REMOTE_EXT_FEATURES
:
2092 hci_remote_ext_features_evt(hdev
, skb
);
2095 case HCI_EV_SYNC_CONN_COMPLETE
:
2096 hci_sync_conn_complete_evt(hdev
, skb
);
2099 case HCI_EV_SYNC_CONN_CHANGED
:
2100 hci_sync_conn_changed_evt(hdev
, skb
);
2103 case HCI_EV_SNIFF_SUBRATE
:
2104 hci_sniff_subrate_evt(hdev
, skb
);
2107 case HCI_EV_EXTENDED_INQUIRY_RESULT
:
2108 hci_extended_inquiry_result_evt(hdev
, skb
);
2111 case HCI_EV_IO_CAPA_REQUEST
:
2112 hci_io_capa_request_evt(hdev
, skb
);
2115 case HCI_EV_IO_CAPA_REPLY
:
2116 hci_io_capa_reply_evt(hdev
, skb
);
2119 case HCI_EV_SIMPLE_PAIR_COMPLETE
:
2120 hci_simple_pair_complete_evt(hdev
, skb
);
2123 case HCI_EV_REMOTE_HOST_FEATURES
:
2124 hci_remote_host_features_evt(hdev
, skb
);
2128 BT_DBG("%s event 0x%x", hdev
->name
, event
);
2133 hdev
->stat
.evt_rx
++;
2136 /* Generate internal stack event */
2137 void hci_si_event(struct hci_dev
*hdev
, int type
, int dlen
, void *data
)
2139 struct hci_event_hdr
*hdr
;
2140 struct hci_ev_stack_internal
*ev
;
2141 struct sk_buff
*skb
;
2143 skb
= bt_skb_alloc(HCI_EVENT_HDR_SIZE
+ sizeof(*ev
) + dlen
, GFP_ATOMIC
);
2147 hdr
= (void *) skb_put(skb
, HCI_EVENT_HDR_SIZE
);
2148 hdr
->evt
= HCI_EV_STACK_INTERNAL
;
2149 hdr
->plen
= sizeof(*ev
) + dlen
;
2151 ev
= (void *) skb_put(skb
, sizeof(*ev
) + dlen
);
2153 memcpy(ev
->data
, data
, dlen
);
2155 bt_cb(skb
)->incoming
= 1;
2156 __net_timestamp(skb
);
2158 bt_cb(skb
)->pkt_type
= HCI_EVENT_PKT
;
2159 skb
->dev
= (void *) hdev
;
2160 hci_send_to_sock(hdev
, skb
, NULL
);