Merge tag 'v3.10.55' into update
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / net / bluetooth / hci_event.c
old mode 100644 (file)
new mode 100755 (executable)
index 81b4448..1526fb2
@@ -48,13 +48,13 @@ static void hci_cc_inquiry_cancel(struct hci_dev *hdev, struct sk_buff *skb)
        }
 
        clear_bit(HCI_INQUIRY, &hdev->flags);
+       smp_mb__after_clear_bit(); /* wake_up_bit advises about this barrier */
+       wake_up_bit(&hdev->flags, HCI_INQUIRY);
 
        hci_dev_lock(hdev);
        hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
        hci_dev_unlock(hdev);
 
-       hci_req_complete(hdev, HCI_OP_INQUIRY_CANCEL, status);
-
        hci_conn_check_pending(hdev);
 }
 
@@ -183,8 +183,6 @@ static void hci_cc_write_def_link_policy(struct hci_dev *hdev,
 
        if (!status)
                hdev->link_policy = get_unaligned_le16(sent);
-
-       hci_req_complete(hdev, HCI_OP_WRITE_DEF_LINK_POLICY, status);
 }
 
 static void hci_cc_reset(struct hci_dev *hdev, struct sk_buff *skb)
@@ -195,11 +193,8 @@ static void hci_cc_reset(struct hci_dev *hdev, struct sk_buff *skb)
 
        clear_bit(HCI_RESET, &hdev->flags);
 
-       hci_req_complete(hdev, HCI_OP_RESET, status);
-
        /* Reset all non-persistent flags */
-       hdev->dev_flags &= ~(BIT(HCI_LE_SCAN) | BIT(HCI_PENDING_CLASS) |
-                            BIT(HCI_PERIODIC_INQ));
+       hdev->dev_flags &= ~HCI_PERSISTENT_MASK;
 
        hdev->discovery.state = DISCOVERY_STOPPED;
        hdev->inq_tx_power = HCI_TX_POWER_INVALID;
@@ -228,11 +223,6 @@ static void hci_cc_write_local_name(struct hci_dev *hdev, struct sk_buff *skb)
                memcpy(hdev->dev_name, sent, HCI_MAX_NAME_LENGTH);
 
        hci_dev_unlock(hdev);
-
-       if (!status && !test_bit(HCI_INIT, &hdev->flags))
-               hci_update_ad(hdev);
-
-       hci_req_complete(hdev, HCI_OP_WRITE_LOCAL_NAME, status);
 }
 
 static void hci_cc_read_local_name(struct hci_dev *hdev, struct sk_buff *skb)
@@ -270,8 +260,6 @@ static void hci_cc_write_auth_enable(struct hci_dev *hdev, struct sk_buff *skb)
 
        if (test_bit(HCI_MGMT, &hdev->dev_flags))
                mgmt_auth_enable_complete(hdev, status);
-
-       hci_req_complete(hdev, HCI_OP_WRITE_AUTH_ENABLE, status);
 }
 
 static void hci_cc_write_encrypt_mode(struct hci_dev *hdev, struct sk_buff *skb)
@@ -293,8 +281,6 @@ static void hci_cc_write_encrypt_mode(struct hci_dev *hdev, struct sk_buff *skb)
                else
                        clear_bit(HCI_ENCRYPT, &hdev->flags);
        }
-
-       hci_req_complete(hdev, HCI_OP_WRITE_ENCRYPT_MODE, status);
 }
 
 static void hci_cc_write_scan_enable(struct hci_dev *hdev, struct sk_buff *skb)
@@ -343,7 +329,6 @@ static void hci_cc_write_scan_enable(struct hci_dev *hdev, struct sk_buff *skb)
 
 done:
        hci_dev_unlock(hdev);
-       hci_req_complete(hdev, HCI_OP_WRITE_SCAN_ENABLE, status);
 }
 
 static void hci_cc_read_class_of_dev(struct hci_dev *hdev, struct sk_buff *skb)
@@ -435,15 +420,6 @@ static void hci_cc_write_voice_setting(struct hci_dev *hdev,
                hdev->notify(hdev, HCI_NOTIFY_VOICE_SETTING);
 }
 
-static void hci_cc_host_buffer_size(struct hci_dev *hdev, struct sk_buff *skb)
-{
-       __u8 status = *((__u8 *) skb->data);
-
-       BT_DBG("%s status 0x%2.2x", hdev->name, status);
-
-       hci_req_complete(hdev, HCI_OP_HOST_BUFFER_SIZE, status);
-}
-
 static void hci_cc_write_ssp_mode(struct hci_dev *hdev, struct sk_buff *skb)
 {
        __u8 status = *((__u8 *) skb->data);
@@ -457,9 +433,9 @@ static void hci_cc_write_ssp_mode(struct hci_dev *hdev, struct sk_buff *skb)
 
        if (!status) {
                if (sent->mode)
-                       hdev->host_features[0] |= LMP_HOST_SSP;
+                       hdev->features[1][0] |= LMP_HOST_SSP;
                else
-                       hdev->host_features[0] &= ~LMP_HOST_SSP;
+                       hdev->features[1][0] &= ~LMP_HOST_SSP;
        }
 
        if (test_bit(HCI_MGMT, &hdev->dev_flags))
@@ -472,202 +448,6 @@ static void hci_cc_write_ssp_mode(struct hci_dev *hdev, struct sk_buff *skb)
        }
 }
 
