Merge tag 'docs-for-linus' of git://git.lwn.net/linux-2.6
[firefly-linux-kernel-4.4.55.git] / net / bluetooth / hci_core.c
index 5ef5221c1813fc43804b7d0e9677e792e3884f6c..3322d3f4c85a25eb4bed8dbfaa1802b907b3f1d3 100644 (file)
@@ -141,7 +141,7 @@ static const struct file_operations dut_mode_fops = {
 
 /* ---- HCI requests ---- */
 
-static void hci_req_sync_complete(struct hci_dev *hdev, u8 result)
+static void hci_req_sync_complete(struct hci_dev *hdev, u8 result, u16 opcode)
 {
        BT_DBG("%s result 0x%2.2x", hdev->name, result);
 
@@ -497,43 +497,6 @@ static void le_setup(struct hci_request *req)
                set_bit(HCI_LE_ENABLED, &hdev->dev_flags);
 }
 
-static u8 hci_get_inquiry_mode(struct hci_dev *hdev)
-{
-       if (lmp_ext_inq_capable(hdev))
-               return 0x02;
-
-       if (lmp_inq_rssi_capable(hdev))
-               return 0x01;
-
-       if (hdev->manufacturer == 11 && hdev->hci_rev == 0x00 &&
-           hdev->lmp_subver == 0x0757)
-               return 0x01;
-
-       if (hdev->manufacturer == 15) {
-               if (hdev->hci_rev == 0x03 && hdev->lmp_subver == 0x6963)
-                       return 0x01;
-               if (hdev->hci_rev == 0x09 && hdev->lmp_subver == 0x6963)
-                       return 0x01;
-               if (hdev->hci_rev == 0x00 && hdev->lmp_subver == 0x6965)
-                       return 0x01;
-       }
-
-       if (hdev->manufacturer == 31 && hdev->hci_rev == 0x2005 &&
-           hdev->lmp_subver == 0x1805)
-               return 0x01;
-
-       return 0x00;
-}
-
-static void hci_setup_inquiry_mode(struct hci_request *req)
-{
-       u8 mode;
-
-       mode = hci_get_inquiry_mode(req->hdev);
-
-       hci_req_add(req, HCI_OP_WRITE_INQUIRY_MODE, 1, &mode);
-}
-
 static void hci_setup_event_mask(struct hci_request *req)
 {
        struct hci_dev *hdev = req->hdev;
@@ -646,6 +609,7 @@ static void hci_init2_req(struct hci_request *req, unsigned long opt)
 
                if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) {
                        u8 mode = 0x01;
+
                        hci_req_add(req, HCI_OP_WRITE_SSP_MODE,
                                    sizeof(mode), &mode);
                } else {
@@ -658,8 +622,18 @@ static void hci_init2_req(struct hci_request *req, unsigned long opt)
                }
        }
 
-       if (lmp_inq_rssi_capable(hdev))
-               hci_setup_inquiry_mode(req);
+       if (lmp_inq_rssi_capable(hdev) ||
+           test_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks)) {
+               u8 mode;
+
+               /* If Extended Inquiry Result events are supported, then
+                * they are clearly preferred over Inquiry Result with RSSI
+                * events.
+                */
+               mode = lmp_ext_inq_capable(hdev) ? 0x02 : 0x01;
+
+               hci_req_add(req, HCI_OP_WRITE_INQUIRY_MODE, 1, &mode);
+       }
 
        if (lmp_inq_tx_pwr_capable(hdev))
                hci_req_add(req, HCI_OP_READ_INQ_RSP_TX_POWER, 0, NULL);
@@ -758,27 +732,12 @@ static void hci_init3_req(struct hci_request *req, unsigned long opt)
 
        hci_setup_event_mask(req);
 
