Bluetooth: Introduce hci_dev_change_flag helper macro
[firefly-linux-kernel-4.4.55.git] / net / bluetooth / mgmt.c
index 49b8e09ffe6784fbc52aabdc525d0c3d2e792ad6..c3af3b87dbb57a617fb773ebb24a5b5b896448f8 100644 (file)
@@ -37,7 +37,7 @@
 #include "smp.h"
 
 #define MGMT_VERSION   1
-#define MGMT_REVISION  8
+#define MGMT_REVISION  9
 
 static const u16 mgmt_commands[] = {
        MGMT_OP_READ_INDEX_LIST,
@@ -385,7 +385,7 @@ static int read_index_list(struct sock *sk, struct hci_dev *hdev, void *data,
        count = 0;
        list_for_each_entry(d, &hci_dev_list, list) {
                if (d->dev_type == HCI_BREDR &&
-                   !test_bit(HCI_UNCONFIGURED, &d->dev_flags))
+                   !hci_dev_test_flag(d, HCI_UNCONFIGURED))
                        count++;
        }
 
@@ -398,9 +398,9 @@ static int read_index_list(struct sock *sk, struct hci_dev *hdev, void *data,
 
        count = 0;
        list_for_each_entry(d, &hci_dev_list, list) {
-               if (test_bit(HCI_SETUP, &d->dev_flags) ||
-                   test_bit(HCI_CONFIG, &d->dev_flags) ||
-                   test_bit(HCI_USER_CHANNEL, &d->dev_flags))
+               if (hci_dev_test_flag(d, HCI_SETUP) ||
+                   hci_dev_test_flag(d, HCI_CONFIG) ||
+                   hci_dev_test_flag(d, HCI_USER_CHANNEL))
                        continue;
 
                /* Devices marked as raw-only are neither configured
@@ -410,7 +410,7 @@ static int read_index_list(struct sock *sk, struct hci_dev *hdev, void *data,
                        continue;
 
                if (d->dev_type == HCI_BREDR &&
-                   !test_bit(HCI_UNCONFIGURED, &d->dev_flags)) {
+                   !hci_dev_test_flag(d, HCI_UNCONFIGURED)) {
                        rp->index[count++] = cpu_to_le16(d->id);
                        BT_DBG("Added hci%u", d->id);
                }
@@ -445,7 +445,7 @@ static int read_unconf_index_list(struct sock *sk, struct hci_dev *hdev,
        count = 0;
        list_for_each_entry(d, &hci_dev_list, list) {
                if (d->dev_type == HCI_BREDR &&
-                   test_bit(HCI_UNCONFIGURED, &d->dev_flags))
+                   hci_dev_test_flag(d, HCI_UNCONFIGURED))
                        count++;
        }
 
@@ -458,9 +458,9 @@ static int read_unconf_index_list(struct sock *sk, struct hci_dev *hdev,
 
        count = 0;
        list_for_each_entry(d, &hci_dev_list, list) {
-               if (test_bit(HCI_SETUP, &d->dev_flags) ||
-                   test_bit(HCI_CONFIG, &d->dev_flags) ||
-                   test_bit(HCI_USER_CHANNEL, &d->dev_flags))
+               if (hci_dev_test_flag(d, HCI_SETUP) ||
+                   hci_dev_test_flag(d, HCI_CONFIG) ||
+                   hci_dev_test_flag(d, HCI_USER_CHANNEL))
                        continue;
 
                /* Devices marked as raw-only are neither configured
@@ -470,7 +470,7 @@ static int read_unconf_index_list(struct sock *sk, struct hci_dev *hdev,
                        continue;
 
                if (d->dev_type == HCI_BREDR &&
-                   test_bit(HCI_UNCONFIGURED, &d->dev_flags)) {
+                   hci_dev_test_flag(d, HCI_UNCONFIGURED)) {
                        rp->index[count++] = cpu_to_le16(d->id);
                        BT_DBG("Added hci%u", d->id);
                }
@@ -492,7 +492,7 @@ static int read_unconf_index_list(struct sock *sk, struct hci_dev *hdev,
 static bool is_configured(struct hci_dev *hdev)
 {
        if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) &&
-           !test_bit(HCI_EXT_CONFIGURED, &hdev->dev_flags))
+           !hci_dev_test_flag(hdev, HCI_EXT_CONFIGURED))
                return false;
 
        if (test_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks) &&
@@ -507,7 +507,7 @@ static __le32 get_missing_options(struct hci_dev *hdev)
        u32 options = 0;
 
        if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) &&
-           !test_bit(HCI_EXT_CONFIGURED, &hdev->dev_flags))
+           !hci_dev_test_flag(hdev, HCI_EXT_CONFIGURED))
                options |= MGMT_OPTION_EXTERNAL_CONFIG;
 
        if (test_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks) &&
@@ -608,43 +608,43 @@ static u32 get_current_settings(struct hci_dev *hdev)
        if (hdev_is_powered(hdev))
                settings |= MGMT_SETTING_POWERED;
 
-       if (test_bit(HCI_CONNECTABLE, &hdev->dev_flags))
+       if (hci_dev_test_flag(hdev, HCI_CONNECTABLE))
                settings |= MGMT_SETTING_CONNECTABLE;
 
-       if (test_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags))
+       if (hci_dev_test_flag(hdev, HCI_FAST_CONNECTABLE))
                settings |= MGMT_SETTING_FAST_CONNECTABLE;
 
-       if (test_bit(HCI_DISCOVERABLE, &hdev->dev_flags))
+       if (hci_dev_test_flag(hdev, HCI_DISCOVERABLE))
                settings |= MGMT_SETTING_DISCOVERABLE;
 
-       if (test_bit(HCI_BONDABLE, &hdev->dev_flags))
+       if (hci_dev_test_flag(hdev, HCI_BONDABLE))
                settings |= MGMT_SETTING_BONDABLE;
 
-       if (test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags))
+       if (hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
                settings |= MGMT_SETTING_BREDR;
 
-       if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags))
+       if (hci_dev_test_flag(hdev, HCI_LE_ENABLED))
                settings |= MGMT_SETTING_LE;
 
-       if (test_bit(HCI_LINK_SECURITY, &hdev->dev_flags))
+       if (hci_dev_test_flag(hdev, HCI_LINK_SECURITY))
                settings |= MGMT_SETTING_LINK_SECURITY;
 
-       if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags))
+       if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED))
                settings |= MGMT_SETTING_SSP;
 
-       if (test_bit(HCI_HS_ENABLED, &hdev->dev_flags))
+       if (hci_dev_test_flag(hdev, HCI_HS_ENABLED))
                settings |= MGMT_SETTING_HS;
 
-       if (test_bit(HCI_ADVERTISING, &hdev->dev_flags))
+       if (hci_dev_test_flag(hdev, HCI_ADVERTISING))
                settings |= MGMT_SETTING_ADVERTISING;
 
-       if (test_bit(HCI_SC_ENABLED, &hdev->dev_flags))
+       if (hci_dev_test_flag(hdev, HCI_SC_ENABLED))
                settings |= MGMT_SETTING_SECURE_CONN;
 
-       if (test_bit(HCI_KEEP_DEBUG_KEYS, &hdev->dev_flags))
+       if (hci_dev_test_flag(hdev, HCI_KEEP_DEBUG_KEYS))
                settings |= MGMT_SETTING_DEBUG_KEYS;
 
-       if (test_bit(HCI_PRIVACY, &hdev->dev_flags))
+       if (hci_dev_test_flag(hdev, HCI_PRIVACY))
                settings |= MGMT_SETTING_PRIVACY;
 
        /* The current setting for static address has two purposes. The
@@ -660,7 +660,7 @@ static u32 get_current_settings(struct hci_dev *hdev)
         * be evaluated.
         */
        if (test_bit(HCI_FORCE_STATIC_ADDR, &hdev->dbg_flags) ||
-           !test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags) ||
+           !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) ||
            !bacmp(&hdev->bdaddr, BDADDR_ANY)) {
                if (bacmp(&hdev->static_addr, BDADDR_ANY))
                        settings |= MGMT_SETTING_STATIC_ADDRESS;
@@ -840,7 +840,7 @@ static void update_scan_rsp_data(struct hci_request *req)
        struct hci_cp_le_set_scan_rsp_data cp;
        u8 len;
 
-       if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags))
+       if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
                return;
 
        memset(&cp, 0, sizeof(cp));
