Bluetooth: Fix setting local name to the existing value
[cascardo/linux.git] / net / bluetooth / mgmt.c
index 39395c7..15305fa 100644 (file)
@@ -591,32 +591,33 @@ static void create_eir(struct hci_dev *hdev, u8 *data)
        ptr = create_uuid128_list(hdev, ptr, HCI_MAX_EIR_LENGTH - (ptr - data));
 }
 
-static int update_eir(struct hci_dev *hdev)
+static void update_eir(struct hci_request *req)
 {
+       struct hci_dev *hdev = req->hdev;
        struct hci_cp_write_eir cp;
 
        if (!hdev_is_powered(hdev))
-               return 0;
+               return;
 
        if (!lmp_ext_inq_capable(hdev))
-               return 0;
+               return;
 
        if (!test_bit(HCI_SSP_ENABLED, &hdev->dev_flags))
-               return 0;
+               return;
 
        if (test_bit(HCI_SERVICE_CACHE, &hdev->dev_flags))
-               return 0;
+               return;
 
        memset(&cp, 0, sizeof(cp));
 
        create_eir(hdev, cp.data);
 
        if (memcmp(cp.data, hdev->eir, sizeof(cp.data)) == 0)
-               return 0;
+               return;
 
        memcpy(hdev->eir, cp.data, sizeof(cp.data));
 
-       return hci_send_cmd(hdev, HCI_OP_WRITE_EIR, sizeof(cp), &cp);
+       hci_req_add(req, HCI_OP_WRITE_EIR, sizeof(cp), &cp);
 }
 
 static u8 get_service_classes(struct hci_dev *hdev)
@@ -630,47 +631,48 @@ static u8 get_service_classes(struct hci_dev *hdev)
        return val;
 }
 
-static int update_class(struct hci_dev *hdev)
+static void update_class(struct hci_request *req)
 {
+       struct hci_dev *hdev = req->hdev;
        u8 cod[3];
-       int err;
 
        BT_DBG("%s", hdev->name);
 
        if (!hdev_is_powered(hdev))
-               return 0;
+               return;
 
        if (test_bit(HCI_SERVICE_CACHE, &hdev->dev_flags))
-               return 0;
+               return;
 
        cod[0] = hdev->minor_class;
        cod[1] = hdev->major_class;
        cod[2] = get_service_classes(hdev);
 
        if (memcmp(cod, hdev->dev_class, 3) == 0)
-               return 0;
-
-       err = hci_send_cmd(hdev, HCI_OP_WRITE_CLASS_OF_DEV, sizeof(cod), cod);
-       if (err == 0)
-               set_bit(HCI_PENDING_CLASS, &hdev->dev_flags);
+               return;
 
-       return err;
+       hci_req_add(req, HCI_OP_WRITE_CLASS_OF_DEV, sizeof(cod), cod);
 }
 
 static void service_cache_off(struct work_struct *work)
 {
        struct hci_dev *hdev = container_of(work, struct hci_dev,
                                            service_cache.work);
+       struct hci_request req;
 
        if (!test_and_clear_bit(HCI_SERVICE_CACHE, &hdev->dev_flags))
                return;
 
+       hci_req_init(&req, hdev);
+
        hci_dev_lock(hdev);
 
-       update_eir(hdev);
-       update_class(hdev);
+       update_eir(&req);
+       update_class(&req);
 
        hci_dev_unlock(hdev);
+
+       hci_req_run(&req, NULL);
 }
 
 static void mgmt_init_hdev(struct sock *sk, struct hci_dev *hdev)
@@ -1332,6 +1334,29 @@ unlock:
        return err;
 }
 
