Bluetooth: Don't let background scanning interfering with discovery
[pandora-kernel.git] / net / bluetooth / hci_core.c
index 2312e77..4f8ba49 100644 (file)
@@ -35,6 +35,7 @@
 #include <net/bluetooth/bluetooth.h>
 #include <net/bluetooth/hci_core.h>
 #include <net/bluetooth/l2cap.h>
+#include <net/bluetooth/mgmt.h>
 
 #include "smp.h"
 
@@ -1436,13 +1437,6 @@ static void hci_setup_event_mask(struct hci_request *req)
                events[7] |= 0x20;      /* LE Meta-Event */
 
        hci_req_add(req, HCI_OP_SET_EVENT_MASK, sizeof(events), events);
-
-       if (lmp_le_capable(hdev)) {
-               memset(events, 0, sizeof(events));
-               events[0] = 0x1f;
-               hci_req_add(req, HCI_OP_LE_SET_EVENT_MASK,
-                           sizeof(events), events);
-       }
 }
 
 static void hci_init2_req(struct hci_request *req, unsigned long opt)
@@ -1612,8 +1606,25 @@ static void hci_init3_req(struct hci_request *req, unsigned long opt)
        if (hdev->commands[5] & 0x10)
                hci_setup_link_policy(req);
 
-       if (lmp_le_capable(hdev))
+       if (lmp_le_capable(hdev)) {
+               u8 events[8];
+
+               memset(events, 0, sizeof(events));
+               events[0] = 0x1f;
+
+               /* If controller supports the Connection Parameters Request
+                * Link Layer Procedure, enable the corresponding event.
+                */
+               if (hdev->le_features[0] & HCI_LE_CONN_PARAM_REQ_PROC)
+                       events[0] |= 0x20;      /* LE Remote Connection
+                                                * Parameter Request
+                                                */
+
+               hci_req_add(req, HCI_OP_LE_SET_EVENT_MASK, sizeof(events),
+                           events);
+
                hci_set_le_support(req);
+       }
 
        /* Read features beyond page 1 if available */
        for (p = 2; p < HCI_MAX_PAGES && p <= hdev->max_page; p++) {
@@ -1780,6 +1791,38 @@ static int __hci_init(struct hci_dev *hdev)
        return 0;
 }
 
