]> git.proxmox.com Git - mirror_edk2.git/blobdiff - EdkModulePkg/Bus/Usb/UsbMassStorage/Dxe/UsbMassStorage.c
Add a lock to protect the critical region in Service APIs for gEfiBlockIoProtocolGuid...
[mirror_edk2.git] / EdkModulePkg / Bus / Usb / UsbMassStorage / Dxe / UsbMassStorage.c
index 932b8c5ed90bb13d172684f26846d1e8b1d680e6..9caa006715feef8313ab104a531a0a54ed35dd2b 100644 (file)
@@ -1,18 +1,18 @@
 /*++\r
 \r
-Copyright (c) 2006, Intel Corporation                                                         \r
-All rights reserved. This program and the accompanying materials                          \r
-are licensed and made available under the terms and conditions of the BSD License         \r
-which accompanies this distribution.  The full text of the license may be found at        \r
-http://opensource.org/licenses/bsd-license.php                                            \r
-                                                                                          \r
-THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,                     \r
-WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.             \r
+Copyright (c) 2006 - 2007, Intel Corporation\r
+All rights reserved. This program and the accompanying materials\r
+are licensed and made available under the terms and conditions of the BSD License\r
+which accompanies this distribution.  The full text of the license may be found at\r
+http://opensource.org/licenses/bsd-license.php\r
+\r
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
 \r
 Module Name:\r
 \r
   UsbMassStorage.c\r
-    \r
+\r
 Abstract:\r
 \r
   USB Mass Storage Driver\r
@@ -24,44 +24,6 @@ Revision History
 #include "UsbMassStorage.h"\r
 #include "UsbMassStorageHelper.h"\r
 \r
-extern EFI_COMPONENT_NAME_PROTOCOL  gUsbMassStorageComponentName;\r
-\r
-//\r
-// Prototypes\r
-// Driver model protocol interface\r
-//\r
-EFI_STATUS\r
-EFIAPI\r
-USBMassStorageDriverBindingEntryPoint (\r
-  IN EFI_HANDLE           ImageHandle,\r
-  IN EFI_SYSTEM_TABLE     *SystemTable\r
-  );\r
-\r
-EFI_STATUS\r
-EFIAPI\r
-USBFloppyDriverBindingSupported (\r
-  IN EFI_DRIVER_BINDING_PROTOCOL    *This,\r
-  IN EFI_HANDLE                     Controller,\r
-  IN EFI_DEVICE_PATH_PROTOCOL       *RemainingDevicePath\r
-  );\r
-\r
-EFI_STATUS\r
-EFIAPI\r
-USBFloppyDriverBindingStart (\r
-  IN EFI_DRIVER_BINDING_PROTOCOL    *This,\r
-  IN EFI_HANDLE                     Controller,\r
-  IN EFI_DEVICE_PATH_PROTOCOL       *RemainingDevicePath\r
-  );\r
-\r
-EFI_STATUS\r
-EFIAPI\r
-USBFloppyDriverBindingStop (\r
-  IN  EFI_DRIVER_BINDING_PROTOCOL    *This,\r
-  IN  EFI_HANDLE                     Controller,\r
-  IN  UINTN                          NumberOfChildren,\r
-  IN  EFI_HANDLE                     *ChildHandleBuffer\r
-  );\r
-\r
 //\r
 // Block I/O Protocol Interface\r
 //\r
@@ -109,7 +71,7 @@ EFI_DRIVER_BINDING_PROTOCOL         gUSBFloppyDriverBinding = {
   USBFloppyDriverBindingSupported,\r
   USBFloppyDriverBindingStart,\r
   USBFloppyDriverBindingStop,\r
-  0x10,\r
+  0xa,\r
   NULL,\r
   NULL\r
 };\r
@@ -192,11 +154,11 @@ USBFloppyDriverBindingStart (
     EFI_OUT_OF_RESOURCES- Can't allocate memory resources\r
     EFI_ALREADY_STARTED - Thios driver has been started\r
 --*/\r