+/* This is a helper function to test for pending mgmt commands that can
+ * cause CoD or EIR HCI commands. We can only allow one such pending
+ * mgmt command at a time since otherwise we cannot easily track what
+ * the current values are, will be, and based on that calculate if a new
+ * HCI command needs to be sent and if yes with what value.
+ */
+static bool pending_eir_or_class(struct hci_dev *hdev)
+{
+       struct pending_cmd *cmd;
+
+       list_for_each_entry(cmd, &hdev->mgmt_pending, list) {
+               switch (cmd->opcode) {
+               case MGMT_OP_ADD_UUID:
+               case MGMT_OP_REMOVE_UUID:
+               case MGMT_OP_SET_DEV_CLASS:
+               case MGMT_OP_SET_POWERED:
+                       return true;
+               }
+       }
+
+       return false;
+}
+
 static const u8 bluetooth_base_uuid[] = {
                        0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80,
                        0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
@@ -1351,10 +1376,37 @@ static u8 get_uuid_size(const u8 *uuid)
        return 16;
 }
 
+static void mgmt_class_complete(struct hci_dev *hdev, u16 mgmt_op, u8 status)
+{
+       struct pending_cmd *cmd;
+
+       hci_dev_lock(hdev);
+
+       cmd = mgmt_pending_find(mgmt_op, hdev);
+       if (!cmd)
+               goto unlock;
+
+       cmd_complete(cmd->sk, cmd->index, cmd->opcode, mgmt_status(status),
+                    hdev->dev_class, 3);
+
+       mgmt_pending_remove(cmd);
+
+unlock:
+       hci_dev_unlock(hdev);
+}
+
+static void add_uuid_complete(struct hci_dev *hdev, u8 status)
+{
+       BT_DBG("status 0x%02x", status);
+
+       mgmt_class_complete(hdev, MGMT_OP_ADD_UUID, status);
+}
+
 static int add_uuid(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
 {
        struct mgmt_cp_add_uuid *cp = data;
        struct pending_cmd *cmd;
+       struct hci_request req;
        struct bt_uuid *uuid;
        int err;
 
@@ -1362,7 +1414,7 @@ static int add_uuid(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
 
        hci_dev_lock(hdev);
 
-       if (test_bit(HCI_PENDING_CLASS, &hdev->dev_flags)) {
+       if (pending_eir_or_class(hdev)) {
                err = cmd_status(sk, hdev->id, MGMT_OP_ADD_UUID,
                                 MGMT_STATUS_BUSY);
                goto failed;
@@ -1380,23 +1432,28 @@ static int add_uuid(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
 
        list_add_tail(&uuid->list, &hdev->uuids);
 
-       err = update_class(hdev);
-       if (err < 0)
-               goto failed;
+       hci_req_init(&req, hdev);
 
-       err = update_eir(hdev);
-       if (err < 0)
-               goto failed;
+       update_class(&req);
+       update_eir(&req);
+
+       err = hci_req_run(&req, add_uuid_complete);
+       if (err < 0) {
+               if (err != -ENODATA)
+                       goto failed;
 
-       if (!test_bit(HCI_PENDING_CLASS, &hdev->dev_flags)) {
                err = cmd_complete(sk, hdev->id, MGMT_OP_ADD_UUID, 0,
                                   hdev->dev_class, 3);
                goto failed;
        }
 
        cmd = mgmt_pending_add(sk, MGMT_OP_ADD_UUID, hdev, data, len);
-       if (!cmd)
+       if (!cmd) {
                err = -ENOMEM;
+               goto failed;
+       }
+
+       err = 0;
 
 failed:
        hci_dev_unlock(hdev);
@@ -1417,6 +1474,13 @@ static bool enable_service_cache(struct hci_dev *hdev)
        return false;
 }
 
+static void remove_uuid_complete(struct hci_dev *hdev, u8 status)
+{
+       BT_DBG("status 0x%02x", status);
+
+       mgmt_class_complete(hdev, MGMT_OP_REMOVE_UUID, status);
+}
+
 static int remove_uuid(struct sock *sk, struct hci_dev *hdev, void *data,
                       u16 len)
 {
@@ -1424,13 +1488,14 @@ static int remove_uuid(struct sock *sk, struct hci_dev *hdev, void *data,
        struct pending_cmd *cmd;
        struct bt_uuid *match, *tmp;
        u8 bt_uuid_any[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
+       struct hci_request req;
        int err, found;
 
        BT_DBG("request for %s", hdev->name);
 
        hci_dev_lock(hdev);
 
-       if (test_bit(HCI_PENDING_CLASS, &hdev->dev_flags)) {
+       if (pending_eir_or_class(hdev)) {
                err = cmd_status(sk, hdev->id, MGMT_OP_REMOVE_UUID,
                                 MGMT_STATUS_BUSY);
                goto unlock;
@@ -1466,34 +1531,47 @@ static int remove_uuid(struct sock *sk, struct hci_dev *hdev, void *data,
        }
 
 update_class:
-       err = update_class(hdev);
-       if (err < 0)
-               goto unlock;
+       hci_req_init(&req, hdev);
 
-       err = update_eir(hdev);
-       if (err < 0)
-               goto unlock;
+       update_class(&req);
+       update_eir(&req);
+
+       err = hci_req_run(&req, remove_uuid_complete);
+       if (err < 0) {
+               if (err != -ENODATA)
+                       goto unlock;
 
-       if (!test_bit(HCI_PENDING_CLASS, &hdev->dev_flags)) {
                err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_UUID, 0,
                                   hdev->dev_class, 3);
                goto unlock;
        }
 
        cmd = mgmt_pending_add(sk, MGMT_OP_REMOVE_UUID, hdev, data, len);
-       if (!cmd)
+       if (!cmd) {
                err = -ENOMEM;
+               goto unlock;
+       }
+
+       err = 0;
 
 unlock:
        hci_dev_unlock(hdev);
        return err;
 }
 
+static void set_class_complete(struct hci_dev *hdev, u8 status)
+{
+       BT_DBG("status 0x%02x", status);
+
+       mgmt_class_complete(hdev, MGMT_OP_SET_DEV_CLASS, status);
+}
+
 static int set_dev_class(struct sock *sk, struct hci_dev *hdev, void *data,
                         u16 len)
 {
        struct mgmt_cp_set_dev_class *cp = data;
        struct pending_cmd *cmd;
+       struct hci_request req;
        int err;
 
        BT_DBG("request for %s", hdev->name);
@@ -1502,15 +1580,19 @@ static int set_dev_class(struct sock *sk, struct hci_dev *hdev, void *data,
                return cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS,
                                  MGMT_STATUS_NOT_SUPPORTED);
 
-       if (test_bit(HCI_PENDING_CLASS, &hdev->dev_flags))
-               return cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS,
-                                 MGMT_STATUS_BUSY);
+       hci_dev_lock(hdev);
 
-       if ((cp->minor & 0x03) != 0 || (cp->major & 0xe0) != 0)
-               return cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS,
-                                 MGMT_STATUS_INVALID_PARAMS);
+       if (pending_eir_or_class(hdev)) {
+               err = cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS,
+                                MGMT_STATUS_BUSY);
+               goto unlock;
+       }
 
-       hci_dev_lock(hdev);
+       if ((cp->minor & 0x03) != 0 || (cp->major & 0xe0) != 0) {
+               err = cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS,
+                                MGMT_STATUS_INVALID_PARAMS);
+               goto unlock;
+       }
 
        hdev->major_class = cp->major;
        hdev->minor_class = cp->minor;
@@ -1521,26 +1603,34 @@ static int set_dev_class(struct sock *sk, struct hci_dev *hdev, void *data,
                goto unlock;
        }
 
+       hci_req_init(&req, hdev);
+
        if (test_and_clear_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) {
                hci_dev_unlock(hdev);
                cancel_delayed_work_sync(&hdev->service_cache);
                hci_dev_lock(hdev);
-               update_eir(hdev);
+               update_eir(&req);
        }
 
-       err = update_class(hdev);
-       if (err < 0)
-               goto unlock;
+       update_class(&req);
+
+       err = hci_req_run(&req, set_class_complete);
+       if (err < 0) {
+               if (err != -ENODATA)
+                       goto unlock;
 
-       if (!test_bit(HCI_PENDING_CLASS, &hdev->dev_flags)) {
                err = cmd_complete(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, 0,
                                   hdev->dev_class, 3);
                goto unlock;
        }
 
        cmd = mgmt_pending_add(sk, MGMT_OP_SET_DEV_CLASS, hdev, data, len);
-       if (!cmd)
+       if (!cmd) {
                err = -ENOMEM;
+               goto unlock;
+       }
+
+       err = 0;
 
 unlock:
        hci_dev_unlock(hdev);
@@ -2268,13 +2358,42 @@ static int user_passkey_neg_reply(struct sock *sk, struct hci_dev *hdev,
                                 HCI_OP_USER_PASSKEY_NEG_REPLY, 0);
 }
 
-static int update_name(struct hci_dev *hdev, const char *name)
+static void update_name(struct hci_request *req)
 {
+       struct hci_dev *hdev = req->hdev;
        struct hci_cp_write_local_name cp;
 
-       memcpy(cp.name, name, sizeof(cp.name));
+       memcpy(cp.name, hdev->dev_name, sizeof(cp.name));
 
-       return hci_send_cmd(hdev, HCI_OP_WRITE_LOCAL_NAME, sizeof(cp), &cp);
+       hci_req_add(req, HCI_OP_WRITE_LOCAL_NAME, sizeof(cp), &cp);
+}
+
+static void set_name_complete(struct hci_dev *hdev, u8 status)
+{
+       struct mgmt_cp_set_local_name *cp;
+       struct pending_cmd *cmd;
+
+       BT_DBG("status 0x%02x", status);
+
+       hci_dev_lock(hdev);
+
+       cmd = mgmt_pending_find(MGMT_OP_SET_LOCAL_NAME, hdev);
+       if (!cmd)
+               goto unlock;
+
+       cp = cmd->param;
+
+       if (status)
+               cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME,
+                          mgmt_status(status));
+       else
+               cmd_complete(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0,
+                            cp, sizeof(*cp));
+
+       mgmt_pending_remove(cmd);
+
+unlock:
+       hci_dev_unlock(hdev);
 }
 
 static int set_local_name(struct sock *sk, struct hci_dev *hdev, void *data,
@@ -2282,12 +2401,24 @@ static int set_local_name(struct sock *sk, struct hci_dev *hdev, void *data,
 {
        struct mgmt_cp_set_local_name *cp = data;
        struct pending_cmd *cmd;
+       struct hci_request req;
        int err;
 
        BT_DBG("");
 
        hci_dev_lock(hdev);
 
+       /* If the old values are the same as the new ones just return a
+        * direct command complete event.
+        */
+       if (!memcmp(hdev->dev_name, cp->name, sizeof(hdev->dev_name)) &&
+           !memcmp(hdev->short_name, cp->short_name,
+                   sizeof(hdev->short_name))) {
+               err = cmd_complete(sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0,
+                                  data, len);
+               goto failed;
+       }
+
        memcpy(hdev->short_name, cp->short_name, sizeof(hdev->short_name));
 
        if (!hdev_is_powered(hdev)) {
@@ -2310,7 +2441,19 @@ static int set_local_name(struct sock *sk, struct hci_dev *hdev, void *data,
                goto failed;
        }
 
-       err = update_name(hdev, cp->name);
+       memcpy(hdev->dev_name, cp->name, sizeof(hdev->dev_name));
+
+       hci_req_init(&req, hdev);
+
+       if (lmp_bredr_capable(hdev)) {
+               update_name(&req);
+               update_eir(&req);
+       }
+
+       if (lmp_le_capable(hdev))
+               hci_update_ad(&req);
+
+       err = hci_req_run(&req, set_name_complete);
        if (err < 0)
                mgmt_pending_remove(cmd);
 
@@ -2698,6 +2841,7 @@ static int set_device_id(struct sock *sk, struct hci_dev *hdev, void *data,
                         u16 len)
 {
        struct mgmt_cp_set_device_id *cp = data;
+       struct hci_request req;
        int err;
        __u16 source;
 
@@ -2718,7 +2862,9 @@ static int set_device_id(struct sock *sk, struct hci_dev *hdev, void *data,
 
        err = cmd_complete(sk, hdev->id, MGMT_OP_SET_DEVICE_ID, 0, NULL, 0);
 
-       update_eir(hdev);
+       hci_req_init(&req, hdev);
+       update_eir(&req);
+       hci_req_run(&req, NULL);
 
        hci_dev_unlock(hdev);
 
@@ -3043,8 +3189,9 @@ static void settings_rsp(struct pending_cmd *cmd, void *data)
        mgmt_pending_free(cmd);
 }
 
-static int set_bredr_scan(struct hci_dev *hdev)
+static void set_bredr_scan(struct hci_request *req)
 {
+       struct hci_dev *hdev = req->hdev;
        u8 scan = 0;
 
        if (test_bit(HCI_CONNECTABLE, &hdev->dev_flags))
@@ -3052,70 +3199,99 @@ static int set_bredr_scan(struct hci_dev *hdev)
        if (test_bit(HCI_DISCOVERABLE, &hdev->dev_flags))
                scan |= SCAN_INQUIRY;
 
-       if (!scan)
-               return 0;
-
-       return hci_send_cmd(hdev, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan);
+       if (scan)
+               hci_req_add(req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan);
 }
 
-int mgmt_powered(struct hci_dev *hdev, u8 powered)
+static void powered_complete(struct hci_dev *hdev, u8 status)
 {
        struct cmd_lookup match = { NULL, hdev };
-       int err;
 
-       if (!test_bit(HCI_MGMT, &hdev->dev_flags))
-               return 0;
+       BT_DBG("status 0x%02x", status);
+
+       hci_dev_lock(hdev);
 
        mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, &match);
 
-       if (powered) {
-               u8 link_sec;
+       new_settings(hdev, match.sk);
 
-               if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags) &&
-                   !lmp_host_ssp_capable(hdev)) {
-                       u8 ssp = 1;
+       hci_dev_unlock(hdev);
 
-                       hci_send_cmd(hdev, HCI_OP_WRITE_SSP_MODE, 1, &ssp);
-               }
+       if (match.sk)
+               sock_put(match.sk);
+}
 
-               if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) {
-                       struct hci_cp_write_le_host_supported cp;
+static int powered_update_hci(struct hci_dev *hdev)
+{
+       struct hci_request req;
+       u8 link_sec;
 
-                       cp.le = 1;
-                       cp.simul = lmp_le_br_capable(hdev);
+       hci_req_init(&req, hdev);
 
-                       /* Check first if we already have the right
-                        * host state (host features set)
-                        */
-                       if (cp.le != lmp_host_le_capable(hdev) ||
-                           cp.simul != lmp_host_le_br_capable(hdev))
-                               hci_send_cmd(hdev,
-                                            HCI_OP_WRITE_LE_HOST_SUPPORTED,
-                                            sizeof(cp), &cp);
-               }
+       if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags) &&
+           !lmp_host_ssp_capable(hdev)) {
+               u8 ssp = 1;
 
-               link_sec = test_bit(HCI_LINK_SECURITY, &hdev->dev_flags);
-               if (link_sec != test_bit(HCI_AUTH, &hdev->flags))
-                       hci_send_cmd(hdev, HCI_OP_WRITE_AUTH_ENABLE,
-                                    sizeof(link_sec), &link_sec);
+               hci_req_add(&req, HCI_OP_WRITE_SSP_MODE, 1, &ssp);
+       }
 
-               if (lmp_bredr_capable(hdev)) {
-                       set_bredr_scan(hdev);
-                       update_class(hdev);
-                       update_name(hdev, hdev->dev_name);
-                       update_eir(hdev);
-               }
-       } else {
-               u8 status = MGMT_STATUS_NOT_POWERED;
-               u8 zero_cod[] = { 0, 0, 0 };
+       if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) {
+               struct hci_cp_write_le_host_supported cp;
 
-               mgmt_pending_foreach(0, hdev, cmd_status_rsp, &status);
+               cp.le = 1;
+               cp.simul = lmp_le_br_capable(hdev);
 
-               if (memcmp(hdev->dev_class, zero_cod, sizeof(zero_cod)) != 0)
-                       mgmt_event(MGMT_EV_CLASS_OF_DEV_CHANGED, hdev,
-                                  zero_cod, sizeof(zero_cod), NULL);
+               /* Check first if we already have the right
+                * host state (host features set)
+                */
+               if (cp.le != lmp_host_le_capable(hdev) ||
+                   cp.simul != lmp_host_le_br_capable(hdev))
+                       hci_req_add(&req, HCI_OP_WRITE_LE_HOST_SUPPORTED,
+                                   sizeof(cp), &cp);
        }
 
+       link_sec = test_bit(HCI_LINK_SECURITY, &hdev->dev_flags);
+       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)) {
+               set_bredr_scan(&req);
+               update_class(&req);
+               update_name(&req);
+               update_eir(&req);
+       }
+
+       return hci_req_run(&req, powered_complete);
+}
+
+int mgmt_powered(struct hci_dev *hdev, u8 powered)
+{
+       struct cmd_lookup match = { NULL, hdev };
+       u8 status_not_powered = MGMT_STATUS_NOT_POWERED;
+       u8 zero_cod[] = { 0, 0, 0 };
+       int err;
+
+       if (!test_bit(HCI_MGMT, &hdev->dev_flags))
+               return 0;
+
+       if (powered) {
+               if (powered_update_hci(hdev) == 0)
+                       return 0;
+
+               mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp,
+                                    &match);
+               goto new_settings;
+       }
+
+       mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, &match);
+       mgmt_pending_foreach(0, hdev, cmd_status_rsp, &status_not_powered);
+
+       if (memcmp(hdev->dev_class, zero_cod, sizeof(zero_cod)) != 0)
+               mgmt_event(MGMT_EV_CLASS_OF_DEV_CHANGED, hdev,
+                          zero_cod, sizeof(zero_cod), NULL);
+
+new_settings:
        err = new_settings(hdev, match.sk);
 
        if (match.sk)
@@ -3555,23 +3731,25 @@ int mgmt_auth_enable_complete(struct hci_dev *hdev, u8 status)
        return err;
 }
 
-static int clear_eir(struct hci_dev *hdev)
+static void clear_eir(struct hci_request *req)
 {
+       struct hci_dev *hdev = req->hdev;
        struct hci_cp_write_eir cp;
 
        if (!lmp_ext_inq_capable(hdev))
-               return 0;
+               return;
 
        memset(hdev->eir, 0, sizeof(hdev->eir));
 
        memset(&cp, 0, sizeof(cp));
 
-       return hci_send_cmd(hdev, HCI_OP_WRITE_EIR, sizeof(cp), &cp);
+       hci_req_add(req, HCI_OP_WRITE_EIR, sizeof(cp), &cp);
 }
 
 int mgmt_ssp_enable_complete(struct hci_dev *hdev, u8 enable, u8 status)
 {
        struct cmd_lookup match = { NULL, hdev };
+       struct hci_request req;
        bool changed = false;
        int err = 0;
 
@@ -3604,29 +3782,26 @@ int mgmt_ssp_enable_complete(struct hci_dev *hdev, u8 enable, u8 status)
        if (match.sk)
                sock_put(match.sk);
 
+       hci_req_init(&req, hdev);
+
        if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags))
