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 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/sched.h>
33 #include <linux/slab.h>
34 #include <linux/poll.h>
35 #include <linux/fcntl.h>
36 #include <linux/init.h>
37 #include <linux/skbuff.h>
38 #include <linux/interrupt.h>
39 #include <linux/notifier.h>
42 #include <asm/system.h>
43 #include <asm/uaccess.h>
44 #include <asm/unaligned.h>
46 #include <net/bluetooth/bluetooth.h>
47 #include <net/bluetooth/hci_core.h>
49 #ifndef CONFIG_BT_HCI_CORE_DEBUG
54 /* Handle HCI Event packets */
56 /* Command Complete OGF LINK_CTL */
57 static void hci_cc_link_ctl(struct hci_dev
*hdev
, __u16 ocf
, struct sk_buff
*skb
)
61 BT_DBG("%s ocf 0x%x", hdev
->name
, ocf
);
64 case OCF_INQUIRY_CANCEL
:
65 status
= *((__u8
*) skb
->data
);
68 BT_DBG("%s Inquiry cancel error: status 0x%x", hdev
->name
, status
);
70 clear_bit(HCI_INQUIRY
, &hdev
->flags
);
71 hci_req_complete(hdev
, status
);
76 BT_DBG("%s Command complete: ogf LINK_CTL ocf %x", hdev
->name
, ocf
);
81 /* Command Complete OGF LINK_POLICY */
82 static void hci_cc_link_policy(struct hci_dev
*hdev
, __u16 ocf
, struct sk_buff
*skb
)
84 struct hci_conn
*conn
;
85 struct hci_rp_role_discovery
*rd
;
86 struct hci_rp_write_link_policy
*lp
;
89 BT_DBG("%s ocf 0x%x", hdev
->name
, ocf
);
92 case OCF_ROLE_DISCOVERY
:
93 rd
= (void *) skb
->data
;
100 conn
= hci_conn_hash_lookup_handle(hdev
, __le16_to_cpu(rd
->handle
));
103 conn
->link_mode
&= ~HCI_LM_MASTER
;
105 conn
->link_mode
|= HCI_LM_MASTER
;
108 hci_dev_unlock(hdev
);
111 case OCF_WRITE_LINK_POLICY
:
112 sent
= hci_sent_cmd_data(hdev
, OGF_LINK_POLICY
, OCF_WRITE_LINK_POLICY
);
116 lp
= (struct hci_rp_write_link_policy
*) skb
->data
;
123 conn
= hci_conn_hash_lookup_handle(hdev
, __le16_to_cpu(lp
->handle
));
125 __le16 policy
= get_unaligned((__le16
*) (sent
+ 2));
126 conn
->link_policy
= __le16_to_cpu(policy
);
129 hci_dev_unlock(hdev
);
133 BT_DBG("%s: Command complete: ogf LINK_POLICY ocf %x",
139 /* Command Complete OGF HOST_CTL */
140 static void hci_cc_host_ctl(struct hci_dev
*hdev
, __u16 ocf
, struct sk_buff
*skb
)
144 struct hci_rp_read_voice_setting
*vs
;
147 BT_DBG("%s ocf 0x%x", hdev
->name
, ocf
);
151 status
= *((__u8
*) skb
->data
);
152 hci_req_complete(hdev
, status
);
155 case OCF_SET_EVENT_FLT
:
156 status
= *((__u8
*) skb
->data
);
158 BT_DBG("%s SET_EVENT_FLT failed %d", hdev
->name
, status
);
160 BT_DBG("%s SET_EVENT_FLT succeseful", hdev
->name
);
164 case OCF_WRITE_AUTH_ENABLE
:
165 sent
= hci_sent_cmd_data(hdev
, OGF_HOST_CTL
, OCF_WRITE_AUTH_ENABLE
);
169 status
= *((__u8
*) skb
->data
);
170 param
= *((__u8
*) sent
);
173 if (param
== AUTH_ENABLED
)
174 set_bit(HCI_AUTH
, &hdev
->flags
);
176 clear_bit(HCI_AUTH
, &hdev
->flags
);
178 hci_req_complete(hdev
, status
);
181 case OCF_WRITE_ENCRYPT_MODE
:
182 sent
= hci_sent_cmd_data(hdev
, OGF_HOST_CTL
, OCF_WRITE_ENCRYPT_MODE
);
186 status
= *((__u8
*) skb
->data
);
187 param
= *((__u8
*) sent
);
191 set_bit(HCI_ENCRYPT
, &hdev
->flags
);
193 clear_bit(HCI_ENCRYPT
, &hdev
->flags
);
195 hci_req_complete(hdev
, status
);
198 case OCF_WRITE_CA_TIMEOUT
:
199 status
= *((__u8
*) skb
->data
);
201 BT_DBG("%s OCF_WRITE_CA_TIMEOUT failed %d", hdev
->name
, status
);
203 BT_DBG("%s OCF_WRITE_CA_TIMEOUT succeseful", hdev
->name
);
207 case OCF_WRITE_PG_TIMEOUT
:
208 status
= *((__u8
*) skb
->data
);
210 BT_DBG("%s OCF_WRITE_PG_TIMEOUT failed %d", hdev
->name
, status
);
212 BT_DBG("%s: OCF_WRITE_PG_TIMEOUT succeseful", hdev
->name
);
216 case OCF_WRITE_SCAN_ENABLE
:
217 sent
= hci_sent_cmd_data(hdev
, OGF_HOST_CTL
, OCF_WRITE_SCAN_ENABLE
);
221 status
= *((__u8
*) skb
->data
);
222 param
= *((__u8
*) sent
);
224 BT_DBG("param 0x%x", param
);
227 clear_bit(HCI_PSCAN
, &hdev
->flags
);
228 clear_bit(HCI_ISCAN
, &hdev
->flags
);
229 if (param
& SCAN_INQUIRY
)
230 set_bit(HCI_ISCAN
, &hdev
->flags
);
232 if (param
& SCAN_PAGE
)
233 set_bit(HCI_PSCAN
, &hdev
->flags
);
235 hci_req_complete(hdev
, status
);
238 case OCF_READ_VOICE_SETTING
:
239 vs
= (struct hci_rp_read_voice_setting
*) skb
->data
;
242 BT_DBG("%s READ_VOICE_SETTING failed %d", hdev
->name
, vs
->status
);
246 setting
= __le16_to_cpu(vs
->voice_setting
);
248 if (hdev
->voice_setting
!= setting
) {
249 hdev
->voice_setting
= setting
;
251 BT_DBG("%s: voice setting 0x%04x", hdev
->name
, setting
);
254 tasklet_disable(&hdev
->tx_task
);
255 hdev
->notify(hdev
, HCI_NOTIFY_VOICE_SETTING
);
256 tasklet_enable(&hdev
->tx_task
);
261 case OCF_WRITE_VOICE_SETTING
:
262 sent
= hci_sent_cmd_data(hdev
, OGF_HOST_CTL
, OCF_WRITE_VOICE_SETTING
);
266 status
= *((__u8
*) skb
->data
);
267 setting
= __le16_to_cpu(get_unaligned((__le16
*) sent
));
269 if (!status
&& hdev
->voice_setting
!= setting
) {
270 hdev
->voice_setting
= setting
;
272 BT_DBG("%s: voice setting 0x%04x", hdev
->name
, setting
);
275 tasklet_disable(&hdev
->tx_task
);
276 hdev
->notify(hdev
, HCI_NOTIFY_VOICE_SETTING
);
277 tasklet_enable(&hdev
->tx_task
);
280 hci_req_complete(hdev
, status
);
283 case OCF_HOST_BUFFER_SIZE
:
284 status
= *((__u8
*) skb
->data
);
286 BT_DBG("%s OCF_BUFFER_SIZE failed %d", hdev
->name
, status
);
287 hci_req_complete(hdev
, status
);
292 BT_DBG("%s Command complete: ogf HOST_CTL ocf %x", hdev
->name
, ocf
);
297 /* Command Complete OGF INFO_PARAM */
298 static void hci_cc_info_param(struct hci_dev
*hdev
, __u16 ocf
, struct sk_buff
*skb
)
300 struct hci_rp_read_local_features
*lf
;
301 struct hci_rp_read_buffer_size
*bs
;
302 struct hci_rp_read_bd_addr
*ba
;
304 BT_DBG("%s ocf 0x%x", hdev
->name
, ocf
);
307 case OCF_READ_LOCAL_FEATURES
:
308 lf
= (struct hci_rp_read_local_features
*) skb
->data
;
311 BT_DBG("%s READ_LOCAL_FEATURES failed %d", hdev
->name
, lf
->status
);
315 memcpy(hdev
->features
, lf
->features
, sizeof(hdev
->features
));
317 /* Adjust default settings according to features
318 * supported by device. */
319 if (hdev
->features
[0] & LMP_3SLOT
)
320 hdev
->pkt_type
|= (HCI_DM3
| HCI_DH3
);
322 if (hdev
->features
[0] & LMP_5SLOT
)
323 hdev
->pkt_type
|= (HCI_DM5
| HCI_DH5
);
325 if (hdev
->features
[1] & LMP_HV2
)
326 hdev
->pkt_type
|= (HCI_HV2
);
328 if (hdev
->features
[1] & LMP_HV3
)
329 hdev
->pkt_type
|= (HCI_HV3
);
331 BT_DBG("%s: features 0x%x 0x%x 0x%x", hdev
->name
, lf
->features
[0], lf
->features
[1], lf
->features
[2]);
335 case OCF_READ_BUFFER_SIZE
:
336 bs
= (struct hci_rp_read_buffer_size
*) skb
->data
;
339 BT_DBG("%s READ_BUFFER_SIZE failed %d", hdev
->name
, bs
->status
);
340 hci_req_complete(hdev
, bs
->status
);
344 hdev
->acl_mtu
= __le16_to_cpu(bs
->acl_mtu
);
345 hdev
->sco_mtu
= bs
->sco_mtu
;
346 hdev
->acl_pkts
= __le16_to_cpu(bs
->acl_max_pkt
);
347 hdev
->sco_pkts
= __le16_to_cpu(bs
->sco_max_pkt
);
349 if (test_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE
, &hdev
->quirks
)) {
354 hdev
->acl_cnt
= hdev
->acl_pkts
;
355 hdev
->sco_cnt
= hdev
->sco_pkts
;
357 BT_DBG("%s mtu: acl %d, sco %d max_pkt: acl %d, sco %d", hdev
->name
,
358 hdev
->acl_mtu
, hdev
->sco_mtu
, hdev
->acl_pkts
, hdev
->sco_pkts
);
361 case OCF_READ_BD_ADDR
:
362 ba
= (struct hci_rp_read_bd_addr
*) skb
->data
;
365 bacpy(&hdev
->bdaddr
, &ba
->bdaddr
);
367 BT_DBG("%s: READ_BD_ADDR failed %d", hdev
->name
, ba
->status
);
370 hci_req_complete(hdev
, ba
->status
);
374 BT_DBG("%s Command complete: ogf INFO_PARAM ocf %x", hdev
->name
, ocf
);
379 /* Command Status OGF LINK_CTL */
380 static inline void hci_cs_create_conn(struct hci_dev
*hdev
, __u8 status
)
382 struct hci_conn
*conn
;
383 struct hci_cp_create_conn
*cp
= hci_sent_cmd_data(hdev
, OGF_LINK_CTL
, OCF_CREATE_CONN
);
390 conn
= hci_conn_hash_lookup_ba(hdev
, ACL_LINK
, &cp
->bdaddr
);
392 BT_DBG("%s status 0x%x bdaddr %s conn %p", hdev
->name
,
393 status
, batostr(&cp
->bdaddr
), conn
);
396 if (conn
&& conn
->state
== BT_CONNECT
) {
397 conn
->state
= BT_CLOSED
;
398 hci_proto_connect_cfm(conn
, status
);
403 conn
= hci_conn_add(hdev
, ACL_LINK
, &cp
->bdaddr
);
406 conn
->link_mode
|= HCI_LM_MASTER
;
408 BT_ERR("No memmory for new connection");
412 hci_dev_unlock(hdev
);
415 static void hci_cs_link_ctl(struct hci_dev
*hdev
, __u16 ocf
, __u8 status
)
417 BT_DBG("%s ocf 0x%x", hdev
->name
, ocf
);
420 case OCF_CREATE_CONN
:
421 hci_cs_create_conn(hdev
, status
);
426 struct hci_conn
*acl
, *sco
;
427 struct hci_cp_add_sco
*cp
= hci_sent_cmd_data(hdev
, OGF_LINK_CTL
, OCF_ADD_SCO
);
433 handle
= __le16_to_cpu(cp
->handle
);
435 BT_DBG("%s Add SCO error: handle %d status 0x%x", hdev
->name
, handle
, status
);
439 acl
= hci_conn_hash_lookup_handle(hdev
, handle
);
440 if (acl
&& (sco
= acl
->link
)) {
441 sco
->state
= BT_CLOSED
;
443 hci_proto_connect_cfm(sco
, status
);
447 hci_dev_unlock(hdev
);
453 BT_DBG("%s Inquiry error: status 0x%x", hdev
->name
, status
);
454 hci_req_complete(hdev
, status
);
456 set_bit(HCI_INQUIRY
, &hdev
->flags
);
461 BT_DBG("%s Command status: ogf LINK_CTL ocf %x status %d",
462 hdev
->name
, ocf
, status
);
467 /* Command Status OGF LINK_POLICY */
468 static void hci_cs_link_policy(struct hci_dev
*hdev
, __u16 ocf
, __u8 status
)
470 BT_DBG("%s ocf 0x%x", hdev
->name
, ocf
);
475 struct hci_conn
*conn
;
476 struct hci_cp_sniff_mode
*cp
= hci_sent_cmd_data(hdev
, OGF_LINK_POLICY
, OCF_SNIFF_MODE
);
483 conn
= hci_conn_hash_lookup_handle(hdev
, __le16_to_cpu(cp
->handle
));
485 clear_bit(HCI_CONN_MODE_CHANGE_PEND
, &conn
->pend
);
488 hci_dev_unlock(hdev
);
492 case OCF_EXIT_SNIFF_MODE
:
494 struct hci_conn
*conn
;
495 struct hci_cp_exit_sniff_mode
*cp
= hci_sent_cmd_data(hdev
, OGF_LINK_POLICY
, OCF_EXIT_SNIFF_MODE
);
502 conn
= hci_conn_hash_lookup_handle(hdev
, __le16_to_cpu(cp
->handle
));
504 clear_bit(HCI_CONN_MODE_CHANGE_PEND
, &conn
->pend
);
507 hci_dev_unlock(hdev
);
512 BT_DBG("%s Command status: ogf LINK_POLICY ocf %x", hdev
->name
, ocf
);
517 /* Command Status OGF HOST_CTL */
518 static void hci_cs_host_ctl(struct hci_dev
*hdev
, __u16 ocf
, __u8 status
)
520 BT_DBG("%s ocf 0x%x", hdev
->name
, ocf
);
524 BT_DBG("%s Command status: ogf HOST_CTL ocf %x", hdev
->name
, ocf
);
529 /* Command Status OGF INFO_PARAM */
530 static void hci_cs_info_param(struct hci_dev
*hdev
, __u16 ocf
, __u8 status
)
532 BT_DBG("%s: hci_cs_info_param: ocf 0x%x", hdev
->name
, ocf
);
536 BT_DBG("%s Command status: ogf INFO_PARAM ocf %x", hdev
->name
, ocf
);
541 /* Inquiry Complete */
542 static inline void hci_inquiry_complete_evt(struct hci_dev
*hdev
, struct sk_buff
*skb
)
544 __u8 status
= *((__u8
*) skb
->data
);
546 BT_DBG("%s status %d", hdev
->name
, status
);
548 clear_bit(HCI_INQUIRY
, &hdev
->flags
);
549 hci_req_complete(hdev
, status
);
553 static inline void hci_inquiry_result_evt(struct hci_dev
*hdev
, struct sk_buff
*skb
)
555 struct inquiry_data data
;
556 struct inquiry_info
*info
= (struct inquiry_info
*) (skb
->data
+ 1);
557 int num_rsp
= *((__u8
*) skb
->data
);
559 BT_DBG("%s num_rsp %d", hdev
->name
, num_rsp
);
566 for (; num_rsp
; num_rsp
--) {
567 bacpy(&data
.bdaddr
, &info
->bdaddr
);
568 data
.pscan_rep_mode
= info
->pscan_rep_mode
;
569 data
.pscan_period_mode
= info
->pscan_period_mode
;
570 data
.pscan_mode
= info
->pscan_mode
;
571 memcpy(data
.dev_class
, info
->dev_class
, 3);
572 data
.clock_offset
= info
->clock_offset
;
575 hci_inquiry_cache_update(hdev
, &data
);
578 hci_dev_unlock(hdev
);
581 /* Inquiry Result With RSSI */
582 static inline void hci_inquiry_result_with_rssi_evt(struct hci_dev
*hdev
, struct sk_buff
*skb
)
584 struct inquiry_data data
;
585 int num_rsp
= *((__u8
*) skb
->data
);
587 BT_DBG("%s num_rsp %d", hdev
->name
, num_rsp
);
594 if ((skb
->len
- 1) / num_rsp
!= sizeof(struct inquiry_info_with_rssi
)) {
595 struct inquiry_info_with_rssi_and_pscan_mode
*info
=
596 (struct inquiry_info_with_rssi_and_pscan_mode
*) (skb
->data
+ 1);
598 for (; num_rsp
; num_rsp
--) {
599 bacpy(&data
.bdaddr
, &info
->bdaddr
);
600 data
.pscan_rep_mode
= info
->pscan_rep_mode
;
601 data
.pscan_period_mode
= info
->pscan_period_mode
;
602 data
.pscan_mode
= info
->pscan_mode
;
603 memcpy(data
.dev_class
, info
->dev_class
, 3);
604 data
.clock_offset
= info
->clock_offset
;
605 data
.rssi
= info
->rssi
;
607 hci_inquiry_cache_update(hdev
, &data
);
610 struct inquiry_info_with_rssi
*info
=
611 (struct inquiry_info_with_rssi
*) (skb
->data
+ 1);
613 for (; num_rsp
; num_rsp
--) {
614 bacpy(&data
.bdaddr
, &info
->bdaddr
);
615 data
.pscan_rep_mode
= info
->pscan_rep_mode
;
616 data
.pscan_period_mode
= info
->pscan_period_mode
;
617 data
.pscan_mode
= 0x00;
618 memcpy(data
.dev_class
, info
->dev_class
, 3);
619 data
.clock_offset
= info
->clock_offset
;
620 data
.rssi
= info
->rssi
;
622 hci_inquiry_cache_update(hdev
, &data
);
626 hci_dev_unlock(hdev
);
629 /* Extended Inquiry Result */
630 static inline void hci_extended_inquiry_result_evt(struct hci_dev
*hdev
, struct sk_buff
*skb
)
632 struct inquiry_data data
;
633 struct extended_inquiry_info
*info
= (struct extended_inquiry_info
*) (skb
->data
+ 1);
634 int num_rsp
= *((__u8
*) skb
->data
);
636 BT_DBG("%s num_rsp %d", hdev
->name
, num_rsp
);
643 for (; num_rsp
; num_rsp
--) {
644 bacpy(&data
.bdaddr
, &info
->bdaddr
);
645 data
.pscan_rep_mode
= info
->pscan_rep_mode
;
646 data
.pscan_period_mode
= info
->pscan_period_mode
;
647 data
.pscan_mode
= 0x00;
648 memcpy(data
.dev_class
, info
->dev_class
, 3);
649 data
.clock_offset
= info
->clock_offset
;
650 data
.rssi
= info
->rssi
;
652 hci_inquiry_cache_update(hdev
, &data
);
655 hci_dev_unlock(hdev
);
658 /* Connect Request */
659 static inline void hci_conn_request_evt(struct hci_dev
*hdev
, struct sk_buff
*skb
)
661 struct hci_ev_conn_request
*ev
= (struct hci_ev_conn_request
*) skb
->data
;
662 int mask
= hdev
->link_mode
;
664 BT_DBG("%s Connection request: %s type 0x%x", hdev
->name
,
665 batostr(&ev
->bdaddr
), ev
->link_type
);
667 mask
|= hci_proto_connect_ind(hdev
, &ev
->bdaddr
, ev
->link_type
);
669 if (mask
& HCI_LM_ACCEPT
) {
670 /* Connection accepted */
671 struct hci_conn
*conn
;
672 struct hci_cp_accept_conn_req cp
;
675 conn
= hci_conn_hash_lookup_ba(hdev
, ev
->link_type
, &ev
->bdaddr
);
677 if (!(conn
= hci_conn_add(hdev
, ev
->link_type
, &ev
->bdaddr
))) {
678 BT_ERR("No memmory for new connection");
679 hci_dev_unlock(hdev
);
683 memcpy(conn
->dev_class
, ev
->dev_class
, 3);
684 conn
->state
= BT_CONNECT
;
685 hci_dev_unlock(hdev
);
687 bacpy(&cp
.bdaddr
, &ev
->bdaddr
);
689 if (lmp_rswitch_capable(hdev
) && (mask
& HCI_LM_MASTER
))
690 cp
.role
= 0x00; /* Become master */
692 cp
.role
= 0x01; /* Remain slave */
694 hci_send_cmd(hdev
, OGF_LINK_CTL
,
695 OCF_ACCEPT_CONN_REQ
, sizeof(cp
), &cp
);
697 /* Connection rejected */
698 struct hci_cp_reject_conn_req cp
;
700 bacpy(&cp
.bdaddr
, &ev
->bdaddr
);
702 hci_send_cmd(hdev
, OGF_LINK_CTL
,
703 OCF_REJECT_CONN_REQ
, sizeof(cp
), &cp
);
707 /* Connect Complete */
708 static inline void hci_conn_complete_evt(struct hci_dev
*hdev
, struct sk_buff
*skb
)
710 struct hci_ev_conn_complete
*ev
= (struct hci_ev_conn_complete
*) skb
->data
;
711 struct hci_conn
*conn
;
713 BT_DBG("%s", hdev
->name
);
717 conn
= hci_conn_hash_lookup_ba(hdev
, ev
->link_type
, &ev
->bdaddr
);
719 hci_dev_unlock(hdev
);
724 conn
->handle
= __le16_to_cpu(ev
->handle
);
725 conn
->state
= BT_CONNECTED
;
727 if (test_bit(HCI_AUTH
, &hdev
->flags
))
728 conn
->link_mode
|= HCI_LM_AUTH
;
730 if (test_bit(HCI_ENCRYPT
, &hdev
->flags
))
731 conn
->link_mode
|= HCI_LM_ENCRYPT
;
733 /* Get remote features */
734 if (conn
->type
== ACL_LINK
) {
735 struct hci_cp_read_remote_features cp
;
736 cp
.handle
= ev
->handle
;
737 hci_send_cmd(hdev
, OGF_LINK_CTL
,
738 OCF_READ_REMOTE_FEATURES
, sizeof(cp
), &cp
);
741 /* Set link policy */
742 if (conn
->type
== ACL_LINK
&& hdev
->link_policy
) {
743 struct hci_cp_write_link_policy cp
;
744 cp
.handle
= ev
->handle
;
745 cp
.policy
= __cpu_to_le16(hdev
->link_policy
);
746 hci_send_cmd(hdev
, OGF_LINK_POLICY
,
747 OCF_WRITE_LINK_POLICY
, sizeof(cp
), &cp
);
750 /* Set packet type for incoming connection */
752 struct hci_cp_change_conn_ptype cp
;
753 cp
.handle
= ev
->handle
;
754 cp
.pkt_type
= (conn
->type
== ACL_LINK
) ?
755 __cpu_to_le16(hdev
->pkt_type
& ACL_PTYPE_MASK
):
756 __cpu_to_le16(hdev
->pkt_type
& SCO_PTYPE_MASK
);
758 hci_send_cmd(hdev
, OGF_LINK_CTL
,
759 OCF_CHANGE_CONN_PTYPE
, sizeof(cp
), &cp
);
762 conn
->state
= BT_CLOSED
;
764 if (conn
->type
== ACL_LINK
) {
765 struct hci_conn
*sco
= conn
->link
;
768 hci_add_sco(sco
, conn
->handle
);
770 hci_proto_connect_cfm(sco
, ev
->status
);
776 hci_proto_connect_cfm(conn
, ev
->status
);
780 hci_dev_unlock(hdev
);
783 /* Disconnect Complete */
784 static inline void hci_disconn_complete_evt(struct hci_dev
*hdev
, struct sk_buff
*skb
)
786 struct hci_ev_disconn_complete
*ev
= (struct hci_ev_disconn_complete
*) skb
->data
;
787 struct hci_conn
*conn
;
789 BT_DBG("%s status %d", hdev
->name
, ev
->status
);
796 conn
= hci_conn_hash_lookup_handle(hdev
, __le16_to_cpu(ev
->handle
));
798 conn
->state
= BT_CLOSED
;
799 hci_proto_disconn_ind(conn
, ev
->reason
);
803 hci_dev_unlock(hdev
);
806 /* Number of completed packets */
807 static inline void hci_num_comp_pkts_evt(struct hci_dev
*hdev
, struct sk_buff
*skb
)
809 struct hci_ev_num_comp_pkts
*ev
= (struct hci_ev_num_comp_pkts
*) skb
->data
;
813 skb_pull(skb
, sizeof(*ev
));
815 BT_DBG("%s num_hndl %d", hdev
->name
, ev
->num_hndl
);
817 if (skb
->len
< ev
->num_hndl
* 4) {
818 BT_DBG("%s bad parameters", hdev
->name
);
822 tasklet_disable(&hdev
->tx_task
);
824 for (i
= 0, ptr
= (__le16
*) skb
->data
; i
< ev
->num_hndl
; i
++) {
825 struct hci_conn
*conn
;
828 handle
= __le16_to_cpu(get_unaligned(ptr
++));
829 count
= __le16_to_cpu(get_unaligned(ptr
++));
831 conn
= hci_conn_hash_lookup_handle(hdev
, handle
);
835 if (conn
->type
== SCO_LINK
) {
836 if ((hdev
->sco_cnt
+= count
) > hdev
->sco_pkts
)
837 hdev
->sco_cnt
= hdev
->sco_pkts
;
839 if ((hdev
->acl_cnt
+= count
) > hdev
->acl_pkts
)
840 hdev
->acl_cnt
= hdev
->acl_pkts
;
846 tasklet_enable(&hdev
->tx_task
);
850 static inline void hci_role_change_evt(struct hci_dev
*hdev
, struct sk_buff
*skb
)
852 struct hci_ev_role_change
*ev
= (struct hci_ev_role_change
*) skb
->data
;
853 struct hci_conn
*conn
;
855 BT_DBG("%s status %d", hdev
->name
, ev
->status
);
859 conn
= hci_conn_hash_lookup_ba(hdev
, ACL_LINK
, &ev
->bdaddr
);
863 conn
->link_mode
&= ~HCI_LM_MASTER
;
865 conn
->link_mode
|= HCI_LM_MASTER
;
868 clear_bit(HCI_CONN_RSWITCH_PEND
, &conn
->pend
);
870 hci_role_switch_cfm(conn
, ev
->status
, ev
->role
);
873 hci_dev_unlock(hdev
);
877 static inline void hci_mode_change_evt(struct hci_dev
*hdev
, struct sk_buff
*skb
)
879 struct hci_ev_mode_change
*ev
= (struct hci_ev_mode_change
*) skb
->data
;
880 struct hci_conn
*conn
;
882 BT_DBG("%s status %d", hdev
->name
, ev
->status
);
886 conn
= hci_conn_hash_lookup_handle(hdev
, __le16_to_cpu(ev
->handle
));
888 conn
->mode
= ev
->mode
;
889 conn
->interval
= __le16_to_cpu(ev
->interval
);
891 if (!test_and_clear_bit(HCI_CONN_MODE_CHANGE_PEND
, &conn
->pend
)) {
892 if (conn
->mode
== HCI_CM_ACTIVE
)
893 conn
->power_save
= 1;
895 conn
->power_save
= 0;
899 hci_dev_unlock(hdev
);
902 /* Authentication Complete */
903 static inline void hci_auth_complete_evt(struct hci_dev
*hdev
, struct sk_buff
*skb
)
905 struct hci_ev_auth_complete
*ev
= (struct hci_ev_auth_complete
*) skb
->data
;
906 struct hci_conn
*conn
;
908 BT_DBG("%s status %d", hdev
->name
, ev
->status
);
912 conn
= hci_conn_hash_lookup_handle(hdev
, __le16_to_cpu(ev
->handle
));
915 conn
->link_mode
|= HCI_LM_AUTH
;
917 clear_bit(HCI_CONN_AUTH_PEND
, &conn
->pend
);
919 hci_auth_cfm(conn
, ev
->status
);
921 if (test_bit(HCI_CONN_ENCRYPT_PEND
, &conn
->pend
)) {
923 struct hci_cp_set_conn_encrypt cp
;
924 cp
.handle
= __cpu_to_le16(conn
->handle
);
926 hci_send_cmd(conn
->hdev
, OGF_LINK_CTL
,
927 OCF_SET_CONN_ENCRYPT
, sizeof(cp
), &cp
);
929 clear_bit(HCI_CONN_ENCRYPT_PEND
, &conn
->pend
);
930 hci_encrypt_cfm(conn
, ev
->status
, 0x00);
935 hci_dev_unlock(hdev
);
938 /* Encryption Change */
939 static inline void hci_encrypt_change_evt(struct hci_dev
*hdev
, struct sk_buff
*skb
)
941 struct hci_ev_encrypt_change
*ev
= (struct hci_ev_encrypt_change
*) skb
->data
;
942 struct hci_conn
*conn
;
944 BT_DBG("%s status %d", hdev
->name
, ev
->status
);
948 conn
= hci_conn_hash_lookup_handle(hdev
, __le16_to_cpu(ev
->handle
));
952 conn
->link_mode
|= HCI_LM_ENCRYPT
;
954 conn
->link_mode
&= ~HCI_LM_ENCRYPT
;
957 clear_bit(HCI_CONN_ENCRYPT_PEND
, &conn
->pend
);
959 hci_encrypt_cfm(conn
, ev
->status
, ev
->encrypt
);
962 hci_dev_unlock(hdev
);
965 /* Change Connection Link Key Complete */
966 static inline void hci_change_conn_link_key_complete_evt(struct hci_dev
*hdev
, struct sk_buff
*skb
)
968 struct hci_ev_change_conn_link_key_complete
*ev
= (struct hci_ev_change_conn_link_key_complete
*) skb
->data
;
969 struct hci_conn
*conn
;
971 BT_DBG("%s status %d", hdev
->name
, ev
->status
);
975 conn
= hci_conn_hash_lookup_handle(hdev
, __le16_to_cpu(ev
->handle
));
978 conn
->link_mode
|= HCI_LM_SECURE
;
980 clear_bit(HCI_CONN_AUTH_PEND
, &conn
->pend
);
982 hci_key_change_cfm(conn
, ev
->status
);
985 hci_dev_unlock(hdev
);
988 /* Pin Code Request*/
989 static inline void hci_pin_code_request_evt(struct hci_dev
*hdev
, struct sk_buff
*skb
)
993 /* Link Key Request */
994 static inline void hci_link_key_request_evt(struct hci_dev
*hdev
, struct sk_buff
*skb
)
998 /* Link Key Notification */
999 static inline void hci_link_key_notify_evt(struct hci_dev
*hdev
, struct sk_buff
*skb
)
1003 /* Remote Features */
1004 static inline void hci_remote_features_evt(struct hci_dev
*hdev
, struct sk_buff
*skb
)
1006 struct hci_ev_remote_features
*ev
= (struct hci_ev_remote_features
*) skb
->data
;
1007 struct hci_conn
*conn
;
1009 BT_DBG("%s status %d", hdev
->name
, ev
->status
);
1013 conn
= hci_conn_hash_lookup_handle(hdev
, __le16_to_cpu(ev
->handle
));
1014 if (conn
&& !ev
->status
) {
1015 memcpy(conn
->features
, ev
->features
, sizeof(conn
->features
));
1018 hci_dev_unlock(hdev
);
1022 static inline void hci_clock_offset_evt(struct hci_dev
*hdev
, struct sk_buff
*skb
)
1024 struct hci_ev_clock_offset
*ev
= (struct hci_ev_clock_offset
*) skb
->data
;
1025 struct hci_conn
*conn
;
1027 BT_DBG("%s status %d", hdev
->name
, ev
->status
);
1031 conn
= hci_conn_hash_lookup_handle(hdev
, __le16_to_cpu(ev
->handle
));
1032 if (conn
&& !ev
->status
) {
1033 struct inquiry_entry
*ie
;
1035 if ((ie
= hci_inquiry_cache_lookup(hdev
, &conn
->dst
))) {
1036 ie
->data
.clock_offset
= ev
->clock_offset
;
1037 ie
->timestamp
= jiffies
;
1041 hci_dev_unlock(hdev
);
1044 /* Page Scan Repetition Mode */
1045 static inline void hci_pscan_rep_mode_evt(struct hci_dev
*hdev
, struct sk_buff
*skb
)
1047 struct hci_ev_pscan_rep_mode
*ev
= (struct hci_ev_pscan_rep_mode
*) skb
->data
;
1048 struct inquiry_entry
*ie
;
1050 BT_DBG("%s", hdev
->name
);
1054 if ((ie
= hci_inquiry_cache_lookup(hdev
, &ev
->bdaddr
))) {
1055 ie
->data
.pscan_rep_mode
= ev
->pscan_rep_mode
;
1056 ie
->timestamp
= jiffies
;
1059 hci_dev_unlock(hdev
);
1063 static inline void hci_sniff_subrate_evt(struct hci_dev
*hdev
, struct sk_buff
*skb
)
1065 struct hci_ev_sniff_subrate
*ev
= (struct hci_ev_sniff_subrate
*) skb
->data
;
1066 struct hci_conn
*conn
;
1068 BT_DBG("%s status %d", hdev
->name
, ev
->status
);
1072 conn
= hci_conn_hash_lookup_handle(hdev
, __le16_to_cpu(ev
->handle
));
1076 hci_dev_unlock(hdev
);
1079 void hci_event_packet(struct hci_dev
*hdev
, struct sk_buff
*skb
)
1081 struct hci_event_hdr
*hdr
= (struct hci_event_hdr
*) skb
->data
;
1082 struct hci_ev_cmd_complete
*ec
;
1083 struct hci_ev_cmd_status
*cs
;
1084 u16 opcode
, ocf
, ogf
;
1086 skb_pull(skb
, HCI_EVENT_HDR_SIZE
);
1088 BT_DBG("%s evt 0x%x", hdev
->name
, hdr
->evt
);
1091 case HCI_EV_NUM_COMP_PKTS
:
1092 hci_num_comp_pkts_evt(hdev
, skb
);
1095 case HCI_EV_INQUIRY_COMPLETE
:
1096 hci_inquiry_complete_evt(hdev
, skb
);
1099 case HCI_EV_INQUIRY_RESULT
:
1100 hci_inquiry_result_evt(hdev
, skb
);
1103 case HCI_EV_INQUIRY_RESULT_WITH_RSSI
:
1104 hci_inquiry_result_with_rssi_evt(hdev
, skb
);
1107 case HCI_EV_EXTENDED_INQUIRY_RESULT
:
1108 hci_extended_inquiry_result_evt(hdev
, skb
);
1111 case HCI_EV_CONN_REQUEST
:
1112 hci_conn_request_evt(hdev
, skb
);
1115 case HCI_EV_CONN_COMPLETE
:
1116 hci_conn_complete_evt(hdev
, skb
);
1119 case HCI_EV_DISCONN_COMPLETE
:
1120 hci_disconn_complete_evt(hdev
, skb
);
1123 case HCI_EV_ROLE_CHANGE
:
1124 hci_role_change_evt(hdev
, skb
);
1127 case HCI_EV_MODE_CHANGE
:
1128 hci_mode_change_evt(hdev
, skb
);
1131 case HCI_EV_AUTH_COMPLETE
:
1132 hci_auth_complete_evt(hdev
, skb
);
1135 case HCI_EV_ENCRYPT_CHANGE
:
1136 hci_encrypt_change_evt(hdev
, skb
);
1139 case HCI_EV_CHANGE_CONN_LINK_KEY_COMPLETE
:
1140 hci_change_conn_link_key_complete_evt(hdev
, skb
);
1143 case HCI_EV_PIN_CODE_REQ
:
1144 hci_pin_code_request_evt(hdev
, skb
);
1147 case HCI_EV_LINK_KEY_REQ
:
1148 hci_link_key_request_evt(hdev
, skb
);
1151 case HCI_EV_LINK_KEY_NOTIFY
:
1152 hci_link_key_notify_evt(hdev
, skb
);
1155 case HCI_EV_REMOTE_FEATURES
:
1156 hci_remote_features_evt(hdev
, skb
);
1159 case HCI_EV_CLOCK_OFFSET
:
1160 hci_clock_offset_evt(hdev
, skb
);
1163 case HCI_EV_PSCAN_REP_MODE
:
1164 hci_pscan_rep_mode_evt(hdev
, skb
);
1167 case HCI_EV_SNIFF_SUBRATE
:
1168 hci_sniff_subrate_evt(hdev
, skb
);
1171 case HCI_EV_CMD_STATUS
:
1172 cs
= (struct hci_ev_cmd_status
*) skb
->data
;
1173 skb_pull(skb
, sizeof(cs
));
1175 opcode
= __le16_to_cpu(cs
->opcode
);
1176 ogf
= hci_opcode_ogf(opcode
);
1177 ocf
= hci_opcode_ocf(opcode
);
1180 case OGF_INFO_PARAM
:
1181 hci_cs_info_param(hdev
, ocf
, cs
->status
);
1185 hci_cs_host_ctl(hdev
, ocf
, cs
->status
);
1189 hci_cs_link_ctl(hdev
, ocf
, cs
->status
);
1192 case OGF_LINK_POLICY
:
1193 hci_cs_link_policy(hdev
, ocf
, cs
->status
);
1197 BT_DBG("%s Command Status OGF %x", hdev
->name
, ogf
);
1202 atomic_set(&hdev
->cmd_cnt
, 1);
1203 if (!skb_queue_empty(&hdev
->cmd_q
))
1204 hci_sched_cmd(hdev
);
1208 case HCI_EV_CMD_COMPLETE
:
1209 ec
= (struct hci_ev_cmd_complete
*) skb
->data
;
1210 skb_pull(skb
, sizeof(*ec
));
1212 opcode
= __le16_to_cpu(ec
->opcode
);
1213 ogf
= hci_opcode_ogf(opcode
);
1214 ocf
= hci_opcode_ocf(opcode
);
1217 case OGF_INFO_PARAM
:
1218 hci_cc_info_param(hdev
, ocf
, skb
);
1222 hci_cc_host_ctl(hdev
, ocf
, skb
);
1226 hci_cc_link_ctl(hdev
, ocf
, skb
);
1229 case OGF_LINK_POLICY
:
1230 hci_cc_link_policy(hdev
, ocf
, skb
);
1234 BT_DBG("%s Command Completed OGF %x", hdev
->name
, ogf
);
1239 atomic_set(&hdev
->cmd_cnt
, 1);
1240 if (!skb_queue_empty(&hdev
->cmd_q
))
1241 hci_sched_cmd(hdev
);
1247 hdev
->stat
.evt_rx
++;
1250 /* Generate internal stack event */
1251 void hci_si_event(struct hci_dev
*hdev
, int type
, int dlen
, void *data
)
1253 struct hci_event_hdr
*hdr
;
1254 struct hci_ev_stack_internal
*ev
;
1255 struct sk_buff
*skb
;
1257 skb
= bt_skb_alloc(HCI_EVENT_HDR_SIZE
+ sizeof(*ev
) + dlen
, GFP_ATOMIC
);
1261 hdr
= (void *) skb_put(skb
, HCI_EVENT_HDR_SIZE
);
1262 hdr
->evt
= HCI_EV_STACK_INTERNAL
;
1263 hdr
->plen
= sizeof(*ev
) + dlen
;
1265 ev
= (void *) skb_put(skb
, sizeof(*ev
) + dlen
);
1267 memcpy(ev
->data
, data
, dlen
);
1269 bt_cb(skb
)->incoming
= 1;
1270 __net_timestamp(skb
);
1272 bt_cb(skb
)->pkt_type
= HCI_EVENT_PKT
;
1273 skb
->dev
= (void *) hdev
;
1274 hci_send_to_sock(hdev
, skb
);