@@ -874,9 +874,9 @@ static u8 get_adv_discov_flags(struct hci_dev *hdev)
                else if (cp->val == 0x02)
                        return LE_AD_LIMITED;
        } else {
-               if (test_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags))
+               if (hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE))
                        return LE_AD_LIMITED;
-               else if (test_bit(HCI_DISCOVERABLE, &hdev->dev_flags))
+               else if (hci_dev_test_flag(hdev, HCI_DISCOVERABLE))
                        return LE_AD_GENERAL;
        }
 
@@ -889,7 +889,7 @@ static u8 create_adv_data(struct hci_dev *hdev, u8 *ptr)
 
        flags |= get_adv_discov_flags(hdev);
 
-       if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags))
+       if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
                flags |= LE_AD_NO_BREDR;
 
        if (flags) {
@@ -921,7 +921,7 @@ static void update_adv_data(struct hci_request *req)
        struct hci_cp_le_set_adv_data cp;
        u8 len;
 
-       if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags))
+       if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
                return;
 
        memset(&cp, 0, sizeof(cp));
@@ -1009,10 +1009,10 @@ static void update_eir(struct hci_request *req)
        if (!lmp_ext_inq_capable(hdev))
                return;
 
-       if (!test_bit(HCI_SSP_ENABLED, &hdev->dev_flags))
+       if (!hci_dev_test_flag(hdev, HCI_SSP_ENABLED))
                return;
 
-       if (test_bit(HCI_SERVICE_CACHE, &hdev->dev_flags))
+       if (hci_dev_test_flag(hdev, HCI_SERVICE_CACHE))
                return;
 
        memset(&cp, 0, sizeof(cp));
@@ -1048,17 +1048,17 @@ static void update_class(struct hci_request *req)
        if (!hdev_is_powered(hdev))
                return;
 
-       if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags))
+       if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
                return;
 
-       if (test_bit(HCI_SERVICE_CACHE, &hdev->dev_flags))
+       if (hci_dev_test_flag(hdev, HCI_SERVICE_CACHE))
                return;
 
        cod[0] = hdev->minor_class;
        cod[1] = hdev->major_class;
        cod[2] = get_service_classes(hdev);
 
-       if (test_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags))
+       if (hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE))
                cod[1] |= 0x20;
 
        if (memcmp(cod, hdev->dev_class, 3) == 0)
@@ -1080,7 +1080,7 @@ static bool get_connectable(struct hci_dev *hdev)
                return cp->val;
        }
 
-       return test_bit(HCI_CONNECTABLE, &hdev->dev_flags);
+       return hci_dev_test_flag(hdev, HCI_CONNECTABLE);
 }
 
 static void disable_advertising(struct hci_request *req)
@@ -1100,7 +1100,7 @@ static void enable_advertising(struct hci_request *req)
        if (hci_conn_num(hdev, LE_LINK) > 0)
                return;
 
-       if (test_bit(HCI_LE_ADV, &hdev->dev_flags))
+       if (hci_dev_test_flag(hdev, HCI_LE_ADV))
                disable_advertising(req);
 
        /* Clear the HCI_LE_ADV bit temporarily so that the
@@ -1108,9 +1108,12 @@ static void enable_advertising(struct hci_request *req)
         * and write a new random address. The flag will be set back on
         * as soon as the SET_ADV_ENABLE HCI command completes.
         */
-       clear_bit(HCI_LE_ADV, &hdev->dev_flags);
+       hci_dev_clear_flag(hdev, HCI_LE_ADV);
 
-       connectable = get_connectable(hdev);
+       if (hci_dev_test_flag(hdev, HCI_ADVERTISING_CONNECTABLE))
+               connectable = true;
+       else
+               connectable = get_connectable(hdev);
 
        /* Set require_privacy to true only when non-connectable
         * advertising is used. In that case it is fine to use a
@@ -1160,9 +1163,9 @@ static void rpa_expired(struct work_struct *work)
 
        BT_DBG("");
 
-       set_bit(HCI_RPA_EXPIRED, &hdev->dev_flags);
+       hci_dev_set_flag(hdev, HCI_RPA_EXPIRED);
 
-       if (!test_bit(HCI_ADVERTISING, &hdev->dev_flags))
+       if (!hci_dev_test_flag(hdev, HCI_ADVERTISING))
                return;
 
        /* The generation of a new RPA and programming it into the
@@ -1186,7 +1189,7 @@ static void mgmt_init_hdev(struct sock *sk, struct hci_dev *hdev)
         * for mgmt we require user-space to explicitly enable
         * it
         */