-       /* Some Broadcom based Bluetooth controllers do not support the
-        * Delete Stored Link Key command. They are clearly indicating its
-        * absence in the bit mask of supported commands.
-        *
-        * Check the supported commands and only if the the command is marked
-        * as supported send it. If not supported assume that the controller
-        * does not have actual support for stored link keys which makes this
-        * command redundant anyway.
-        *
-        * Some controllers indicate that they support handling deleting
-        * stored link keys, but they don't. The quirk lets a driver
-        * just disable this command.
-        */
-       if (hdev->commands[6] & 0x80 &&
-           !test_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks)) {
-               struct hci_cp_delete_stored_link_key cp;
+       if (hdev->commands[6] & 0x20) {
+               struct hci_cp_read_stored_link_key cp;
 
                bacpy(&cp.bdaddr, BDADDR_ANY);
-               cp.delete_all = 0x01;
-               hci_req_add(req, HCI_OP_DELETE_STORED_LINK_KEY,
-                           sizeof(cp), &cp);
+               cp.read_all = 0x01;
+               hci_req_add(req, HCI_OP_READ_STORED_LINK_KEY, sizeof(cp), &cp);
        }
 
        if (hdev->commands[5] & 0x10)
@@ -872,6 +831,29 @@ static void hci_init4_req(struct hci_request *req, unsigned long opt)
 {
        struct hci_dev *hdev = req->hdev;
 
+       /* Some Broadcom based Bluetooth controllers do not support the
+        * Delete Stored Link Key command. They are clearly indicating its
+        * absence in the bit mask of supported commands.
+        *
+        * Check the supported commands and only if the the command is marked
+        * as supported send it. If not supported assume that the controller
+        * does not have actual support for stored link keys which makes this
+        * command redundant anyway.
+        *
+        * Some controllers indicate that they support handling deleting
+        * stored link keys, but they don't. The quirk lets a driver
+        * just disable this command.
+        */
+       if (hdev->commands[6] & 0x80 &&
+           !test_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks)) {
+               struct hci_cp_delete_stored_link_key cp;
+
+               bacpy(&cp.bdaddr, BDADDR_ANY);
+               cp.delete_all = 0x01;
+               hci_req_add(req, HCI_OP_DELETE_STORED_LINK_KEY,
+                           sizeof(cp), &cp);
+       }
+
        /* Set event mask page 2 if the HCI command for it is supported */
        if (hdev->commands[22] & 0x04)
                hci_set_event_mask_page_2(req);
@@ -889,8 +871,10 @@ static void hci_init4_req(struct hci_request *req, unsigned long opt)
                hci_req_add(req, HCI_OP_READ_SYNC_TRAIN_PARAMS, 0, NULL);
 
        /* Enable Secure Connections if supported and configured */
-       if (bredr_sc_enabled(hdev)) {
+       if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags) &&
+           bredr_sc_enabled(hdev)) {
                u8 support = 0x01;
+
                hci_req_add(req, HCI_OP_WRITE_SC_SUPPORT,
                            sizeof(support), &support);
        }
@@ -931,10 +915,20 @@ static int __hci_init(struct hci_dev *hdev)
        if (err < 0)
                return err;
 
-       /* Only create debugfs entries during the initial setup
-        * phase and not every time the controller gets powered on.
+       /* This function is only called when the controller is actually in
+        * configured state. When the controller is marked as unconfigured,
+        * this initialization procedure is not run.
+        *
+        * It means that it is possible that a controller runs through its
+        * setup phase and then discovers missing settings. If that is the
+        * case, then this function will not be called. It then will only
+        * be called during the config phase.
+        *
+        * So only when in setup phase or config phase, create the debugfs
+        * entries and register the SMP channels.
         */
-       if (!test_bit(HCI_SETUP, &hdev->dev_flags))
+       if (!test_bit(HCI_SETUP, &hdev->dev_flags) &&
+           !test_bit(HCI_CONFIG, &hdev->dev_flags))
                return 0;
 
        hci_debugfs_create_common(hdev);
@@ -942,10 +936,8 @@ static int __hci_init(struct hci_dev *hdev)
        if (lmp_bredr_capable(hdev))
                hci_debugfs_create_bredr(hdev);
 
-       if (lmp_le_capable(hdev)) {
+       if (lmp_le_capable(hdev))
                hci_debugfs_create_le(hdev);
-               smp_register(hdev);
-       }
 
        return 0;
 }
@@ -1625,6 +1617,7 @@ static int hci_dev_do_close(struct hci_dev *hdev)
                cancel_delayed_work(&hdev->service_cache);
 
        cancel_delayed_work_sync(&hdev->le_scan_disable);
+       cancel_delayed_work_sync(&hdev->le_scan_restart);
 
        if (test_bit(HCI_MGMT, &hdev->dev_flags))
                cancel_delayed_work_sync(&hdev->rpa_expired);
@@ -1636,6 +1629,8 @@ static int hci_dev_do_close(struct hci_dev *hdev)
 
        hci_dev_lock(hdev);
 
+       hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
+
        if (!test_and_clear_bit(HCI_AUTO_OFF, &hdev->dev_flags)) {
                if (hdev->dev_type == HCI_BREDR)
                        mgmt_powered(hdev, 0);
@@ -1646,6 +1641,8 @@ static int hci_dev_do_close(struct hci_dev *hdev)
        hci_conn_hash_flush(hdev);
        hci_dev_unlock(hdev);
 
+       smp_unregister(hdev);
+
        hci_notify(hdev, HCI_DEV_DOWN);
 
        if (hdev->flush)
@@ -1725,32 +1722,14 @@ done:
        return err;
 }
 
