Merge tag 'pm+acpi-3.15-rc1-3' of git://git.kernel.org/pub/scm/linux/kernel/git/rafae...
[cascardo/linux.git] / net / bluetooth / hci_conn.c
index 5c392aa..d958e2d 100644 (file)
@@ -82,7 +82,7 @@ static void hci_acl_create_connection(struct hci_conn *conn)
                        cp.pscan_rep_mode = ie->data.pscan_rep_mode;
                        cp.pscan_mode     = ie->data.pscan_mode;
                        cp.clock_offset   = ie->data.clock_offset |
-                                           __constant_cpu_to_le16(0x8000);
+                                           cpu_to_le16(0x8000);
                }
 
                memcpy(conn->dev_class, ie->data.dev_class, 3);
@@ -182,8 +182,8 @@ bool hci_setup_sync(struct hci_conn *conn, __u16 handle)
 
        cp.handle   = cpu_to_le16(handle);
 
-       cp.tx_bandwidth   = __constant_cpu_to_le32(0x00001f40);
-       cp.rx_bandwidth   = __constant_cpu_to_le32(0x00001f40);
+       cp.tx_bandwidth   = cpu_to_le32(0x00001f40);
+       cp.rx_bandwidth   = cpu_to_le32(0x00001f40);
        cp.voice_setting  = cpu_to_le16(conn->setting);
 
        switch (conn->setting & SCO_AIRMODE_MASK) {
@@ -225,13 +225,13 @@ void hci_le_conn_update(struct hci_conn *conn, u16 min, u16 max,
        cp.conn_interval_max    = cpu_to_le16(max);
        cp.conn_latency         = cpu_to_le16(latency);
        cp.supervision_timeout  = cpu_to_le16(to_multiplier);
-       cp.min_ce_len           = __constant_cpu_to_le16(0x0000);
-       cp.max_ce_len           = __constant_cpu_to_le16(0x0000);
+       cp.min_ce_len           = cpu_to_le16(0x0000);
+       cp.max_ce_len           = cpu_to_le16(0x0000);
 
        hci_send_cmd(hdev, HCI_OP_LE_CONN_UPDATE, sizeof(cp), &cp);
 }
 
-void hci_le_start_enc(struct hci_conn *conn, __le16 ediv, __u8 rand[8],
+void hci_le_start_enc(struct hci_conn *conn, __le16 ediv, __le64 rand,
                      __u8 ltk[16])
 {
        struct hci_dev *hdev = conn->hdev;
@@ -242,9 +242,9 @@ void hci_le_start_enc(struct hci_conn *conn, __le16 ediv, __u8 rand[8],
        memset(&cp, 0, sizeof(cp));
 
        cp.handle = cpu_to_le16(conn->handle);
-       memcpy(cp.ltk, ltk, sizeof(cp.ltk));
+       cp.rand = rand;
        cp.ediv = ediv;
-       memcpy(cp.rand, rand, sizeof(cp.rand));
+       memcpy(cp.ltk, ltk, sizeof(cp.ltk));
 
        hci_send_cmd(hdev, HCI_OP_LE_START_ENC, sizeof(cp), &cp);
 }
@@ -337,9 +337,9 @@ static void hci_conn_idle(struct work_struct *work)
        if (lmp_sniffsubr_capable(hdev) && lmp_sniffsubr_capable(conn)) {
                struct hci_cp_sniff_subrate cp;
                cp.handle             = cpu_to_le16(conn->handle);
-               cp.max_latency        = __constant_cpu_to_le16(0);
-               cp.min_remote_timeout = __constant_cpu_to_le16(0);
-               cp.min_local_timeout  = __constant_cpu_to_le16(0);
+               cp.max_latency        = cpu_to_le16(0);
+               cp.min_remote_timeout = cpu_to_le16(0);
+               cp.min_local_timeout  = cpu_to_le16(0);
                hci_send_cmd(hdev, HCI_OP_SNIFF_SUBRATE, sizeof(cp), &cp);
        }
 
@@ -348,8 +348,8 @@ static void hci_conn_idle(struct work_struct *work)
                cp.handle       = cpu_to_le16(conn->handle);
                cp.max_interval = cpu_to_le16(hdev->sniff_max_interval);
                cp.min_interval = cpu_to_le16(hdev->sniff_min_interval);
-               cp.attempt      = __constant_cpu_to_le16(4);
-               cp.timeout      = __constant_cpu_to_le16(1);
+               cp.attempt      = cpu_to_le16(4);
+               cp.timeout      = cpu_to_le16(1);
                hci_send_cmd(hdev, HCI_OP_SNIFF_MODE, sizeof(cp), &cp);
        }
 }
@@ -363,6 +363,16 @@ static void hci_conn_auto_accept(struct work_struct *work)
                     &conn->dst);
 }
 
