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 <asm/uaccess.h>
43 #include <asm/unaligned.h>
45 #include <net/bluetooth/bluetooth.h>
46 #include <net/bluetooth/hci_core.h>
48 /* Code under HCI_AUTO_NAME_RESOLVE contains the CQ fix code taken from the
49 * kernel release 2.6.39. If we are migrating to a kernel version which is
50 * less than 2.6.39, then need to retain this patch. For more information
53 #define HCI_AUTO_NAME_RESOLVE
55 /* Handle HCI Event packets */
57 static void hci_cc_inquiry_cancel(struct hci_dev *hdev, struct sk_buff *skb)
59 __u8 status = *((__u8 *) skb->data);
61 BT_DBG("%s status 0x%x", hdev->name, status);
66 clear_bit(HCI_INQUIRY, &hdev->flags);
68 hci_req_complete(hdev, status);
70 hci_conn_check_pending(hdev);
73 static void hci_cc_exit_periodic_inq(struct hci_dev *hdev, struct sk_buff *skb)
75 __u8 status = *((__u8 *) skb->data);
77 BT_DBG("%s status 0x%x", hdev->name, status);
82 clear_bit(HCI_INQUIRY, &hdev->flags);
84 hci_conn_check_pending(hdev);
87 static void hci_cc_remote_name_req_cancel(struct hci_dev *hdev, struct sk_buff *skb)
89 BT_DBG("%s", hdev->name);
92 static void hci_cc_role_discovery(struct hci_dev *hdev, struct sk_buff *skb)
94 struct hci_rp_role_discovery *rp = (void *) skb->data;
95 struct hci_conn *conn;
97 BT_DBG("%s status 0x%x", hdev->name, rp->status);
104 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
107 conn->link_mode &= ~HCI_LM_MASTER;
109 conn->link_mode |= HCI_LM_MASTER;
112 hci_dev_unlock(hdev);
115 static void hci_cc_read_link_policy(struct hci_dev *hdev, struct sk_buff *skb)
117 struct hci_rp_read_link_policy *rp = (void *) skb->data;
118 struct hci_conn *conn;
120 BT_DBG("%s status 0x%x", hdev->name, rp->status);
127 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
129 conn->link_policy = __le16_to_cpu(rp->policy);
131 hci_dev_unlock(hdev);
134 static void hci_cc_write_link_policy(struct hci_dev *hdev, struct sk_buff *skb)
136 struct hci_rp_write_link_policy *rp = (void *) skb->data;
137 struct hci_conn *conn;
140 BT_DBG("%s status 0x%x", hdev->name, rp->status);
145 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_LINK_POLICY);
151 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
153 conn->link_policy = get_unaligned_le16(sent + 2);
155 hci_dev_unlock(hdev);
158 static void hci_cc_read_def_link_policy(struct hci_dev *hdev, struct sk_buff *skb)
160 struct hci_rp_read_def_link_policy *rp = (void *) skb->data;
162 BT_DBG("%s status 0x%x", hdev->name, rp->status);
167 hdev->link_policy = __le16_to_cpu(rp->policy);
170 static void hci_cc_write_def_link_policy(struct hci_dev *hdev, struct sk_buff *skb)
172 __u8 status = *((__u8 *) skb->data);
175 BT_DBG("%s status 0x%x", hdev->name, status);
177 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_DEF_LINK_POLICY);
182 hdev->link_policy = get_unaligned_le16(sent);
184 hci_req_complete(hdev, status);
187 static void hci_cc_reset(struct hci_dev *hdev, struct sk_buff *skb)
189 __u8 status = *((__u8 *) skb->data);
191 BT_DBG("%s status 0x%x", hdev->name, status);
193 hci_req_complete(hdev, status);
196 static void hci_cc_write_local_name(struct hci_dev *hdev, struct sk_buff *skb)
198 __u8 status = *((__u8 *) skb->data);
201 BT_DBG("%s status 0x%x", hdev->name, status);
206 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_LOCAL_NAME);
210 memcpy(hdev->dev_name, sent, 248);
213 static void hci_cc_read_local_name(struct hci_dev *hdev, struct sk_buff *skb)
215 struct hci_rp_read_local_name *rp = (void *) skb->data;
217 BT_DBG("%s status 0x%x", hdev->name, rp->status);
222 memcpy(hdev->dev_name, rp->name, 248);
225 static void hci_cc_write_auth_enable(struct hci_dev *hdev, struct sk_buff *skb)
227 __u8 status = *((__u8 *) skb->data);
230 BT_DBG("%s status 0x%x", hdev->name, status);
232 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_AUTH_ENABLE);
237 __u8 param = *((__u8 *) sent);
239 if (param == AUTH_ENABLED)
240 set_bit(HCI_AUTH, &hdev->flags);
242 clear_bit(HCI_AUTH, &hdev->flags);
245 hci_req_complete(hdev, status);
248 static void hci_cc_write_encrypt_mode(struct hci_dev *hdev, struct sk_buff *skb)
250 __u8 status = *((__u8 *) skb->data);
253 BT_DBG("%s status 0x%x", hdev->name, status);
255 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_ENCRYPT_MODE);
260 __u8 param = *((__u8 *) sent);
263 set_bit(HCI_ENCRYPT, &hdev->flags);
265 clear_bit(HCI_ENCRYPT, &hdev->flags);
268 hci_req_complete(hdev, status);
271 static void hci_cc_write_scan_enable(struct hci_dev *hdev, struct sk_buff *skb)
273 __u8 status = *((__u8 *) skb->data);
276 BT_DBG("%s status 0x%x", hdev->name, status);
278 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_SCAN_ENABLE);
283 __u8 param = *((__u8 *) sent);
285 clear_bit(HCI_PSCAN, &hdev->flags);
286 clear_bit(HCI_ISCAN, &hdev->flags);
288 if (param & SCAN_INQUIRY)
289 set_bit(HCI_ISCAN, &hdev->flags);
291 if (param & SCAN_PAGE)
292 set_bit(HCI_PSCAN, &hdev->flags);
295 hci_req_complete(hdev, status);
298 static void hci_cc_read_class_of_dev(struct hci_dev *hdev, struct sk_buff *skb)
300 struct hci_rp_read_class_of_dev *rp = (void *) skb->data;
302 BT_DBG("%s status 0x%x", hdev->name, rp->status);
307 memcpy(hdev->dev_class, rp->dev_class, 3);
309 BT_DBG("%s class 0x%.2x%.2x%.2x", hdev->name,
310 hdev->dev_class[2], hdev->dev_class[1], hdev->dev_class[0]);
313 static void hci_cc_write_class_of_dev(struct hci_dev *hdev, struct sk_buff *skb)
315 __u8 status = *((__u8 *) skb->data);
318 BT_DBG("%s status 0x%x", hdev->name, status);
323 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_CLASS_OF_DEV);
327 memcpy(hdev->dev_class, sent, 3);
330 static void hci_cc_read_voice_setting(struct hci_dev *hdev, struct sk_buff *skb)
332 struct hci_rp_read_voice_setting *rp = (void *) skb->data;
335 BT_DBG("%s status 0x%x", hdev->name, rp->status);
340 setting = __le16_to_cpu(rp->voice_setting);
342 if (hdev->voice_setting == setting)
345 hdev->voice_setting = setting;
347 BT_DBG("%s voice setting 0x%04x", hdev->name, setting);
350 tasklet_disable(&hdev->tx_task);
351 hdev->notify(hdev, HCI_NOTIFY_VOICE_SETTING);
352 tasklet_enable(&hdev->tx_task);
356 static void hci_cc_write_voice_setting(struct hci_dev *hdev, struct sk_buff *skb)
358 __u8 status = *((__u8 *) skb->data);
362 BT_DBG("%s status 0x%x", hdev->name, status);
367 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_VOICE_SETTING);
371 setting = get_unaligned_le16(sent);
373 if (hdev->voice_setting == setting)
376 hdev->voice_setting = setting;
378 BT_DBG("%s voice setting 0x%04x", hdev->name, setting);
381 tasklet_disable(&hdev->tx_task);
382 hdev->notify(hdev, HCI_NOTIFY_VOICE_SETTING);
383 tasklet_enable(&hdev->tx_task);
387 static void hci_cc_host_buffer_size(struct hci_dev *hdev, struct sk_buff *skb)
389 __u8 status = *((__u8 *) skb->data);
391 BT_DBG("%s status 0x%x", hdev->name, status);
393 hci_req_complete(hdev, status);
396 static void hci_cc_read_ssp_mode(struct hci_dev *hdev, struct sk_buff *skb)
398 struct hci_rp_read_ssp_mode *rp = (void *) skb->data;
400 BT_DBG("%s status 0x%x", hdev->name, rp->status);
405 hdev->ssp_mode = rp->mode;
408 static void hci_cc_write_ssp_mode(struct hci_dev *hdev, struct sk_buff *skb)
410 __u8 status = *((__u8 *) skb->data);
413 BT_DBG("%s status 0x%x", hdev->name, status);
418 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_SSP_MODE);
422 hdev->ssp_mode = *((__u8 *) sent);
425 static void hci_cc_read_local_version(struct hci_dev *hdev, struct sk_buff *skb)
427 struct hci_rp_read_local_version *rp = (void *) skb->data;
429 BT_DBG("%s status 0x%x", hdev->name, rp->status);
434 hdev->hci_ver = rp->hci_ver;
435 hdev->hci_rev = __le16_to_cpu(rp->hci_rev);
436 hdev->manufacturer = __le16_to_cpu(rp->manufacturer);
438 BT_DBG("%s manufacturer %d hci ver %d:%d", hdev->name,
440 hdev->hci_ver, hdev->hci_rev);
443 static void hci_cc_read_local_commands(struct hci_dev *hdev, struct sk_buff *skb)
445 struct hci_rp_read_local_commands *rp = (void *) skb->data;
447 BT_DBG("%s status 0x%x", hdev->name, rp->status);
452 memcpy(hdev->commands, rp->commands, sizeof(hdev->commands));
455 static void hci_cc_read_local_features(struct hci_dev *hdev, struct sk_buff *skb)
457 struct hci_rp_read_local_features *rp = (void *) skb->data;
459 BT_DBG("%s status 0x%x", hdev->name, rp->status);
464 memcpy(hdev->features, rp->features, 8);
466 /* Adjust default settings according to features
467 * supported by device. */
469 if (hdev->features[0] & LMP_3SLOT)
470 hdev->pkt_type |= (HCI_DM3 | HCI_DH3);
472 if (hdev->features[0] & LMP_5SLOT)
473 hdev->pkt_type |= (HCI_DM5 | HCI_DH5);
475 if (hdev->features[1] & LMP_HV2) {
476 hdev->pkt_type |= (HCI_HV2);
477 hdev->esco_type |= (ESCO_HV2);
480 if (hdev->features[1] & LMP_HV3) {
481 hdev->pkt_type |= (HCI_HV3);
482 hdev->esco_type |= (ESCO_HV3);
485 if (hdev->features[3] & LMP_ESCO)
486 hdev->esco_type |= (ESCO_EV3);
488 if (hdev->features[4] & LMP_EV4)
489 hdev->esco_type |= (ESCO_EV4);
491 if (hdev->features[4] & LMP_EV5)
492 hdev->esco_type |= (ESCO_EV5);
494 if (hdev->features[5] & LMP_EDR_ESCO_2M)
495 hdev->esco_type |= (ESCO_2EV3);
497 if (hdev->features[5] & LMP_EDR_ESCO_3M)
498 hdev->esco_type |= (ESCO_3EV3);
500 if (hdev->features[5] & LMP_EDR_3S_ESCO)
501 hdev->esco_type |= (ESCO_2EV5 | ESCO_3EV5);
503 BT_DBG("%s features 0x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x", hdev->name,
504 hdev->features[0], hdev->features[1],
505 hdev->features[2], hdev->features[3],
506 hdev->features[4], hdev->features[5],
507 hdev->features[6], hdev->features[7]);
510 static void hci_cc_read_buffer_size(struct hci_dev *hdev, struct sk_buff *skb)
512 struct hci_rp_read_buffer_size *rp = (void *) skb->data;
514 BT_DBG("%s status 0x%x", hdev->name, rp->status);
519 hdev->acl_mtu = __le16_to_cpu(rp->acl_mtu);
520 hdev->sco_mtu = rp->sco_mtu;
521 hdev->acl_pkts = __le16_to_cpu(rp->acl_max_pkt);
522 hdev->sco_pkts = __le16_to_cpu(rp->sco_max_pkt);
524 if (test_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks)) {
529 hdev->acl_cnt = hdev->acl_pkts;
530 hdev->sco_cnt = hdev->sco_pkts;
532 BT_DBG("%s acl mtu %d:%d sco mtu %d:%d", hdev->name,
533 hdev->acl_mtu, hdev->acl_pkts,
534 hdev->sco_mtu, hdev->sco_pkts);
537 static void hci_cc_read_bd_addr(struct hci_dev *hdev, struct sk_buff *skb)
539 struct hci_rp_read_bd_addr *rp = (void *) skb->data;
541 BT_DBG("%s status 0x%x", hdev->name, rp->status);
544 bacpy(&hdev->bdaddr, &rp->bdaddr);
546 hci_req_complete(hdev, rp->status);
549 static inline void hci_cs_inquiry(struct hci_dev *hdev, __u8 status)
551 BT_DBG("%s status 0x%x", hdev->name, status);
554 hci_req_complete(hdev, status);
556 hci_conn_check_pending(hdev);
558 set_bit(HCI_INQUIRY, &hdev->flags);
561 static inline void hci_cs_create_conn(struct hci_dev *hdev, __u8 status)
563 struct hci_cp_create_conn *cp;
564 struct hci_conn *conn;
566 BT_DBG("%s status 0x%x", hdev->name, status);
568 cp = hci_sent_cmd_data(hdev, HCI_OP_CREATE_CONN);
574 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->bdaddr);
576 BT_DBG("%s bdaddr %s conn %p", hdev->name, batostr(&cp->bdaddr), conn);
579 if (conn && conn->state == BT_CONNECT) {
580 if (status != 0x0c || conn->attempt > 2) {
581 conn->state = BT_CLOSED;
582 hci_proto_connect_cfm(conn, status);
585 conn->state = BT_CONNECT2;
589 conn = hci_conn_add(hdev, ACL_LINK, &cp->bdaddr);
592 conn->link_mode |= HCI_LM_MASTER;
594 BT_ERR("No memory for new connection");
598 hci_dev_unlock(hdev);
601 static void hci_cs_add_sco(struct hci_dev *hdev, __u8 status)
603 struct hci_cp_add_sco *cp;
604 struct hci_conn *acl, *sco;
607 BT_DBG("%s status 0x%x", hdev->name, status);
612 cp = hci_sent_cmd_data(hdev, HCI_OP_ADD_SCO);
616 handle = __le16_to_cpu(cp->handle);
618 BT_DBG("%s handle %d", hdev->name, handle);
622 acl = hci_conn_hash_lookup_handle(hdev, handle);
623 if (acl && (sco = acl->link)) {
624 sco->state = BT_CLOSED;
626 hci_proto_connect_cfm(sco, status);
630 hci_dev_unlock(hdev);
633 static void hci_cs_auth_requested(struct hci_dev *hdev, __u8 status)
635 struct hci_cp_auth_requested *cp;
636 struct hci_conn *conn;
638 BT_DBG("%s status 0x%x", hdev->name, status);
643 cp = hci_sent_cmd_data(hdev, HCI_OP_AUTH_REQUESTED);
649 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
651 if (conn->state == BT_CONFIG) {
652 hci_proto_connect_cfm(conn, status);
657 hci_dev_unlock(hdev);
660 static void hci_cs_set_conn_encrypt(struct hci_dev *hdev, __u8 status)
662 struct hci_cp_set_conn_encrypt *cp;
663 struct hci_conn *conn;
665 BT_DBG("%s status 0x%x", hdev->name, status);
670 cp = hci_sent_cmd_data(hdev, HCI_OP_SET_CONN_ENCRYPT);
676 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
678 if (conn->state == BT_CONFIG) {
679 hci_proto_connect_cfm(conn, status);
684 hci_dev_unlock(hdev);
687 static void hci_cs_remote_name_req(struct hci_dev *hdev, __u8 status)
689 BT_DBG("%s status 0x%x", hdev->name, status);
692 static void hci_cs_read_remote_features(struct hci_dev *hdev, __u8 status)
694 struct hci_cp_read_remote_features *cp;
695 struct hci_conn *conn;
697 BT_DBG("%s status 0x%x", hdev->name, status);
702 cp = hci_sent_cmd_data(hdev, HCI_OP_READ_REMOTE_FEATURES);
708 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
710 if (conn->state == BT_CONFIG) {
711 hci_proto_connect_cfm(conn, status);
716 hci_dev_unlock(hdev);
719 static void hci_cs_read_remote_ext_features(struct hci_dev *hdev, __u8 status)
721 struct hci_cp_read_remote_ext_features *cp;
722 struct hci_conn *conn;
724 BT_DBG("%s status 0x%x", hdev->name, status);
729 cp = hci_sent_cmd_data(hdev, HCI_OP_READ_REMOTE_EXT_FEATURES);
735 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
737 if (conn->state == BT_CONFIG) {
738 hci_proto_connect_cfm(conn, status);
743 hci_dev_unlock(hdev);
746 static void hci_cs_setup_sync_conn(struct hci_dev *hdev, __u8 status)
748 struct hci_cp_setup_sync_conn *cp;
749 struct hci_conn *acl, *sco;
752 BT_DBG("%s status 0x%x", hdev->name, status);
757 cp = hci_sent_cmd_data(hdev, HCI_OP_SETUP_SYNC_CONN);
761 handle = __le16_to_cpu(cp->handle);
763 BT_DBG("%s handle %d", hdev->name, handle);
767 acl = hci_conn_hash_lookup_handle(hdev, handle);
768 if (acl && (sco = acl->link)) {
769 sco->state = BT_CLOSED;
771 hci_proto_connect_cfm(sco, status);
775 hci_dev_unlock(hdev);
778 static void hci_cs_sniff_mode(struct hci_dev *hdev, __u8 status)
780 struct hci_cp_sniff_mode *cp;
781 struct hci_conn *conn;
783 BT_DBG("%s status 0x%x", hdev->name, status);
788 cp = hci_sent_cmd_data(hdev, HCI_OP_SNIFF_MODE);
794 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
796 clear_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->pend);
798 if (test_and_clear_bit(HCI_CONN_SCO_SETUP_PEND, &conn->pend))
799 hci_sco_setup(conn, status);
802 hci_dev_unlock(hdev);
805 static void hci_cs_exit_sniff_mode(struct hci_dev *hdev, __u8 status)
807 struct hci_cp_exit_sniff_mode *cp;
808 struct hci_conn *conn;
810 BT_DBG("%s status 0x%x", hdev->name, status);
815 cp = hci_sent_cmd_data(hdev, HCI_OP_EXIT_SNIFF_MODE);
821 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
823 clear_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->pend);
825 if (test_and_clear_bit(HCI_CONN_SCO_SETUP_PEND, &conn->pend))
826 hci_sco_setup(conn, status);
829 hci_dev_unlock(hdev);
832 static inline void hci_inquiry_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
834 __u8 status = *((__u8 *) skb->data);
836 BT_DBG("%s status %d", hdev->name, status);
838 clear_bit(HCI_INQUIRY, &hdev->flags);
840 hci_req_complete(hdev, status);
842 hci_conn_check_pending(hdev);
845 static inline void hci_inquiry_result_evt(struct hci_dev *hdev, struct sk_buff *skb)
847 struct inquiry_data data;
848 struct inquiry_info *info = (void *) (skb->data + 1);
849 int num_rsp = *((__u8 *) skb->data);
851 BT_DBG("%s num_rsp %d", hdev->name, num_rsp);
858 for (; num_rsp; num_rsp--) {
859 bacpy(&data.bdaddr, &info->bdaddr);
860 data.pscan_rep_mode = info->pscan_rep_mode;
861 data.pscan_period_mode = info->pscan_period_mode;
862 data.pscan_mode = info->pscan_mode;
863 memcpy(data.dev_class, info->dev_class, 3);
864 data.clock_offset = info->clock_offset;
866 data.ssp_mode = 0x00;
868 hci_inquiry_cache_update(hdev, &data);
871 hci_dev_unlock(hdev);
874 static inline void hci_conn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
876 struct hci_ev_conn_complete *ev = (void *) skb->data;
877 struct hci_conn *conn;
879 BT_DBG("%s", hdev->name);
883 conn = hci_conn_hash_lookup_ba(hdev, ev->link_type, &ev->bdaddr);
885 if (ev->link_type != SCO_LINK)
888 conn = hci_conn_hash_lookup_ba(hdev, ESCO_LINK, &ev->bdaddr);
892 conn->type = SCO_LINK;
896 conn->handle = __le16_to_cpu(ev->handle);
898 if (conn->type == ACL_LINK) {
899 conn->state = BT_CONFIG;
901 conn->disc_timeout = HCI_DISCONN_TIMEOUT;
903 conn->state = BT_CONNECTED;
905 hci_conn_hold_device(conn);
906 hci_conn_add_sysfs(conn);
908 if (test_bit(HCI_AUTH, &hdev->flags))
909 conn->link_mode |= HCI_LM_AUTH;
911 if (test_bit(HCI_ENCRYPT, &hdev->flags))
912 conn->link_mode |= HCI_LM_ENCRYPT;
914 /* Get remote features */
915 if (conn->type == ACL_LINK) {
916 struct hci_cp_read_remote_features cp;
917 cp.handle = ev->handle;
918 hci_send_cmd(hdev, HCI_OP_READ_REMOTE_FEATURES,
922 /* Set packet type for incoming connection */
923 if (!conn->out && hdev->hci_ver < 3) {
924 struct hci_cp_change_conn_ptype cp;
925 cp.handle = ev->handle;
926 cp.pkt_type = cpu_to_le16(conn->pkt_type);
927 hci_send_cmd(hdev, HCI_OP_CHANGE_CONN_PTYPE,
931 conn->state = BT_CLOSED;
933 if (conn->type == ACL_LINK)
934 hci_sco_setup(conn, ev->status);
937 hci_proto_connect_cfm(conn, ev->status);
939 } else if (ev->link_type != ACL_LINK)
940 hci_proto_connect_cfm(conn, ev->status);
943 hci_dev_unlock(hdev);
945 hci_conn_check_pending(hdev);
948 #ifdef HCI_AUTO_NAME_RESOLVE
949 static int hci_outgoing_auth_needed(struct hci_dev *hdev,
950 struct hci_conn *conn)
952 if (conn->state != BT_CONFIG || !conn->out)
955 if (conn->sec_level == BT_SECURITY_SDP)
958 /* Only request authentication for SSP connections */
959 if (!(hdev->ssp_mode > 0 && conn->ssp_mode > 0))
966 static inline void hci_conn_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
968 struct hci_ev_conn_request *ev = (void *) skb->data;
969 int mask = hdev->link_mode;
971 BT_DBG("%s bdaddr %s type 0x%x", hdev->name,
972 batostr(&ev->bdaddr), ev->link_type);
974 mask |= hci_proto_connect_ind(hdev, &ev->bdaddr, ev->link_type);
976 if ((mask & HCI_LM_ACCEPT) && !hci_blacklist_lookup(hdev, &ev->bdaddr)) {
977 /* Connection accepted */
978 struct inquiry_entry *ie;
979 struct hci_conn *conn;
983 if ((ie = hci_inquiry_cache_lookup(hdev, &ev->bdaddr)))
984 memcpy(ie->data.dev_class, ev->dev_class, 3);
986 conn = hci_conn_hash_lookup_ba(hdev, ev->link_type, &ev->bdaddr);
988 if (!(conn = hci_conn_add(hdev, ev->link_type, &ev->bdaddr))) {
989 BT_ERR("No memory for new connection");
990 hci_dev_unlock(hdev);
995 memcpy(conn->dev_class, ev->dev_class, 3);
996 conn->state = BT_CONNECT;
998 hci_dev_unlock(hdev);
1000 if (ev->link_type == ACL_LINK || !lmp_esco_capable(hdev)) {
1001 struct hci_cp_accept_conn_req cp;
1003 bacpy(&cp.bdaddr, &ev->bdaddr);
1005 if (lmp_rswitch_capable(hdev) && (mask & HCI_LM_MASTER))
1006 cp.role = 0x00; /* Become master */
1008 cp.role = 0x01; /* Remain slave */
1010 hci_send_cmd(hdev, HCI_OP_ACCEPT_CONN_REQ,
1013 struct hci_cp_accept_sync_conn_req cp;
1015 bacpy(&cp.bdaddr, &ev->bdaddr);
1016 cp.pkt_type = cpu_to_le16(conn->pkt_type);
1018 cp.tx_bandwidth = cpu_to_le32(0x00001f40);
1019 cp.rx_bandwidth = cpu_to_le32(0x00001f40);
1020 cp.max_latency = cpu_to_le16(0xffff);
1021 cp.content_format = cpu_to_le16(hdev->voice_setting);
1022 cp.retrans_effort = 0xff;
1024 hci_send_cmd(hdev, HCI_OP_ACCEPT_SYNC_CONN_REQ,
1028 /* Connection rejected */
1029 struct hci_cp_reject_conn_req cp;
1031 bacpy(&cp.bdaddr, &ev->bdaddr);
1033 hci_send_cmd(hdev, HCI_OP_REJECT_CONN_REQ, sizeof(cp), &cp);
1037 static inline void hci_disconn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
1039 struct hci_ev_disconn_complete *ev = (void *) skb->data;
1040 struct hci_conn *conn;
1042 BT_DBG("%s status %d", hdev->name, ev->status);
1049 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
1051 conn->state = BT_CLOSED;
1053 hci_proto_disconn_cfm(conn, ev->reason);
1057 hci_dev_unlock(hdev);
1060 static inline void hci_auth_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
1062 struct hci_ev_auth_complete *ev = (void *) skb->data;
1063 struct hci_conn *conn;
1065 BT_DBG("%s status %d", hdev->name, ev->status);
1069 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
1072 conn->link_mode |= HCI_LM_AUTH;
1074 conn->sec_level = BT_SECURITY_LOW;
1076 clear_bit(HCI_CONN_AUTH_PEND, &conn->pend);
1078 if (conn->state == BT_CONFIG) {
1079 if (!ev->status && hdev->ssp_mode > 0 &&
1080 conn->ssp_mode > 0) {
1081 struct hci_cp_set_conn_encrypt cp;
1082 cp.handle = ev->handle;
1084 hci_send_cmd(hdev, HCI_OP_SET_CONN_ENCRYPT,
1087 conn->state = BT_CONNECTED;
1088 hci_proto_connect_cfm(conn, ev->status);
1092 hci_auth_cfm(conn, ev->status);
1094 hci_conn_hold(conn);
1095 conn->disc_timeout = HCI_DISCONN_TIMEOUT;
1099 if (test_bit(HCI_CONN_ENCRYPT_PEND, &conn->pend)) {
1101 struct hci_cp_set_conn_encrypt cp;
1102 cp.handle = ev->handle;
1104 hci_send_cmd(hdev, HCI_OP_SET_CONN_ENCRYPT,
1107 clear_bit(HCI_CONN_ENCRYPT_PEND, &conn->pend);
1108 hci_encrypt_cfm(conn, ev->status, 0x00);
1113 hci_dev_unlock(hdev);
1115 #ifdef HCI_AUTO_NAME_RESOLVE
1116 static inline void hci_remote_name_evt(struct hci_dev *hdev, struct sk_buff *skb)
1118 struct hci_ev_remote_name *ev = (void *) skb->data;
1119 struct hci_conn *conn;
1120 BT_DBG("%s", hdev->name);
1122 hci_conn_check_pending(hdev);
1126 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
1127 if (conn && hci_outgoing_auth_needed(hdev, conn)) {
1128 struct hci_cp_auth_requested cp;
1129 cp.handle = __cpu_to_le16(conn->handle);
1130 hci_send_cmd(hdev, HCI_OP_AUTH_REQUESTED, sizeof(cp), &cp);
1133 hci_dev_unlock(hdev);
1136 static inline void hci_remote_name_evt(struct hci_dev *hdev, struct sk_buff *skb)
1138 BT_DBG("%s", hdev->name);
1140 hci_conn_check_pending(hdev);
1144 static inline void hci_encrypt_change_evt(struct hci_dev *hdev, struct sk_buff *skb)
1146 struct hci_ev_encrypt_change *ev = (void *) skb->data;
1147 struct hci_conn *conn;
1149 BT_DBG("%s status %d", hdev->name, ev->status);
1153 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
1157 /* Encryption implies authentication */
1158 conn->link_mode |= HCI_LM_AUTH;
1159 conn->link_mode |= HCI_LM_ENCRYPT;
1161 conn->link_mode &= ~HCI_LM_ENCRYPT;
1164 clear_bit(HCI_CONN_ENCRYPT_PEND, &conn->pend);
1166 if (conn->state == BT_CONFIG) {
1168 conn->state = BT_CONNECTED;
1170 hci_proto_connect_cfm(conn, ev->status);
1173 hci_encrypt_cfm(conn, ev->status, ev->encrypt);
1176 hci_dev_unlock(hdev);
1179 static inline void hci_change_link_key_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
1181 struct hci_ev_change_link_key_complete *ev = (void *) skb->data;
1182 struct hci_conn *conn;
1184 BT_DBG("%s status %d", hdev->name, ev->status);
1188 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
1191 conn->link_mode |= HCI_LM_SECURE;
1193 clear_bit(HCI_CONN_AUTH_PEND, &conn->pend);
1195 hci_key_change_cfm(conn, ev->status);
1198 hci_dev_unlock(hdev);
1201 #ifdef HCI_AUTO_NAME_RESOLVE
1202 static inline void hci_remote_features_evt(struct hci_dev *hdev, struct sk_buff *skb)
1204 struct hci_ev_remote_features *ev = (void *) skb->data;
1205 struct hci_conn *conn;
1207 BT_DBG("%s status %d", hdev->name, ev->status);
1211 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
1216 memcpy(conn->features, ev->features, 8);
1218 if (conn->state != BT_CONFIG)
1221 if (!ev->status && lmp_ssp_capable(hdev) && lmp_ssp_capable(conn)) {
1222 struct hci_cp_read_remote_ext_features cp;
1223 cp.handle = ev->handle;
1225 hci_send_cmd(hdev, HCI_OP_READ_REMOTE_EXT_FEATURES,
1231 struct hci_cp_remote_name_req cp;
1232 memset(&cp, 0, sizeof(cp));
1233 bacpy(&cp.bdaddr, &conn->dst);
1234 cp.pscan_rep_mode = 0x02;
1235 hci_send_cmd(hdev, HCI_OP_REMOTE_NAME_REQ, sizeof(cp), &cp);
1238 if (!hci_outgoing_auth_needed(hdev, conn)) {
1239 conn->state = BT_CONNECTED;
1240 hci_proto_connect_cfm(conn, ev->status);
1245 hci_dev_unlock(hdev);
1248 static inline void hci_remote_features_evt(struct hci_dev *hdev, struct sk_buff *skb)
1250 struct hci_ev_remote_features *ev = (void *) skb->data;
1251 struct hci_conn *conn;
1253 BT_DBG("%s status %d", hdev->name, ev->status);
1257 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
1260 memcpy(conn->features, ev->features, 8);
1262 if (conn->state == BT_CONFIG) {
1263 if (!ev->status && lmp_ssp_capable(hdev) &&
1264 lmp_ssp_capable(conn)) {
1265 struct hci_cp_read_remote_ext_features cp;
1266 cp.handle = ev->handle;
1269 HCI_OP_READ_REMOTE_EXT_FEATURES,
1272 conn->state = BT_CONNECTED;
1273 hci_proto_connect_cfm(conn, ev->status);
1279 hci_dev_unlock(hdev);
1283 static inline void hci_remote_version_evt(struct hci_dev *hdev, struct sk_buff *skb)
1285 BT_DBG("%s", hdev->name);
1288 static inline void hci_qos_setup_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
1290 BT_DBG("%s", hdev->name);
1293 static inline void hci_cmd_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
1295 struct hci_ev_cmd_complete *ev = (void *) skb->data;
1298 skb_pull(skb, sizeof(*ev));
1300 opcode = __le16_to_cpu(ev->opcode);
1303 case HCI_OP_INQUIRY_CANCEL:
1304 hci_cc_inquiry_cancel(hdev, skb);
1307 case HCI_OP_EXIT_PERIODIC_INQ:
1308 hci_cc_exit_periodic_inq(hdev, skb);
1311 case HCI_OP_REMOTE_NAME_REQ_CANCEL:
1312 hci_cc_remote_name_req_cancel(hdev, skb);
1315 case HCI_OP_ROLE_DISCOVERY:
1316 hci_cc_role_discovery(hdev, skb);
1319 case HCI_OP_READ_LINK_POLICY:
1320 hci_cc_read_link_policy(hdev, skb);
1323 case HCI_OP_WRITE_LINK_POLICY:
1324 hci_cc_write_link_policy(hdev, skb);
1327 case HCI_OP_READ_DEF_LINK_POLICY:
1328 hci_cc_read_def_link_policy(hdev, skb);
1331 case HCI_OP_WRITE_DEF_LINK_POLICY:
1332 hci_cc_write_def_link_policy(hdev, skb);
1336 hci_cc_reset(hdev, skb);
1339 case HCI_OP_WRITE_LOCAL_NAME:
1340 hci_cc_write_local_name(hdev, skb);
1343 case HCI_OP_READ_LOCAL_NAME:
1344 hci_cc_read_local_name(hdev, skb);
1347 case HCI_OP_WRITE_AUTH_ENABLE:
1348 hci_cc_write_auth_enable(hdev, skb);
1351 case HCI_OP_WRITE_ENCRYPT_MODE:
1352 hci_cc_write_encrypt_mode(hdev, skb);
1355 case HCI_OP_WRITE_SCAN_ENABLE:
1356 hci_cc_write_scan_enable(hdev, skb);
1359 case HCI_OP_READ_CLASS_OF_DEV:
1360 hci_cc_read_class_of_dev(hdev, skb);
1363 case HCI_OP_WRITE_CLASS_OF_DEV:
1364 hci_cc_write_class_of_dev(hdev, skb);
1367 case HCI_OP_READ_VOICE_SETTING:
1368 hci_cc_read_voice_setting(hdev, skb);
1371 case HCI_OP_WRITE_VOICE_SETTING:
1372 hci_cc_write_voice_setting(hdev, skb);
1375 case HCI_OP_HOST_BUFFER_SIZE:
1376 hci_cc_host_buffer_size(hdev, skb);
1379 case HCI_OP_READ_SSP_MODE:
1380 hci_cc_read_ssp_mode(hdev, skb);
1383 case HCI_OP_WRITE_SSP_MODE:
1384 hci_cc_write_ssp_mode(hdev, skb);
1387 case HCI_OP_READ_LOCAL_VERSION:
1388 hci_cc_read_local_version(hdev, skb);
1391 case HCI_OP_READ_LOCAL_COMMANDS:
1392 hci_cc_read_local_commands(hdev, skb);
1395 case HCI_OP_READ_LOCAL_FEATURES:
1396 hci_cc_read_local_features(hdev, skb);
1399 case HCI_OP_READ_BUFFER_SIZE:
1400 hci_cc_read_buffer_size(hdev, skb);
1403 case HCI_OP_READ_BD_ADDR:
1404 hci_cc_read_bd_addr(hdev, skb);
1408 BT_DBG("%s opcode 0x%x", hdev->name, opcode);
1413 atomic_set(&hdev->cmd_cnt, 1);
1414 if (!skb_queue_empty(&hdev->cmd_q))
1415 tasklet_schedule(&hdev->cmd_task);
1419 static inline void hci_cmd_status_evt(struct hci_dev *hdev, struct sk_buff *skb)
1421 struct hci_ev_cmd_status *ev = (void *) skb->data;
1424 skb_pull(skb, sizeof(*ev));
1426 opcode = __le16_to_cpu(ev->opcode);
1429 case HCI_OP_INQUIRY:
1430 hci_cs_inquiry(hdev, ev->status);
1433 case HCI_OP_CREATE_CONN:
1434 hci_cs_create_conn(hdev, ev->status);
1437 case HCI_OP_ADD_SCO:
1438 hci_cs_add_sco(hdev, ev->status);
1441 case HCI_OP_AUTH_REQUESTED:
1442 hci_cs_auth_requested(hdev, ev->status);
1445 case HCI_OP_SET_CONN_ENCRYPT:
1446 hci_cs_set_conn_encrypt(hdev, ev->status);
1449 case HCI_OP_REMOTE_NAME_REQ:
1450 hci_cs_remote_name_req(hdev, ev->status);
1453 case HCI_OP_READ_REMOTE_FEATURES:
1454 hci_cs_read_remote_features(hdev, ev->status);
1457 case HCI_OP_READ_REMOTE_EXT_FEATURES:
1458 hci_cs_read_remote_ext_features(hdev, ev->status);
1461 case HCI_OP_SETUP_SYNC_CONN:
1462 hci_cs_setup_sync_conn(hdev, ev->status);
1465 case HCI_OP_SNIFF_MODE:
1466 hci_cs_sniff_mode(hdev, ev->status);
1469 case HCI_OP_EXIT_SNIFF_MODE:
1470 hci_cs_exit_sniff_mode(hdev, ev->status);
1474 BT_DBG("%s opcode 0x%x", hdev->name, opcode);
1479 atomic_set(&hdev->cmd_cnt, 1);
1480 if (!skb_queue_empty(&hdev->cmd_q))
1481 tasklet_schedule(&hdev->cmd_task);
1485 static inline void hci_role_change_evt(struct hci_dev *hdev, struct sk_buff *skb)
1487 struct hci_ev_role_change *ev = (void *) skb->data;
1488 struct hci_conn *conn;
1490 BT_DBG("%s status %d", hdev->name, ev->status);
1494 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
1498 conn->link_mode &= ~HCI_LM_MASTER;
1500 conn->link_mode |= HCI_LM_MASTER;
1503 clear_bit(HCI_CONN_RSWITCH_PEND, &conn->pend);
1505 hci_role_switch_cfm(conn, ev->status, ev->role);
1508 hci_dev_unlock(hdev);
1511 static inline void hci_num_comp_pkts_evt(struct hci_dev *hdev, struct sk_buff *skb)
1513 struct hci_ev_num_comp_pkts *ev = (void *) skb->data;
1517 skb_pull(skb, sizeof(*ev));
1519 BT_DBG("%s num_hndl %d", hdev->name, ev->num_hndl);
1521 if (skb->len < ev->num_hndl * 4) {
1522 BT_DBG("%s bad parameters", hdev->name);
1526 tasklet_disable(&hdev->tx_task);
1528 for (i = 0, ptr = (__le16 *) skb->data; i < ev->num_hndl; i++) {
1529 struct hci_conn *conn;
1530 __u16 handle, count;
1532 handle = get_unaligned_le16(ptr++);
1533 count = get_unaligned_le16(ptr++);
1535 conn = hci_conn_hash_lookup_handle(hdev, handle);
1537 conn->sent -= count;
1539 if (conn->type == ACL_LINK) {
1540 if ((hdev->acl_cnt += count) > hdev->acl_pkts)
1541 hdev->acl_cnt = hdev->acl_pkts;
1543 if ((hdev->sco_cnt += count) > hdev->sco_pkts)
1544 hdev->sco_cnt = hdev->sco_pkts;
1549 tasklet_schedule(&hdev->tx_task);
1551 tasklet_enable(&hdev->tx_task);
1554 static inline void hci_mode_change_evt(struct hci_dev *hdev, struct sk_buff *skb)
1556 struct hci_ev_mode_change *ev = (void *) skb->data;
1557 struct hci_conn *conn;
1559 BT_DBG("%s status %d", hdev->name, ev->status);
1563 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
1565 conn->mode = ev->mode;
1566 conn->interval = __le16_to_cpu(ev->interval);
1568 if (!test_and_clear_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->pend)) {
1569 if (conn->mode == HCI_CM_ACTIVE)
1570 conn->power_save = 1;
1572 conn->power_save = 0;
1575 if (test_and_clear_bit(HCI_CONN_SCO_SETUP_PEND, &conn->pend))
1576 hci_sco_setup(conn, ev->status);
1579 hci_dev_unlock(hdev);
1582 static inline void hci_pin_code_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
1584 struct hci_ev_pin_code_req *ev = (void *) skb->data;
1585 struct hci_conn *conn;
1587 BT_DBG("%s", hdev->name);
1591 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
1592 if (conn && conn->state == BT_CONNECTED) {
1593 hci_conn_hold(conn);
1594 conn->disc_timeout = HCI_PAIRING_TIMEOUT;
1598 hci_dev_unlock(hdev);
1601 static inline void hci_link_key_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
1603 BT_DBG("%s", hdev->name);
1606 static inline void hci_link_key_notify_evt(struct hci_dev *hdev, struct sk_buff *skb)
1608 struct hci_ev_link_key_notify *ev = (void *) skb->data;
1609 struct hci_conn *conn;
1611 BT_DBG("%s", hdev->name);
1615 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
1617 hci_conn_hold(conn);
1618 conn->disc_timeout = HCI_DISCONN_TIMEOUT;
1622 hci_dev_unlock(hdev);
1625 static inline void hci_clock_offset_evt(struct hci_dev *hdev, struct sk_buff *skb)
1627 struct hci_ev_clock_offset *ev = (void *) skb->data;
1628 struct hci_conn *conn;
1630 BT_DBG("%s status %d", hdev->name, ev->status);
1634 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
1635 if (conn && !ev->status) {
1636 struct inquiry_entry *ie;
1638 if ((ie = hci_inquiry_cache_lookup(hdev, &conn->dst))) {
1639 ie->data.clock_offset = ev->clock_offset;
1640 ie->timestamp = jiffies;
1644 hci_dev_unlock(hdev);
1647 static inline void hci_pkt_type_change_evt(struct hci_dev *hdev, struct sk_buff *skb)
1649 struct hci_ev_pkt_type_change *ev = (void *) skb->data;
1650 struct hci_conn *conn;
1652 BT_DBG("%s status %d", hdev->name, ev->status);
1656 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
1657 if (conn && !ev->status)
1658 conn->pkt_type = __le16_to_cpu(ev->pkt_type);
1660 hci_dev_unlock(hdev);
1663 static inline void hci_pscan_rep_mode_evt(struct hci_dev *hdev, struct sk_buff *skb)
1665 struct hci_ev_pscan_rep_mode *ev = (void *) skb->data;
1666 struct inquiry_entry *ie;
1668 BT_DBG("%s", hdev->name);
1672 if ((ie = hci_inquiry_cache_lookup(hdev, &ev->bdaddr))) {
1673 ie->data.pscan_rep_mode = ev->pscan_rep_mode;
1674 ie->timestamp = jiffies;
1677 hci_dev_unlock(hdev);
1680 static inline void hci_inquiry_result_with_rssi_evt(struct hci_dev *hdev, struct sk_buff *skb)
1682 struct inquiry_data data;
1683 int num_rsp = *((__u8 *) skb->data);
1685 BT_DBG("%s num_rsp %d", hdev->name, num_rsp);
1692 if ((skb->len - 1) / num_rsp != sizeof(struct inquiry_info_with_rssi)) {
1693 struct inquiry_info_with_rssi_and_pscan_mode *info = (void *) (skb->data + 1);
1695 for (; num_rsp; num_rsp--) {
1696 bacpy(&data.bdaddr, &info->bdaddr);
1697 data.pscan_rep_mode = info->pscan_rep_mode;
1698 data.pscan_period_mode = info->pscan_period_mode;
1699 data.pscan_mode = info->pscan_mode;
1700 memcpy(data.dev_class, info->dev_class, 3);
1701 data.clock_offset = info->clock_offset;
1702 data.rssi = info->rssi;
1703 data.ssp_mode = 0x00;
1705 hci_inquiry_cache_update(hdev, &data);
1708 struct inquiry_info_with_rssi *info = (void *) (skb->data + 1);
1710 for (; num_rsp; num_rsp--) {
1711 bacpy(&data.bdaddr, &info->bdaddr);
1712 data.pscan_rep_mode = info->pscan_rep_mode;
1713 data.pscan_period_mode = info->pscan_period_mode;
1714 data.pscan_mode = 0x00;
1715 memcpy(data.dev_class, info->dev_class, 3);
1716 data.clock_offset = info->clock_offset;
1717 data.rssi = info->rssi;
1718 data.ssp_mode = 0x00;
1720 hci_inquiry_cache_update(hdev, &data);
1724 hci_dev_unlock(hdev);
1727 #ifdef HCI_AUTO_NAME_RESOLVE
1728 static inline void hci_remote_ext_features_evt(struct hci_dev *hdev, struct sk_buff *skb)
1730 struct hci_ev_remote_ext_features *ev = (void *) skb->data;
1731 struct hci_conn *conn;
1733 BT_DBG("%s", hdev->name);
1737 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
1739 if (!ev->status && ev->page == 0x01) {
1740 struct inquiry_entry *ie;
1742 if ((ie = hci_inquiry_cache_lookup(hdev, &conn->dst)))
1743 ie->data.ssp_mode = (ev->features[0] & 0x01);
1745 conn->ssp_mode = (ev->features[0] & 0x01);
1748 if (conn->state != BT_CONFIG)
1752 struct hci_cp_remote_name_req cp;
1753 memset(&cp, 0, sizeof(cp));
1754 bacpy(&cp.bdaddr, &conn->dst);
1755 cp.pscan_rep_mode = 0x02;
1756 hci_send_cmd(hdev, HCI_OP_REMOTE_NAME_REQ, sizeof(cp), &cp);
1759 if (!hci_outgoing_auth_needed(hdev, conn)) {
1760 conn->state = BT_CONNECTED;
1761 hci_proto_connect_cfm(conn, ev->status);
1767 hci_dev_unlock(hdev);
1770 static inline void hci_remote_ext_features_evt(struct hci_dev *hdev, struct sk_buff *skb)
1772 struct hci_ev_remote_ext_features *ev = (void *) skb->data;
1773 struct hci_conn *conn;
1775 BT_DBG("%s", hdev->name);
1779 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
1781 if (!ev->status && ev->page == 0x01) {
1782 struct inquiry_entry *ie;
1784 if ((ie = hci_inquiry_cache_lookup(hdev, &conn->dst)))
1785 ie->data.ssp_mode = (ev->features[0] & 0x01);
1787 conn->ssp_mode = (ev->features[0] & 0x01);
1790 if (conn->state == BT_CONFIG) {
1791 if (!ev->status && hdev->ssp_mode > 0 &&
1792 conn->ssp_mode > 0 && conn->out &&
1793 conn->sec_level != BT_SECURITY_SDP) {
1794 struct hci_cp_auth_requested cp;
1795 cp.handle = ev->handle;
1796 hci_send_cmd(hdev, HCI_OP_AUTH_REQUESTED,
1799 conn->state = BT_CONNECTED;
1800 hci_proto_connect_cfm(conn, ev->status);
1806 hci_dev_unlock(hdev);
1810 static inline void hci_sync_conn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
1812 struct hci_ev_sync_conn_complete *ev = (void *) skb->data;
1813 struct hci_conn *conn;
1815 BT_DBG("%s status %d", hdev->name, ev->status);
1819 conn = hci_conn_hash_lookup_ba(hdev, ev->link_type, &ev->bdaddr);
1821 if (ev->link_type == ESCO_LINK)
1824 conn = hci_conn_hash_lookup_ba(hdev, ESCO_LINK, &ev->bdaddr);
1828 conn->type = SCO_LINK;
1831 switch (ev->status) {
1833 conn->handle = __le16_to_cpu(ev->handle);
1834 conn->state = BT_CONNECTED;
1836 hci_conn_hold_device(conn);
1837 hci_conn_add_sysfs(conn);
1840 case 0x11: /* Unsupported Feature or Parameter Value */
1841 case 0x1c: /* SCO interval rejected */
1842 case 0x1a: /* Unsupported Remote Feature */
1843 case 0x1f: /* Unspecified error */
1844 if (conn->out && conn->attempt < 2) {
1845 conn->pkt_type = (hdev->esco_type & SCO_ESCO_MASK) |
1846 (hdev->esco_type & EDR_ESCO_MASK);
1847 hci_setup_sync(conn, conn->link->handle);
1853 conn->state = BT_CLOSED;
1857 hci_proto_connect_cfm(conn, ev->status);
1862 hci_dev_unlock(hdev);
1865 static inline void hci_sync_conn_changed_evt(struct hci_dev *hdev, struct sk_buff *skb)
1867 BT_DBG("%s", hdev->name);
1870 static inline void hci_sniff_subrate_evt(struct hci_dev *hdev, struct sk_buff *skb)
1872 struct hci_ev_sniff_subrate *ev = (void *) skb->data;
1873 struct hci_conn *conn;
1875 BT_DBG("%s status %d", hdev->name, ev->status);
1879 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
1883 hci_dev_unlock(hdev);
1886 static inline void hci_extended_inquiry_result_evt(struct hci_dev *hdev, struct sk_buff *skb)
1888 struct inquiry_data data;
1889 struct extended_inquiry_info *info = (void *) (skb->data + 1);
1890 int num_rsp = *((__u8 *) skb->data);
1892 BT_DBG("%s num_rsp %d", hdev->name, num_rsp);
1899 for (; num_rsp; num_rsp--) {
1900 bacpy(&data.bdaddr, &info->bdaddr);
1901 data.pscan_rep_mode = info->pscan_rep_mode;
1902 data.pscan_period_mode = info->pscan_period_mode;
1903 data.pscan_mode = 0x00;
1904 memcpy(data.dev_class, info->dev_class, 3);
1905 data.clock_offset = info->clock_offset;
1906 data.rssi = info->rssi;
1907 data.ssp_mode = 0x01;
1909 hci_inquiry_cache_update(hdev, &data);
1912 hci_dev_unlock(hdev);
1915 static inline void hci_io_capa_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
1917 struct hci_ev_io_capa_request *ev = (void *) skb->data;
1918 struct hci_conn *conn;
1920 BT_DBG("%s", hdev->name);
1924 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
1926 hci_conn_hold(conn);
1928 hci_dev_unlock(hdev);
1931 static inline void hci_simple_pair_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
1933 struct hci_ev_simple_pair_complete *ev = (void *) skb->data;
1934 struct hci_conn *conn;
1936 BT_DBG("%s", hdev->name);
1940 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
1944 hci_dev_unlock(hdev);
1947 static inline void hci_remote_host_features_evt(struct hci_dev *hdev, struct sk_buff *skb)
1949 struct hci_ev_remote_host_features *ev = (void *) skb->data;
1950 struct inquiry_entry *ie;
1952 BT_DBG("%s", hdev->name);
1956 if ((ie = hci_inquiry_cache_lookup(hdev, &ev->bdaddr)))
1957 ie->data.ssp_mode = (ev->features[0] & 0x01);
1959 hci_dev_unlock(hdev);
1962 void hci_event_packet(struct hci_dev *hdev, struct sk_buff *skb)
1964 struct hci_event_hdr *hdr = (void *) skb->data;
1965 __u8 event = hdr->evt;
1967 skb_pull(skb, HCI_EVENT_HDR_SIZE);
1970 case HCI_EV_INQUIRY_COMPLETE:
1971 hci_inquiry_complete_evt(hdev, skb);
1974 case HCI_EV_INQUIRY_RESULT:
1975 hci_inquiry_result_evt(hdev, skb);
1978 case HCI_EV_CONN_COMPLETE:
1979 hci_conn_complete_evt(hdev, skb);
1982 case HCI_EV_CONN_REQUEST:
1983 hci_conn_request_evt(hdev, skb);
1986 case HCI_EV_DISCONN_COMPLETE:
1987 hci_disconn_complete_evt(hdev, skb);
1990 case HCI_EV_AUTH_COMPLETE:
1991 hci_auth_complete_evt(hdev, skb);
1994 case HCI_EV_REMOTE_NAME:
1995 hci_remote_name_evt(hdev, skb);
1998 case HCI_EV_ENCRYPT_CHANGE:
1999 hci_encrypt_change_evt(hdev, skb);
2002 case HCI_EV_CHANGE_LINK_KEY_COMPLETE:
2003 hci_change_link_key_complete_evt(hdev, skb);
2006 case HCI_EV_REMOTE_FEATURES:
2007 hci_remote_features_evt(hdev, skb);
2010 case HCI_EV_REMOTE_VERSION:
2011 hci_remote_version_evt(hdev, skb);
2014 case HCI_EV_QOS_SETUP_COMPLETE:
2015 hci_qos_setup_complete_evt(hdev, skb);
2018 case HCI_EV_CMD_COMPLETE:
2019 hci_cmd_complete_evt(hdev, skb);
2022 case HCI_EV_CMD_STATUS:
2023 hci_cmd_status_evt(hdev, skb);
2026 case HCI_EV_ROLE_CHANGE:
2027 hci_role_change_evt(hdev, skb);
2030 case HCI_EV_NUM_COMP_PKTS:
2031 hci_num_comp_pkts_evt(hdev, skb);
2034 case HCI_EV_MODE_CHANGE:
2035 hci_mode_change_evt(hdev, skb);
2038 case HCI_EV_PIN_CODE_REQ:
2039 hci_pin_code_request_evt(hdev, skb);
2042 case HCI_EV_LINK_KEY_REQ:
2043 hci_link_key_request_evt(hdev, skb);
2046 case HCI_EV_LINK_KEY_NOTIFY:
2047 hci_link_key_notify_evt(hdev, skb);
2050 case HCI_EV_CLOCK_OFFSET:
2051 hci_clock_offset_evt(hdev, skb);
2054 case HCI_EV_PKT_TYPE_CHANGE:
2055 hci_pkt_type_change_evt(hdev, skb);
2058 case HCI_EV_PSCAN_REP_MODE:
2059 hci_pscan_rep_mode_evt(hdev, skb);
2062 case HCI_EV_INQUIRY_RESULT_WITH_RSSI:
2063 hci_inquiry_result_with_rssi_evt(hdev, skb);
2066 case HCI_EV_REMOTE_EXT_FEATURES:
2067 hci_remote_ext_features_evt(hdev, skb);
2070 case HCI_EV_SYNC_CONN_COMPLETE:
2071 hci_sync_conn_complete_evt(hdev, skb);
2074 case HCI_EV_SYNC_CONN_CHANGED:
2075 hci_sync_conn_changed_evt(hdev, skb);
2078 case HCI_EV_SNIFF_SUBRATE:
2079 hci_sniff_subrate_evt(hdev, skb);
2082 case HCI_EV_EXTENDED_INQUIRY_RESULT:
2083 hci_extended_inquiry_result_evt(hdev, skb);
2086 case HCI_EV_IO_CAPA_REQUEST:
2087 hci_io_capa_request_evt(hdev, skb);
2090 case HCI_EV_SIMPLE_PAIR_COMPLETE:
2091 hci_simple_pair_complete_evt(hdev, skb);
2094 case HCI_EV_REMOTE_HOST_FEATURES:
2095 hci_remote_host_features_evt(hdev, skb);
2099 BT_DBG("%s event 0x%x", hdev->name, event);
2104 hdev->stat.evt_rx++;
2107 /* Generate internal stack event */
2108 void hci_si_event(struct hci_dev *hdev, int type, int dlen, void *data)
2110 struct hci_event_hdr *hdr;
2111 struct hci_ev_stack_internal *ev;
2112 struct sk_buff *skb;
2114 skb = bt_skb_alloc(HCI_EVENT_HDR_SIZE + sizeof(*ev) + dlen, GFP_ATOMIC);
2118 hdr = (void *) skb_put(skb, HCI_EVENT_HDR_SIZE);
2119 hdr->evt = HCI_EV_STACK_INTERNAL;
2120 hdr->plen = sizeof(*ev) + dlen;
2122 ev = (void *) skb_put(skb, sizeof(*ev) + dlen);
2124 memcpy(ev->data, data, dlen);
2126 bt_cb(skb)->incoming = 1;
2127 __net_timestamp(skb);
2129 bt_cb(skb)->pkt_type = HCI_EVENT_PKT;
2130 skb->dev = (void *) hdev;
2131 hci_send_to_sock(hdev, skb);