-       clear_bit(HCI_BONDABLE, &hdev->dev_flags);
+       hci_dev_clear_flag(hdev, HCI_BONDABLE);
 }
 
 static int read_controller_info(struct sock *sk, struct hci_dev *hdev,
@@ -1325,7 +1328,7 @@ static bool hci_stop_discovery(struct hci_request *req)
 
        default:
                /* Passive scanning */
-               if (test_bit(HCI_LE_SCAN, &hdev->dev_flags)) {
+               if (hci_dev_test_flag(hdev, HCI_LE_SCAN)) {
                        hci_req_add_le_scan_disable(req);
                        return true;
                }
@@ -1351,7 +1354,7 @@ static int clean_up_hci_state(struct hci_dev *hdev)
                hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan);
        }
 
-       if (test_bit(HCI_LE_ADV, &hdev->dev_flags))
+       if (hci_dev_test_flag(hdev, HCI_LE_ADV))
                disable_advertising(&req);
 
        discov_stopped = hci_stop_discovery(&req);
@@ -1535,7 +1538,7 @@ static u8 mgmt_bredr_support(struct hci_dev *hdev)
 {
        if (!lmp_bredr_capable(hdev))
                return MGMT_STATUS_NOT_SUPPORTED;
-       else if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags))
+       else if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
                return MGMT_STATUS_REJECTED;
        else
                return MGMT_STATUS_SUCCESS;
@@ -1545,7 +1548,7 @@ static u8 mgmt_le_support(struct hci_dev *hdev)
 {
        if (!lmp_le_capable(hdev))
                return MGMT_STATUS_NOT_SUPPORTED;
-       else if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags))
+       else if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
                return MGMT_STATUS_REJECTED;
        else
                return MGMT_STATUS_SUCCESS;
@@ -1570,7 +1573,7 @@ static void set_discoverable_complete(struct hci_dev *hdev, u8 status,
        if (status) {
                u8 mgmt_err = mgmt_status(status);
                mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err);
-               clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags);
+               hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE);
                goto remove_cmd;
        }
 
@@ -1623,8 +1626,8 @@ static int set_discoverable(struct sock *sk, struct hci_dev *hdev, void *data,
 
        BT_DBG("request for %s", hdev->name);
 
-       if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags) &&
-           !test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags))
+       if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED) &&
+           !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
                return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
                                       MGMT_STATUS_REJECTED);
 
@@ -1657,7 +1660,7 @@ static int set_discoverable(struct sock *sk, struct hci_dev *hdev, void *data,
                goto failed;
        }
 
-       if (!test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) {
+       if (!hci_dev_test_flag(hdev, HCI_CONNECTABLE)) {
                err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
                                      MGMT_STATUS_REJECTED);
                goto failed;
@@ -1670,8 +1673,8 @@ static int set_discoverable(struct sock *sk, struct hci_dev *hdev, void *data,
                 * not a valid operation since it requires a timeout
                 * and so no need to check HCI_LIMITED_DISCOVERABLE.
                 */
-               if (!!cp->val != test_bit(HCI_DISCOVERABLE, &hdev->dev_flags)) {
-                       change_bit(HCI_DISCOVERABLE, &hdev->dev_flags);
+               if (!!cp->val != hci_dev_test_flag(hdev, HCI_DISCOVERABLE)) {
+                       hci_dev_change_flag(hdev, HCI_DISCOVERABLE);
                        changed = true;
                }
 
@@ -1689,9 +1692,9 @@ static int set_discoverable(struct sock *sk, struct hci_dev *hdev, void *data,
         * value with the new value. And if only the timeout gets updated,
         * then no need for any HCI transactions.
         */
-       if (!!cp->val == test_bit(HCI_DISCOVERABLE, &hdev->dev_flags) &&
-           (cp->val == 0x02) == test_bit(HCI_LIMITED_DISCOVERABLE,
-                                         &hdev->dev_flags)) {
+       if (!!cp->val == hci_dev_test_flag(hdev, HCI_DISCOVERABLE) &&
+           (cp->val == 0x02) == hci_dev_test_flag(hdev,
+                                                  HCI_LIMITED_DISCOVERABLE)) {
                cancel_delayed_work(&hdev->discov_off);
                hdev->discov_timeout = timeout;
 
@@ -1720,16 +1723,16 @@ static int set_discoverable(struct sock *sk, struct hci_dev *hdev, void *data,
 
        /* Limited discoverable mode */
        if (cp->val == 0x02)
-               set_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags);
+               hci_dev_set_flag(hdev, HCI_LIMITED_DISCOVERABLE);
        else
-               clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags);
+               hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE);
 
        hci_req_init(&req, hdev);
 
        /* The procedure for LE-only controllers is much simpler - just
         * update the advertising data.
         */
-       if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags))
+       if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
                goto update_ad;
 
        scan = SCAN_PAGE;
@@ -1759,7 +1762,7 @@ static int set_discoverable(struct sock *sk, struct hci_dev *hdev, void *data,
 
                scan |= SCAN_INQUIRY;
        } else {
-               clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags);
+               hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE);
        }
 
        hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, sizeof(scan), &scan);
@@ -1782,7 +1785,7 @@ static void write_fast_connectable(struct hci_request *req, bool enable)
        struct hci_cp_write_page_scan_activity acp;
        u8 type;
 
-       if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags))
+       if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
                return;
 
        if (hdev->hci_ver < BLUETOOTH_VER_1_2)
@@ -1867,14 +1870,14 @@ static int set_connectable_update_settings(struct hci_dev *hdev,
        bool changed = false;
        int err;
 
-       if (!!val != test_bit(HCI_CONNECTABLE, &hdev->dev_flags))
+       if (!!val != hci_dev_test_flag(hdev, HCI_CONNECTABLE))
                changed = true;
 
        if (val) {
-               set_bit(HCI_CONNECTABLE, &hdev->dev_flags);
+               hci_dev_set_flag(hdev, HCI_CONNECTABLE);
        } else {
-               clear_bit(HCI_CONNECTABLE, &hdev->dev_flags);
-               clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags);
+               hci_dev_clear_flag(hdev, HCI_CONNECTABLE);
+               hci_dev_clear_flag(hdev, HCI_DISCOVERABLE);
        }
 
        err = send_settings_rsp(sk, MGMT_OP_SET_CONNECTABLE, hdev);
@@ -1901,8 +1904,8 @@ static int set_connectable(struct sock *sk, struct hci_dev *hdev, void *data,
 
        BT_DBG("request for %s", hdev->name);
 
-       if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags) &&
-           !test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags))
+       if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED) &&
+           !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
                return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE,
                                       MGMT_STATUS_REJECTED);
 