+static void le_conn_timeout(struct work_struct *work)
+{
+       struct hci_conn *conn = container_of(work, struct hci_conn,
+                                            le_conn_timeout.work);
+
+       BT_DBG("");
+
+       hci_le_create_connection_cancel(conn);
+}
+
 struct hci_conn *hci_conn_add(struct hci_dev *hdev, int type, bdaddr_t *dst)
 {
        struct hci_conn *conn;
@@ -410,6 +420,7 @@ struct hci_conn *hci_conn_add(struct hci_dev *hdev, int type, bdaddr_t *dst)
        INIT_DELAYED_WORK(&conn->disc_work, hci_conn_timeout);
        INIT_DELAYED_WORK(&conn->auto_accept_work, hci_conn_auto_accept);
        INIT_DELAYED_WORK(&conn->idle_work, hci_conn_idle);
+       INIT_DELAYED_WORK(&conn->le_conn_timeout, le_conn_timeout);
 
        atomic_set(&conn->refcnt, 0);
 
@@ -442,6 +453,8 @@ int hci_conn_del(struct hci_conn *conn)
                /* Unacked frames */
                hdev->acl_cnt += conn->sent;
        } else if (conn->type == LE_LINK) {
+               cancel_delayed_work_sync(&conn->le_conn_timeout);
+
                if (hdev->le_pkts)
                        hdev->le_cnt += conn->sent;
                else
@@ -527,6 +540,11 @@ void hci_le_conn_failed(struct hci_conn *conn, u8 status)
        hci_proto_connect_cfm(conn, status);
 
        hci_conn_del(conn);
+
+       /* Since we may have temporarily stopped the background scanning in
+        * favor of connection establishment, we should restart it.
+        */
+       hci_update_background_scan(hdev);
 }
 
 static void create_le_conn_complete(struct hci_dev *hdev, u8 status)
@@ -578,50 +596,13 @@ static void hci_req_add_le_create_conn(struct hci_request *req,
        cp.own_address_type = own_addr_type;
        cp.conn_interval_min = cpu_to_le16(conn->le_conn_min_interval);
        cp.conn_interval_max = cpu_to_le16(conn->le_conn_max_interval);
-       cp.supervision_timeout = __constant_cpu_to_le16(0x002a);
-       cp.min_ce_len = __constant_cpu_to_le16(0x0000);
-       cp.max_ce_len = __constant_cpu_to_le16(0x0000);
+       cp.supervision_timeout = cpu_to_le16(0x002a);
+       cp.min_ce_len = cpu_to_le16(0x0000);
+       cp.max_ce_len = cpu_to_le16(0x0000);
 
        hci_req_add(req, HCI_OP_LE_CREATE_CONN, sizeof(cp), &cp);
-}
 