+static void hci_init0_req(struct hci_request *req, unsigned long opt)
+{
+       struct hci_dev *hdev = req->hdev;
+
+       BT_DBG("%s %ld", hdev->name, opt);
+
+       /* Reset */
+       if (!test_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks))
+               hci_reset_req(req, 0);
+
+       /* Read Local Version */
+       hci_req_add(req, HCI_OP_READ_LOCAL_VERSION, 0, NULL);
+
+       /* Read BD Address */
+       if (hdev->set_bdaddr)
+               hci_req_add(req, HCI_OP_READ_BD_ADDR, 0, NULL);
+}
+
+static int __hci_unconf_init(struct hci_dev *hdev)
+{
+       int err;
+
+       if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks))
+               return 0;
+
+       err = __hci_req_sync(hdev, hci_init0_req, 0, HCI_INIT_TIMEOUT);
+       if (err < 0)
+               return err;
+
+       return 0;
+}
+
 static void hci_scan_req(struct hci_request *req, unsigned long opt)
 {
        __u8 scan = opt;
@@ -1860,16 +1903,20 @@ bool hci_discovery_active(struct hci_dev *hdev)
 
 void hci_discovery_set_state(struct hci_dev *hdev, int state)
 {
+       int old_state = hdev->discovery.state;
+
        BT_DBG("%s state %u -> %u", hdev->name, hdev->discovery.state, state);
 
-       if (hdev->discovery.state == state)
+       if (old_state == state)
                return;
 
+       hdev->discovery.state = state;
+
        switch (state) {
        case DISCOVERY_STOPPED:
                hci_update_background_scan(hdev);
 
-               if (hdev->discovery.state != DISCOVERY_STARTING)
+               if (old_state != DISCOVERY_STARTING)
                        mgmt_discovering(hdev, 0);
                break;
        case DISCOVERY_STARTING:
@@ -1882,8 +1929,6 @@ void hci_discovery_set_state(struct hci_dev *hdev, int state)
        case DISCOVERY_STOPPING:
                break;
        }
-
-       hdev->discovery.state = state;
 }
 
 void hci_inquiry_cache_flush(struct hci_dev *hdev)
@@ -1970,22 +2015,24 @@ void hci_inquiry_cache_update_resolve(struct hci_dev *hdev,
        list_add(&ie->list, pos);
 }
 
-bool hci_inquiry_cache_update(struct hci_dev *hdev, struct inquiry_data *data,
-                             bool name_known, bool *ssp)
+u32 hci_inquiry_cache_update(struct hci_dev *hdev, struct inquiry_data *data,
+                            bool name_known)
 {
        struct discovery_state *cache = &hdev->discovery;
        struct inquiry_entry *ie;
+       u32 flags = 0;
 
        BT_DBG("cache %p, %pMR", cache, &data->bdaddr);
 
        hci_remove_remote_oob_data(hdev, &data->bdaddr);
 
-       *ssp = data->ssp_mode;
+       if (!data->ssp_mode)
+               flags |= MGMT_DEV_FOUND_LEGACY_PAIRING;
 
        ie = hci_inquiry_cache_lookup(hdev, &data->bdaddr);
        if (ie) {
-               if (ie->data.ssp_mode)
-                       *ssp = true;
+               if (!ie->data.ssp_mode)
+                       flags |= MGMT_DEV_FOUND_LEGACY_PAIRING;
 
                if (ie->name_state == NAME_NEEDED &&
                    data->rssi != ie->data.rssi) {
@@ -1998,8 +2045,10 @@ bool hci_inquiry_cache_update(struct hci_dev *hdev, struct inquiry_data *data,
 
        /* Entry not in the cache. Add new one. */
        ie = kzalloc(sizeof(struct inquiry_entry), GFP_ATOMIC);
-       if (!ie)
-               return false;
+       if (!ie) {
+               flags |= MGMT_DEV_FOUND_CONFIRM_NAME;
+               goto done;
+       }
 
        list_add(&ie->all, &cache->all);
 
@@ -2022,9 +2071,10 @@ update:
        cache->timestamp = jiffies;
 
        if (ie->name_state == NAME_NOT_KNOWN)
-               return false;
+               flags |= MGMT_DEV_FOUND_CONFIRM_NAME;
 
-       return true;
+done:
+       return flags;
 }
 
 static int inquiry_cache_dump(struct hci_dev *hdev, int num, __u8 *buf)
@@ -2100,7 +2150,7 @@ int hci_inquiry(void __user *arg)
                goto done;
        }
 
-       if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks)) {
+       if (test_bit(HCI_UNCONFIGURED, &hdev->dev_flags)) {
                err = -EOPNOTSUPP;
                goto done;
        }
@@ -2187,7 +2237,8 @@ static int hci_dev_do_open(struct hci_dev *hdev)
                goto done;
        }
 
-       if (!test_bit(HCI_SETUP, &hdev->dev_flags)) {
+       if (!test_bit(HCI_SETUP, &hdev->dev_flags) &&
+           !test_bit(HCI_CONFIG, &hdev->dev_flags)) {
                /* Check for rfkill but allow the HCI setup stage to
                 * proceed (which in itself doesn't cause any RF activity).
                 */
@@ -2230,11 +2281,47 @@ static int hci_dev_do_open(struct hci_dev *hdev)
        atomic_set(&hdev->cmd_cnt, 1);
        set_bit(HCI_INIT, &hdev->flags);
 
-       if (hdev->setup && test_bit(HCI_SETUP, &hdev->dev_flags))
-               ret = hdev->setup(hdev);
+       if (test_bit(HCI_SETUP, &hdev->dev_flags)) {
+               if (hdev->setup)
+                       ret = hdev->setup(hdev);
+
+               /* The transport driver can set these quirks before
+                * creating the HCI device or in its setup callback.
+                *
+                * In case any of them is set, the controller has to
+                * start up as unconfigured.
+                */
+               if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) ||
+                   test_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks))
+                       set_bit(HCI_UNCONFIGURED, &hdev->dev_flags);
+
+               /* For an unconfigured controller it is required to
+                * read at least the version information provided by
+                * the Read Local Version Information command.
+                *
+                * If the set_bdaddr driver callback is provided, then
+                * also the original Bluetooth public device address
+                * will be read using the Read BD Address command.
+                */
+               if (test_bit(HCI_UNCONFIGURED, &hdev->dev_flags))
+                       ret = __hci_unconf_init(hdev);
+       }
+
+       if (test_bit(HCI_CONFIG, &hdev->dev_flags)) {
+               /* If public address change is configured, ensure that
+                * the address gets programmed. If the driver does not
+                * support changing the public address, fail the power
+                * on procedure.
+                */
+               if (bacmp(&hdev->public_addr, BDADDR_ANY) &&
+                   hdev->set_bdaddr)
+                       ret = hdev->set_bdaddr(hdev, &hdev->public_addr);
+               else
+                       ret = -EADDRNOTAVAIL;
+       }
 
        if (!ret) {
-               if (!test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks) &&
+               if (!test_bit(HCI_UNCONFIGURED, &hdev->dev_flags) &&
                    !test_bit(HCI_USER_CHANNEL, &hdev->dev_flags))
                        ret = __hci_init(hdev);
        }
