Bluetooth: Fix doing some useless casts when receiving MGMT commands
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / net / bluetooth / hci_core.c
index 546a42941477cdcea6fddcb709b6aaac573a4867..45e2d2a72b15cb257bcccd8a93178a9da881c39e 100644 (file)
@@ -55,7 +55,7 @@
 
 #define AUTO_OFF_TIMEOUT 2000
 
-int enable_hs;
+bool enable_hs;
 
 static void hci_rx_work(struct work_struct *work);
 static void hci_cmd_work(struct work_struct *work);
@@ -457,6 +457,25 @@ struct inquiry_entry *hci_inquiry_cache_lookup_resolve(struct hci_dev *hdev,
        return NULL;
 }
 
+void hci_inquiry_cache_update_resolve(struct hci_dev *hdev,
+                                               struct inquiry_entry *ie)
+{
+       struct discovery_state *cache = &hdev->discovery;
+       struct list_head *pos = &cache->resolve;
+       struct inquiry_entry *p;
+
+       list_del(&ie->list);
+
+       list_for_each_entry(p, &cache->resolve, list) {
+               if (p->name_state != NAME_PENDING &&
+                               abs(p->data.rssi) >= abs(ie->data.rssi))
+                       break;
+               pos = &p->list;
+       }
+
+       list_add(&ie->list, pos);
+}
+
 bool hci_inquiry_cache_update(struct hci_dev *hdev, struct inquiry_data *data,
                                                        bool name_known)
 {
@@ -466,8 +485,15 @@ bool hci_inquiry_cache_update(struct hci_dev *hdev, struct inquiry_data *data,
        BT_DBG("cache %p, %s", cache, batostr(&data->bdaddr));
 
        ie = hci_inquiry_cache_lookup(hdev, &data->bdaddr);
-       if (ie)
+       if (ie) {
+               if (ie->name_state == NAME_NEEDED &&
+                                               data->rssi != ie->data.rssi) {
+                       ie->data.rssi = data->rssi;
+                       hci_inquiry_cache_update_resolve(hdev, ie);
+               }
+
                goto update;
+       }
 
        /* Entry not in the cache. Add new one. */
        ie = kzalloc(sizeof(struct inquiry_entry), GFP_ATOMIC);
@@ -668,7 +694,7 @@ int hci_dev_open(__u16 dev)
                hci_dev_hold(hdev);
                set_bit(HCI_UP, &hdev->flags);
                hci_notify(hdev, HCI_DEV_UP);
-               if (!test_bit(HCI_SETUP, &hdev->flags)) {
+               if (!test_bit(HCI_SETUP, &hdev->dev_flags)) {
                        hci_dev_lock(hdev);
                        mgmt_powered(hdev, 1);
                        hci_dev_unlock(hdev);
@@ -722,10 +748,10 @@ static int hci_dev_do_close(struct hci_dev *hdev)
                hdev->discov_timeout = 0;
        }
 
-       if (test_and_clear_bit(HCI_AUTO_OFF, &hdev->flags))
+       if (test_and_clear_bit(HCI_AUTO_OFF, &hdev->dev_flags))
                cancel_delayed_work(&hdev->power_off);
 
-       if (test_and_clear_bit(HCI_SERVICE_CACHE, &hdev->flags))
+       if (test_and_clear_bit(HCI_SERVICE_CACHE, &hdev->dev_flags))
                cancel_delayed_work(&hdev->service_cache);
 
        hci_dev_lock(hdev);
@@ -947,11 +973,11 @@ int hci_get_dev_list(void __user *arg)
 
        read_lock(&hci_dev_list_lock);
        list_for_each_entry(hdev, &hci_dev_list, list) {
-               if (test_and_clear_bit(HCI_AUTO_OFF, &hdev->flags))
+               if (test_and_clear_bit(HCI_AUTO_OFF, &hdev->dev_flags))
                        cancel_delayed_work(&hdev->power_off);
 
-               if (!test_bit(HCI_MGMT, &hdev->flags))
-                       set_bit(HCI_PAIRABLE, &hdev->flags);
+               if (!test_bit(HCI_MGMT, &hdev->dev_flags))
+                       set_bit(HCI_PAIRABLE, &hdev->dev_flags);
 
                (dr + n)->dev_id  = hdev->id;
                (dr + n)->dev_opt = hdev->flags;
@@ -983,11 +1009,11 @@ int hci_get_dev_info(void __user *arg)
        if (!hdev)
                return -ENODEV;
 
-       if (test_and_clear_bit(HCI_AUTO_OFF, &hdev->flags))
+       if (test_and_clear_bit(HCI_AUTO_OFF, &hdev->dev_flags))
                cancel_delayed_work_sync(&hdev->power_off);
 
-       if (!test_bit(HCI_MGMT, &hdev->flags))
-               set_bit(HCI_PAIRABLE, &hdev->flags);
+       if (!test_bit(HCI_MGMT, &hdev->dev_flags))
+               set_bit(HCI_PAIRABLE, &hdev->dev_flags);
 
        strcpy(di.name, hdev->name);
        di.bdaddr   = hdev->bdaddr;
@@ -1067,11 +1093,11 @@ static void hci_power_on(struct work_struct *work)
        if (hci_dev_open(hdev->id) < 0)
                return;
 
-       if (test_bit(HCI_AUTO_OFF, &hdev->flags))
+       if (test_bit(HCI_AUTO_OFF, &hdev->dev_flags))
                schedule_delayed_work(&hdev->power_off,
                                        msecs_to_jiffies(AUTO_OFF_TIMEOUT));
 
-       if (test_and_clear_bit(HCI_SETUP, &hdev->flags))
+       if (test_and_clear_bit(HCI_SETUP, &hdev->dev_flags))
                mgmt_index_added(hdev);
 }
 
@@ -1082,7 +1108,7 @@ static void hci_power_off(struct work_struct *work)
 
        BT_DBG("%s", hdev->name);
 
-       clear_bit(HCI_AUTO_OFF, &hdev->flags);
+       clear_bit(HCI_AUTO_OFF, &hdev->dev_flags);
 
        hci_dev_close(hdev->id);
 }
@@ -1527,7 +1553,7 @@ int hci_add_adv_entry(struct hci_dev *hdev,
        if (hci_find_adv_entry(hdev, &ev->bdaddr))
                return 0;
 
-       entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
+       entry = kzalloc(sizeof(*entry), GFP_KERNEL);
        if (!entry)
                return -ENOMEM;
 
@@ -1548,10 +1574,9 @@ int hci_register_dev(struct hci_dev *hdev)
        struct list_head *head = &hci_dev_list, *p;
        int i, id, error;
 
-       BT_DBG("%p name %s bus %d owner %p", hdev, hdev->name,
-                                               hdev->bus, hdev->owner);
+       BT_DBG("%p name %s bus %d", hdev, hdev->name, hdev->bus);
 
-       if (!hdev->open || !hdev->close || !hdev->destruct)
+       if (!hdev->open || !hdev->close)
                return -EINVAL;
 
        /* Do not allow HCI_AMP devices to register at index 0,
@@ -1572,7 +1597,6 @@ int hci_register_dev(struct hci_dev *hdev)
        hdev->id = id;
        list_add_tail(&hdev->list, head);
 
-       atomic_set(&hdev->refcnt, 1);
        mutex_init(&hdev->lock);
 
        hdev->flags = 0;
@@ -1651,11 +1675,12 @@ int hci_register_dev(struct hci_dev *hdev)
                }
        }
 
-       set_bit(HCI_AUTO_OFF, &hdev->flags);
-       set_bit(HCI_SETUP, &hdev->flags);
+       set_bit(HCI_AUTO_OFF, &hdev->dev_flags);
+       set_bit(HCI_SETUP, &hdev->dev_flags);
        schedule_work(&hdev->power_on);
 
        hci_notify(hdev, HCI_DEV_REG);
+       hci_dev_hold(hdev);
 
        return id;
 
@@ -1687,7 +1712,7 @@ void hci_unregister_dev(struct hci_dev *hdev)
                kfree_skb(hdev->reassembly[i]);
 
        if (!test_bit(HCI_INIT, &hdev->flags) &&
-                                       !test_bit(HCI_SETUP, &hdev->flags)) {
+                               !test_bit(HCI_SETUP, &hdev->dev_flags)) {
                hci_dev_lock(hdev);
                mgmt_index_removed(hdev);
                hci_dev_unlock(hdev);
@@ -1718,7 +1743,7 @@ void hci_unregister_dev(struct hci_dev *hdev)
        hci_adv_entries_clear(hdev);
        hci_dev_unlock(hdev);
 
-       __hci_dev_put(hdev);
+       hci_dev_put(hdev);
 }
 EXPORT_SYMBOL(hci_unregister_dev);
 
@@ -2350,7 +2375,8 @@ static inline void hci_sched_acl(struct hci_dev *hdev)
        if (!test_bit(HCI_RAW, &hdev->flags)) {
                /* ACL tx timeout must be longer than maximum
                 * link supervision timeout (40.9 seconds) */
-               if (!hdev->acl_cnt && time_after(jiffies, hdev->acl_last_tx + HZ * 45))
+               if (!hdev->acl_cnt && time_after(jiffies, hdev->acl_last_tx +
+                                       msecs_to_jiffies(HCI_ACL_TX_TIMEOUT)))
                        hci_link_tx_to(hdev, ACL_LINK);
        }