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 ed0efb7..1526fb2
@@ -433,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))
@@ -493,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);
        }
@@ -512,26 +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]);
+              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,
@@ -544,14 +544,10 @@ static void hci_cc_read_local_ext_features(struct hci_dev *hdev,
        if (rp->status)
                return;
 
-       switch (rp->page) {
-       case 0:
-               memcpy(hdev->features, rp->features, 8);
-               break;
-       case 1:
-               memcpy(hdev->host_features, rp->features, 8);
-               break;
-       }
+       hdev->max_page = rp->max_page;
+
+       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,
@@ -968,7 +964,7 @@ static void hci_cc_le_set_scan_enable(struct hci_dev *hdev,
                return;
 
        switch (cp->enable) {
-       case LE_SCANNING_ENABLED:
+       case LE_SCAN_ENABLE:
                if (status) {
                        hci_dev_lock(hdev);
                        mgmt_start_discovery_failed(hdev, status);
@@ -983,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);
@@ -1046,14 +1042,14 @@ 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) &&
@@ -1123,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;
@@ -1190,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);
                }
        }
 
@@ -1217,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);
                }
        }
 
@@ -1379,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);
                }
        }
 
@@ -1406,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);
                }
        }
 
@@ -1706,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))
@@ -1753,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)
@@ -1816,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);
@@ -1835,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 */
@@ -1860,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 */
@@ -1967,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)) {
@@ -2058,7 +2027,7 @@ static void hci_encrypt_change_evt(struct hci_dev *hdev, struct sk_buff *skb)
 
                if (ev->status && conn->state == BT_CONNECTED) {
                        hci_disconnect(conn, HCI_ERROR_AUTH_FAILURE);
-                       hci_conn_put(conn);
+                       hci_conn_drop(conn);
                        goto unlock;
                }
 
@@ -2067,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);
        }
@@ -2114,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;
@@ -2142,7 +2111,7 @@ 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:
@@ -2463,7 +2432,9 @@ static void hci_cmd_status_evt(struct hci_dev *hdev, struct sk_buff *skb)
        if (opcode != HCI_OP_NOP)
                del_timer(&hdev->cmd_timer);
 
-       hci_req_cmd_complete(hdev, opcode, ev->status);
+       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);
@@ -2680,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))
@@ -2783,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))
@@ -2924,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;
 
@@ -2931,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)
@@ -2952,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:
@@ -2986,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 */
@@ -3078,6 +3063,12 @@ 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;
 
@@ -3085,7 +3076,7 @@ static void hci_key_refresh_complete_evt(struct hci_dev *hdev,
 
        if (ev->status && conn->state == BT_CONNECTED) {
                hci_disconnect(conn, HCI_ERROR_AUTH_FAILURE);
-               hci_conn_put(conn);
+               hci_conn_drop(conn);
                goto unlock;
        }
 
@@ -3094,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:
@@ -3239,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;
@@ -3361,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);
@@ -3372,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);
@@ -3449,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);
@@ -3554,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;
@@ -3585,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);
@@ -3635,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);
        }
@@ -3713,6 +3720,13 @@ void hci_event_packet(struct hci_dev *hdev, struct sk_buff *skb)
 
        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);