@@ -1936,10 +1939,10 @@ static int set_connectable(struct sock *sk, struct hci_dev *hdev, void *data,
         * by-product of disabling connectable, we need to update the
         * advertising flags.
         */
-       if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) {
+       if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) {
                if (!cp->val) {
-                       clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags);
-                       clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags);
+                       hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE);
+                       hci_dev_clear_flag(hdev, HCI_DISCOVERABLE);
                }
                update_adv_data(&req);
        } else if (cp->val != test_bit(HCI_PSCAN, &hdev->flags)) {
@@ -1969,7 +1972,7 @@ static int set_connectable(struct sock *sk, struct hci_dev *hdev, void *data,
 
 no_scan_update:
        /* Update the advertising parameters if necessary */
-       if (test_bit(HCI_ADVERTISING, &hdev->dev_flags))
+       if (hci_dev_test_flag(hdev, HCI_ADVERTISING))
                enable_advertising(&req);
 
        err = hci_req_run(&req, set_connectable_complete);
@@ -2042,9 +2045,8 @@ static int set_link_security(struct sock *sk, struct hci_dev *hdev, void *data,
        if (!hdev_is_powered(hdev)) {
                bool changed = false;
 
-               if (!!cp->val != test_bit(HCI_LINK_SECURITY,
-                                         &hdev->dev_flags)) {
-                       change_bit(HCI_LINK_SECURITY, &hdev->dev_flags);
+               if (!!cp->val != hci_dev_test_flag(hdev, HCI_LINK_SECURITY)) {
+                       hci_dev_change_flag(hdev, HCI_LINK_SECURITY);
                        changed = true;
                }
 
@@ -2124,7 +2126,7 @@ static int set_ssp(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
                                changed = test_and_clear_bit(HCI_HS_ENABLED,
                                                             &hdev->dev_flags);
                        else
-                               clear_bit(HCI_HS_ENABLED, &hdev->dev_flags);
+                               hci_dev_clear_flag(hdev, HCI_HS_ENABLED);
                }
 
                err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev);
@@ -2143,7 +2145,7 @@ static int set_ssp(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
                goto failed;
        }
 
-       if (!!cp->val == test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) {
+       if (!!cp->val == hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) {
                err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev);
                goto failed;
        }
@@ -2154,7 +2156,7 @@ static int set_ssp(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
                goto failed;
        }
 
-       if (!cp->val && test_bit(HCI_USE_DEBUG_KEYS, &hdev->dev_flags))
+       if (!cp->val && hci_dev_test_flag(hdev, HCI_USE_DEBUG_KEYS))
                hci_send_cmd(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE,
                             sizeof(cp->val), &cp->val);
 
@@ -2186,7 +2188,7 @@ static int set_hs(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
                return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS,
                                       MGMT_STATUS_NOT_SUPPORTED);
 
-       if (!test_bit(HCI_SSP_ENABLED, &hdev->dev_flags))
+       if (!hci_dev_test_flag(hdev, HCI_SSP_ENABLED))
                return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS,
                                       MGMT_STATUS_REJECTED);
 
@@ -2252,7 +2254,7 @@ static void le_enable_complete(struct hci_dev *hdev, u8 status, u16 opcode)
         * has actually been enabled. During power on, the
         * update in powered_update_hci will take care of it.
         */
-       if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) {
+       if (hci_dev_test_flag(hdev, HCI_LE_ENABLED)) {
                struct hci_request req;
 
                hci_req_init(&req, hdev);
@@ -2286,7 +2288,7 @@ static int set_le(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
                                       MGMT_STATUS_INVALID_PARAMS);
 
        /* LE-only devices do not allow toggling LE on/off */
-       if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags))
+       if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
                return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE,
                                       MGMT_STATUS_REJECTED);
 
@@ -2298,13 +2300,13 @@ static int set_le(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
        if (!hdev_is_powered(hdev) || val == enabled) {
                bool changed = false;
 
-               if (val != test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) {
-                       change_bit(HCI_LE_ENABLED, &hdev->dev_flags);
+               if (val != hci_dev_test_flag(hdev, HCI_LE_ENABLED)) {
+                       hci_dev_change_flag(hdev, HCI_LE_ENABLED);
                        changed = true;
                }
 
-               if (!val && test_bit(HCI_ADVERTISING, &hdev->dev_flags)) {
-                       clear_bit(HCI_ADVERTISING, &hdev->dev_flags);
+               if (!val && hci_dev_test_flag(hdev, HCI_ADVERTISING)) {
+                       hci_dev_clear_flag(hdev, HCI_ADVERTISING);
                        changed = true;
                }
 
@@ -2339,7 +2341,7 @@ static int set_le(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
                hci_cp.le = val;
                hci_cp.simul = 0x00;
        } else {
-               if (test_bit(HCI_LE_ADV, &hdev->dev_flags))
+               if (hci_dev_test_flag(hdev, HCI_LE_ADV))
                        disable_advertising(&req);
        }
 
@@ -3245,6 +3247,13 @@ static int pair_device(struct sock *sk, struct hci_dev *hdev, void *data,
                goto unlock;
        }
 
+       if (hci_bdaddr_is_paired(hdev, &cp->addr.bdaddr, cp->addr.type)) {
+               err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE,
+                                       MGMT_STATUS_ALREADY_PAIRED, &rp,
+                                       sizeof(rp));
+               goto unlock;
+       }
+
        sec_level = BT_SECURITY_MEDIUM;
        auth_type = HCI_AT_DEDICATED_BONDING;
 
@@ -3850,12 +3859,12 @@ static bool trigger_discovery(struct hci_request *req, u8 *status)
                        return false;
 
                if (hdev->discovery.type == DISCOV_TYPE_INTERLEAVED &&
-                   !test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) {
+                   !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) {
                        *status = MGMT_STATUS_NOT_SUPPORTED;
                        return false;
                }
 
-               if (test_bit(HCI_LE_ADV, &hdev->dev_flags)) {
+               if (hci_dev_test_flag(hdev, HCI_LE_ADV)) {
                        /* Don't let discovery abort an outgoing
                         * connection attempt that's using directed
                         * advertising.
@@ -3873,7 +3882,7 @@ static bool trigger_discovery(struct hci_request *req, u8 *status)
                 * is running. Thus, we should temporarily stop it in order to
                 * set the discovery scanning parameters.
                 */
-               if (test_bit(HCI_LE_SCAN, &hdev->dev_flags))
+               if (hci_dev_test_flag(hdev, HCI_LE_SCAN))
                        hci_req_add_le_scan_disable(req);
 
                memset(&param_cp, 0, sizeof(param_cp));
@@ -3997,7 +4006,7 @@ static int start_discovery(struct sock *sk, struct hci_dev *hdev,
        }
 
        if (hdev->discovery.state != DISCOVERY_STOPPED ||
-           test_bit(HCI_PERIODIC_INQ, &hdev->dev_flags)) {
+           hci_dev_test_flag(hdev, HCI_PERIODIC_INQ)) {
                err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_START_DISCOVERY,
                                        MGMT_STATUS_BUSY, &cp->type,
                                        sizeof(cp->type));
@@ -4073,7 +4082,7 @@ static int start_service_discovery(struct sock *sk, struct hci_dev *hdev,
        }
 
        if (hdev->discovery.state != DISCOVERY_STOPPED ||
-           test_bit(HCI_PERIODIC_INQ, &hdev->dev_flags)) {
+           hci_dev_test_flag(hdev, HCI_PERIODIC_INQ)) {
                err = mgmt_cmd_complete(sk, hdev->id,
                                        MGMT_OP_START_SERVICE_DISCOVERY,
                                        MGMT_STATUS_BUSY, &cp->type,
@@ -4400,10 +4409,10 @@ static void set_advertising_complete(struct hci_dev *hdev, u8 status,
                goto unlock;
        }
 
-       if (test_bit(HCI_LE_ADV, &hdev->dev_flags))
-               set_bit(HCI_ADVERTISING, &hdev->dev_flags);
+       if (hci_dev_test_flag(hdev, HCI_LE_ADV))
+               hci_dev_set_flag(hdev, HCI_ADVERTISING);
        else
-               clear_bit(HCI_ADVERTISING, &hdev->dev_flags);
+               hci_dev_clear_flag(hdev, HCI_ADVERTISING);
 
        mgmt_pending_foreach(MGMT_OP_SET_ADVERTISING, hdev, settings_rsp,
                             &match);
@@ -4423,7 +4432,7 @@ static int set_advertising(struct sock *sk, struct hci_dev *hdev, void *data,
        struct mgmt_mode *cp = data;
        struct mgmt_pending_cmd *cmd;
        struct hci_request req;
-       u8 val, enabled, status;
+       u8 val, status;
        int err;
 
        BT_DBG("request for %s", hdev->name);
@@ -4433,29 +4442,38 @@ static int set_advertising(struct sock *sk, struct hci_dev *hdev, void *data,
                return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING,
                                       status);
 
-       if (cp->val != 0x00 && cp->val != 0x01)
+       if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02)
                return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING,
                                       MGMT_STATUS_INVALID_PARAMS);
 
        hci_dev_lock(hdev);
 
        val = !!cp->val;
-       enabled = test_bit(HCI_ADVERTISING, &hdev->dev_flags);
 
        /* The following conditions are ones which mean that we should
         * not do any HCI communication but directly send a mgmt
         * response to user space (after toggling the flag if
         * necessary).
         */
-       if (!hdev_is_powered(hdev) || val == enabled ||
+       if (!hdev_is_powered(hdev) ||
+           (val == hci_dev_test_flag(hdev, HCI_ADVERTISING) &&
+            (cp->val == 0x02) == hci_dev_test_flag(hdev, HCI_ADVERTISING_CONNECTABLE)) ||
            hci_conn_num(hdev, LE_LINK) > 0 ||
-           (test_bit(HCI_LE_SCAN, &hdev->dev_flags) &&
+           (hci_dev_test_flag(hdev, HCI_LE_SCAN) &&
             hdev->le_scan_type == LE_SCAN_ACTIVE)) {
-               bool changed = false;
+               bool changed;
 
-               if (val != test_bit(HCI_ADVERTISING, &hdev->dev_flags)) {
-                       change_bit(HCI_ADVERTISING, &hdev->dev_flags);
-                       changed = true;
+               if (cp->val) {
+                       changed = !test_and_set_bit(HCI_ADVERTISING,
+                                                   &hdev->dev_flags);
+                       if (cp->val == 0x02)
+                               hci_dev_set_flag(hdev, HCI_ADVERTISING_CONNECTABLE);
+                       else
+                               hci_dev_clear_flag(hdev, HCI_ADVERTISING_CONNECTABLE);
+               } else {
+                       changed = test_and_clear_bit(HCI_ADVERTISING,
+                                                    &hdev->dev_flags);
+                       hci_dev_clear_flag(hdev, HCI_ADVERTISING_CONNECTABLE);
                }
 
                err = send_settings_rsp(sk, MGMT_OP_SET_ADVERTISING, hdev);
@@ -4483,6 +4501,11 @@ static int set_advertising(struct sock *sk, struct hci_dev *hdev, void *data,
 
        hci_req_init(&req, hdev);
 
+       if (cp->val == 0x02)
+               hci_dev_set_flag(hdev, HCI_ADVERTISING_CONNECTABLE);
+       else
+               hci_dev_clear_flag(hdev, HCI_ADVERTISING_CONNECTABLE);
+
        if (val)
                enable_advertising(&req);
        else
@@ -4581,7 +4604,7 @@ static int set_scan_params(struct sock *sk, struct hci_dev *hdev,
        /* If background scan is running, restart it so new parameters are
         * loaded.
         */
-       if (test_bit(HCI_LE_SCAN, &hdev->dev_flags) &&
+       if (hci_dev_test_flag(hdev, HCI_LE_SCAN) &&
            hdev->discovery.state == DISCOVERY_STOPPED) {
                struct hci_request req;
 
@@ -4618,9 +4641,9 @@ static void fast_connectable_complete(struct hci_dev *hdev, u8 status,
                struct mgmt_mode *cp = cmd->param;
 
                if (cp->val)
-                       set_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags);
+                       hci_dev_set_flag(hdev, HCI_FAST_CONNECTABLE);
                else
-                       clear_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags);
+                       hci_dev_clear_flag(hdev, HCI_FAST_CONNECTABLE);
 
                send_settings_rsp(cmd->sk, MGMT_OP_SET_FAST_CONNECTABLE, hdev);
                new_settings(hdev, cmd->sk);
@@ -4642,7 +4665,7 @@ static int set_fast_connectable(struct sock *sk, struct hci_dev *hdev,
 
        BT_DBG("%s", hdev->name);
 
-       if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags) ||
+       if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) ||
            hdev->hci_ver < BLUETOOTH_VER_1_2)
                return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE,
                                       MGMT_STATUS_NOT_SUPPORTED);
@@ -4659,14 +4682,14 @@ static int set_fast_connectable(struct sock *sk, struct hci_dev *hdev,
                goto unlock;
        }
 
-       if (!!cp->val == test_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags)) {
+       if (!!cp->val == hci_dev_test_flag(hdev, HCI_FAST_CONNECTABLE)) {
                err = send_settings_rsp(sk, MGMT_OP_SET_FAST_CONNECTABLE,
                                        hdev);
                goto unlock;
        }
 
        if (!hdev_is_powered(hdev)) {
-               change_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags);
+               hci_dev_change_flag(hdev, HCI_FAST_CONNECTABLE);
                err = send_settings_rsp(sk, MGMT_OP_SET_FAST_CONNECTABLE,
                                        hdev);
                new_settings(hdev, sk);
@@ -4715,7 +4738,7 @@ static void set_bredr_complete(struct hci_dev *hdev, u8 status, u16 opcode)
                /* We need to restore the flag if related HCI commands
                 * failed.
                 */
-               clear_bit(HCI_BREDR_ENABLED, &hdev->dev_flags);
+               hci_dev_clear_flag(hdev, HCI_BREDR_ENABLED);
 
                mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err);
        } else {
@@ -4742,7 +4765,7 @@ static int set_bredr(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
                return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR,
                                       MGMT_STATUS_NOT_SUPPORTED);
 
-       if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags))
+       if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
                return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR,
                                       MGMT_STATUS_REJECTED);
 
@@ -4752,21 +4775,21 @@ static int set_bredr(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
 
        hci_dev_lock(hdev);
 
-       if (cp->val == test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) {
+       if (cp->val == hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) {
                err = send_settings_rsp(sk, MGMT_OP_SET_BREDR, hdev);
                goto unlock;
        }
 
        if (!hdev_is_powered(hdev)) {
                if (!cp->val) {
-                       clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags);
-                       clear_bit(HCI_SSP_ENABLED, &hdev->dev_flags);
-                       clear_bit(HCI_LINK_SECURITY, &hdev->dev_flags);
-                       clear_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags);
-                       clear_bit(HCI_HS_ENABLED, &hdev->dev_flags);
+                       hci_dev_clear_flag(hdev, HCI_DISCOVERABLE);
+                       hci_dev_clear_flag(hdev, HCI_SSP_ENABLED);
+                       hci_dev_clear_flag(hdev, HCI_LINK_SECURITY);
+                       hci_dev_clear_flag(hdev, HCI_FAST_CONNECTABLE);
+                       hci_dev_clear_flag(hdev, HCI_HS_ENABLED);
                }
 
-               change_bit(HCI_BREDR_ENABLED, &hdev->dev_flags);
+               hci_dev_change_flag(hdev, HCI_BREDR_ENABLED);
 
                err = send_settings_rsp(sk, MGMT_OP_SET_BREDR, hdev);
                if (err < 0)
@@ -4796,9 +4819,9 @@ static int set_bredr(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
                 * switching BR/EDR back on when secure connections has been
                 * enabled is not a supported transaction.
                 */
-               if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags) &&
+               if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) &&
                    (bacmp(&hdev->static_addr, BDADDR_ANY) ||
-                    test_bit(HCI_SC_ENABLED, &hdev->dev_flags))) {
+                    hci_dev_test_flag(hdev, HCI_SC_ENABLED))) {
                        err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR,
                                              MGMT_STATUS_REJECTED);
                        goto unlock;
@@ -4820,7 +4843,7 @@ static int set_bredr(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
        /* We need to flip the bit already here so that update_adv_data
         * generates the correct flags.
         */
-       set_bit(HCI_BREDR_ENABLED, &hdev->dev_flags);
+       hci_dev_set_flag(hdev, HCI_BREDR_ENABLED);
 
        hci_req_init(&req, hdev);
 
@@ -4864,16 +4887,16 @@ static void sc_enable_complete(struct hci_dev *hdev, u8 status, u16 opcode)
 
        switch (cp->val) {
        case 0x00:
-               clear_bit(HCI_SC_ENABLED, &hdev->dev_flags);
-               clear_bit(HCI_SC_ONLY, &hdev->dev_flags);
+               hci_dev_clear_flag(hdev, HCI_SC_ENABLED);
+               hci_dev_clear_flag(hdev, HCI_SC_ONLY);
                break;
        case 0x01:
-               set_bit(HCI_SC_ENABLED, &hdev->dev_flags);
-               clear_bit(HCI_SC_ONLY, &hdev->dev_flags);
+               hci_dev_set_flag(hdev, HCI_SC_ENABLED);
+               hci_dev_clear_flag(hdev, HCI_SC_ONLY);
                break;
        case 0x02:
-               set_bit(HCI_SC_ENABLED, &hdev->dev_flags);
-               set_bit(HCI_SC_ONLY, &hdev->dev_flags);
+               hci_dev_set_flag(hdev, HCI_SC_ENABLED);
+               hci_dev_set_flag(hdev, HCI_SC_ONLY);
                break;
        }
 
@@ -4898,13 +4921,13 @@ static int set_secure_conn(struct sock *sk, struct hci_dev *hdev,
        BT_DBG("request for %s", hdev->name);
 
        if (!lmp_sc_capable(hdev) &&
-           !test_bit(HCI_LE_ENABLED, &hdev->dev_flags))
+           !hci_dev_test_flag(hdev, HCI_LE_ENABLED))
                return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN,
                                       MGMT_STATUS_NOT_SUPPORTED);
 
-       if (test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags) &&
+       if (hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) &&
            lmp_sc_capable(hdev) &&
-           !test_bit(HCI_SSP_ENABLED, &hdev->dev_flags))
+           !hci_dev_test_flag(hdev, HCI_SSP_ENABLED))
                return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN,
                                       MGMT_STATUS_REJECTED);
 
@@ -4915,20 +4938,20 @@ static int set_secure_conn(struct sock *sk, struct hci_dev *hdev,
        hci_dev_lock(hdev);
 
        if (!hdev_is_powered(hdev) || !lmp_sc_capable(hdev) ||
-           !test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) {
+           !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) {
                bool changed;
 
                if (cp->val) {
                        changed = !test_and_set_bit(HCI_SC_ENABLED,
                                                    &hdev->dev_flags);
                        if (cp->val == 0x02)
-                               set_bit(HCI_SC_ONLY, &hdev->dev_flags);
+                               hci_dev_set_flag(hdev, HCI_SC_ONLY);
                        else
-                               clear_bit(HCI_SC_ONLY, &hdev->dev_flags);
+                               hci_dev_clear_flag(hdev, HCI_SC_ONLY);
                } else {
                        changed = test_and_clear_bit(HCI_SC_ENABLED,
                                                     &hdev->dev_flags);
-                       clear_bit(HCI_SC_ONLY, &hdev->dev_flags);
+                       hci_dev_clear_flag(hdev, HCI_SC_ONLY);
                }
 
                err = send_settings_rsp(sk, MGMT_OP_SET_SECURE_CONN, hdev);
@@ -4949,8 +4972,8 @@ static int set_secure_conn(struct sock *sk, struct hci_dev *hdev,
 
        val = !!cp->val;
 
-       if (val == test_bit(HCI_SC_ENABLED, &hdev->dev_flags) &&
-           (cp->val == 0x02) == test_bit(HCI_SC_ONLY, &hdev->dev_flags)) {
+       if (val == hci_dev_test_flag(hdev, HCI_SC_ENABLED) &&
+           (cp->val == 0x02) == hci_dev_test_flag(hdev, HCI_SC_ONLY)) {
                err = send_settings_rsp(sk, MGMT_OP_SET_SECURE_CONN, hdev);
                goto failed;
        }
@@ -5004,7 +5027,7 @@ static int set_debug_keys(struct sock *sk, struct hci_dev *hdev,
                                                 &hdev->dev_flags);
 
        if (hdev_is_powered(hdev) && use_changed &&
-           test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) {
+           hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) {
                u8 mode = (cp->val == 0x02) ? 0x01 : 0x00;
                hci_send_cmd(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE,
                             sizeof(mode), &mode);
@@ -5048,16 +5071,16 @@ static int set_privacy(struct sock *sk, struct hci_dev *hdev, void *cp_data,
        /* If user space supports this command it is also expected to
         * handle IRKs. Therefore, set the HCI_RPA_RESOLVING flag.
         */
-       set_bit(HCI_RPA_RESOLVING, &hdev->dev_flags);
+       hci_dev_set_flag(hdev, HCI_RPA_RESOLVING);
 
        if (cp->privacy) {
                changed = !test_and_set_bit(HCI_PRIVACY, &hdev->dev_flags);
                memcpy(hdev->irk, cp->irk, sizeof(hdev->irk));
-               set_bit(HCI_RPA_EXPIRED, &hdev->dev_flags);
+               hci_dev_set_flag(hdev, HCI_RPA_EXPIRED);
        } else {
                changed = test_and_clear_bit(HCI_PRIVACY, &hdev->dev_flags);
                memset(hdev->irk, 0, sizeof(hdev->irk));
-               clear_bit(HCI_RPA_EXPIRED, &hdev->dev_flags);
+               hci_dev_clear_flag(hdev, HCI_RPA_EXPIRED);
        }
 
        err = send_settings_rsp(sk, MGMT_OP_SET_PRIVACY, hdev);
@@ -5146,7 +5169,7 @@ static int load_irks(struct sock *sk, struct hci_dev *hdev, void *cp_data,
                            BDADDR_ANY);
        }
 
-       set_bit(HCI_RPA_RESOLVING, &hdev->dev_flags);
+       hci_dev_set_flag(hdev, HCI_RPA_RESOLVING);
 
        err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_LOAD_IRKS, 0, NULL, 0);
 
@@ -6076,12 +6099,12 @@ static int set_external_config(struct sock *sk, struct hci_dev *hdev,
 
        err = new_options(hdev, sk);
 
-       if (test_bit(HCI_UNCONFIGURED, &hdev->dev_flags) == is_configured(hdev)) {
+       if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED) == is_configured(hdev)) {
                mgmt_index_removed(hdev);
 
                if (test_and_change_bit(HCI_UNCONFIGURED, &hdev->dev_flags)) {
-                       set_bit(HCI_CONFIG, &hdev->dev_flags);
-                       set_bit(HCI_AUTO_OFF, &hdev->dev_flags);
+                       hci_dev_set_flag(hdev, HCI_CONFIG);
+                       hci_dev_set_flag(hdev, HCI_AUTO_OFF);
 
                        queue_work(hdev->req_workqueue, &hdev->power_on);
                } else {
@@ -6128,16 +6151,16 @@ static int set_public_address(struct sock *sk, struct hci_dev *hdev,
        if (!changed)
                goto unlock;
 
-       if (test_bit(HCI_UNCONFIGURED, &hdev->dev_flags))
+       if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED))
                err = new_options(hdev, sk);
 
        if (is_configured(hdev)) {
                mgmt_index_removed(hdev);
 
-               clear_bit(HCI_UNCONFIGURED, &hdev->dev_flags);
+               hci_dev_clear_flag(hdev, HCI_UNCONFIGURED);
 
-               set_bit(HCI_CONFIG, &hdev->dev_flags);
-               set_bit(HCI_AUTO_OFF, &hdev->dev_flags);
+               hci_dev_set_flag(hdev, HCI_CONFIG);
+               hci_dev_set_flag(hdev, HCI_AUTO_OFF);
 
                queue_work(hdev->req_workqueue, &hdev->power_on);
        }
@@ -6276,15 +6299,15 @@ int mgmt_control(struct hci_mgmt_chan *chan, struct sock *sk,
                        goto done;
                }
 
-               if (test_bit(HCI_SETUP, &hdev->dev_flags) ||
-                   test_bit(HCI_CONFIG, &hdev->dev_flags) ||
-                   test_bit(HCI_USER_CHANNEL, &hdev->dev_flags)) {
+               if (hci_dev_test_flag(hdev, HCI_SETUP) ||
+                   hci_dev_test_flag(hdev, HCI_CONFIG) ||
+                   hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) {
                        err = mgmt_cmd_status(sk, index, opcode,
                                              MGMT_STATUS_INVALID_INDEX);
                        goto done;
                }
 
-               if (test_bit(HCI_UNCONFIGURED, &hdev->dev_flags) &&
+               if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED) &&
                    !(handler->flags & HCI_MGMT_UNCONFIGURED)) {
                        err = mgmt_cmd_status(sk, index, opcode,
                                              MGMT_STATUS_INVALID_INDEX);
@@ -6334,7 +6357,7 @@ void mgmt_index_added(struct hci_dev *hdev)
        if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks))
                return;
 
-       if (test_bit(HCI_UNCONFIGURED, &hdev->dev_flags))
+       if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED))
                mgmt_event(MGMT_EV_UNCONF_INDEX_ADDED, hdev, NULL, 0, NULL);
        else
                mgmt_event(MGMT_EV_INDEX_ADDED, hdev, NULL, 0, NULL);
@@ -6352,7 +6375,7 @@ void mgmt_index_removed(struct hci_dev *hdev)
 
        mgmt_pending_foreach(0, hdev, cmd_complete_rsp, &status);
 
-       if (test_bit(HCI_UNCONFIGURED, &hdev->dev_flags))
+       if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED))
                mgmt_event(MGMT_EV_UNCONF_INDEX_REMOVED, hdev, NULL, 0, NULL);
        else
                mgmt_event(MGMT_EV_INDEX_REMOVED, hdev, NULL, 0, NULL);
@@ -6420,7 +6443,7 @@ static int powered_update_hci(struct hci_dev *hdev)
 
        hci_req_init(&req, hdev);
 
-       if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags) &&
+       if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED) &&
            !lmp_host_ssp_capable(hdev)) {
                u8 mode = 0x01;
 
@@ -6434,7 +6457,7 @@ static int powered_update_hci(struct hci_dev *hdev)
                }
        }
 
