Merge tag 'scsi-misc' of git://git.kernel.org/pub/scm/linux/kernel/git/jejb/scsi
[linux-2.6-microblaze.git] / net / bluetooth / hci_request.c
index e55976d..805ce54 100644 (file)
@@ -272,12 +272,16 @@ int hci_req_sync(struct hci_dev *hdev, int (*req)(struct hci_request *req,
 {
        int ret;
 
-       if (!test_bit(HCI_UP, &hdev->flags))
-               return -ENETDOWN;
-
        /* Serialize all requests */
        hci_req_sync_lock(hdev);
-       ret = __hci_req_sync(hdev, req, opt, timeout, hci_status);
+       /* check the state after obtaing the lock to protect the HCI_UP
+        * against any races from hci_dev_do_close when the controller
+        * gets removed.
+        */
+       if (test_bit(HCI_UP, &hdev->flags))
+               ret = __hci_req_sync(hdev, req, opt, timeout, hci_status);
+       else
+               ret = -ENETDOWN;
        hci_req_sync_unlock(hdev);
 
        return ret;