BT_DBG("All LE pending actions cleared");
 }
 
+static int hci_dev_shutdown(struct hci_dev *hdev)
+{
+       int err = 0;
+       /* Similar to how we first do setup and then set the exclusive access
+        * bit for userspace, we must first unset userchannel and then clean up.
+        * Otherwise, the kernel can't properly use the hci channel to clean up
+        * the controller (some shutdown routines require sending additional
+        * commands to the controller for example).
+        */
+       bool was_userchannel =
+               hci_dev_test_and_clear_flag(hdev, HCI_USER_CHANNEL);
+
+       if (!hci_dev_test_flag(hdev, HCI_UNREGISTER) &&
+           test_bit(HCI_UP, &hdev->flags)) {
+               /* Execute vendor specific shutdown routine */
+               if (hdev->shutdown)
+                       err = hdev->shutdown(hdev);
+       }
+
+       if (was_userchannel)
+               hci_dev_set_flag(hdev, HCI_USER_CHANNEL);
+
+       return err;
+}
+
 int hci_dev_close_sync(struct hci_dev *hdev)
 {
        bool auto_off;
                hdev->adv_instance_timeout = 0;
        }
 
-       if (!hci_dev_test_flag(hdev, HCI_UNREGISTER) &&
-           !hci_dev_test_flag(hdev, HCI_USER_CHANNEL) &&
-           test_bit(HCI_UP, &hdev->flags)) {
-               /* Execute vendor specific shutdown routine */
-               if (hdev->shutdown)
-                       err = hdev->shutdown(hdev);
-       }
+       err = hci_dev_shutdown(hdev);
 
        if (!test_and_clear_bit(HCI_UP, &hdev->flags)) {
                cancel_delayed_work_sync(&hdev->cmd_timer);