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;
87 BT_DBG("%s ocf 0x%x", hdev->name, ocf);
90 case OCF_ROLE_DISCOVERY:
91 rd = (void *) skb->data;
98 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rd->handle));
101 conn->link_mode &= ~HCI_LM_MASTER;
103 conn->link_mode |= HCI_LM_MASTER;
106 hci_dev_unlock(hdev);
110 BT_DBG("%s: Command complete: ogf LINK_POLICY ocf %x",
116 /* Command Complete OGF HOST_CTL */
117 static void hci_cc_host_ctl(struct hci_dev *hdev, __u16 ocf, struct sk_buff *skb)
121 struct hci_rp_read_voice_setting *vs;
124 BT_DBG("%s ocf 0x%x", hdev->name, ocf);
128 status = *((__u8 *) skb->data);
129 hci_req_complete(hdev, status);
132 case OCF_SET_EVENT_FLT:
133 status = *((__u8 *) skb->data);
135 BT_DBG("%s SET_EVENT_FLT failed %d", hdev->name, status);
137 BT_DBG("%s SET_EVENT_FLT succeseful", hdev->name);
141 case OCF_WRITE_AUTH_ENABLE:
142 sent = hci_sent_cmd_data(hdev, OGF_HOST_CTL, OCF_WRITE_AUTH_ENABLE);
146 status = *((__u8 *) skb->data);
147 param = *((__u8 *) sent);
150 if (param == AUTH_ENABLED)
151 set_bit(HCI_AUTH, &hdev->flags);
153 clear_bit(HCI_AUTH, &hdev->flags);
155 hci_req_complete(hdev, status);
158 case OCF_WRITE_ENCRYPT_MODE:
159 sent = hci_sent_cmd_data(hdev, OGF_HOST_CTL, OCF_WRITE_ENCRYPT_MODE);
163 status = *((__u8 *) skb->data);
164 param = *((__u8 *) sent);
168 set_bit(HCI_ENCRYPT, &hdev->flags);
170 clear_bit(HCI_ENCRYPT, &hdev->flags);
172 hci_req_complete(hdev, status);
175 case OCF_WRITE_CA_TIMEOUT:
176 status = *((__u8 *) skb->data);
178 BT_DBG("%s OCF_WRITE_CA_TIMEOUT failed %d", hdev->name, status);
180 BT_DBG("%s OCF_WRITE_CA_TIMEOUT succeseful", hdev->name);
184 case OCF_WRITE_PG_TIMEOUT:
185 status = *((__u8 *) skb->data);
187 BT_DBG("%s OCF_WRITE_PG_TIMEOUT failed %d", hdev->name, status);
189 BT_DBG("%s: OCF_WRITE_PG_TIMEOUT succeseful", hdev->name);
193 case OCF_WRITE_SCAN_ENABLE:
194 sent = hci_sent_cmd_data(hdev, OGF_HOST_CTL, OCF_WRITE_SCAN_ENABLE);
198 status = *((__u8 *) skb->data);
199 param = *((__u8 *) sent);
201 BT_DBG("param 0x%x", param);
204 clear_bit(HCI_PSCAN, &hdev->flags);
205 clear_bit(HCI_ISCAN, &hdev->flags);
206 if (param & SCAN_INQUIRY)
207 set_bit(HCI_ISCAN, &hdev->flags);
209 if (param & SCAN_PAGE)
210 set_bit(HCI_PSCAN, &hdev->flags);
212 hci_req_complete(hdev, status);
215 case OCF_READ_VOICE_SETTING:
216 vs = (struct hci_rp_read_voice_setting *) skb->data;
219 BT_DBG("%s READ_VOICE_SETTING failed %d", hdev->name, vs->status);
223 setting = __le16_to_cpu(vs->voice_setting);
225 if (hdev->voice_setting != setting ) {
226 hdev->voice_setting = setting;
228 BT_DBG("%s: voice setting 0x%04x", hdev->name, setting);
231 tasklet_disable(&hdev->tx_task);
232 hdev->notify(hdev, HCI_NOTIFY_VOICE_SETTING);
233 tasklet_enable(&hdev->tx_task);
238 case OCF_WRITE_VOICE_SETTING:
239 sent = hci_sent_cmd_data(hdev, OGF_HOST_CTL, OCF_WRITE_VOICE_SETTING);
243 status = *((__u8 *) skb->data);
244 setting = __le16_to_cpu(get_unaligned((__le16 *) sent));
246 if (!status && hdev->voice_setting != setting) {
247 hdev->voice_setting = setting;
249 BT_DBG("%s: voice setting 0x%04x", hdev->name, setting);
252 tasklet_disable(&hdev->tx_task);
253 hdev->notify(hdev, HCI_NOTIFY_VOICE_SETTING);
254 tasklet_enable(&hdev->tx_task);
257 hci_req_complete(hdev, status);
260 case OCF_HOST_BUFFER_SIZE:
261 status = *((__u8 *) skb->data);
263 BT_DBG("%s OCF_BUFFER_SIZE failed %d", hdev->name, status);
264 hci_req_complete(hdev, status);
269 BT_DBG("%s Command complete: ogf HOST_CTL ocf %x", hdev->name, ocf);
274 /* Command Complete OGF INFO_PARAM */
275 static void hci_cc_info_param(struct hci_dev *hdev, __u16 ocf, struct sk_buff *skb)
277 struct hci_rp_read_loc_features *lf;
278 struct hci_rp_read_buffer_size *bs;
279 struct hci_rp_read_bd_addr *ba;
281 BT_DBG("%s ocf 0x%x", hdev->name, ocf);
284 case OCF_READ_LOCAL_FEATURES:
285 lf = (struct hci_rp_read_loc_features *) skb->data;
288 BT_DBG("%s READ_LOCAL_FEATURES failed %d", hdev->name, lf->status);
292 memcpy(hdev->features, lf->features, sizeof(hdev->features));
294 /* Adjust default settings according to features
295 * supported by device. */
296 if (hdev->features[0] & LMP_3SLOT)
297 hdev->pkt_type |= (HCI_DM3 | HCI_DH3);
299 if (hdev->features[0] & LMP_5SLOT)
300 hdev->pkt_type |= (HCI_DM5 | HCI_DH5);
302 if (hdev->features[1] & LMP_HV2)
303 hdev->pkt_type |= (HCI_HV2);
305 if (hdev->features[1] & LMP_HV3)
306 hdev->pkt_type |= (HCI_HV3);
308 BT_DBG("%s: features 0x%x 0x%x 0x%x", hdev->name, lf->features[0], lf->features[1], lf->features[2]);
312 case OCF_READ_BUFFER_SIZE:
313 bs = (struct hci_rp_read_buffer_size *) skb->data;
316 BT_DBG("%s READ_BUFFER_SIZE failed %d", hdev->name, bs->status);
317 hci_req_complete(hdev, bs->status);
321 hdev->acl_mtu = __le16_to_cpu(bs->acl_mtu);
322 hdev->sco_mtu = bs->sco_mtu;
323 hdev->acl_pkts = __le16_to_cpu(bs->acl_max_pkt);
324 hdev->sco_pkts = __le16_to_cpu(bs->sco_max_pkt);
326 if (test_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks)) {
331 hdev->acl_cnt = hdev->acl_pkts;
332 hdev->sco_cnt = hdev->sco_pkts;
334 BT_DBG("%s mtu: acl %d, sco %d max_pkt: acl %d, sco %d", hdev->name,
335 hdev->acl_mtu, hdev->sco_mtu, hdev->acl_pkts, hdev->sco_pkts);
338 case OCF_READ_BD_ADDR:
339 ba = (struct hci_rp_read_bd_addr *) skb->data;
342 bacpy(&hdev->bdaddr, &ba->bdaddr);
344 BT_DBG("%s: READ_BD_ADDR failed %d", hdev->name, ba->status);
347 hci_req_complete(hdev, ba->status);
351 BT_DBG("%s Command complete: ogf INFO_PARAM ocf %x", hdev->name, ocf);
356 /* Command Status OGF LINK_CTL */
357 static inline void hci_cs_create_conn(struct hci_dev *hdev, __u8 status)
359 struct hci_conn *conn;
360 struct hci_cp_create_conn *cp = hci_sent_cmd_data(hdev, OGF_LINK_CTL, OCF_CREATE_CONN);
367 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->bdaddr);
369 BT_DBG("%s status 0x%x bdaddr %s conn %p", hdev->name,
370 status, batostr(&cp->bdaddr), conn);
373 if (conn && conn->state == BT_CONNECT) {
374 conn->state = BT_CLOSED;
375 hci_proto_connect_cfm(conn, status);
380 conn = hci_conn_add(hdev, ACL_LINK, &cp->bdaddr);
383 conn->link_mode |= HCI_LM_MASTER;
385 BT_ERR("No memmory for new connection");
389 hci_dev_unlock(hdev);
392 static void hci_cs_link_ctl(struct hci_dev *hdev, __u16 ocf, __u8 status)
394 BT_DBG("%s ocf 0x%x", hdev->name, ocf);
397 case OCF_CREATE_CONN:
398 hci_cs_create_conn(hdev, status);
403 struct hci_conn *acl, *sco;
404 struct hci_cp_add_sco *cp = hci_sent_cmd_data(hdev, OGF_LINK_CTL, OCF_ADD_SCO);
410 handle = __le16_to_cpu(cp->handle);
412 BT_DBG("%s Add SCO error: handle %d status 0x%x", hdev->name, handle, status);
416 acl = hci_conn_hash_lookup_handle(hdev, handle);
417 if (acl && (sco = acl->link)) {
418 sco->state = BT_CLOSED;
420 hci_proto_connect_cfm(sco, status);
424 hci_dev_unlock(hdev);
430 BT_DBG("%s Inquiry error: status 0x%x", hdev->name, status);
431 hci_req_complete(hdev, status);
433 set_bit(HCI_INQUIRY, &hdev->flags);
438 BT_DBG("%s Command status: ogf LINK_CTL ocf %x status %d",
439 hdev->name, ocf, status);
444 /* Command Status OGF LINK_POLICY */
445 static void hci_cs_link_policy(struct hci_dev *hdev, __u16 ocf, __u8 status)
447 BT_DBG("%s ocf 0x%x", hdev->name, ocf);
451 BT_DBG("%s Command status: ogf HOST_POLICY ocf %x", hdev->name, ocf);
456 /* Command Status OGF HOST_CTL */
457 static void hci_cs_host_ctl(struct hci_dev *hdev, __u16 ocf, __u8 status)
459 BT_DBG("%s ocf 0x%x", hdev->name, ocf);
463 BT_DBG("%s Command status: ogf HOST_CTL ocf %x", hdev->name, ocf);
468 /* Command Status OGF INFO_PARAM */
469 static void hci_cs_info_param(struct hci_dev *hdev, __u16 ocf, __u8 status)
471 BT_DBG("%s: hci_cs_info_param: ocf 0x%x", hdev->name, ocf);
475 BT_DBG("%s Command status: ogf INFO_PARAM ocf %x", hdev->name, ocf);
480 /* Inquiry Complete */
481 static inline void hci_inquiry_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
483 __u8 status = *((__u8 *) skb->data);
485 BT_DBG("%s status %d", hdev->name, status);
487 clear_bit(HCI_INQUIRY, &hdev->flags);
488 hci_req_complete(hdev, status);
492 static inline void hci_inquiry_result_evt(struct hci_dev *hdev, struct sk_buff *skb)
494 struct inquiry_data data;
495 struct inquiry_info *info = (struct inquiry_info *) (skb->data + 1);
496 int num_rsp = *((__u8 *) skb->data);
498 BT_DBG("%s num_rsp %d", hdev->name, num_rsp);
505 for (; num_rsp; num_rsp--) {
506 bacpy(&data.bdaddr, &info->bdaddr);
507 data.pscan_rep_mode = info->pscan_rep_mode;
508 data.pscan_period_mode = info->pscan_period_mode;
509 data.pscan_mode = info->pscan_mode;
510 memcpy(data.dev_class, info->dev_class, 3);
511 data.clock_offset = info->clock_offset;
514 hci_inquiry_cache_update(hdev, &data);
517 hci_dev_unlock(hdev);
520 /* Inquiry Result With RSSI */
521 static inline void hci_inquiry_result_with_rssi_evt(struct hci_dev *hdev, struct sk_buff *skb)
523 struct inquiry_data data;
524 int num_rsp = *((__u8 *) skb->data);
526 BT_DBG("%s num_rsp %d", hdev->name, num_rsp);
533 if ((skb->len - 1) / num_rsp != sizeof(struct inquiry_info_with_rssi)) {
534 struct inquiry_info_with_rssi_and_pscan_mode *info =
535 (struct inquiry_info_with_rssi_and_pscan_mode *) (skb->data + 1);
537 for (; num_rsp; num_rsp--) {
538 bacpy(&data.bdaddr, &info->bdaddr);
539 data.pscan_rep_mode = info->pscan_rep_mode;
540 data.pscan_period_mode = info->pscan_period_mode;
541 data.pscan_mode = info->pscan_mode;
542 memcpy(data.dev_class, info->dev_class, 3);
543 data.clock_offset = info->clock_offset;
544 data.rssi = info->rssi;
546 hci_inquiry_cache_update(hdev, &data);
549 struct inquiry_info_with_rssi *info =
550 (struct inquiry_info_with_rssi *) (skb->data + 1);
552 for (; num_rsp; num_rsp--) {
553 bacpy(&data.bdaddr, &info->bdaddr);
554 data.pscan_rep_mode = info->pscan_rep_mode;
555 data.pscan_period_mode = info->pscan_period_mode;
556 data.pscan_mode = 0x00;
557 memcpy(data.dev_class, info->dev_class, 3);
558 data.clock_offset = info->clock_offset;
559 data.rssi = info->rssi;
561 hci_inquiry_cache_update(hdev, &data);
565 hci_dev_unlock(hdev);
568 /* Extended Inquiry Result */
569 static inline void hci_extended_inquiry_result_evt(struct hci_dev *hdev, struct sk_buff *skb)
571 struct inquiry_data data;
572 struct extended_inquiry_info *info = (struct extended_inquiry_info *) (skb->data + 1);
573 int num_rsp = *((__u8 *) skb->data);
575 BT_DBG("%s num_rsp %d", hdev->name, num_rsp);
582 for (; num_rsp; num_rsp--) {
583 bacpy(&data.bdaddr, &info->bdaddr);
584 data.pscan_rep_mode = info->pscan_rep_mode;
585 data.pscan_period_mode = info->pscan_period_mode;
586 data.pscan_mode = 0x00;
587 memcpy(data.dev_class, info->dev_class, 3);
588 data.clock_offset = info->clock_offset;
589 data.rssi = info->rssi;
591 hci_inquiry_cache_update(hdev, &data);
594 hci_dev_unlock(hdev);
597 /* Connect Request */
598 static inline void hci_conn_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
600 struct hci_ev_conn_request *ev = (struct hci_ev_conn_request *) skb->data;
601 int mask = hdev->link_mode;
603 BT_DBG("%s Connection request: %s type 0x%x", hdev->name,
604 batostr(&ev->bdaddr), ev->link_type);
606 mask |= hci_proto_connect_ind(hdev, &ev->bdaddr, ev->link_type);
608 if (mask & HCI_LM_ACCEPT) {
609 /* Connection accepted */
610 struct hci_conn *conn;
611 struct hci_cp_accept_conn_req cp;
614 conn = hci_conn_hash_lookup_ba(hdev, ev->link_type, &ev->bdaddr);
616 if (!(conn = hci_conn_add(hdev, ev->link_type, &ev->bdaddr))) {
617 BT_ERR("No memmory for new connection");
618 hci_dev_unlock(hdev);
622 memcpy(conn->dev_class, ev->dev_class, 3);
623 conn->state = BT_CONNECT;
624 hci_dev_unlock(hdev);
626 bacpy(&cp.bdaddr, &ev->bdaddr);
628 if (lmp_rswitch_capable(hdev) && (mask & HCI_LM_MASTER))
629 cp.role = 0x00; /* Become master */
631 cp.role = 0x01; /* Remain slave */
633 hci_send_cmd(hdev, OGF_LINK_CTL, OCF_ACCEPT_CONN_REQ, sizeof(cp), &cp);
635 /* Connection rejected */
636 struct hci_cp_reject_conn_req cp;
638 bacpy(&cp.bdaddr, &ev->bdaddr);
640 hci_send_cmd(hdev, OGF_LINK_CTL, OCF_REJECT_CONN_REQ, sizeof(cp), &cp);
644 /* Connect Complete */
645 static inline void hci_conn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
647 struct hci_ev_conn_complete *ev = (struct hci_ev_conn_complete *) skb->data;
648 struct hci_conn *conn = NULL;
650 BT_DBG("%s", hdev->name);
654 conn = hci_conn_hash_lookup_ba(hdev, ev->link_type, &ev->bdaddr);
656 hci_dev_unlock(hdev);
661 conn->handle = __le16_to_cpu(ev->handle);
662 conn->state = BT_CONNECTED;
664 if (test_bit(HCI_AUTH, &hdev->flags))
665 conn->link_mode |= HCI_LM_AUTH;
667 if (test_bit(HCI_ENCRYPT, &hdev->flags))
668 conn->link_mode |= HCI_LM_ENCRYPT;
670 /* Set link policy */
671 if (conn->type == ACL_LINK && hdev->link_policy) {
672 struct hci_cp_write_link_policy cp;
673 cp.handle = ev->handle;
674 cp.policy = __cpu_to_le16(hdev->link_policy);
675 hci_send_cmd(hdev, OGF_LINK_POLICY, OCF_WRITE_LINK_POLICY, sizeof(cp), &cp);
678 /* Set packet type for incoming connection */
680 struct hci_cp_change_conn_ptype cp;
681 cp.handle = ev->handle;
682 cp.pkt_type = (conn->type == ACL_LINK) ?
683 __cpu_to_le16(hdev->pkt_type & ACL_PTYPE_MASK):
684 __cpu_to_le16(hdev->pkt_type & SCO_PTYPE_MASK);
686 hci_send_cmd(hdev, OGF_LINK_CTL, OCF_CHANGE_CONN_PTYPE, sizeof(cp), &cp);
689 conn->state = BT_CLOSED;
691 if (conn->type == ACL_LINK) {
692 struct hci_conn *sco = conn->link;
695 hci_add_sco(sco, conn->handle);
697 hci_proto_connect_cfm(sco, ev->status);
703 hci_proto_connect_cfm(conn, ev->status);
707 hci_dev_unlock(hdev);
710 /* Disconnect Complete */
711 static inline void hci_disconn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
713 struct hci_ev_disconn_complete *ev = (struct hci_ev_disconn_complete *) skb->data;
714 struct hci_conn *conn = NULL;
715 __u16 handle = __le16_to_cpu(ev->handle);
717 BT_DBG("%s status %d", hdev->name, ev->status);
724 conn = hci_conn_hash_lookup_handle(hdev, handle);
726 conn->state = BT_CLOSED;
727 hci_proto_disconn_ind(conn, ev->reason);
731 hci_dev_unlock(hdev);
734 /* Number of completed packets */
735 static inline void hci_num_comp_pkts_evt(struct hci_dev *hdev, struct sk_buff *skb)
737 struct hci_ev_num_comp_pkts *ev = (struct hci_ev_num_comp_pkts *) skb->data;
741 skb_pull(skb, sizeof(*ev));
743 BT_DBG("%s num_hndl %d", hdev->name, ev->num_hndl);
745 if (skb->len < ev->num_hndl * 4) {
746 BT_DBG("%s bad parameters", hdev->name);
750 tasklet_disable(&hdev->tx_task);
752 for (i = 0, ptr = (__le16 *) skb->data; i < ev->num_hndl; i++) {
753 struct hci_conn *conn;
756 handle = __le16_to_cpu(get_unaligned(ptr++));
757 count = __le16_to_cpu(get_unaligned(ptr++));
759 conn = hci_conn_hash_lookup_handle(hdev, handle);
763 if (conn->type == SCO_LINK) {
764 if ((hdev->sco_cnt += count) > hdev->sco_pkts)
765 hdev->sco_cnt = hdev->sco_pkts;
767 if ((hdev->acl_cnt += count) > hdev->acl_pkts)
768 hdev->acl_cnt = hdev->acl_pkts;
774 tasklet_enable(&hdev->tx_task);
778 static inline void hci_role_change_evt(struct hci_dev *hdev, struct sk_buff *skb)
780 struct hci_ev_role_change *ev = (struct hci_ev_role_change *) skb->data;
781 struct hci_conn *conn = NULL;
783 BT_DBG("%s status %d", hdev->name, ev->status);
787 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
791 conn->link_mode &= ~HCI_LM_MASTER;
793 conn->link_mode |= HCI_LM_MASTER;
796 clear_bit(HCI_CONN_RSWITCH_PEND, &conn->pend);
798 hci_role_switch_cfm(conn, ev->status, ev->role);
801 hci_dev_unlock(hdev);
804 /* Authentication Complete */
805 static inline void hci_auth_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
807 struct hci_ev_auth_complete *ev = (struct hci_ev_auth_complete *) skb->data;
808 struct hci_conn *conn = NULL;
809 __u16 handle = __le16_to_cpu(ev->handle);
811 BT_DBG("%s status %d", hdev->name, ev->status);
815 conn = hci_conn_hash_lookup_handle(hdev, handle);
818 conn->link_mode |= HCI_LM_AUTH;
820 clear_bit(HCI_CONN_AUTH_PEND, &conn->pend);
822 hci_auth_cfm(conn, ev->status);
824 if (test_bit(HCI_CONN_ENCRYPT_PEND, &conn->pend)) {
826 struct hci_cp_set_conn_encrypt cp;
827 cp.handle = __cpu_to_le16(conn->handle);
829 hci_send_cmd(conn->hdev, OGF_LINK_CTL,
830 OCF_SET_CONN_ENCRYPT,
833 clear_bit(HCI_CONN_ENCRYPT_PEND, &conn->pend);
834 hci_encrypt_cfm(conn, ev->status, 0x00);
839 hci_dev_unlock(hdev);
842 /* Encryption Change */
843 static inline void hci_encrypt_change_evt(struct hci_dev *hdev, struct sk_buff *skb)
845 struct hci_ev_encrypt_change *ev = (struct hci_ev_encrypt_change *) skb->data;
846 struct hci_conn *conn = NULL;
847 __u16 handle = __le16_to_cpu(ev->handle);
849 BT_DBG("%s status %d", hdev->name, ev->status);
853 conn = hci_conn_hash_lookup_handle(hdev, handle);
857 conn->link_mode |= HCI_LM_ENCRYPT;
859 conn->link_mode &= ~HCI_LM_ENCRYPT;
862 clear_bit(HCI_CONN_ENCRYPT_PEND, &conn->pend);
864 hci_encrypt_cfm(conn, ev->status, ev->encrypt);
867 hci_dev_unlock(hdev);
870 /* Change Connection Link Key Complete */
871 static inline void hci_change_conn_link_key_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
873 struct hci_ev_change_conn_link_key_complete *ev = (struct hci_ev_change_conn_link_key_complete *) skb->data;
874 struct hci_conn *conn = NULL;
875 __u16 handle = __le16_to_cpu(ev->handle);
877 BT_DBG("%s status %d", hdev->name, ev->status);
881 conn = hci_conn_hash_lookup_handle(hdev, handle);
884 conn->link_mode |= HCI_LM_SECURE;
886 clear_bit(HCI_CONN_AUTH_PEND, &conn->pend);
888 hci_key_change_cfm(conn, ev->status);
891 hci_dev_unlock(hdev);
894 /* Pin Code Request*/
895 static inline void hci_pin_code_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
899 /* Link Key Request */
900 static inline void hci_link_key_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
904 /* Link Key Notification */
905 static inline void hci_link_key_notify_evt(struct hci_dev *hdev, struct sk_buff *skb)
910 static inline void hci_clock_offset_evt(struct hci_dev *hdev, struct sk_buff *skb)
912 struct hci_ev_clock_offset *ev = (struct hci_ev_clock_offset *) skb->data;
913 struct hci_conn *conn = NULL;
914 __u16 handle = __le16_to_cpu(ev->handle);
916 BT_DBG("%s status %d", hdev->name, ev->status);
920 conn = hci_conn_hash_lookup_handle(hdev, handle);
921 if (conn && !ev->status) {
922 struct inquiry_entry *ie;
924 if ((ie = hci_inquiry_cache_lookup(hdev, &conn->dst))) {
925 ie->data.clock_offset = ev->clock_offset;
926 ie->timestamp = jiffies;
930 hci_dev_unlock(hdev);
933 /* Page Scan Repetition Mode */
934 static inline void hci_pscan_rep_mode_evt(struct hci_dev *hdev, struct sk_buff *skb)
936 struct hci_ev_pscan_rep_mode *ev = (struct hci_ev_pscan_rep_mode *) skb->data;
937 struct inquiry_entry *ie;
939 BT_DBG("%s", hdev->name);
943 if ((ie = hci_inquiry_cache_lookup(hdev, &ev->bdaddr))) {
944 ie->data.pscan_rep_mode = ev->pscan_rep_mode;
945 ie->timestamp = jiffies;
948 hci_dev_unlock(hdev);
951 void hci_event_packet(struct hci_dev *hdev, struct sk_buff *skb)
953 struct hci_event_hdr *hdr = (struct hci_event_hdr *) skb->data;
954 struct hci_ev_cmd_complete *ec;
955 struct hci_ev_cmd_status *cs;
956 u16 opcode, ocf, ogf;
958 skb_pull(skb, HCI_EVENT_HDR_SIZE);
960 BT_DBG("%s evt 0x%x", hdev->name, hdr->evt);
963 case HCI_EV_NUM_COMP_PKTS:
964 hci_num_comp_pkts_evt(hdev, skb);
967 case HCI_EV_INQUIRY_COMPLETE:
968 hci_inquiry_complete_evt(hdev, skb);
971 case HCI_EV_INQUIRY_RESULT:
972 hci_inquiry_result_evt(hdev, skb);
975 case HCI_EV_INQUIRY_RESULT_WITH_RSSI:
976 hci_inquiry_result_with_rssi_evt(hdev, skb);
979 case HCI_EV_EXTENDED_INQUIRY_RESULT:
980 hci_extended_inquiry_result_evt(hdev, skb);
983 case HCI_EV_CONN_REQUEST:
984 hci_conn_request_evt(hdev, skb);
987 case HCI_EV_CONN_COMPLETE:
988 hci_conn_complete_evt(hdev, skb);
991 case HCI_EV_DISCONN_COMPLETE:
992 hci_disconn_complete_evt(hdev, skb);
995 case HCI_EV_ROLE_CHANGE:
996 hci_role_change_evt(hdev, skb);
999 case HCI_EV_AUTH_COMPLETE:
1000 hci_auth_complete_evt(hdev, skb);
1003 case HCI_EV_ENCRYPT_CHANGE:
1004 hci_encrypt_change_evt(hdev, skb);
1007 case HCI_EV_CHANGE_CONN_LINK_KEY_COMPLETE:
1008 hci_change_conn_link_key_complete_evt(hdev, skb);
1011 case HCI_EV_PIN_CODE_REQ:
1012 hci_pin_code_request_evt(hdev, skb);
1015 case HCI_EV_LINK_KEY_REQ:
1016 hci_link_key_request_evt(hdev, skb);
1019 case HCI_EV_LINK_KEY_NOTIFY:
1020 hci_link_key_notify_evt(hdev, skb);
1023 case HCI_EV_CLOCK_OFFSET:
1024 hci_clock_offset_evt(hdev, skb);
1027 case HCI_EV_PSCAN_REP_MODE:
1028 hci_pscan_rep_mode_evt(hdev, skb);
1031 case HCI_EV_CMD_STATUS:
1032 cs = (struct hci_ev_cmd_status *) skb->data;
1033 skb_pull(skb, sizeof(cs));
1035 opcode = __le16_to_cpu(cs->opcode);
1036 ogf = hci_opcode_ogf(opcode);
1037 ocf = hci_opcode_ocf(opcode);
1040 case OGF_INFO_PARAM:
1041 hci_cs_info_param(hdev, ocf, cs->status);
1045 hci_cs_host_ctl(hdev, ocf, cs->status);
1049 hci_cs_link_ctl(hdev, ocf, cs->status);
1052 case OGF_LINK_POLICY:
1053 hci_cs_link_policy(hdev, ocf, cs->status);
1057 BT_DBG("%s Command Status OGF %x", hdev->name, ogf);
1062 atomic_set(&hdev->cmd_cnt, 1);
1063 if (!skb_queue_empty(&hdev->cmd_q))
1064 hci_sched_cmd(hdev);
1068 case HCI_EV_CMD_COMPLETE:
1069 ec = (struct hci_ev_cmd_complete *) skb->data;
1070 skb_pull(skb, sizeof(*ec));
1072 opcode = __le16_to_cpu(ec->opcode);
1073 ogf = hci_opcode_ogf(opcode);
1074 ocf = hci_opcode_ocf(opcode);
1077 case OGF_INFO_PARAM:
1078 hci_cc_info_param(hdev, ocf, skb);
1082 hci_cc_host_ctl(hdev, ocf, skb);
1086 hci_cc_link_ctl(hdev, ocf, skb);
1089 case OGF_LINK_POLICY:
1090 hci_cc_link_policy(hdev, ocf, skb);
1094 BT_DBG("%s Command Completed OGF %x", hdev->name, ogf);
1099 atomic_set(&hdev->cmd_cnt, 1);
1100 if (!skb_queue_empty(&hdev->cmd_q))
1101 hci_sched_cmd(hdev);
1107 hdev->stat.evt_rx++;
1110 /* Generate internal stack event */
1111 void hci_si_event(struct hci_dev *hdev, int type, int dlen, void *data)
1113 struct hci_event_hdr *hdr;
1114 struct hci_ev_stack_internal *ev;
1115 struct sk_buff *skb;
1117 skb = bt_skb_alloc(HCI_EVENT_HDR_SIZE + sizeof(*ev) + dlen, GFP_ATOMIC);
1121 hdr = (void *) skb_put(skb, HCI_EVENT_HDR_SIZE);
1122 hdr->evt = HCI_EV_STACK_INTERNAL;
1123 hdr->plen = sizeof(*ev) + dlen;
1125 ev = (void *) skb_put(skb, sizeof(*ev) + dlen);
1127 memcpy(ev->data, data, dlen);
1129 bt_cb(skb)->incoming = 1;
1130 __net_timestamp(skb);
1132 bt_cb(skb)->pkt_type = HCI_EVENT_PKT;
1133 skb->dev = (void *) hdev;
1134 hci_send_to_sock(hdev, skb);