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 688c1a9..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;
@@ -1752,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)
@@ -1815,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);
@@ -1834,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 */
@@ -2112,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;
@@ -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)
@@ -2989,6 +2974,7 @@ static void hci_sync_conn_complete_evt(struct hci_dev *hdev,
                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 */
@@ -3077,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;
 
@@ -3238,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;
@@ -3371,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);
@@ -3552,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;
@@ -3632,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);
        }