-static void stop_scan_complete(struct hci_dev *hdev, u8 status)
-{
-       struct hci_request req;
-       struct hci_conn *conn;
-       int err;
-
-       conn = hci_conn_hash_lookup_state(hdev, LE_LINK, BT_CONNECT);
-       if (!conn)
-               return;
-
-       if (status) {
-               BT_DBG("HCI request failed to stop scanning: status 0x%2.2x",
-                      status);
-
-               hci_dev_lock(hdev);
-               hci_le_conn_failed(conn, status);
-               hci_dev_unlock(hdev);
-               return;
-       }
-
-       /* Since we may have prematurely stopped discovery procedure, we should
-        * update discovery state.
-        */
-       cancel_delayed_work(&hdev->le_scan_disable);
-       hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
-
-       hci_req_init(&req, hdev);
-
-       hci_req_add_le_create_conn(&req, conn);
-
-       err = hci_req_run(&req, create_le_conn_complete);
-       if (err) {
-               hci_dev_lock(hdev);
-               hci_le_conn_failed(conn, HCI_ERROR_MEMORY_EXCEEDED);
-               hci_dev_unlock(hdev);
-               return;
-       }
+       conn->state = BT_CONNECT;
 }
 
 struct hci_conn *hci_connect_le(struct hci_dev *hdev, bdaddr_t *dst,
@@ -659,12 +640,6 @@ struct hci_conn *hci_connect_le(struct hci_dev *hdev, bdaddr_t *dst,
        if (conn)
                return ERR_PTR(-EBUSY);
 
-       /* Convert from L2CAP channel address type to HCI address type */
-       if (dst_type == BDADDR_LE_PUBLIC)
-               dst_type = ADDR_LE_DEV_PUBLIC;
-       else
-               dst_type = ADDR_LE_DEV_RANDOM;
-
        /* When given an identity address with existing identity
         * resolving key, the connection needs to be established
         * to a resolvable random address.
@@ -690,7 +665,6 @@ struct hci_conn *hci_connect_le(struct hci_dev *hdev, bdaddr_t *dst,
 
        conn->dst_type = dst_type;
 
-       conn->state = BT_CONNECT;
        conn->out = true;
        conn->link_mode |= HCI_LM_MASTER;
        conn->sec_level = BT_SECURITY_LOW;
@@ -709,16 +683,19 @@ struct hci_conn *hci_connect_le(struct hci_dev *hdev, bdaddr_t *dst,
        hci_req_init(&req, hdev);
 
        /* If controller is scanning, we stop it since some controllers are
-        * not able to scan and connect at the same time.
+        * not able to scan and connect at the same time. Also set the
+        * HCI_LE_SCAN_INTERRUPTED flag so that the command complete
+        * handler for scan disabling knows to set the correct discovery
+        * state.
         */
        if (test_bit(HCI_LE_SCAN, &hdev->dev_flags)) {
                hci_req_add_le_scan_disable(&req);
-               err = hci_req_run(&req, stop_scan_complete);
-       } else {
-               hci_req_add_le_create_conn(&req, conn);
-               err = hci_req_run(&req, create_le_conn_complete);
+               set_bit(HCI_LE_SCAN_INTERRUPTED, &hdev->dev_flags);
        }
 
+       hci_req_add_le_create_conn(&req, conn);
+
+       err = hci_req_run(&req, create_le_conn_complete);
        if (err) {
                hci_conn_del(conn);
                return ERR_PTR(err);
@@ -804,6 +781,17 @@ int hci_conn_check_link_mode(struct hci_conn *conn)
 {
        BT_DBG("hcon %p", conn);
 
+       /* In Secure Connections Only mode, it is required that Secure
+        * Connections is used and the link is encrypted with AES-CCM
+        * using a P-256 authenticated combination key.
+        */
+       if (test_bit(HCI_SC_ONLY, &conn->hdev->flags)) {
+               if (!hci_conn_sc_enabled(conn) ||
+                   !test_bit(HCI_CONN_AES_CCM, &conn->flags) ||
+                   conn->key_type != HCI_LK_AUTH_COMBINATION_P256)
+                       return 0;
+       }
+
        if (hci_conn_ssp_enabled(conn) && !(conn->link_mode & HCI_LM_ENCRYPT))
                return 0;