-int hci_dev_reset(__u16 dev)
+static int hci_dev_do_reset(struct hci_dev *hdev)
 {
-       struct hci_dev *hdev;
-       int ret = 0;
+       int ret;
 
-       hdev = hci_dev_get(dev);
-       if (!hdev)
-               return -ENODEV;
+       BT_DBG("%s %p", hdev->name, hdev);
 
        hci_req_lock(hdev);
 
-       if (!test_bit(HCI_UP, &hdev->flags)) {
-               ret = -ENETDOWN;
-               goto done;
-       }
-
-       if (test_bit(HCI_USER_CHANNEL, &hdev->dev_flags)) {
-               ret = -EBUSY;
-               goto done;
-       }
-
-       if (test_bit(HCI_UNCONFIGURED, &hdev->dev_flags)) {
-               ret = -EOPNOTSUPP;
-               goto done;
-       }
-
        /* Drop queues */
        skb_queue_purge(&hdev->rx_q);
        skb_queue_purge(&hdev->cmd_q);
@@ -1773,12 +1752,41 @@ int hci_dev_reset(__u16 dev)
 
        ret = __hci_req_sync(hdev, hci_reset_req, 0, HCI_INIT_TIMEOUT);
 
-done:
        hci_req_unlock(hdev);
-       hci_dev_put(hdev);
        return ret;
 }
 
+int hci_dev_reset(__u16 dev)
+{
+       struct hci_dev *hdev;
+       int err;
+
+       hdev = hci_dev_get(dev);
+       if (!hdev)
+               return -ENODEV;
+
+       if (!test_bit(HCI_UP, &hdev->flags)) {
+               err = -ENETDOWN;
+               goto done;
+       }
+
+       if (test_bit(HCI_USER_CHANNEL, &hdev->dev_flags)) {
+               err = -EBUSY;
+               goto done;
+       }
+
+       if (test_bit(HCI_UNCONFIGURED, &hdev->dev_flags)) {
+               err = -EOPNOTSUPP;
+               goto done;
+       }
+
+       err = hci_dev_do_reset(hdev);
+
+done:
+       hci_dev_put(hdev);
+       return err;
+}
+
 int hci_dev_reset_stat(__u16 dev)
 {
        struct hci_dev *hdev;
@@ -2144,6 +2152,24 @@ static void hci_power_off(struct work_struct *work)
        hci_dev_do_close(hdev);
 }
 