-{ \r
-  EFI_STATUS                Status; \r
+{\r
+  EFI_STATUS                Status;\r
   EFI_USB_ATAPI_PROTOCOL    *AtapiProtocol;\r
   USB_FLOPPY_DEV            *UsbFloppyDevice;\r
-  \r
+\r
   UsbFloppyDevice = NULL;\r
   //\r
   // Check whether Usb Atapi Protocol attached on the controller handle.\r
@@ -311,7 +273,7 @@ USBFloppyDriverBindingStop (
     EFI_DEVICE_ERROR\r
     others\r
 \r
---*/  \r
+--*/\r
 {\r
   EFI_STATUS            Status;\r
   USB_FLOPPY_DEV        *UsbFloppyDevice;\r
@@ -373,21 +335,24 @@ USBFloppyReset (
 \r
   Routine Description:\r
     Implements EFI_BLOCK_IO_PROTOCOL.Reset() function.\r
-  \r
+\r
   Arguments:\r
     This     The EFI_BLOCK_IO_PROTOCOL instance.\r
     ExtendedVerification\r
               Indicates that the driver may perform a more exhaustive\r
               verification operation of the device during reset.\r
               (This parameter is ingored in this driver.)\r
-    \r
-  Returns:  \r
+\r
+  Returns:\r
     EFI_SUCCESS - Success\r
---*/      \r
+--*/\r
 {\r
   USB_FLOPPY_DEV          *UsbFloppyDevice;\r
   EFI_USB_ATAPI_PROTOCOL  *UsbAtapiInterface;\r
   EFI_STATUS              Status;\r
+  EFI_TPL                 OldTpl;\r
+\r
+  OldTpl = gBS->RaiseTPL (EFI_TPL_CALLBACK);\r
 \r
   UsbFloppyDevice   = USB_FLOPPY_DEV_FROM_THIS (This);\r
 \r
@@ -396,7 +361,9 @@ USBFloppyReset (
   //\r
   // directly calling EFI_USB_ATAPI_PROTOCOL.Reset() to implement reset.\r
   //\r
-  Status = UsbAtapiInterface->UsbAtapiReset (UsbAtapiInterface, TRUE);\r
+  Status = UsbAtapiInterface->UsbAtapiReset (UsbAtapiInterface, ExtendedVerification);\r
+\r
+  gBS->RestoreTPL (OldTpl);\r
 \r
   return Status;\r
 }\r
@@ -415,26 +382,26 @@ USBFloppyReadBlocks (
 \r
   Routine Description:\r
     Implements EFI_BLOCK_IO_PROTOCOL.ReadBlocks() function.\r
-  \r
+\r
   Arguments:\r
     This     The EFI_BLOCK_IO_PROTOCOL instance.\r
     MediaId  The media id that the read request is for.\r
     LBA      The starting logical block address to read from on the device.\r
     BufferSize\r
-              The size of the Buffer in bytes. This must be a multiple of \r
+              The size of the Buffer in bytes. This must be a multiple of\r
               the intrinsic block size of the device.\r
-    Buffer    A pointer to the destination buffer for the data. The caller \r
+    Buffer    A pointer to the destination buffer for the data. The caller\r
               is responsible for either having implicit or explicit ownership\r
-              of the buffer.                               \r
-  \r
-  Returns:  \r
+              of the buffer.\r
+\r
+  Returns:\r
     EFI_INVALID_PARAMETER - Parameter is error\r
-    EFI_SUCCESS           - Success  \r
+    EFI_SUCCESS           - Success\r
     EFI_DEVICE_ERROR      - Hardware Error\r
     EFI_NO_MEDIA          - No media\r
     EFI_MEDIA_CHANGED     - Media Change\r
     EFI_BAD_BUFFER_SIZE   - Buffer size is bad\r
- --*/      \r
+ --*/\r
 {\r
   USB_FLOPPY_DEV      *UsbFloppyDevice;\r
   EFI_STATUS          Status;\r
@@ -443,28 +410,24 @@ USBFloppyReadBlocks (
   UINTN               NumberOfBlocks;\r
   BOOLEAN             MediaChange;\r
   EFI_TPL             OldTpl;\r
-  UINT32              Retry;\r
 \r
-  OldTpl          = gBS->RaiseTPL (EFI_TPL_NOTIFY);\r
   Status          = EFI_SUCCESS;\r
   MediaChange     = FALSE;\r
-  Retry           = 0;\r
-\r
   UsbFloppyDevice = USB_FLOPPY_DEV_FROM_THIS (This);\r
 \r
   //\r
   // Check parameters\r
   //\r
-  if (!Buffer) {\r
-    Status = EFI_INVALID_PARAMETER;\r
-    goto Done;\r
+  if (Buffer == NULL) {\r
+    return EFI_INVALID_PARAMETER;\r
   }\r
 \r
   if (BufferSize == 0) {\r
-    Status = EFI_SUCCESS;\r
-    goto Done;\r
+    return EFI_SUCCESS;\r
   }\r
 \r
+  OldTpl = gBS->RaiseTPL (EFI_TPL_CALLBACK);\r
+\r
   UsbFloppyTestUnitReady (UsbFloppyDevice);\r
 \r
   Status = UsbFloppyDetectMedia (UsbFloppyDevice, &MediaChange);\r
@@ -475,14 +438,12 @@ USBFloppyReadBlocks (
   }\r
 \r
   if (MediaChange) {\r
-    gBS->RestoreTPL (OldTpl);\r
     gBS->ReinstallProtocolInterface (\r
           UsbFloppyDevice->Handle,\r
           &gEfiBlockIoProtocolGuid,\r
           &UsbFloppyDevice->BlkIo,\r
           &UsbFloppyDevice->BlkIo\r
           );\r
-    gBS->RaiseTPL (EFI_TPL_NOTIFY);\r
   }\r
 \r
   Media           = UsbFloppyDevice->BlkIo.Media;\r
@@ -519,32 +480,32 @@ USBFloppyReadBlocks (
     goto Done;\r
   }\r
 \r
-  if (!EFI_ERROR (Status)) {\r
+  while (NumberOfBlocks > 0) {\r
+\r
+    if (NumberOfBlocks > BLOCK_UNIT) {\r
+      Status = USBFloppyRead10 (UsbFloppyDevice, Buffer, LBA, BLOCK_UNIT);\r
+    } else {\r
+      Status = USBFloppyRead10 (UsbFloppyDevice, Buffer, LBA, NumberOfBlocks);\r
+    }\r
 \r
-    Status = USBFloppyRead10 (UsbFloppyDevice, Buffer, LBA, 1);\r
     if (EFI_ERROR (Status)) {\r
       This->Reset (This, TRUE);\r
       Status = EFI_DEVICE_ERROR;\r
       goto Done;\r
     }\r
 \r
-    LBA += 1;\r
-    NumberOfBlocks -= 1;\r
-    Buffer = (UINT8 *) Buffer + This->Media->BlockSize;\r
-\r
-    if (NumberOfBlocks == 0) {\r
-      Status = EFI_SUCCESS;\r
-      goto Done;\r
+    if (NumberOfBlocks > BLOCK_UNIT) {\r
+       NumberOfBlocks -= BLOCK_UNIT;\r
+       LBA += BLOCK_UNIT;\r
+       Buffer = (UINT8 *) Buffer + This->Media->BlockSize * BLOCK_UNIT;\r
+    } else {\r
+       NumberOfBlocks -= NumberOfBlocks;\r
+       LBA += NumberOfBlocks;\r
+       Buffer = (UINT8 *) Buffer + This->Media->BlockSize * NumberOfBlocks;\r
     }\r
+ }\r
 \r
-    Status = USBFloppyRead10 (UsbFloppyDevice, Buffer, LBA, NumberOfBlocks);\r
-    if (EFI_ERROR (Status)) {\r
-      This->Reset (This, TRUE);\r
-      Status = EFI_DEVICE_ERROR;\r
-    }\r
-  }\r
-\r
-Done:\r
+ Done:\r
   gBS->RestoreTPL (OldTpl);\r
   return Status;\r
 }\r
@@ -563,29 +524,29 @@ USBFloppyWriteBlocks (
 \r
   Routine Description:\r
     Implements EFI_BLOCK_IO_PROTOCOL.WriteBlocks() function.\r
-  \r
+\r
   Arguments:\r
     This     The EFI_BLOCK_IO_PROTOCOL instance.\r
     MediaId  The media id that the write request is for.\r
     LBA      The starting logical block address to be written.\r
-             The caller is responsible for writing to only \r
+             The caller is responsible for writing to only\r
              legitimate locations.\r
     BufferSize\r
-              The size of the Buffer in bytes. This must be a multiple of \r
+              The size of the Buffer in bytes. This must be a multiple of\r
               the intrinsic block size of the device.\r
-    Buffer    A pointer to the source buffer for the data. The caller \r
+    Buffer    A pointer to the source buffer for the data. The caller\r
               is responsible for either having implicit or explicit ownership\r
-              of the buffer.                               \r
-  \r
-  Returns:  \r
+              of the buffer.\r
+\r
+  Returns:\r
     EFI_INVALID_PARAMETER - Parameter is error\r
-    EFI_SUCCESS           - Success  \r
+    EFI_SUCCESS           - Success\r
     EFI_DEVICE_ERROR      - Hardware Error\r
     EFI_NO_MEDIA          - No media\r
     EFI_MEDIA_CHANGED     - Media Change\r
     EFI_BAD_BUFFER_SIZE   - Buffer size is bad\r
 \r
---*/        \r
+--*/\r
 {\r
   USB_FLOPPY_DEV      *UsbFloppyDevice;\r
   EFI_STATUS          Status;\r
@@ -594,28 +555,25 @@ USBFloppyWriteBlocks (
   UINTN               NumberOfBlocks;\r
   BOOLEAN             MediaChange;\r
   EFI_TPL             OldTpl;\r
-  UINT32              Retry;\r
 \r
-  OldTpl          = gBS->RaiseTPL (EFI_TPL_NOTIFY);\r
   Status          = EFI_SUCCESS;\r
   MediaChange     = FALSE;\r
-  Retry           = 0;\r
 \r
   UsbFloppyDevice = USB_FLOPPY_DEV_FROM_THIS (This);\r
 \r
   //\r
   // Check parameters\r
   //\r
-  if (!Buffer) {\r
-    Status = EFI_INVALID_PARAMETER;\r
-    goto Done;\r
+  if (Buffer == NULL) {\r
+    return EFI_INVALID_PARAMETER;\r
   }\r
 \r
   if (BufferSize == 0) {\r
-    Status = EFI_SUCCESS;\r
-    goto Done;\r
+    return EFI_SUCCESS;\r
   }\r
 \r
+  OldTpl = gBS->RaiseTPL (EFI_TPL_CALLBACK);\r
+\r
   UsbFloppyTestUnitReady (UsbFloppyDevice);\r
 \r
   Status = UsbFloppyDetectMedia (UsbFloppyDevice, &MediaChange);\r
@@ -626,14 +584,12 @@ USBFloppyWriteBlocks (
   }\r
 \r
   if (MediaChange) {\r
-    gBS->RestoreTPL (OldTpl);\r
     gBS->ReinstallProtocolInterface (\r
           UsbFloppyDevice->Handle,\r
           &gEfiBlockIoProtocolGuid,\r
           &UsbFloppyDevice->BlkIo,\r
           &UsbFloppyDevice->BlkIo\r
           );\r
-    gBS->RaiseTPL (EFI_TPL_NOTIFY);\r
   }\r
 \r
   Media           = UsbFloppyDevice->BlkIo.Media;\r
@@ -675,29 +631,30 @@ USBFloppyWriteBlocks (
     goto Done;\r
   }\r
 \r
-  if (!EFI_ERROR (Status)) {\r
-    Status = USBFloppyWrite10 (UsbFloppyDevice, Buffer, LBA, 1);\r
-    if (EFI_ERROR (Status)) {\r
-      This->Reset (This, TRUE);\r
-      Status = EFI_DEVICE_ERROR;\r
-      goto Done;\r
-    }\r
-\r
-    LBA += 1;\r
-    NumberOfBlocks -= 1;\r
-    Buffer = (UINT8 *) Buffer + This->Media->BlockSize;\r
+  while (NumberOfBlocks > 0) {\r
 \r
-    if (NumberOfBlocks == 0) {\r
-      Status = EFI_SUCCESS;\r
-      goto Done;\r
+    if (NumberOfBlocks > BLOCK_UNIT) {\r
+      Status = USBFloppyWrite10 (UsbFloppyDevice, Buffer, LBA, BLOCK_UNIT);\r
+    } else {\r
+      Status = USBFloppyWrite10 (UsbFloppyDevice, Buffer, LBA, NumberOfBlocks);\r
     }\r
 \r
-    Status = USBFloppyWrite10 (UsbFloppyDevice, Buffer, LBA, NumberOfBlocks);\r
     if (EFI_ERROR (Status)) {\r
       This->Reset (This, TRUE);\r
       Status = EFI_DEVICE_ERROR;\r
+      goto Done;\r
     }\r
-  }\r
+\r
+    if (NumberOfBlocks > BLOCK_UNIT) {\r
+       NumberOfBlocks -= BLOCK_UNIT;\r
+       LBA += BLOCK_UNIT;\r
+       Buffer = (UINT8 *) Buffer + This->Media->BlockSize * BLOCK_UNIT;\r
+    } else {\r
+       NumberOfBlocks -= NumberOfBlocks;\r
+       LBA += NumberOfBlocks;\r
+       Buffer = (UINT8 *) Buffer + This->Media->BlockSize * NumberOfBlocks;\r
+    }\r
+ }\r
 \r
 Done:\r
   gBS->RestoreTPL (OldTpl);\r
@@ -715,13 +672,13 @@ USBFloppyFlushBlocks (
   Routine Description:\r
     Implements EFI_BLOCK_IO_PROTOCOL.FlushBlocks() function.\r
     (In this driver, this function just returns EFI_SUCCESS.)\r
-  \r
+\r
   Arguments:\r
     This     The EFI_BLOCK_IO_PROTOCOL instance.\r
-  \r
-  Returns:  \r
+\r
+  Returns:\r
     EFI_SUCCESS - Success\r
---*/    \r
+--*/\r
 {\r
   return EFI_SUCCESS;\r
 }\r