-       if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags) &&
+       if (hci_dev_test_flag(hdev, HCI_LE_ENABLED) &&
            lmp_bredr_capable(hdev)) {
                struct hci_cp_write_le_host_supported cp;
 
@@ -6455,24 +6478,24 @@ static int powered_update_hci(struct hci_dev *hdev)
                 * advertising data. This also applies to the case
                 * where BR/EDR was toggled during the AUTO_OFF phase.
                 */
-               if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) {
+               if (hci_dev_test_flag(hdev, HCI_LE_ENABLED)) {
                        update_adv_data(&req);
                        update_scan_rsp_data(&req);
                }
 
-               if (test_bit(HCI_ADVERTISING, &hdev->dev_flags))
+               if (hci_dev_test_flag(hdev, HCI_ADVERTISING))
                        enable_advertising(&req);
 
                restart_le_actions(&req);
        }
 
-       link_sec = test_bit(HCI_LINK_SECURITY, &hdev->dev_flags);
+       link_sec = hci_dev_test_flag(hdev, HCI_LINK_SECURITY);
        if (link_sec != test_bit(HCI_AUTH, &hdev->flags))
                hci_req_add(&req, HCI_OP_WRITE_AUTH_ENABLE,
                            sizeof(link_sec), &link_sec);
 
        if (lmp_bredr_capable(hdev)) {
-               if (test_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags))
+               if (hci_dev_test_flag(hdev, HCI_FAST_CONNECTABLE))
                        write_fast_connectable(&req, true);
                else
                        write_fast_connectable(&req, false);