-               update_eir(hdev);
+               update_eir(&req);
        else
-               clear_eir(hdev);
+               clear_eir(&req);
+
+       hci_req_run(&req, NULL);
 
        return err;
 }
 
-static void class_rsp(struct pending_cmd *cmd, void *data)
+static void sk_lookup(struct pending_cmd *cmd, void *data)
 {
        struct cmd_lookup *match = data;
 
-       cmd_complete(cmd->sk, cmd->index, cmd->opcode, match->mgmt_status,
-                    match->hdev->dev_class, 3);
-
-       list_del(&cmd->list);
-
        if (match->sk == NULL) {
                match->sk = cmd->sk;
                sock_hold(match->sk);
        }
-
-       mgmt_pending_free(cmd);
 }
 
 int mgmt_set_class_of_dev_complete(struct hci_dev *hdev, u8 *dev_class,
@@ -3635,11 +3810,9 @@ int mgmt_set_class_of_dev_complete(struct hci_dev *hdev, u8 *dev_class,
        struct cmd_lookup match = { NULL, hdev, mgmt_status(status) };
        int err = 0;
 
-       clear_bit(HCI_PENDING_CLASS, &hdev->dev_flags);
-
-       mgmt_pending_foreach(MGMT_OP_SET_DEV_CLASS, hdev, class_rsp, &match);
-       mgmt_pending_foreach(MGMT_OP_ADD_UUID, hdev, class_rsp, &match);
-       mgmt_pending_foreach(MGMT_OP_REMOVE_UUID, hdev, class_rsp, &match);
+       mgmt_pending_foreach(MGMT_OP_SET_DEV_CLASS, hdev, sk_lookup, &match);
+       mgmt_pending_foreach(MGMT_OP_ADD_UUID, hdev, sk_lookup, &match);
+       mgmt_pending_foreach(MGMT_OP_REMOVE_UUID, hdev, sk_lookup, &match);
 
        if (!status)
                err = mgmt_event(MGMT_EV_CLASS_OF_DEV_CHANGED, hdev, dev_class,
@@ -3653,55 +3826,29 @@ int mgmt_set_class_of_dev_complete(struct hci_dev *hdev, u8 *dev_class,
 
 int mgmt_set_local_name_complete(struct hci_dev *hdev, u8 *name, u8 status)
 {
-       struct pending_cmd *cmd;
        struct mgmt_cp_set_local_name ev;
-       bool changed = false;
-       int err = 0;
+       struct pending_cmd *cmd;
 
-       if (memcmp(name, hdev->dev_name, sizeof(hdev->dev_name)) != 0) {
-               memcpy(hdev->dev_name, name, sizeof(hdev->dev_name));
-               changed = true;
-       }
+       if (status)
+               return 0;
 
        memset(&ev, 0, sizeof(ev));
        memcpy(ev.name, name, HCI_MAX_NAME_LENGTH);
        memcpy(ev.short_name, hdev->short_name, HCI_MAX_SHORT_NAME_LENGTH);
 
        cmd = mgmt_pending_find(MGMT_OP_SET_LOCAL_NAME, hdev);
-       if (!cmd)
-               goto send_event;
-
-       /* Always assume that either the short or the complete name has
-        * changed if there was a pending mgmt command */
-       changed = true;
+       if (!cmd) {
+               memcpy(hdev->dev_name, name, sizeof(hdev->dev_name));
 
-       if (status) {
-               err = cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME,
-                                mgmt_status(status));
-               goto failed;
+               /* If this is a HCI command related to powering on the
+                * HCI dev don't send any mgmt signals.
+                */
+               if (mgmt_pending_find(MGMT_OP_SET_POWERED, hdev))
+                       return 0;
        }
 
-       err = cmd_complete(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0, &ev,
-                          sizeof(ev));
-       if (err < 0)
-               goto failed;
-
-send_event:
-       if (changed)
-               err = mgmt_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev, &ev,
-                                sizeof(ev), cmd ? cmd->sk : NULL);
-
-       /* EIR is taken care of separately when powering on the
-        * adapter so only update them here if this is a name change
-        * unrelated to power on.
-        */
-       if (!test_bit(HCI_INIT, &hdev->flags))
-               update_eir(hdev);
-
-failed:
-       if (cmd)
-               mgmt_pending_remove(cmd);
-       return err;
+       return mgmt_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev, &ev, sizeof(ev),
+                         cmd ? cmd->sk : NULL);
 }
 
 int mgmt_read_local_oob_data_reply_complete(struct hci_dev *hdev, u8 *hash,