@@ -2247,6 +2334,8 @@ static int hci_dev_do_open(struct hci_dev *hdev)
                set_bit(HCI_UP, &hdev->flags);
                hci_notify(hdev, HCI_DEV_UP);
                if (!test_bit(HCI_SETUP, &hdev->dev_flags) &&
+                   !test_bit(HCI_CONFIG, &hdev->dev_flags) &&
+                   !test_bit(HCI_UNCONFIGURED, &hdev->dev_flags) &&
                    !test_bit(HCI_USER_CHANNEL, &hdev->dev_flags) &&
                    hdev->dev_type == HCI_BREDR) {
                        hci_dev_lock(hdev);
@@ -2290,7 +2379,7 @@ int hci_dev_open(__u16 dev)
        if (!hdev)
                return -ENODEV;
 
-       /* Devices that are marked for raw-only usage can only be powered
+       /* Devices that are marked as unconfigured can only be powered
         * up as user channel. Trying to bring them up as normal devices
         * will result into a failure. Only user channel operation is
         * possible.
@@ -2299,7 +2388,7 @@ int hci_dev_open(__u16 dev)
         * HCI_USER_CHANNEL will be set first before attempting to
         * open the device.
         */
-       if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks) &&
+       if (test_bit(HCI_UNCONFIGURED, &hdev->dev_flags) &&
            !test_bit(HCI_USER_CHANNEL, &hdev->dev_flags)) {
                err = -EOPNOTSUPP;
                goto done;
@@ -2326,6 +2415,17 @@ done:
        return err;
 }
 
+/* This function requires the caller holds hdev->lock */
+static void hci_pend_le_actions_clear(struct hci_dev *hdev)
+{
+       struct hci_conn_params *p;
+
+       list_for_each_entry(p, &hdev->le_conn_params, list)
+               list_del_init(&p->action);
+
+       BT_DBG("All LE pending actions cleared");
+}
+
 static int hci_dev_do_close(struct hci_dev *hdev)
 {
        BT_DBG("%s %p", hdev->name, hdev);
@@ -2363,7 +2463,7 @@ static int hci_dev_do_close(struct hci_dev *hdev)
        hci_dev_lock(hdev);
        hci_inquiry_cache_flush(hdev);
        hci_conn_hash_flush(hdev);
-       hci_pend_le_conns_clear(hdev);
+       hci_pend_le_actions_clear(hdev);
        hci_dev_unlock(hdev);
 
        hci_notify(hdev, HCI_DEV_DOWN);
@@ -2374,8 +2474,8 @@ static int hci_dev_do_close(struct hci_dev *hdev)
        /* Reset device */
        skb_queue_purge(&hdev->cmd_q);
        atomic_set(&hdev->cmd_cnt, 1);
-       if (!test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks) &&
-           !test_bit(HCI_AUTO_OFF, &hdev->dev_flags) &&
+       if (!test_bit(HCI_AUTO_OFF, &hdev->dev_flags) &&
+           !test_bit(HCI_UNCONFIGURED, &hdev->dev_flags) &&
            test_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks)) {
                set_bit(HCI_INIT, &hdev->flags);
                __hci_req_sync(hdev, hci_reset_req, 0, HCI_CMD_TIMEOUT);
@@ -2474,7 +2574,7 @@ int hci_dev_reset(__u16 dev)
                goto done;
        }
 