@@ -6491,7 +6514,7 @@ int mgmt_powered(struct hci_dev *hdev, u8 powered)
        u8 status, zero_cod[] = { 0, 0, 0 };
        int err;
 
-       if (!test_bit(HCI_MGMT, &hdev->dev_flags))
+       if (!hci_dev_test_flag(hdev, HCI_MGMT))
                return 0;
 
        if (powered) {
@@ -6512,7 +6535,7 @@ int mgmt_powered(struct hci_dev *hdev, u8 powered)
         * been triggered, potentially causing misleading DISCONNECTED
         * status responses.
         */
-       if (test_bit(HCI_UNREGISTER, &hdev->dev_flags))
+       if (hci_dev_test_flag(hdev, HCI_UNREGISTER))
                status = MGMT_STATUS_INVALID_INDEX;
        else
                status = MGMT_STATUS_NOT_POWERED;
@@ -6562,11 +6585,11 @@ void mgmt_discoverable_timeout(struct hci_dev *hdev)
         * of a timeout triggered from general discoverable, it is
         * safe to unconditionally clear the flag.
         */
-       clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags);
-       clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags);
+       hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE);
+       hci_dev_clear_flag(hdev, HCI_DISCOVERABLE);
 
        hci_req_init(&req, hdev);