-static u8 hci_get_inquiry_mode(struct hci_dev *hdev)
-{
-       if (lmp_ext_inq_capable(hdev))
-               return 2;
-
-       if (lmp_inq_rssi_capable(hdev))
-               return 1;
-
-       if (hdev->manufacturer == 11 && hdev->hci_rev == 0x00 &&
-           hdev->lmp_subver == 0x0757)
-               return 1;
-
-       if (hdev->manufacturer == 15) {
-               if (hdev->hci_rev == 0x03 && hdev->lmp_subver == 0x6963)
-                       return 1;
-               if (hdev->hci_rev == 0x09 && hdev->lmp_subver == 0x6963)
-                       return 1;
-               if (hdev->hci_rev == 0x00 && hdev->lmp_subver == 0x6965)
-                       return 1;
-       }
-
-       if (hdev->manufacturer == 31 && hdev->hci_rev == 0x2005 &&
-           hdev->lmp_subver == 0x1805)
-               return 1;
-
-       return 0;
-}
-
-static void hci_setup_inquiry_mode(struct hci_dev *hdev)
-{
-       u8 mode;
-
-       mode = hci_get_inquiry_mode(hdev);
-
-       hci_send_cmd(hdev, HCI_OP_WRITE_INQUIRY_MODE, 1, &mode);
-}
-
-static void hci_setup_event_mask(struct hci_dev *hdev)
-{
-       /* The second byte is 0xff instead of 0x9f (two reserved bits
-        * disabled) since a Broadcom 1.2 dongle doesn't respond to the
-        * command otherwise */
-       u8 events[8] = { 0xff, 0xff, 0xfb, 0xff, 0x00, 0x00, 0x00, 0x00 };
-
-       /* CSR 1.1 dongles does not accept any bitfield so don't try to set
-        * any event mask for pre 1.2 devices */
-       if (hdev->hci_ver < BLUETOOTH_VER_1_2)
-               return;
-
-       if (lmp_bredr_capable(hdev)) {
-               events[4] |= 0x01; /* Flow Specification Complete */
-               events[4] |= 0x02; /* Inquiry Result with RSSI */
-               events[4] |= 0x04; /* Read Remote Extended Features Complete */
-               events[5] |= 0x08; /* Synchronous Connection Complete */
-               events[5] |= 0x10; /* Synchronous Connection Changed */
-       }
-
-       if (lmp_inq_rssi_capable(hdev))
-               events[4] |= 0x02; /* Inquiry Result with RSSI */
-
-       if (lmp_sniffsubr_capable(hdev))
-               events[5] |= 0x20; /* Sniff Subrating */
-
-       if (lmp_pause_enc_capable(hdev))
-               events[5] |= 0x80; /* Encryption Key Refresh Complete */
-
-       if (lmp_ext_inq_capable(hdev))
-               events[5] |= 0x40; /* Extended Inquiry Result */
-
-       if (lmp_no_flush_capable(hdev))
-               events[7] |= 0x01; /* Enhanced Flush Complete */
-
-       if (lmp_lsto_capable(hdev))
-               events[6] |= 0x80; /* Link Supervision Timeout Changed */
-
-       if (lmp_ssp_capable(hdev)) {
-               events[6] |= 0x01;      /* IO Capability Request */
-               events[6] |= 0x02;      /* IO Capability Response */
-               events[6] |= 0x04;      /* User Confirmation Request */
-               events[6] |= 0x08;      /* User Passkey Request */
-               events[6] |= 0x10;      /* Remote OOB Data Request */
-               events[6] |= 0x20;      /* Simple Pairing Complete */
-               events[7] |= 0x04;      /* User Passkey Notification */
-               events[7] |= 0x08;      /* Keypress Notification */
-               events[7] |= 0x10;      /* Remote Host Supported
-                                        * Features Notification */
-       }
-
-       if (lmp_le_capable(hdev))
-               events[7] |= 0x20;      /* LE Meta-Event */
-
-       hci_send_cmd(hdev, HCI_OP_SET_EVENT_MASK, sizeof(events), events);
-
-       if (lmp_le_capable(hdev)) {
-               memset(events, 0, sizeof(events));
-               events[0] = 0x1f;
-               hci_send_cmd(hdev, HCI_OP_LE_SET_EVENT_MASK,
-                            sizeof(events), events);
-       }
-}
-
-static void bredr_setup(struct hci_dev *hdev)
-{
-       struct hci_cp_delete_stored_link_key cp;
-       __le16 param;
-       __u8 flt_type;
-
-       /* Read Buffer Size (ACL mtu, max pkt, etc.) */
-       hci_send_cmd(hdev, HCI_OP_READ_BUFFER_SIZE, 0, NULL);
-
-       /* Read Class of Device */
-       hci_send_cmd(hdev, HCI_OP_READ_CLASS_OF_DEV, 0, NULL);
-
-       /* Read Local Name */
-       hci_send_cmd(hdev, HCI_OP_READ_LOCAL_NAME, 0, NULL);
-
-       /* Read Voice Setting */
-       hci_send_cmd(hdev, HCI_OP_READ_VOICE_SETTING, 0, NULL);
-
-       /* Clear Event Filters */
-       flt_type = HCI_FLT_CLEAR_ALL;
-       hci_send_cmd(hdev, HCI_OP_SET_EVENT_FLT, 1, &flt_type);
-
-       /* Connection accept timeout ~20 secs */
-       param = __constant_cpu_to_le16(0x7d00);
-       hci_send_cmd(hdev, HCI_OP_WRITE_CA_TIMEOUT, 2, &param);
-
-       bacpy(&cp.bdaddr, BDADDR_ANY);
-       cp.delete_all = 1;
-       hci_send_cmd(hdev, HCI_OP_DELETE_STORED_LINK_KEY, sizeof(cp), &cp);
-}
-
-static void le_setup(struct hci_dev *hdev)
-{
-       /* Read LE Buffer Size */
-       hci_send_cmd(hdev, HCI_OP_LE_READ_BUFFER_SIZE, 0, NULL);
-
-       /* Read LE Advertising Channel TX Power */
-       hci_send_cmd(hdev, HCI_OP_LE_READ_ADV_TX_POWER, 0, NULL);
-}
-
-static void hci_setup(struct hci_dev *hdev)
-{
-       if (hdev->dev_type != HCI_BREDR)
-               return;
-
-       /* Read BD Address */
-       hci_send_cmd(hdev, HCI_OP_READ_BD_ADDR, 0, NULL);
-
-       if (lmp_bredr_capable(hdev))
-               bredr_setup(hdev);
-
-       if (lmp_le_capable(hdev))
-               le_setup(hdev);
-
-       hci_setup_event_mask(hdev);
-
-       if (hdev->hci_ver > BLUETOOTH_VER_1_1)
-               hci_send_cmd(hdev, HCI_OP_READ_LOCAL_COMMANDS, 0, NULL);
-
-       if (lmp_ssp_capable(hdev)) {
-               if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) {
-                       u8 mode = 0x01;
-                       hci_send_cmd(hdev, HCI_OP_WRITE_SSP_MODE,
-                                    sizeof(mode), &mode);
-               } else {
-                       struct hci_cp_write_eir cp;
-
-                       memset(hdev->eir, 0, sizeof(hdev->eir));
-                       memset(&cp, 0, sizeof(cp));
-
-                       hci_send_cmd(hdev, HCI_OP_WRITE_EIR, sizeof(cp), &cp);
-               }
-       }
-
-       if (lmp_inq_rssi_capable(hdev))
-               hci_setup_inquiry_mode(hdev);
-
-       if (lmp_inq_tx_pwr_capable(hdev))
-               hci_send_cmd(hdev, HCI_OP_READ_INQ_RSP_TX_POWER, 0, NULL);
-
-       if (lmp_ext_feat_capable(hdev)) {
-               struct hci_cp_read_local_ext_features cp;
-
-               cp.page = 0x01;
-               hci_send_cmd(hdev, HCI_OP_READ_LOCAL_EXT_FEATURES, sizeof(cp),
-                            &cp);
-       }
-
-       if (test_bit(HCI_LINK_SECURITY, &hdev->dev_flags)) {
-               u8 enable = 1;
-               hci_send_cmd(hdev, HCI_OP_WRITE_AUTH_ENABLE, sizeof(enable),
-                            &enable);
-       }
-}
-
 static void hci_cc_read_local_version(struct hci_dev *hdev, struct sk_buff *skb)
 {
        struct hci_rp_read_local_version *rp = (void *) skb->data;
@@ -675,7 +455,7 @@ static void hci_cc_read_local_version(struct hci_dev *hdev, struct sk_buff *skb)
        BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
 
        if (rp->status)
-               goto done;
+               return;
 
        hdev->hci_ver = rp->hci_ver;
        hdev->hci_rev = __le16_to_cpu(rp->hci_rev);
@@ -685,30 +465,6 @@ static void hci_cc_read_local_version(struct hci_dev *hdev, struct sk_buff *skb)
 
        BT_DBG("%s manufacturer 0x%4.4x hci ver %d:%d", hdev->name,
               hdev->manufacturer, hdev->hci_ver, hdev->hci_rev);
-
-       if (test_bit(HCI_INIT, &hdev->flags))
-               hci_setup(hdev);
-
-done:
-       hci_req_complete(hdev, HCI_OP_READ_LOCAL_VERSION, rp->status);
-}
-
-static void hci_setup_link_policy(struct hci_dev *hdev)
-{
-       struct hci_cp_write_def_link_policy cp;
-       u16 link_policy = 0;
-
-       if (lmp_rswitch_capable(hdev))
-               link_policy |= HCI_LP_RSWITCH;
-       if (lmp_hold_capable(hdev))
-               link_policy |= HCI_LP_HOLD;
-       if (lmp_sniff_capable(hdev))
-               link_policy |= HCI_LP_SNIFF;
-       if (lmp_park_capable(hdev))
-               link_policy |= HCI_LP_PARK;
-
-       cp.policy = cpu_to_le16(link_policy);
-       hci_send_cmd(hdev, HCI_OP_WRITE_DEF_LINK_POLICY, sizeof(cp), &cp);
 }
 
 static void hci_cc_read_local_commands(struct hci_dev *hdev,
@@ -718,16 +474,8 @@ static void hci_cc_read_local_commands(struct hci_dev *hdev,
 
        BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
 
-       if (rp->status)
-               goto done;
-
-       memcpy(hdev->commands, rp->commands, sizeof(hdev->commands));
-
-       if (test_bit(HCI_INIT, &hdev->flags) && (hdev->commands[5] & 0x10))
-               hci_setup_link_policy(hdev);
-
-done:
-       hci_req_complete(hdev, HCI_OP_READ_LOCAL_COMMANDS, rp->status);
+       if (!rp->status)
+               memcpy(hdev->commands, rp->commands, sizeof(hdev->commands));
 }
 
 static void hci_cc_read_local_features(struct hci_dev *hdev,
@@ -745,18 +493,18 @@ static void hci_cc_read_local_features(struct hci_dev *hdev,
        /* Adjust default settings according to features
         * supported by device. */
 
-       if (hdev->features[0] & LMP_3SLOT)
+       if (hdev->features[0][0] & LMP_3SLOT)
                hdev->pkt_type |= (HCI_DM3 | HCI_DH3);
 
-       if (hdev->features[0] & LMP_5SLOT)
+       if (hdev->features[0][0] & LMP_5SLOT)
                hdev->pkt_type |= (HCI_DM5 | HCI_DH5);
 
-       if (hdev->features[1] & LMP_HV2) {
+       if (hdev->features[0][1] & LMP_HV2) {
                hdev->pkt_type  |= (HCI_HV2);
                hdev->esco_type |= (ESCO_HV2);
        }
 
-       if (hdev->features[1] & LMP_HV3) {
+       if (hdev->features[0][1] & LMP_HV3) {
                hdev->pkt_type  |= (HCI_HV3);
                hdev->esco_type |= (ESCO_HV3);
        }
@@ -764,42 +512,26 @@ static void hci_cc_read_local_features(struct hci_dev *hdev,
        if (lmp_esco_capable(hdev))
                hdev->esco_type |= (ESCO_EV3);
 
-       if (hdev->features[4] & LMP_EV4)
+       if (hdev->features[0][4] & LMP_EV4)
                hdev->esco_type |= (ESCO_EV4);
 
-       if (hdev->features[4] & LMP_EV5)
+       if (hdev->features[0][4] & LMP_EV5)
                hdev->esco_type |= (ESCO_EV5);
 
-       if (hdev->features[5] & LMP_EDR_ESCO_2M)
+       if (hdev->features[0][5] & LMP_EDR_ESCO_2M)
                hdev->esco_type |= (ESCO_2EV3);
 
-       if (hdev->features[5] & LMP_EDR_ESCO_3M)
+       if (hdev->features[0][5] & LMP_EDR_ESCO_3M)
                hdev->esco_type |= (ESCO_3EV3);
 
-       if (hdev->features[5] & LMP_EDR_3S_ESCO)
+       if (hdev->features[0][5] & LMP_EDR_3S_ESCO)
                hdev->esco_type |= (ESCO_2EV5 | ESCO_3EV5);
 
        BT_DBG("%s features 0x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x", hdev->name,
-              hdev->features[0], hdev->features[1],
-              hdev->features[2], hdev->features[3],
-              hdev->features[4], hdev->features[5],
-              hdev->features[6], hdev->features[7]);
-}
-
-static void hci_set_le_support(struct hci_dev *hdev)
-{
-       struct hci_cp_write_le_host_supported cp;
-
-       memset(&cp, 0, sizeof(cp));
-
-       if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) {
-               cp.le = 1;
-               cp.simul = lmp_le_br_capable(hdev);
-       }
-
-       if (cp.le != lmp_host_le_capable(hdev))
-               hci_send_cmd(hdev, HCI_OP_WRITE_LE_HOST_SUPPORTED, sizeof(cp),
-                            &cp);
+              hdev->features[0][0], hdev->features[0][1],
+              hdev->features[0][2], hdev->features[0][3],
+              hdev->features[0][4], hdev->features[0][5],
+              hdev->features[0][6], hdev->features[0][7]);
 }
 
 static void hci_cc_read_local_ext_features(struct hci_dev *hdev,
@@ -810,22 +542,12 @@ static void hci_cc_read_local_ext_features(struct hci_dev *hdev,
        BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
 
        if (rp->status)
-               goto done;
-
-       switch (rp->page) {
-       case 0:
-               memcpy(hdev->features, rp->features, 8);
-               break;
-       case 1:
-               memcpy(hdev->host_features, rp->features, 8);
-               break;
-       }
+               return;
 
-       if (test_bit(HCI_INIT, &hdev->flags) && lmp_le_capable(hdev))
-               hci_set_le_support(hdev);
+       hdev->max_page = rp->max_page;
 
-done:
-       hci_req_complete(hdev, HCI_OP_READ_LOCAL_EXT_FEATURES, rp->status);
+       if (rp->page < HCI_MAX_PAGES)
+               memcpy(hdev->features[rp->page], rp->features, 8);
 }
 
 static void hci_cc_read_flow_control_mode(struct hci_dev *hdev,
@@ -835,12 +557,8 @@ static void hci_cc_read_flow_control_mode(struct hci_dev *hdev,
 
        BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
 
-       if (rp->status)
-               return;
-
-       hdev->flow_ctl_mode = rp->mode;
-
-       hci_req_complete(hdev, HCI_OP_READ_FLOW_CONTROL_MODE, rp->status);
+       if (!rp->status)
+               hdev->flow_ctl_mode = rp->mode;
 }
 
 static void hci_cc_read_buffer_size(struct hci_dev *hdev, struct sk_buff *skb)
@@ -877,8 +595,65 @@ static void hci_cc_read_bd_addr(struct hci_dev *hdev, struct sk_buff *skb)
 
        if (!rp->status)
                bacpy(&hdev->bdaddr, &rp->bdaddr);
+}
+
+static void hci_cc_read_page_scan_activity(struct hci_dev *hdev,
+                                          struct sk_buff *skb)
+{
+       struct hci_rp_read_page_scan_activity *rp = (void *) skb->data;
 
-       hci_req_complete(hdev, HCI_OP_READ_BD_ADDR, rp->status);
+       BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
+
+       if (test_bit(HCI_INIT, &hdev->flags) && !rp->status) {
+               hdev->page_scan_interval = __le16_to_cpu(rp->interval);
+               hdev->page_scan_window = __le16_to_cpu(rp->window);
+       }
+}
+
+static void hci_cc_write_page_scan_activity(struct hci_dev *hdev,
+                                           struct sk_buff *skb)
+{
+       u8 status = *((u8 *) skb->data);
+       struct hci_cp_write_page_scan_activity *sent;
+
+       BT_DBG("%s status 0x%2.2x", hdev->name, status);
+
+       if (status)
+               return;
+
+       sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_PAGE_SCAN_ACTIVITY);
+       if (!sent)
+               return;
+
+       hdev->page_scan_interval = __le16_to_cpu(sent->interval);
+       hdev->page_scan_window = __le16_to_cpu(sent->window);
+}
+
+static void hci_cc_read_page_scan_type(struct hci_dev *hdev,
+                                          struct sk_buff *skb)
+{
+       struct hci_rp_read_page_scan_type *rp = (void *) skb->data;
+
+       BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
+
+       if (test_bit(HCI_INIT, &hdev->flags) && !rp->status)
+               hdev->page_scan_type = rp->type;
+}
+
+static void hci_cc_write_page_scan_type(struct hci_dev *hdev,
+                                       struct sk_buff *skb)
+{
+       u8 status = *((u8 *) skb->data);
+       u8 *type;
+
+       BT_DBG("%s status 0x%2.2x", hdev->name, status);
+
+       if (status)
+               return;
+
+       type = hci_sent_cmd_data(hdev, HCI_OP_WRITE_PAGE_SCAN_TYPE);
+       if (type)
+               hdev->page_scan_type = *type;
 }
 
 static void hci_cc_read_data_block_size(struct hci_dev *hdev,
@@ -899,17 +674,6 @@ static void hci_cc_read_data_block_size(struct hci_dev *hdev,
 
        BT_DBG("%s blk mtu %d cnt %d len %d", hdev->name, hdev->block_mtu,
               hdev->block_cnt, hdev->block_len);
-
-       hci_req_complete(hdev, HCI_OP_READ_DATA_BLOCK_SIZE, rp->status);
-}
-
-static void hci_cc_write_ca_timeout(struct hci_dev *hdev, struct sk_buff *skb)
-{
-       __u8 status = *((__u8 *) skb->data);
-
-       BT_DBG("%s status 0x%2.2x", hdev->name, status);
-
-       hci_req_complete(hdev, HCI_OP_WRITE_CA_TIMEOUT, status);
 }
 
 static void hci_cc_read_local_amp_info(struct hci_dev *hdev,
@@ -933,8 +697,6 @@ static void hci_cc_read_local_amp_info(struct hci_dev *hdev,
        hdev->amp_be_flush_to = __le32_to_cpu(rp->be_flush_to);
        hdev->amp_max_flush_to = __le32_to_cpu(rp->max_flush_to);
 
-       hci_req_complete(hdev, HCI_OP_READ_LOCAL_AMP_INFO, rp->status);
-
 a2mp_rsp:
        a2mp_send_getinfo_rsp(hdev);
 }
@@ -976,35 +738,6 @@ a2mp_rsp:
        a2mp_send_create_phy_link_req(hdev, rp->status);
 }
 
-static void hci_cc_delete_stored_link_key(struct hci_dev *hdev,
-                                         struct sk_buff *skb)
-{
-       __u8 status = *((__u8 *) skb->data);
-
-       BT_DBG("%s status 0x%2.2x", hdev->name, status);
-
-       hci_req_complete(hdev, HCI_OP_DELETE_STORED_LINK_KEY, status);
-}
-
-static void hci_cc_set_event_mask(struct hci_dev *hdev, struct sk_buff *skb)
-{
-       __u8 status = *((__u8 *) skb->data);
-
-       BT_DBG("%s status 0x%2.2x", hdev->name, status);
-
-       hci_req_complete(hdev, HCI_OP_SET_EVENT_MASK, status);
-}
-
-static void hci_cc_write_inquiry_mode(struct hci_dev *hdev,
-                                     struct sk_buff *skb)
-{
-       __u8 status = *((__u8 *) skb->data);
-
-       BT_DBG("%s status 0x%2.2x", hdev->name, status);
-
-       hci_req_complete(hdev, HCI_OP_WRITE_INQUIRY_MODE, status);
-}
-
 static void hci_cc_read_inq_rsp_tx_power(struct hci_dev *hdev,
                                         struct sk_buff *skb)
 {
@@ -1014,17 +747,6 @@ static void hci_cc_read_inq_rsp_tx_power(struct hci_dev *hdev,
 
        if (!rp->status)
                hdev->inq_tx_power = rp->tx_power;
-
-       hci_req_complete(hdev, HCI_OP_READ_INQ_RSP_TX_POWER, rp->status);
-}
-
-static void hci_cc_set_event_flt(struct hci_dev *hdev, struct sk_buff *skb)
-{
-       __u8 status = *((__u8 *) skb->data);
-
-       BT_DBG("%s status 0x%2.2x", hdev->name, status);
-
-       hci_req_complete(hdev, HCI_OP_SET_EVENT_FLT, status);
 }
 
 static void hci_cc_pin_code_reply(struct hci_dev *hdev, struct sk_buff *skb)
@@ -1086,33 +808,28 @@ static void hci_cc_le_read_buffer_size(struct hci_dev *hdev,
        hdev->le_cnt = hdev->le_pkts;
 
        BT_DBG("%s le mtu %d:%d", hdev->name, hdev->le_mtu, hdev->le_pkts);
-
-       hci_req_complete(hdev, HCI_OP_LE_READ_BUFFER_SIZE, rp->status);
 }
 
-static void hci_cc_le_read_adv_tx_power(struct hci_dev *hdev,
-                                       struct sk_buff *skb)
+static void hci_cc_le_read_local_features(struct hci_dev *hdev,
+                                         struct sk_buff *skb)
 {
-       struct hci_rp_le_read_adv_tx_power *rp = (void *) skb->data;
+       struct hci_rp_le_read_local_features *rp = (void *) skb->data;
 
        BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
 
-       if (!rp->status) {
-               hdev->adv_tx_power = rp->tx_power;
-               if (!test_bit(HCI_INIT, &hdev->flags))
-                       hci_update_ad(hdev);
-       }
-
-       hci_req_complete(hdev, HCI_OP_LE_READ_ADV_TX_POWER, rp->status);
+       if (!rp->status)
+               memcpy(hdev->le_features, rp->features, 8);
 }
 
-static void hci_cc_le_set_event_mask(struct hci_dev *hdev, struct sk_buff *skb)
+static void hci_cc_le_read_adv_tx_power(struct hci_dev *hdev,
+                                       struct sk_buff *skb)
 {
-       __u8 status = *((__u8 *) skb->data);
+       struct hci_rp_le_read_adv_tx_power *rp = (void *) skb->data;
 
-       BT_DBG("%s status 0x%2.2x", hdev->name, status);
+       BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
 
-       hci_req_complete(hdev, HCI_OP_LE_SET_EVENT_MASK, status);
+       if (!rp->status)
+               hdev->adv_tx_power = rp->tx_power;
 }
 
 static void hci_cc_user_confirm_reply(struct hci_dev *hdev, struct sk_buff *skb)
@@ -1209,12 +926,15 @@ static void hci_cc_le_set_adv_enable(struct hci_dev *hdev, struct sk_buff *skb)
                        clear_bit(HCI_LE_PERIPHERAL, &hdev->dev_flags);
        }
 
-       hci_dev_unlock(hdev);
+       if (!test_bit(HCI_INIT, &hdev->flags)) {
+               struct hci_request req;
 
-       if (!test_bit(HCI_INIT, &hdev->flags))
-               hci_update_ad(hdev);
+               hci_req_init(&req, hdev);
+               hci_update_ad(&req);
+               hci_req_run(&req, NULL);
+       }
 
-       hci_req_complete(hdev, HCI_OP_LE_SET_ADV_ENABLE, status);
+       hci_dev_unlock(hdev);
 }
 
 static void hci_cc_le_set_scan_param(struct hci_dev *hdev, struct sk_buff *skb)
@@ -1223,8 +943,6 @@ static void hci_cc_le_set_scan_param(struct hci_dev *hdev, struct sk_buff *skb)
 
        BT_DBG("%s status 0x%2.2x", hdev->name, status);
 
-       hci_req_complete(hdev, HCI_OP_LE_SET_SCAN_PARAM, status);
-
        if (status) {
                hci_dev_lock(hdev);
                mgmt_start_discovery_failed(hdev, status);
@@ -1246,9 +964,7 @@ static void hci_cc_le_set_scan_enable(struct hci_dev *hdev,
                return;
 
        switch (cp->enable) {
-       case LE_SCANNING_ENABLED:
-               hci_req_complete(hdev, HCI_OP_LE_SET_SCAN_ENABLE, status);
-
+       case LE_SCAN_ENABLE:
                if (status) {
                        hci_dev_lock(hdev);
                        mgmt_start_discovery_failed(hdev, status);
@@ -1263,7 +979,7 @@ static void hci_cc_le_set_scan_enable(struct hci_dev *hdev,
                hci_dev_unlock(hdev);
                break;
 
-       case LE_SCANNING_DISABLED:
+       case LE_SCAN_DISABLE:
                if (status) {
                        hci_dev_lock(hdev);
                        mgmt_stop_discovery_failed(hdev, status);
@@ -1290,28 +1006,26 @@ static void hci_cc_le_set_scan_enable(struct hci_dev *hdev,
        }
 }
 
-static void hci_cc_le_ltk_reply(struct hci_dev *hdev, struct sk_buff *skb)
+static void hci_cc_le_read_white_list_size(struct hci_dev *hdev,
+                                          struct sk_buff *skb)
 {
-       struct hci_rp_le_ltk_reply *rp = (void *) skb->data;
+       struct hci_rp_le_read_white_list_size *rp = (void *) skb->data;
 
-       BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
+       BT_DBG("%s status 0x%2.2x size %u", hdev->name, rp->status, rp->size);
 
-       if (rp->status)
-               return;
-
-       hci_req_complete(hdev, HCI_OP_LE_LTK_REPLY, rp->status);
+       if (!rp->status)
+               hdev->le_white_list_size = rp->size;
 }
 
-static void hci_cc_le_ltk_neg_reply(struct hci_dev *hdev, struct sk_buff *skb)
+static void hci_cc_le_read_supported_states(struct hci_dev *hdev,
+                                           struct sk_buff *skb)
 {
-       struct hci_rp_le_ltk_neg_reply *rp = (void *) skb->data;
+       struct hci_rp_le_read_supported_states *rp = (void *) skb->data;
 
        BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
 
-       if (rp->status)
-               return;
-
-       hci_req_complete(hdev, HCI_OP_LE_LTK_NEG_REPLY, rp->status);
+       if (!rp->status)
+               memcpy(hdev->le_states, rp->le_states, 8);
 }
 
 static void hci_cc_write_le_host_supported(struct hci_dev *hdev,
@@ -1328,21 +1042,19 @@ static void hci_cc_write_le_host_supported(struct hci_dev *hdev,
 
        if (!status) {
                if (sent->le)
-                       hdev->host_features[0] |= LMP_HOST_LE;
+                       hdev->features[1][0] |= LMP_HOST_LE;
                else
-                       hdev->host_features[0] &= ~LMP_HOST_LE;
+                       hdev->features[1][0] &= ~LMP_HOST_LE;
 
                if (sent->simul)
-                       hdev->host_features[0] |= LMP_HOST_LE_BREDR;
+                       hdev->features[1][0] |= LMP_HOST_LE_BREDR;
                else
-                       hdev->host_features[0] &= ~LMP_HOST_LE_BREDR;
+                       hdev->features[1][0] &= ~LMP_HOST_LE_BREDR;
        }
 
        if (test_bit(HCI_MGMT, &hdev->dev_flags) &&
            !test_bit(HCI_INIT, &hdev->flags))
                mgmt_le_enable_complete(hdev, sent->le, status);
-
-       hci_req_complete(hdev, HCI_OP_WRITE_LE_HOST_SUPPORTED, status);
 }
 
 static void hci_cc_write_remote_amp_assoc(struct hci_dev *hdev,
@@ -1364,7 +1076,6 @@ static void hci_cs_inquiry(struct hci_dev *hdev, __u8 status)
        BT_DBG("%s status 0x%2.2x", hdev->name, status);
 
        if (status) {
-               hci_req_complete(hdev, HCI_OP_INQUIRY, status);
                hci_conn_check_pending(hdev);
                hci_dev_lock(hdev);
                if (test_bit(HCI_MGMT, &hdev->dev_flags))
@@ -1408,7 +1119,7 @@ static void hci_cs_create_conn(struct hci_dev *hdev, __u8 status)
                }
        } else {
                if (!conn) {
-                       conn = hci_conn_add(hdev, ACL_LINK, &cp->bdaddr);
+                       conn = hci_conn_add(hdev, ACL_LINK, 0, &cp->bdaddr);
                        if (conn) {
                                conn->out = true;
                                conn->link_mode |= HCI_LM_MASTER;
@@ -1475,7 +1186,7 @@ static void hci_cs_auth_requested(struct hci_dev *hdev, __u8 status)
        if (conn) {
                if (conn->state == BT_CONFIG) {
                        hci_proto_connect_cfm(conn, status);
-                       hci_conn_put(conn);
+                       hci_conn_drop(conn);
                }
        }
 
@@ -1502,7 +1213,7 @@ static void hci_cs_set_conn_encrypt(struct hci_dev *hdev, __u8 status)
        if (conn) {
                if (conn->state == BT_CONFIG) {
                        hci_proto_connect_cfm(conn, status);
-                       hci_conn_put(conn);
+                       hci_conn_drop(conn);
                }
        }
 
@@ -1664,7 +1375,7 @@ static void hci_cs_read_remote_features(struct hci_dev *hdev, __u8 status)
        if (conn) {
                if (conn->state == BT_CONFIG) {
                        hci_proto_connect_cfm(conn, status);
-                       hci_conn_put(conn);
+                       hci_conn_drop(conn);
                }
        }
 
@@ -1691,7 +1402,7 @@ static void hci_cs_read_remote_ext_features(struct hci_dev *hdev, __u8 status)
        if (conn) {
                if (conn->state == BT_CONFIG) {
                        hci_proto_connect_cfm(conn, status);
-                       hci_conn_put(conn);
+                       hci_conn_drop(conn);
                }
        }
 
@@ -1836,11 +1547,6 @@ static void hci_cs_le_create_conn(struct hci_dev *hdev, __u8 status)
        }
 }
 
-static void hci_cs_le_start_enc(struct hci_dev *hdev, u8 status)
-{
-       BT_DBG("%s status 0x%2.2x", hdev->name, status);
-}
-
 static void hci_cs_create_phylink(struct hci_dev *hdev, u8 status)
 {
        struct hci_cp_create_phy_link *cp;
@@ -1882,11 +1588,6 @@ static void hci_cs_accept_phylink(struct hci_dev *hdev, u8 status)
        amp_write_remote_assoc(hdev, cp->phy_handle);
 }
 
-static void hci_cs_create_logical_link(struct hci_dev *hdev, u8 status)
-{
-       BT_DBG("%s status 0x%2.2x", hdev->name, status);
-}
-
 static void hci_inquiry_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
 {
        __u8 status = *((__u8 *) skb->data);
@@ -1895,13 +1596,14 @@ static void hci_inquiry_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
 
        BT_DBG("%s status 0x%2.2x", hdev->name, status);
 
-       hci_req_complete(hdev, HCI_OP_INQUIRY, status);
-
        hci_conn_check_pending(hdev);
 
        if (!test_and_clear_bit(HCI_INQUIRY, &hdev->flags))
                return;
 
+       smp_mb__after_clear_bit(); /* wake_up_bit advises about this barrier */
+       wake_up_bit(&hdev->flags, HCI_INQUIRY);
+
        if (!test_bit(HCI_MGMT, &hdev->dev_flags))
                return;
 
@@ -2000,7 +1702,6 @@ static void hci_conn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
                } else
                        conn->state = BT_CONNECTED;
 
-               hci_conn_hold_device(conn);
                hci_conn_add_sysfs(conn);
 
                if (test_bit(HCI_AUTH, &hdev->flags))
@@ -2047,40 +1748,13 @@ unlock:
        hci_conn_check_pending(hdev);
 }
 
-void hci_conn_accept(struct hci_conn *conn, int mask)
+static inline bool is_sco_active(struct hci_dev *hdev)
 {
-       struct hci_dev *hdev = conn->hdev;
-
-       BT_DBG("conn %p", conn);
-
-       conn->state = BT_CONFIG;
-
-       if (!lmp_esco_capable(hdev)) {
-               struct hci_cp_accept_conn_req cp;
-
-               bacpy(&cp.bdaddr, &conn->dst);
-
-               if (lmp_rswitch_capable(hdev) && (mask & HCI_LM_MASTER))
-                       cp.role = 0x00; /* Become master */
-               else
-                       cp.role = 0x01; /* Remain slave */
-
-               hci_send_cmd(hdev, HCI_OP_ACCEPT_CONN_REQ, sizeof(cp), &cp);
-       } else /* lmp_esco_capable(hdev)) */ {
-               struct hci_cp_accept_sync_conn_req cp;
-
-               bacpy(&cp.bdaddr, &conn->dst);
-               cp.pkt_type = cpu_to_le16(conn->pkt_type);
-
-               cp.tx_bandwidth   = __constant_cpu_to_le32(0x00001f40);
-               cp.rx_bandwidth   = __constant_cpu_to_le32(0x00001f40);
-               cp.max_latency    = __constant_cpu_to_le16(0xffff);
-               cp.content_format = cpu_to_le16(hdev->voice_setting);
-               cp.retrans_effort = 0xff;
-
-               hci_send_cmd(hdev, HCI_OP_ACCEPT_SYNC_CONN_REQ,
-                            sizeof(cp), &cp);
-       }
+       if (hci_conn_hash_lookup_state(hdev, SCO_LINK, BT_CONNECTED) ||
+                       (hci_conn_hash_lookup_state(hdev, ESCO_LINK,
+                                                   BT_CONNECTED)))
+               return true;
+       return false;
 }
 
 static void hci_conn_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
@@ -2110,7 +1784,8 @@ static void hci_conn_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
                conn = hci_conn_hash_lookup_ba(hdev, ev->link_type,
                                               &ev->bdaddr);
                if (!conn) {
-                       conn = hci_conn_add(hdev, ev->link_type, &ev->bdaddr);
+                       /* pkt_type not yet used for incoming connections */
+                       conn = hci_conn_add(hdev, ev->link_type, 0, &ev->bdaddr);
                        if (!conn) {
                                BT_ERR("No memory for new connection");
                                hci_dev_unlock(hdev);
@@ -2129,7 +1804,8 @@ static void hci_conn_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
 
                        bacpy(&cp.bdaddr, &ev->bdaddr);
 
-                       if (lmp_rswitch_capable(hdev) && (mask & HCI_LM_MASTER))
+                       if (lmp_rswitch_capable(hdev) && ((mask & HCI_LM_MASTER)
+                                               || is_sco_active(hdev)))
                                cp.role = 0x00; /* Become master */
                        else
                                cp.role = 0x01; /* Remain slave */
@@ -2154,7 +1830,6 @@ static void hci_conn_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
                } else {
                        conn->state = BT_CONNECT2;
                        hci_proto_connect_cfm(conn, 0);
-                       hci_conn_put(conn);
                }
        } else {
                /* Connection rejected */
@@ -2261,14 +1936,14 @@ static void hci_auth_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
                } else {
                        conn->state = BT_CONNECTED;
                        hci_proto_connect_cfm(conn, ev->status);
-                       hci_conn_put(conn);
+                       hci_conn_drop(conn);
                }
        } else {
                hci_auth_cfm(conn, ev->status);
 
                hci_conn_hold(conn);
                conn->disc_timeout = HCI_DISCONN_TIMEOUT;
-               hci_conn_put(conn);
+               hci_conn_drop(conn);
        }
 
        if (test_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags)) {
@@ -2351,8 +2026,8 @@ static void hci_encrypt_change_evt(struct hci_dev *hdev, struct sk_buff *skb)
                clear_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags);
 
                if (ev->status && conn->state == BT_CONNECTED) {
-                       hci_acl_disconn(conn, HCI_ERROR_AUTH_FAILURE);
-                       hci_conn_put(conn);
+                       hci_disconnect(conn, HCI_ERROR_AUTH_FAILURE);
+                       hci_conn_drop(conn);
                        goto unlock;
                }
 
@@ -2361,7 +2036,7 @@ static void hci_encrypt_change_evt(struct hci_dev *hdev, struct sk_buff *skb)
                                conn->state = BT_CONNECTED;
 
                        hci_proto_connect_cfm(conn, ev->status);
-                       hci_conn_put(conn);
+                       hci_conn_drop(conn);
                } else
                        hci_encrypt_cfm(conn, ev->status, ev->encrypt);
        }
@@ -2408,7 +2083,7 @@ static void hci_remote_features_evt(struct hci_dev *hdev,
                goto unlock;
 
        if (!ev->status)
-               memcpy(conn->features, ev->features, 8);
+               memcpy(conn->features[0], ev->features, 8);
 
        if (conn->state != BT_CONFIG)
                goto unlock;
@@ -2436,27 +2111,17 @@ static void hci_remote_features_evt(struct hci_dev *hdev,
        if (!hci_outgoing_auth_needed(hdev, conn)) {
                conn->state = BT_CONNECTED;
                hci_proto_connect_cfm(conn, ev->status);
-               hci_conn_put(conn);
+               hci_conn_drop(conn);
        }
 
 unlock:
        hci_dev_unlock(hdev);
 }
 
-static void hci_remote_version_evt(struct hci_dev *hdev, struct sk_buff *skb)
-{
-       BT_DBG("%s", hdev->name);
-}
-
-static void hci_qos_setup_complete_evt(struct hci_dev *hdev,
-                                      struct sk_buff *skb)
-{
-       BT_DBG("%s", hdev->name);
-}
-
 static void hci_cmd_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
 {
        struct hci_ev_cmd_complete *ev = (void *) skb->data;
+       u8 status = skb->data[sizeof(*ev)];
        __u16 opcode;
 
        skb_pull(skb, sizeof(*ev));
@@ -2540,10 +2205,6 @@ static void hci_cmd_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
                hci_cc_write_voice_setting(hdev, skb);
                break;
 
-       case HCI_OP_HOST_BUFFER_SIZE:
-               hci_cc_host_buffer_size(hdev, skb);
-               break;
-
        case HCI_OP_WRITE_SSP_MODE:
                hci_cc_write_ssp_mode(hdev, skb);
                break;
@@ -2572,46 +2233,42 @@ static void hci_cmd_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
                hci_cc_read_bd_addr(hdev, skb);
                break;
 
-       case HCI_OP_READ_DATA_BLOCK_SIZE:
-               hci_cc_read_data_block_size(hdev, skb);
+       case HCI_OP_READ_PAGE_SCAN_ACTIVITY:
+               hci_cc_read_page_scan_activity(hdev, skb);
                break;
 
-       case HCI_OP_WRITE_CA_TIMEOUT:
-               hci_cc_write_ca_timeout(hdev, skb);
+       case HCI_OP_WRITE_PAGE_SCAN_ACTIVITY:
+               hci_cc_write_page_scan_activity(hdev, skb);
                break;
 
-       case HCI_OP_READ_FLOW_CONTROL_MODE:
-               hci_cc_read_flow_control_mode(hdev, skb);
+       case HCI_OP_READ_PAGE_SCAN_TYPE:
+               hci_cc_read_page_scan_type(hdev, skb);
                break;
 
-       case HCI_OP_READ_LOCAL_AMP_INFO:
-               hci_cc_read_local_amp_info(hdev, skb);
+       case HCI_OP_WRITE_PAGE_SCAN_TYPE:
+               hci_cc_write_page_scan_type(hdev, skb);
                break;
 
-       case HCI_OP_READ_LOCAL_AMP_ASSOC:
-               hci_cc_read_local_amp_assoc(hdev, skb);
+       case HCI_OP_READ_DATA_BLOCK_SIZE:
+               hci_cc_read_data_block_size(hdev, skb);
                break;
 
-       case HCI_OP_DELETE_STORED_LINK_KEY:
-               hci_cc_delete_stored_link_key(hdev, skb);
+       case HCI_OP_READ_FLOW_CONTROL_MODE:
+               hci_cc_read_flow_control_mode(hdev, skb);
                break;
 
-       case HCI_OP_SET_EVENT_MASK:
-               hci_cc_set_event_mask(hdev, skb);
+       case HCI_OP_READ_LOCAL_AMP_INFO:
+               hci_cc_read_local_amp_info(hdev, skb);
                break;
 
-       case HCI_OP_WRITE_INQUIRY_MODE:
-               hci_cc_write_inquiry_mode(hdev, skb);
+       case HCI_OP_READ_LOCAL_AMP_ASSOC:
+               hci_cc_read_local_amp_assoc(hdev, skb);
                break;
 
        case HCI_OP_READ_INQ_RSP_TX_POWER:
                hci_cc_read_inq_rsp_tx_power(hdev, skb);
                break;
 
-       case HCI_OP_SET_EVENT_FLT:
-               hci_cc_set_event_flt(hdev, skb);
-               break;
-
        case HCI_OP_PIN_CODE_REPLY:
                hci_cc_pin_code_reply(hdev, skb);
                break;
@@ -2628,12 +2285,12 @@ static void hci_cmd_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
                hci_cc_le_read_buffer_size(hdev, skb);
                break;
 
-       case HCI_OP_LE_READ_ADV_TX_POWER:
-               hci_cc_le_read_adv_tx_power(hdev, skb);
+       case HCI_OP_LE_READ_LOCAL_FEATURES:
+               hci_cc_le_read_local_features(hdev, skb);
                break;
 
-       case HCI_OP_LE_SET_EVENT_MASK:
-               hci_cc_le_set_event_mask(hdev, skb);
+       case HCI_OP_LE_READ_ADV_TX_POWER:
+               hci_cc_le_read_adv_tx_power(hdev, skb);
                break;
 
        case HCI_OP_USER_CONFIRM_REPLY:
@@ -2664,12 +2321,12 @@ static void hci_cmd_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
                hci_cc_le_set_scan_enable(hdev, skb);
                break;
 
-       case HCI_OP_LE_LTK_REPLY:
-               hci_cc_le_ltk_reply(hdev, skb);
+       case HCI_OP_LE_READ_WHITE_LIST_SIZE:
+               hci_cc_le_read_white_list_size(hdev, skb);
                break;
 
-       case HCI_OP_LE_LTK_NEG_REPLY:
-               hci_cc_le_ltk_neg_reply(hdev, skb);
+       case HCI_OP_LE_READ_SUPPORTED_STATES:
+               hci_cc_le_read_supported_states(hdev, skb);
                break;
 
        case HCI_OP_WRITE_LE_HOST_SUPPORTED:
@@ -2685,9 +2342,11 @@ static void hci_cmd_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
                break;
        }
 
-       if (ev->opcode != HCI_OP_NOP)
+       if (opcode != HCI_OP_NOP)
                del_timer(&hdev->cmd_timer);
 
+       hci_req_cmd_complete(hdev, opcode, status);
+
        if (ev->ncmd && !test_bit(HCI_RESET, &hdev->flags)) {
                atomic_set(&hdev->cmd_cnt, 1);
                if (!skb_queue_empty(&hdev->cmd_q))
@@ -2757,10 +2416,6 @@ static void hci_cmd_status_evt(struct hci_dev *hdev, struct sk_buff *skb)
                hci_cs_le_create_conn(hdev, ev->status);
                break;
 
-       case HCI_OP_LE_START_ENC:
-               hci_cs_le_start_enc(hdev, ev->status);
-               break;
-
        case HCI_OP_CREATE_PHY_LINK:
                hci_cs_create_phylink(hdev, ev->status);
                break;
@@ -2769,18 +2424,18 @@ static void hci_cmd_status_evt(struct hci_dev *hdev, struct sk_buff *skb)
                hci_cs_accept_phylink(hdev, ev->status);
                break;
 
-       case HCI_OP_CREATE_LOGICAL_LINK:
-               hci_cs_create_logical_link(hdev, ev->status);
-               break;
-
        default:
                BT_DBG("%s opcode 0x%4.4x", hdev->name, opcode);
                break;
        }
 
-       if (ev->opcode != HCI_OP_NOP)
+       if (opcode != HCI_OP_NOP)
                del_timer(&hdev->cmd_timer);
 
+       if (ev->status ||
+           (hdev->sent_cmd && !bt_cb(hdev->sent_cmd)->req.event))
+               hci_req_cmd_complete(hdev, opcode, ev->status);
+
        if (ev->ncmd && !test_bit(HCI_RESET, &hdev->flags)) {
                atomic_set(&hdev->cmd_cnt, 1);
                if (!skb_queue_empty(&hdev->cmd_q))
@@ -2996,7 +2651,7 @@ static void hci_pin_code_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
        if (conn->state == BT_CONNECTED) {
                hci_conn_hold(conn);
                conn->disc_timeout = HCI_PAIRING_TIMEOUT;
-               hci_conn_put(conn);
+               hci_conn_drop(conn);
        }
 
        if (!test_bit(HCI_PAIRABLE, &hdev->dev_flags))
@@ -3099,7 +2754,7 @@ static void hci_link_key_notify_evt(struct hci_dev *hdev, struct sk_buff *skb)
                if (ev->key_type != HCI_LK_CHANGED_COMBINATION)
                        conn->key_type = ev->key_type;
 
-               hci_conn_put(conn);
+               hci_conn_drop(conn);
        }
 
        if (test_bit(HCI_LINK_KEYS, &hdev->dev_flags))
@@ -3240,6 +2895,9 @@ static void hci_remote_ext_features_evt(struct hci_dev *hdev,
        if (!conn)
                goto unlock;
 
+       if (ev->page < HCI_MAX_PAGES)
+               memcpy(conn->features[ev->page], ev->features, 8);
+
        if (!ev->status && ev->page == 0x01) {
                struct inquiry_entry *ie;
 
@@ -3247,8 +2905,19 @@ static void hci_remote_ext_features_evt(struct hci_dev *hdev,
                if (ie)
                        ie->data.ssp_mode = (ev->features[0] & LMP_HOST_SSP);
 
-               if (ev->features[0] & LMP_HOST_SSP)
+               if (ev->features[0] & LMP_HOST_SSP) {
                        set_bit(HCI_CONN_SSP_ENABLED, &conn->flags);
+               } else {
+                       /* It is mandatory by the Bluetooth specification that
+                        * Extended Inquiry Results are only used when Secure
+                        * Simple Pairing is enabled, but some devices violate
+                        * this.
+                        *
+                        * To make these devices work, the internal SSP
+                        * enabled flag needs to be cleared if the remote host
+                        * features do not indicate SSP support */
+                       clear_bit(HCI_CONN_SSP_ENABLED, &conn->flags);
+               }
        }
 
        if (conn->state != BT_CONFIG)
@@ -3268,7 +2937,7 @@ static void hci_remote_ext_features_evt(struct hci_dev *hdev,
        if (!hci_outgoing_auth_needed(hdev, conn)) {
                conn->state = BT_CONNECTED;
                hci_proto_connect_cfm(conn, ev->status);
-               hci_conn_put(conn);
+               hci_conn_drop(conn);
        }
 
 unlock:
@@ -3302,10 +2971,10 @@ static void hci_sync_conn_complete_evt(struct hci_dev *hdev,
                conn->handle = __le16_to_cpu(ev->handle);
                conn->state  = BT_CONNECTED;
 
-               hci_conn_hold_device(conn);
                hci_conn_add_sysfs(conn);
                break;
 
+       case 0x10:      /* Connection Accept Timeout */
        case 0x11:      /* Unsupported Feature or Parameter Value */
        case 0x1c:      /* SCO interval rejected */
        case 0x1a:      /* Unsupported Remote Feature */
@@ -3331,18 +3000,6 @@ unlock:
        hci_dev_unlock(hdev);
 }
 
-static void hci_sync_conn_changed_evt(struct hci_dev *hdev, struct sk_buff *skb)
-{
-       BT_DBG("%s", hdev->name);
-}
-
-static void hci_sniff_subrate_evt(struct hci_dev *hdev, struct sk_buff *skb)
-{
-       struct hci_ev_sniff_subrate *ev = (void *) skb->data;
-
-       BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
-}
-
 static void hci_extended_inquiry_result_evt(struct hci_dev *hdev,
                                            struct sk_buff *skb)
 {
@@ -3406,14 +3063,20 @@ static void hci_key_refresh_complete_evt(struct hci_dev *hdev,
        if (!conn)
                goto unlock;
 
+       /* For BR/EDR the necessary steps are taken through the
+        * auth_complete event.
+        */
+       if (conn->type != LE_LINK)
+               goto unlock;
+
        if (!ev->status)
                conn->sec_level = conn->pending_sec_level;
 
        clear_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags);
 
        if (ev->status && conn->state == BT_CONNECTED) {
-               hci_acl_disconn(conn, HCI_ERROR_AUTH_FAILURE);
-               hci_conn_put(conn);
+               hci_disconnect(conn, HCI_ERROR_AUTH_FAILURE);
+               hci_conn_drop(conn);
                goto unlock;
        }
 
@@ -3422,13 +3085,13 @@ static void hci_key_refresh_complete_evt(struct hci_dev *hdev,
                        conn->state = BT_CONNECTED;
 
                hci_proto_connect_cfm(conn, ev->status);
-               hci_conn_put(conn);
+               hci_conn_drop(conn);
        } else {
                hci_auth_cfm(conn, ev->status);
 
                hci_conn_hold(conn);
                conn->disc_timeout = HCI_DISCONN_TIMEOUT;
-               hci_conn_put(conn);
+               hci_conn_drop(conn);
        }
 
 unlock:
@@ -3567,8 +3230,11 @@ static void hci_user_confirm_request_evt(struct hci_dev *hdev,
 
                /* If we're not the initiators request authorization to
                 * proceed from user space (mgmt_user_confirm with
-                * confirm_hint set to 1). */
-               if (!test_bit(HCI_CONN_AUTH_PEND, &conn->flags)) {
+                * confirm_hint set to 1). The exception is if neither
+                * side had MITM in which case we do auto-accept.
+                */
+               if (!test_bit(HCI_CONN_AUTH_PEND, &conn->flags) &&
+                   (loc_mitm || rem_mitm)) {
                        BT_DBG("Confirming auto-accept as acceptor");
                        confirm_hint = 1;
                        goto confirm;
@@ -3689,7 +3355,7 @@ static void hci_simple_pair_complete_evt(struct hci_dev *hdev,
                mgmt_auth_failed(hdev, &conn->dst, conn->type, conn->dst_type,
                                 ev->status);
 
-       hci_conn_put(conn);
+       hci_conn_drop(conn);
 
 unlock:
        hci_dev_unlock(hdev);
@@ -3700,11 +3366,16 @@ static void hci_remote_host_features_evt(struct hci_dev *hdev,
 {
        struct hci_ev_remote_host_features *ev = (void *) skb->data;
        struct inquiry_entry *ie;
+       struct hci_conn *conn;
 
        BT_DBG("%s", hdev->name);
 
        hci_dev_lock(hdev);
 
+       conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
+       if (conn)
+               memcpy(conn->features[1], ev->features, 8);
+
        ie = hci_inquiry_cache_lookup(hdev, &ev->bdaddr);
        if (ie)
                ie->data.ssp_mode = (ev->features[0] & LMP_HOST_SSP);
@@ -3777,9 +3448,8 @@ static void hci_phy_link_complete_evt(struct hci_dev *hdev,
 
        hci_conn_hold(hcon);
        hcon->disc_timeout = HCI_DISCONN_TIMEOUT;
-       hci_conn_put(hcon);
+       hci_conn_drop(hcon);
 
-       hci_conn_hold_device(hcon);
        hci_conn_add_sysfs(hcon);
 
        amp_physical_cfm(bredr_hcon, hcon);
@@ -3882,7 +3552,7 @@ static void hci_le_conn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
 
        conn = hci_conn_hash_lookup_state(hdev, LE_LINK, BT_CONNECT);
        if (!conn) {
-               conn = hci_conn_add(hdev, LE_LINK, &ev->bdaddr);
+               conn = hci_conn_add(hdev, LE_LINK, 0, &ev->bdaddr);
                if (!conn) {
                        BT_ERR("No memory for new connection");
                        goto unlock;
@@ -3913,7 +3583,6 @@ static void hci_le_conn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
        conn->handle = __le16_to_cpu(ev->handle);
        conn->state = BT_CONNECTED;
 
-       hci_conn_hold_device(conn);
        hci_conn_add_sysfs(conn);
 
        hci_proto_connect_cfm(conn, ev->status);
@@ -3928,8 +3597,6 @@ static void hci_le_adv_report_evt(struct hci_dev *hdev, struct sk_buff *skb)
        void *ptr = &skb->data[1];
        s8 rssi;
 
-       hci_dev_lock(hdev);
-
        while (num_reports--) {
                struct hci_ev_le_advertising_info *ev = ptr;
 
@@ -3939,8 +3606,6 @@ static void hci_le_adv_report_evt(struct hci_dev *hdev, struct sk_buff *skb)
 
                ptr += sizeof(*ev) + ev->length + 1;
        }
-
-       hci_dev_unlock(hdev);
 }
 
 static void hci_le_ltk_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
@@ -3967,11 +3632,21 @@ static void hci_le_ltk_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
        cp.handle = cpu_to_le16(conn->handle);
 
        if (ltk->authenticated)
-               conn->sec_level = BT_SECURITY_HIGH;
+               conn->pending_sec_level = BT_SECURITY_HIGH;
+       else
+               conn->pending_sec_level = BT_SECURITY_MEDIUM;
+
+       conn->enc_key_size = ltk->enc_size;
 
        hci_send_cmd(hdev, HCI_OP_LE_LTK_REPLY, sizeof(cp), &cp);
 
-       if (ltk->type & HCI_SMP_STK) {
+       /* Ref. Bluetooth Core SPEC pages 1975 and 2004. STK is a
+        * temporary key used to encrypt a connection following
+        * pairing. It is used during the Encrypted Session Setup to
+        * distribute the keys. Later, security can be re-established
+        * using a distributed LTK.
+        */
+       if (ltk->type == HCI_SMP_STK_SLAVE) {
                list_del(&ltk->list);
                kfree(ltk);
        }
@@ -4031,8 +3706,27 @@ void hci_event_packet(struct hci_dev *hdev, struct sk_buff *skb)
        struct hci_event_hdr *hdr = (void *) skb->data;
        __u8 event = hdr->evt;
 
+       hci_dev_lock(hdev);
+
+       /* Received events are (currently) only needed when a request is
+        * ongoing so avoid unnecessary memory allocation.
+        */
+       if (hdev->req_status == HCI_REQ_PEND) {
+               kfree_skb(hdev->recv_evt);
+               hdev->recv_evt = skb_clone(skb, GFP_KERNEL);
+       }
+
+       hci_dev_unlock(hdev);
+
        skb_pull(skb, HCI_EVENT_HDR_SIZE);
 
+       if (hdev->sent_cmd && bt_cb(hdev->sent_cmd)->req.event == event) {
+               struct hci_command_hdr *hdr = (void *) hdev->sent_cmd->data;
+               u16 opcode = __le16_to_cpu(hdr->opcode);
+
+               hci_req_cmd_complete(hdev, opcode, 0);
+       }
+
        switch (event) {
        case HCI_EV_INQUIRY_COMPLETE:
                hci_inquiry_complete_evt(hdev, skb);
@@ -4074,14 +3768,6 @@ void hci_event_packet(struct hci_dev *hdev, struct sk_buff *skb)
                hci_remote_features_evt(hdev, skb);
                break;
 
-       case HCI_EV_REMOTE_VERSION:
-               hci_remote_version_evt(hdev, skb);
-               break;
-
-       case HCI_EV_QOS_SETUP_COMPLETE:
-               hci_qos_setup_complete_evt(hdev, skb);
-               break;
-
        case HCI_EV_CMD_COMPLETE:
                hci_cmd_complete_evt(hdev, skb);
                break;
@@ -4138,14 +3824,6 @@ void hci_event_packet(struct hci_dev *hdev, struct sk_buff *skb)
                hci_sync_conn_complete_evt(hdev, skb);
                break;
 
-       case HCI_EV_SYNC_CONN_CHANGED:
-               hci_sync_conn_changed_evt(hdev, skb);
-               break;
-
-       case HCI_EV_SNIFF_SUBRATE:
-               hci_sniff_subrate_evt(hdev, skb);
-               break;
-
        case HCI_EV_EXTENDED_INQUIRY_RESULT:
                hci_extended_inquiry_result_evt(hdev, skb);
                break;