-       if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks)) {
+       if (test_bit(HCI_UNCONFIGURED, &hdev->dev_flags)) {
                ret = -EOPNOTSUPP;
                goto done;
        }
@@ -2516,7 +2616,7 @@ int hci_dev_reset_stat(__u16 dev)
                goto done;
        }
 
-       if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks)) {
+       if (test_bit(HCI_UNCONFIGURED, &hdev->dev_flags)) {
                ret = -EOPNOTSUPP;
                goto done;
        }
@@ -2546,7 +2646,7 @@ int hci_dev_cmd(unsigned int cmd, void __user *arg)
                goto done;
        }
 
-       if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks)) {
+       if (test_bit(HCI_UNCONFIGURED, &hdev->dev_flags)) {
                err = -EOPNOTSUPP;
                goto done;
        }
@@ -2733,7 +2833,8 @@ static int hci_rfkill_set_block(void *data, bool blocked)
 
        if (blocked) {
                set_bit(HCI_RFKILLED, &hdev->dev_flags);
-               if (!test_bit(HCI_SETUP, &hdev->dev_flags))
+               if (!test_bit(HCI_SETUP, &hdev->dev_flags) &&
+                   !test_bit(HCI_CONFIG, &hdev->dev_flags))
                        hci_dev_do_close(hdev);
        } else {
                clear_bit(HCI_RFKILLED, &hdev->dev_flags);
@@ -2764,6 +2865,7 @@ static void hci_power_on(struct work_struct *work)
         * valid, it is important to turn the device back off.
         */
        if (test_bit(HCI_RFKILLED, &hdev->dev_flags) ||
+           test_bit(HCI_UNCONFIGURED, &hdev->dev_flags) ||
            (hdev->dev_type == HCI_BREDR &&
             !bacmp(&hdev->bdaddr, BDADDR_ANY) &&
             !bacmp(&hdev->static_addr, BDADDR_ANY))) {
@@ -2775,8 +2877,32 @@ static void hci_power_on(struct work_struct *work)
        }
 
        if (test_and_clear_bit(HCI_SETUP, &hdev->dev_flags)) {
-               if (!test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks))
-                       mgmt_index_added(hdev);
+               /* For unconfigured devices, set the HCI_RAW flag
+                * so that userspace can easily identify them.
+                */
+               if (test_bit(HCI_UNCONFIGURED, &hdev->dev_flags))
+                       set_bit(HCI_RAW, &hdev->flags);
+
+               /* For fully configured devices, this will send
+                * the Index Added event. For unconfigured devices,
+                * it will send Unconfigued Index Added event.
+                *
+                * Devices with HCI_QUIRK_RAW_DEVICE are ignored
+                * and no event will be send.
+                */
+               mgmt_index_added(hdev);
+       } else if (test_and_clear_bit(HCI_CONFIG, &hdev->dev_flags)) {
+               /* When the controller is now configured, then it
+                * is important to clear the HCI_RAW flag.
+                */
+               if (!test_bit(HCI_UNCONFIGURED, &hdev->dev_flags))
+                       clear_bit(HCI_RAW, &hdev->flags);
+
+               /* Powering on the controller with HCI_CONFIG set only
+                * happens with the transition from unconfigured to
+                * configured. This will send the Index Added event.
+                */
+               mgmt_index_added(hdev);
        }
 }
 