-       if (test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) {
+       if (hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) {
                u8 scan = SCAN_PAGE;
                hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE,
                            sizeof(scan), &scan);
@@ -7112,7 +7135,7 @@ void mgmt_ssp_enable_complete(struct hci_dev *hdev, u8 enable, u8 status)
 
                if (enable && test_and_clear_bit(HCI_SSP_ENABLED,
                                                 &hdev->dev_flags)) {
-                       clear_bit(HCI_HS_ENABLED, &hdev->dev_flags);
+                       hci_dev_clear_flag(hdev, HCI_HS_ENABLED);
                        new_settings(hdev, NULL);
                }
 
@@ -7129,7 +7152,7 @@ void mgmt_ssp_enable_complete(struct hci_dev *hdev, u8 enable, u8 status)
                        changed = test_and_clear_bit(HCI_HS_ENABLED,
                                                     &hdev->dev_flags);
                else
-                       clear_bit(HCI_HS_ENABLED, &hdev->dev_flags);
+                       hci_dev_clear_flag(hdev, HCI_HS_ENABLED);
        }
 
        mgmt_pending_foreach(MGMT_OP_SET_SSP, hdev, settings_rsp, &match);
@@ -7142,8 +7165,8 @@ void mgmt_ssp_enable_complete(struct hci_dev *hdev, u8 enable, u8 status)
 
        hci_req_init(&req, hdev);
 