+static void hci_error_reset(struct work_struct *work)
+{
+       struct hci_dev *hdev = container_of(work, struct hci_dev, error_reset);
+
+       BT_DBG("%s", hdev->name);
+
+       if (hdev->hw_error)
+               hdev->hw_error(hdev, hdev->hw_error_code);
+       else
+               BT_ERR("%s hardware error 0x%2.2x", hdev->name,
+                      hdev->hw_error_code);
+
+       if (hci_dev_do_close(hdev))
+               return;
+
+       hci_dev_do_open(hdev);
+}
+
 static void hci_discov_off(struct work_struct *work)
 {
        struct hci_dev *hdev;
@@ -2556,9 +2582,15 @@ int hci_add_remote_oob_data(struct hci_dev *hdev, bdaddr_t *bdaddr,
        if (hash192 && rand192) {
                memcpy(data->hash192, hash192, sizeof(data->hash192));
                memcpy(data->rand192, rand192, sizeof(data->rand192));
+               if (hash256 && rand256)
+                       data->present = 0x03;
        } else {
                memset(data->hash192, 0, sizeof(data->hash192));
                memset(data->rand192, 0, sizeof(data->rand192));
+               if (hash256 && rand256)
+                       data->present = 0x02;
+               else
+                       data->present = 0x00;
        }
 
        if (hash256 && rand256) {
@@ -2567,6 +2599,8 @@ int hci_add_remote_oob_data(struct hci_dev *hdev, bdaddr_t *bdaddr,
        } else {
                memset(data->hash256, 0, sizeof(data->hash256));
                memset(data->rand256, 0, sizeof(data->rand256));
+               if (hash192 && rand192)
+                       data->present = 0x01;
        }
 
        BT_DBG("%s for %pMR", hdev->name, bdaddr);
@@ -2771,7 +2805,7 @@ void hci_conn_params_clear_all(struct hci_dev *hdev)
        BT_DBG("All LE connection parameters were removed");
 }
 
-static void inquiry_complete(struct hci_dev *hdev, u8 status)
+static void inquiry_complete(struct hci_dev *hdev, u8 status, u16 opcode)
 {
        if (status) {
                BT_ERR("Failed to start inquiry: status %d", status);
@@ -2783,7 +2817,8 @@ static void inquiry_complete(struct hci_dev *hdev, u8 status)
        }
 }
 
-static void le_scan_disable_work_complete(struct hci_dev *hdev, u8 status)
+static void le_scan_disable_work_complete(struct hci_dev *hdev, u8 status,
+                                         u16 opcode)
 {
        /* General inquiry access code (GIAC) */
        u8 lap[3] = { 0x33, 0x8b, 0x9e };
@@ -2796,6 +2831,8 @@ static void le_scan_disable_work_complete(struct hci_dev *hdev, u8 status)
                return;
        }
 
+       hdev->discovery.scan_start = 0;
+
        switch (hdev->discovery.type) {
        case DISCOV_TYPE_LE:
                hci_dev_lock(hdev);
@@ -2835,6 +2872,8 @@ static void le_scan_disable_work(struct work_struct *work)
 
        BT_DBG("%s", hdev->name);
 
+       cancel_delayed_work_sync(&hdev->le_scan_restart);
+
        hci_req_init(&req, hdev);
 
        hci_req_add_le_scan_disable(&req);
@@ -2844,6 +2883,74 @@ static void le_scan_disable_work(struct work_struct *work)
                BT_ERR("Disable LE scanning request failed: err %d", err);
 }
 
+static void le_scan_restart_work_complete(struct hci_dev *hdev, u8 status,
+                                         u16 opcode)
+{
+       unsigned long timeout, duration, scan_start, now;
+
+       BT_DBG("%s", hdev->name);
+
+       if (status) {
+               BT_ERR("Failed to restart LE scan: status %d", status);
+               return;
+       }
+
+       if (!test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks) ||
+           !hdev->discovery.scan_start)
+               return;
+
+       /* When the scan was started, hdev->le_scan_disable has been queued
+        * after duration from scan_start. During scan restart this job
+        * has been canceled, and we need to queue it again after proper
+        * timeout, to make sure that scan does not run indefinitely.
+        */
+       duration = hdev->discovery.scan_duration;
+       scan_start = hdev->discovery.scan_start;
+       now = jiffies;
+       if (now - scan_start <= duration) {
+               int elapsed;
+
+               if (now >= scan_start)
+                       elapsed = now - scan_start;
+               else
+                       elapsed = ULONG_MAX - scan_start + now;
+
+               timeout = duration - elapsed;
+       } else {
+               timeout = 0;
+       }
+       queue_delayed_work(hdev->workqueue,
+                          &hdev->le_scan_disable, timeout);
+}
+
+static void le_scan_restart_work(struct work_struct *work)
+{
+       struct hci_dev *hdev = container_of(work, struct hci_dev,
+                                           le_scan_restart.work);
+       struct hci_request req;
+       struct hci_cp_le_set_scan_enable cp;
+       int err;
+
+       BT_DBG("%s", hdev->name);
+
+       /* If controller is not scanning we are done. */
+       if (!test_bit(HCI_LE_SCAN, &hdev->dev_flags))
+               return;
+
+       hci_req_init(&req, hdev);
+
+       hci_req_add_le_scan_disable(&req);
+
+       memset(&cp, 0, sizeof(cp));
+       cp.enable = LE_SCAN_ENABLE;
+       cp.filter_dup = LE_SCAN_FILTER_DUP_ENABLE;
+       hci_req_add(&req, HCI_OP_LE_SET_SCAN_ENABLE, sizeof(cp), &cp);
+
+       err = hci_req_run(&req, le_scan_restart_work_complete);
+       if (err)
+               BT_ERR("Restart LE scan request failed: err %d", err);
+}
+
 /* Copy the Identity Address of the controller.
  *
  * If the controller has a public BD_ADDR, then by default use that one.
@@ -2935,10 +3042,12 @@ struct hci_dev *hci_alloc_dev(void)
        INIT_WORK(&hdev->cmd_work, hci_cmd_work);
        INIT_WORK(&hdev->tx_work, hci_tx_work);
        INIT_WORK(&hdev->power_on, hci_power_on);
+       INIT_WORK(&hdev->error_reset, hci_error_reset);
 
        INIT_DELAYED_WORK(&hdev->power_off, hci_power_off);
        INIT_DELAYED_WORK(&hdev->discov_off, hci_discov_off);
        INIT_DELAYED_WORK(&hdev->le_scan_disable, le_scan_disable_work);
+       INIT_DELAYED_WORK(&hdev->le_scan_restart, le_scan_restart_work);
 
        skb_queue_head_init(&hdev->rx_q);
        skb_queue_head_init(&hdev->cmd_q);
@@ -3108,8 +3217,6 @@ void hci_unregister_dev(struct hci_dev *hdev)
                rfkill_destroy(hdev->rfkill);
        }
 
-       smp_unregister(hdev);
-
        device_del(&hdev->dev);
 
        debugfs_remove_recursive(hdev->debugfs);
@@ -4176,7 +4283,7 @@ void hci_req_cmd_complete(struct hci_dev *hdev, u16 opcode, u8 status)
 
 call_complete:
        if (req_complete)
-               req_complete(hdev, status);
+               req_complete(hdev, status, status ? opcode : HCI_OP_NOP);
 }
 
 static void hci_rx_work(struct work_struct *work)