@@ -3269,7 +3395,7 @@ int hci_blacklist_add(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 type)
 
        list_add(&entry->list, &hdev->blacklist);
 
-       return mgmt_device_blocked(hdev, bdaddr, type);
+       return 0;
 }
 
 int hci_blacklist_del(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 type)
@@ -3288,7 +3414,7 @@ int hci_blacklist_del(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 type)
        list_del(&entry->list);
        kfree(entry);
 
-       return mgmt_device_unblocked(hdev, bdaddr, type);
+       return 0;
 }
 
 struct bdaddr_list *hci_white_list_lookup(struct hci_dev *hdev,
@@ -3358,6 +3484,10 @@ struct hci_conn_params *hci_conn_params_lookup(struct hci_dev *hdev,
 {
        struct hci_conn_params *params;
 
+       /* The conn params list only contains identity addresses */
+       if (!hci_is_identity_address(addr, addr_type))
+               return NULL;
+
        list_for_each_entry(params, &hdev->le_conn_params, list) {
                if (bacmp(&params->addr, addr) == 0 &&
                    params->addr_type == addr_type) {
@@ -3385,114 +3515,49 @@ static bool is_connected(struct hci_dev *hdev, bdaddr_t *addr, u8 type)
        return true;
 }
 
-static bool is_identity_address(bdaddr_t *addr, u8 addr_type)
-{
-       if (addr_type == ADDR_LE_DEV_PUBLIC)
-               return true;
-
-       /* Check for Random Static address type */
-       if ((addr->b[5] & 0xc0) == 0xc0)
-               return true;
-
-       return false;
-}
-
-/* This function requires the caller holds hdev->lock */
-struct bdaddr_list *hci_pend_le_conn_lookup(struct hci_dev *hdev,
-                                           bdaddr_t *addr, u8 addr_type)
-{
-       struct bdaddr_list *entry;
-
-       list_for_each_entry(entry, &hdev->pend_le_conns, list) {
-               if (bacmp(&entry->bdaddr, addr) == 0 &&
-                   entry->bdaddr_type == addr_type)
-                       return entry;
-       }
-
-       return NULL;
-}
-
 /* This function requires the caller holds hdev->lock */
-void hci_pend_le_conn_add(struct hci_dev *hdev, bdaddr_t *addr, u8 addr_type)
+struct hci_conn_params *hci_pend_le_action_lookup(struct list_head *list,
+                                                 bdaddr_t *addr, u8 addr_type)
 {
-       struct bdaddr_list *entry;
-
-       entry = hci_pend_le_conn_lookup(hdev, addr, addr_type);
-       if (entry)
-               goto done;
-
-       entry = kzalloc(sizeof(*entry), GFP_KERNEL);
-       if (!entry) {
-               BT_ERR("Out of memory");
-               return;
-       }
-
-       bacpy(&entry->bdaddr, addr);
-       entry->bdaddr_type = addr_type;
-
-       list_add(&entry->list, &hdev->pend_le_conns);
+       struct hci_conn_params *param;
 
-       BT_DBG("addr %pMR (type %u)", addr, addr_type);
-
-done:
-       hci_update_background_scan(hdev);
-}
-
-/* This function requires the caller holds hdev->lock */
-void hci_pend_le_conn_del(struct hci_dev *hdev, bdaddr_t *addr, u8 addr_type)
-{
-       struct bdaddr_list *entry;
-
-       entry = hci_pend_le_conn_lookup(hdev, addr, addr_type);
-       if (!entry)
-               goto done;
-
-       list_del(&entry->list);
-       kfree(entry);
-
-       BT_DBG("addr %pMR (type %u)", addr, addr_type);
-
-done:
-       hci_update_background_scan(hdev);
-}
-
-/* This function requires the caller holds hdev->lock */
-void hci_pend_le_conns_clear(struct hci_dev *hdev)
-{
-       struct bdaddr_list *entry, *tmp;
+       /* The list only contains identity addresses */
+       if (!hci_is_identity_address(addr, addr_type))
+               return NULL;
 
-       list_for_each_entry_safe(entry, tmp, &hdev->pend_le_conns, list) {
-               list_del(&entry->list);
-               kfree(entry);
+       list_for_each_entry(param, list, action) {
+               if (bacmp(&param->addr, addr) == 0 &&
+                   param->addr_type == addr_type)
+                       return param;
        }
 
-       BT_DBG("All LE pending connections cleared");
-
-       hci_update_background_scan(hdev);
+       return NULL;
 }
 
 /* This function requires the caller holds hdev->lock */
-int hci_conn_params_add(struct hci_dev *hdev, bdaddr_t *addr, u8 addr_type)
+struct hci_conn_params *hci_conn_params_add(struct hci_dev *hdev,
+                                           bdaddr_t *addr, u8 addr_type)
 {
        struct hci_conn_params *params;
 
-       if (!is_identity_address(addr, addr_type))
-               return -EINVAL;
+       if (!hci_is_identity_address(addr, addr_type))
+               return NULL;
 
        params = hci_conn_params_lookup(hdev, addr, addr_type);
        if (params)
-               return 0;
+               return params;
 
        params = kzalloc(sizeof(*params), GFP_KERNEL);
        if (!params) {
                BT_ERR("Out of memory");
-               return -ENOMEM;
+               return NULL;
        }
 
        bacpy(&params->addr, addr);
        params->addr_type = addr_type;
 
        list_add(&params->list, &hdev->le_conn_params);
+       INIT_LIST_HEAD(&params->action);
 
        params->conn_min_interval = hdev->le_conn_min_interval;
        params->conn_max_interval = hdev->le_conn_max_interval;
@@ -3502,55 +3567,45 @@ int hci_conn_params_add(struct hci_dev *hdev, bdaddr_t *addr, u8 addr_type)
 
        BT_DBG("addr %pMR (type %u)", addr, addr_type);
 
-       return 0;
+       return params;
 }
 
 /* This function requires the caller holds hdev->lock */
 int hci_conn_params_set(struct hci_dev *hdev, bdaddr_t *addr, u8 addr_type,
-                       u8 auto_connect, u16 conn_min_interval,
-                       u16 conn_max_interval)
+                       u8 auto_connect)
 {
        struct hci_conn_params *params;
 
-       if (!is_identity_address(addr, addr_type))
-               return -EINVAL;
-
-       params = hci_conn_params_lookup(hdev, addr, addr_type);
-       if (params)
-               goto update;
-
-       params = kzalloc(sizeof(*params), GFP_KERNEL);
-       if (!params) {
-               BT_ERR("Out of memory");
-               return -ENOMEM;
-       }
-
-       bacpy(&params->addr, addr);
-       params->addr_type = addr_type;
+       params = hci_conn_params_add(hdev, addr, addr_type);
+       if (!params)
+               return -EIO;
 
-       list_add(&params->list, &hdev->le_conn_params);
+       if (params->auto_connect == auto_connect)
+               return 0;
 
-update:
-       params->conn_min_interval = conn_min_interval;
-       params->conn_max_interval = conn_max_interval;
-       params->conn_latency = hdev->le_conn_latency;
-       params->supervision_timeout = hdev->le_supv_timeout;
-       params->auto_connect = auto_connect;
+       list_del_init(&params->action);
 
        switch (auto_connect) {
        case HCI_AUTO_CONN_DISABLED:
        case HCI_AUTO_CONN_LINK_LOSS:
-               hci_pend_le_conn_del(hdev, addr, addr_type);
+               hci_update_background_scan(hdev);
+               break;
+       case HCI_AUTO_CONN_REPORT:
+               list_add(&params->action, &hdev->pend_le_reports);
+               hci_update_background_scan(hdev);
                break;
        case HCI_AUTO_CONN_ALWAYS:
-               if (!is_connected(hdev, addr, addr_type))
-                       hci_pend_le_conn_add(hdev, addr, addr_type);
+               if (!is_connected(hdev, addr, addr_type)) {
+                       list_add(&params->action, &hdev->pend_le_conns);
+                       hci_update_background_scan(hdev);
+               }
                break;
        }
 
-       BT_DBG("addr %pMR (type %u) auto_connect %u conn_min_interval 0x%.4x "
-              "conn_max_interval 0x%.4x", addr, addr_type, auto_connect,
-              conn_min_interval, conn_max_interval);
+       params->auto_connect = auto_connect;
+
+       BT_DBG("addr %pMR (type %u) auto_connect %u", addr, addr_type,
+              auto_connect);
 
        return 0;
 }
@@ -3564,25 +3619,42 @@ void hci_conn_params_del(struct hci_dev *hdev, bdaddr_t *addr, u8 addr_type)
        if (!params)
                return;
 
-       hci_pend_le_conn_del(hdev, addr, addr_type);
-
+       list_del(&params->action);
        list_del(&params->list);
        kfree(params);
 
+       hci_update_background_scan(hdev);
+
        BT_DBG("addr %pMR (type %u)", addr, addr_type);
 }
 
 /* This function requires the caller holds hdev->lock */
-void hci_conn_params_clear(struct hci_dev *hdev)
+void hci_conn_params_clear_disabled(struct hci_dev *hdev)
+{
+       struct hci_conn_params *params, *tmp;
+
+       list_for_each_entry_safe(params, tmp, &hdev->le_conn_params, list) {
+               if (params->auto_connect != HCI_AUTO_CONN_DISABLED)
+                       continue;
+               list_del(&params->list);
+               kfree(params);
+       }
+
+       BT_DBG("All LE disabled connection parameters were removed");
+}
+
+/* This function requires the caller holds hdev->lock */
+void hci_conn_params_clear_all(struct hci_dev *hdev)
 {
        struct hci_conn_params *params, *tmp;
 
        list_for_each_entry_safe(params, tmp, &hdev->le_conn_params, list) {
+               list_del(&params->action);
                list_del(&params->list);
                kfree(params);
        }
 
-       hci_pend_le_conns_clear(hdev);
+       hci_update_background_scan(hdev);
 
        BT_DBG("All LE connection parameters were removed");
 }
@@ -3789,6 +3861,7 @@ struct hci_dev *hci_alloc_dev(void)
        hdev->link_mode = (HCI_LM_ACCEPT);
        hdev->num_iac = 0x01;           /* One IAC support is mandatory */
        hdev->io_capability = 0x03;     /* No Input No Output */
+       hdev->manufacturer = 0xffff;    /* Default to internal use */
        hdev->inq_tx_power = HCI_TX_POWER_INVALID;
        hdev->adv_tx_power = HCI_TX_POWER_INVALID;
 
@@ -3821,6 +3894,7 @@ struct hci_dev *hci_alloc_dev(void)
        INIT_LIST_HEAD(&hdev->le_white_list);
        INIT_LIST_HEAD(&hdev->le_conn_params);
        INIT_LIST_HEAD(&hdev->pend_le_conns);
+       INIT_LIST_HEAD(&hdev->pend_le_reports);
        INIT_LIST_HEAD(&hdev->conn_hash.list);
 
        INIT_WORK(&hdev->rx_work, hci_rx_work);
@@ -3860,7 +3934,7 @@ int hci_register_dev(struct hci_dev *hdev)
 {
        int id, error;
 
-       if (!hdev->open || !hdev->close)
+       if (!hdev->open || !hdev->close || !hdev->send)
                return -EINVAL;
 
        /* Do not allow HCI_AMP devices to register at index 0,
@@ -3945,12 +4019,11 @@ int hci_register_dev(struct hci_dev *hdev)
        list_add(&hdev->list, &hci_dev_list);
        write_unlock(&hci_dev_list_lock);
 
-       /* Devices that are marked for raw-only usage need to set
-        * the HCI_RAW flag to indicate that only user channel is
-        * supported.
+       /* Devices that are marked for raw-only usage are unconfigured
+        * and should not be included in normal operation.
         */
        if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks))
-               set_bit(HCI_RAW, &hdev->flags);
+               set_bit(HCI_UNCONFIGURED, &hdev->dev_flags);
 
        hci_notify(hdev, HCI_DEV_REG);
        hci_dev_hold(hdev);
@@ -3995,7 +4068,7 @@ void hci_unregister_dev(struct hci_dev *hdev)
 
        if (!test_bit(HCI_INIT, &hdev->flags) &&
            !test_bit(HCI_SETUP, &hdev->dev_flags) &&
-           !test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks)) {
+           !test_bit(HCI_CONFIG, &hdev->dev_flags)) {
                hci_dev_lock(hdev);
                mgmt_index_removed(hdev);
                hci_dev_unlock(hdev);
@@ -4030,7 +4103,7 @@ void hci_unregister_dev(struct hci_dev *hdev)
        hci_smp_irks_clear(hdev);
        hci_remote_oob_data_clear(hdev);
        hci_white_list_clear(hdev);
-       hci_conn_params_clear(hdev);
+       hci_conn_params_clear_all(hdev);
        hci_dev_unlock(hdev);
 
        hci_dev_put(hdev);
@@ -4268,6 +4341,8 @@ EXPORT_SYMBOL(hci_unregister_cb);
 
 static void hci_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
 {
+       int err;
+
        BT_DBG("%s type %d len %d", hdev->name, bt_cb(skb)->pkt_type, skb->len);
 
        /* Time stamp */
@@ -4284,8 +4359,11 @@ static void hci_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
        /* Get rid of skb owner, prior to sending to the driver. */
        skb_orphan(skb);
 
-       if (hdev->send(hdev, skb) < 0)
-               BT_ERR("%s sending frame failed", hdev->name);
+       err = hdev->send(hdev, skb);
+       if (err < 0) {
+               BT_ERR("%s sending frame failed (%d)", hdev->name, err);
+               kfree_skb(skb);
+       }
 }
 
 void hci_req_init(struct hci_request *req, struct hci_dev *hdev)
@@ -4759,7 +4837,7 @@ static inline int __get_blocks(struct hci_dev *hdev, struct sk_buff *skb)
 
 static void __check_timeout(struct hci_dev *hdev, unsigned int cnt)
 {
-       if (!test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks)) {
+       if (!test_bit(HCI_UNCONFIGURED, &hdev->dev_flags)) {
                /* ACL tx timeout must be longer than maximum
                 * link supervision timeout (40.9 seconds) */
                if (!cnt && time_after(jiffies, hdev->acl_last_tx +
@@ -4942,7 +5020,7 @@ static void hci_sched_le(struct hci_dev *hdev)
        if (!hci_conn_num(hdev, LE_LINK))
                return;
 
-       if (!test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks)) {
+       if (!test_bit(HCI_UNCONFIGURED, &hdev->dev_flags)) {
                /* LE tx timeout must be longer than maximum
                 * link supervision timeout (40.9 seconds) */
                if (!hdev->le_cnt && hdev->le_pkts &&
@@ -5320,14 +5398,22 @@ void hci_update_background_scan(struct hci_dev *hdev)
        if (!test_bit(HCI_UP, &hdev->flags) ||
            test_bit(HCI_INIT, &hdev->flags) ||
            test_bit(HCI_SETUP, &hdev->dev_flags) ||
+           test_bit(HCI_CONFIG, &hdev->dev_flags) ||
+           test_bit(HCI_AUTO_OFF, &hdev->dev_flags) ||
            test_bit(HCI_UNREGISTER, &hdev->dev_flags))
                return;
 
+       /* If discovery is active don't interfere with it */
+       if (hdev->discovery.state != DISCOVERY_STOPPED)
+               return;
+
        hci_req_init(&req, hdev);
 
-       if (list_empty(&hdev->pend_le_conns)) {
-               /* If there is no pending LE connections, we should stop
-                * the background scanning.
+       if (list_empty(&hdev->pend_le_conns) &&
+           list_empty(&hdev->pend_le_reports)) {
+               /* If there is no pending LE connections or devices
+                * to be scanned for, we should stop the background
+                * scanning.
                 */
 
                /* If controller is not scanning we are done. */