-       if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) {
-               if (test_bit(HCI_USE_DEBUG_KEYS, &hdev->dev_flags))
+       if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) {
+               if (hci_dev_test_flag(hdev, HCI_USE_DEBUG_KEYS))
                        hci_req_add(&req, HCI_OP_WRITE_SSP_DEBUG_MODE,
                                    sizeof(enable), &enable);
                update_eir(&req);
@@ -7315,7 +7338,7 @@ static bool eir_has_uuids(u8 *eir, u16 eir_len, u16 uuid_count, u8 (*uuids)[16])
 static void restart_le_scan(struct hci_dev *hdev)
 {
        /* If controller is not scanning we are done. */
-       if (!test_bit(HCI_LE_SCAN, &hdev->dev_flags))
+       if (!hci_dev_test_flag(hdev, HCI_LE_SCAN))
                return;
 
        if (time_after(jiffies + DISCOV_LE_RESTART_DELAY,
@@ -7486,7 +7509,7 @@ void mgmt_reenable_advertising(struct hci_dev *hdev)
 {
        struct hci_request req;
 
-       if (!test_bit(HCI_ADVERTISING, &hdev->dev_flags))
+       if (!hci_dev_test_flag(hdev, HCI_ADVERTISING))
                return;
 
        hci_req_init(&req, hdev);