]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Bus/Usb/UsbMassStorageDxe/UsbMassImpl.c
MdeModulePkg: Clean up source files
[mirror_edk2.git] / MdeModulePkg / Bus / Usb / UsbMassStorageDxe / UsbMassImpl.c
index 6efdf10af1363a2d692bcfa9b8a6f244587a3b79..62bf3c588306fd7ebb193c567efe14f471aa9f30 100644 (file)
@@ -1,8 +1,8 @@
 /** @file\r
   USB Mass Storage Driver that manages USB Mass Storage Device and produces Block I/O Protocol.\r
 \r
-Copyright (c) 2007 - 2008, Intel Corporation\r
-All rights reserved. This program and the accompanying materials\r
+Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR>\r
+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
@@ -12,16 +12,16 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 \r
 **/\r
 \r
-#include "UsbMassImpl.h"\r
+#include "UsbMass.h"\r
 \r
+#define USB_MASS_TRANSPORT_COUNT    3\r
 //\r
-// Array of USB transport interfaces. \r
+// Array of USB transport interfaces.\r
 //\r
-USB_MASS_TRANSPORT *mUsbMassTransport[] = {\r
+USB_MASS_TRANSPORT *mUsbMassTransport[USB_MASS_TRANSPORT_COUNT] = {\r
   &mUsbCbi0Transport,\r
   &mUsbCbi1Transport,\r
   &mUsbBotTransport,\r
-  NULL\r
 };\r
 \r
 EFI_DRIVER_BINDING_PROTOCOL gUSBMassDriverBinding = {\r
@@ -36,7 +36,7 @@ EFI_DRIVER_BINDING_PROTOCOL gUSBMassDriverBinding = {
 /**\r
   Reset the block device.\r
 \r
-  This function implements EFI_BLOCK_IO_PROTOCOL.Reset(). \r
+  This function implements EFI_BLOCK_IO_PROTOCOL.Reset().\r
   It resets the block device hardware.\r
   ExtendedVerification is ignored in this implementation.\r
 \r
@@ -60,10 +60,10 @@ UsbMassReset (
   EFI_STATUS      Status;\r
 \r
   //\r
-  // Raise TPL to TPL_NOTIFY to serialize all its operations\r
+  // Raise TPL to TPL_CALLBACK to serialize all its operations\r
   // to protect shared data structures.\r
   //\r
-  OldTpl  = gBS->RaiseTPL (TPL_NOTIFY);\r
+  OldTpl  = gBS->RaiseTPL (TPL_CALLBACK);\r
 \r
   UsbMass = USB_MASS_DEVICE_FROM_BLOCK_IO (This);\r
   Status  = UsbMass->Transport->Reset (UsbMass->Context, ExtendedVerification);\r
@@ -76,7 +76,7 @@ UsbMassReset (
 /**\r
   Reads the requested number of blocks from the device.\r
 \r
-  This function implements EFI_BLOCK_IO_PROTOCOL.ReadBlocks(). \r
+  This function implements EFI_BLOCK_IO_PROTOCOL.ReadBlocks().\r
   It reads the requested number of blocks from the device.\r
   All the blocks are read, or an error is returned.\r
 \r
@@ -114,17 +114,10 @@ UsbMassReadBlocks (
   UINTN               TotalBlock;\r
 \r
   //\r
-  // First, validate the parameters\r
-  //\r
-  if ((Buffer == NULL) || (BufferSize == 0)) {\r
-    return EFI_INVALID_PARAMETER;\r
-  }\r
-\r
-  //\r
-  // Raise TPL to TPL_NOTIFY to serialize all its operations\r
+  // Raise TPL to TPL_CALLBACK to serialize all its operations\r
   // to protect shared data structures.\r
   //\r
-  OldTpl  = gBS->RaiseTPL (TPL_NOTIFY);\r
+  OldTpl  = gBS->RaiseTPL (TPL_CALLBACK);\r
   UsbMass = USB_MASS_DEVICE_FROM_BLOCK_IO (This);\r
   Media   = &UsbMass->BlockIoMedia;\r
 \r
@@ -140,6 +133,26 @@ UsbMassReadBlocks (
     }\r
   }\r
 \r
+  if (!(Media->MediaPresent)) {\r
+    Status = EFI_NO_MEDIA;\r
+    goto ON_EXIT;\r
+  }\r
+\r
+  if (MediaId != Media->MediaId) {\r
+    Status = EFI_MEDIA_CHANGED;\r
+    goto ON_EXIT;\r
+  }\r
+\r
+  if (BufferSize == 0) {\r
+    Status = EFI_SUCCESS;\r
+    goto ON_EXIT;\r
+  }\r
+\r
+  if (Buffer == NULL) {\r
+    Status = EFI_INVALID_PARAMETER;\r
+    goto ON_EXIT;\r
+  }\r
+\r
   //\r
   // BufferSize must be a multiple of the intrinsic block size of the device.\r
   //\r
@@ -158,17 +171,12 @@ UsbMassReadBlocks (
     goto ON_EXIT;\r
   }\r
 \r
-  if (!(Media->MediaPresent)) {\r
-    Status = EFI_NO_MEDIA;\r
-    goto ON_EXIT;\r
-  }\r
-\r
-  if (MediaId != Media->MediaId) {\r
-    Status = EFI_MEDIA_CHANGED;\r
-    goto ON_EXIT;\r
+  if (UsbMass->Cdb16Byte) {\r
+    Status = UsbBootReadBlocks16 (UsbMass, Lba, TotalBlock, Buffer);\r
+  } else {\r
+    Status = UsbBootReadBlocks (UsbMass, (UINT32) Lba, TotalBlock, Buffer);\r
   }\r
 \r
-  Status = UsbBootReadBlocks (UsbMass, (UINT32) Lba, TotalBlock, Buffer);\r
   if (EFI_ERROR (Status)) {\r
     DEBUG ((EFI_D_ERROR, "UsbMassReadBlocks: UsbBootReadBlocks (%r) -> Reset\n", Status));\r
     UsbMassReset (This, TRUE);\r
@@ -183,7 +191,7 @@ ON_EXIT:
 /**\r
   Writes a specified number of blocks to the device.\r
 \r
-  This function implements EFI_BLOCK_IO_PROTOCOL.WriteBlocks(). \r
+  This function implements EFI_BLOCK_IO_PROTOCOL.WriteBlocks().\r
   It writes a specified number of blocks to the device.\r
   All blocks are written, or an error is returned.\r
 \r
@@ -222,17 +230,10 @@ UsbMassWriteBlocks (
   UINTN               TotalBlock;\r
 \r
   //\r
-  // First, validate the parameters\r
-  //\r
-  if ((Buffer == NULL) || (BufferSize == 0)) {\r
-    return EFI_INVALID_PARAMETER;\r
-  }\r
-\r
-  //\r
-  // Raise TPL to TPL_NOTIFY to serialize all its operations\r
+  // Raise TPL to TPL_CALLBACK to serialize all its operations\r
   // to protect shared data structures.\r
   //\r
-  OldTpl  = gBS->RaiseTPL (TPL_NOTIFY);\r
+  OldTpl  = gBS->RaiseTPL (TPL_CALLBACK);\r
   UsbMass = USB_MASS_DEVICE_FROM_BLOCK_IO (This);\r
   Media   = &UsbMass->BlockIoMedia;\r
 \r
@@ -248,6 +249,26 @@ UsbMassWriteBlocks (
     }\r
   }\r
 \r
+  if (!(Media->MediaPresent)) {\r
+    Status = EFI_NO_MEDIA;\r
+    goto ON_EXIT;\r
+  }\r
+\r
+  if (MediaId != Media->MediaId) {\r
+    Status = EFI_MEDIA_CHANGED;\r
+    goto ON_EXIT;\r
+  }\r
+\r
+  if (BufferSize == 0) {\r
+    Status = EFI_SUCCESS;\r
+    goto ON_EXIT;\r
+  }\r
+\r
+  if (Buffer == NULL) {\r
+    Status = EFI_INVALID_PARAMETER;\r
+    goto ON_EXIT;\r
+  }\r
+\r
   //\r
   // BufferSize must be a multiple of the intrinsic block size of the device.\r
   //\r
@@ -266,21 +287,16 @@ UsbMassWriteBlocks (
     goto ON_EXIT;\r
   }\r
 \r
-  if (!(Media->MediaPresent)) {\r
-    Status = EFI_NO_MEDIA;\r
-    goto ON_EXIT;\r
-  }\r
-\r
-  if (MediaId != Media->MediaId) {\r
-    Status = EFI_MEDIA_CHANGED;\r
-    goto ON_EXIT;\r
-  }\r
-\r
   //\r
   // Try to write the data even the device is marked as ReadOnly,\r
   // and clear the status should the write succeed.\r
   //\r
-  Status = UsbBootWriteBlocks (UsbMass, (UINT32) Lba, TotalBlock, Buffer);\r
+  if (UsbMass->Cdb16Byte) {\r
+    Status = UsbBootWriteBlocks16 (UsbMass, Lba, TotalBlock, Buffer);\r
+  } else {\r
+    Status = UsbBootWriteBlocks (UsbMass, (UINT32) Lba, TotalBlock, Buffer);\r
+  }\r
+\r
   if (EFI_ERROR (Status)) {\r
     DEBUG ((EFI_D_ERROR, "UsbMassWriteBlocks: UsbBootWriteBlocks (%r) -> Reset\n", Status));\r
     UsbMassReset (This, TRUE);\r
@@ -330,7 +346,6 @@ UsbMassInitMedia (
 {\r
   EFI_BLOCK_IO_MEDIA          *Media;\r
   EFI_STATUS                  Status;\r
-  UINTN                       Index;\r
 \r
   Media = &UsbMass->BlockIoMedia;\r
 \r
@@ -345,25 +360,15 @@ UsbMassInitMedia (
   Media->IoAlign          = 0;\r
   Media->MediaId          = 1;\r
 \r
-  //\r
-  // Some device may spend several seconds before it is ready.\r
-  // Try several times before giving up. Wait 5s at most.\r
-  //\r
-  Status = EFI_SUCCESS;\r
-\r
-  for (Index = 0; Index < USB_BOOT_INIT_MEDIA_RETRY; Index++) {\r
-\r
-    Status = UsbBootGetParams (UsbMass);\r
-    if ((Status != EFI_MEDIA_CHANGED) && (Status != EFI_NOT_READY) && (Status != EFI_TIMEOUT)) {\r
-      break;\r
-    }\r
-\r
-    Status = UsbBootIsUnitReady (UsbMass);\r
-    if (EFI_ERROR (Status)) {\r
-      gBS->Stall (USB_BOOT_RETRY_UNIT_READY_STALL * (Index + 1));\r
-    }\r
+  Status = UsbBootGetParams (UsbMass);\r
+  DEBUG ((DEBUG_INFO, "UsbMassInitMedia: UsbBootGetParams (%r)\n", Status));\r
+  if (Status == EFI_MEDIA_CHANGED) {\r
+    //\r
+    // Some USB storage devices may report MEDIA_CHANGED sense key when hot-plugged.\r
+    // Treat it as SUCCESS\r
+    //\r
+    Status = EFI_SUCCESS;\r
   }\r
-\r
   return Status;\r
 }\r
 \r
@@ -376,7 +381,7 @@ UsbMassInitMedia (
   @param  This            The USB mass driver's driver binding.\r
   @param  Controller      The device to test.\r
   @param  Transport       The pointer to pointer to USB_MASS_TRANSPORT.\r
-  @param  Context         The passing parameter.\r
+  @param  Context         The parameter for USB_MASS_DEVICE.Context.\r
   @param  MaxLun          Get the MaxLun if is BOT dev.\r
 \r
   @retval EFI_SUCCESS     The initialization is successful.\r
@@ -397,7 +402,7 @@ UsbMassInitTransport (
   EFI_USB_INTERFACE_DESCRIPTOR  Interface;\r
   UINT8                         Index;\r
   EFI_STATUS                    Status;\r
\r
+\r
   Status = gBS->OpenProtocol (\r
                   Controller,\r
                   &gEfiUsbIoProtocolGuid,\r
@@ -410,12 +415,12 @@ UsbMassInitTransport (
   if (EFI_ERROR (Status)) {\r
     return Status;\r
   }\r
-  \r
+\r
   Status = UsbIo->UsbGetInterfaceDescriptor (UsbIo, &Interface);\r
   if (EFI_ERROR (Status)) {\r
     goto ON_EXIT;\r
   }\r
-  \r
+\r
   Status = EFI_UNSUPPORTED;\r
 \r
   //\r
@@ -424,7 +429,7 @@ UsbMassInitTransport (
   // If not found, return EFI_UNSUPPORTED.\r
   // If found, execute USB_MASS_TRANSPORT.Init() to initialize the transport context.\r
   //\r
-  for (Index = 0; mUsbMassTransport[Index] != NULL; Index++) {\r
+  for (Index = 0; Index < USB_MASS_TRANSPORT_COUNT; Index++) {\r
     *Transport = mUsbMassTransport[Index];\r
 \r
     if (Interface.InterfaceProtocol == (*Transport)->Protocol) {\r
@@ -438,7 +443,7 @@ UsbMassInitTransport (
   }\r
 \r
   //\r
-  // For BOT device, try to get its max LUN. \r
+  // For BOT device, try to get its max LUN.\r
   // If max LUN is 0, then it is a non-lun device.\r
   // Otherwise, it is a multi-lun device.\r
   //\r
@@ -453,26 +458,26 @@ ON_EXIT:
          This->DriverBindingHandle,\r
          Controller\r
          );\r
-  return Status;  \r
+  return Status;\r
 }\r
 \r
 /**\r
-  Usb mass storage driver initializes multi lun.\r
+  Initialize data for device that supports multiple LUNSs.\r
 \r
-  @param  This            The USB mass driver's driver binding.\r
-  @param  Controller      The device to test.\r
-  @param  Transport       The pointer to USB_MASS_TRANSPORT.\r
-  @param  Context         The passing parameter.\r
-  @param  DevicePath      The remaining device path\r
-  @param  MaxLun          The MaxLun number passed.\r
+  @param  This                 The Driver Binding Protocol instance.\r
+  @param  Controller           The device to initialize.\r
+  @param  Transport            Pointer to USB_MASS_TRANSPORT.\r
+  @param  Context              Parameter for USB_MASS_DEVICE.Context.\r
+  @param  DevicePath           The remaining device path.\r
+  @param  MaxLun               The max LUN number.\r
 \r
-  @retval EFI_SUCCESS     Initialization is success.\r
-  @retval Other           Initialization fails.\r
+  @retval EFI_SUCCESS          At least one LUN is initialized successfully.\r
+  @retval EFI_NOT_FOUND        Fail to initialize any of multiple LUNs.\r
 \r
 **/\r
 EFI_STATUS\r
 UsbMassInitMultiLun (\r
-  IN  EFI_DRIVER_BINDING_PROTOCOL  *This,\r
+  IN EFI_DRIVER_BINDING_PROTOCOL   *This,\r
   IN EFI_HANDLE                    Controller,\r
   IN USB_MASS_TRANSPORT            *Transport,\r
   IN VOID                          *Context,\r
@@ -485,20 +490,19 @@ UsbMassInitMultiLun (
   DEVICE_LOGICAL_UNIT_DEVICE_PATH  LunNode;\r
   UINT8                            Index;\r
   EFI_STATUS                       Status;\r
+  EFI_STATUS                       ReturnStatus;\r
 \r
   ASSERT (MaxLun > 0);\r
+  ReturnStatus = EFI_NOT_FOUND;\r
 \r
-  for (Index = 0; Index <= MaxLun; Index++) { \r
+  for (Index = 0; Index <= MaxLun; Index++) {\r
 \r
     DEBUG ((EFI_D_INFO, "UsbMassInitMultiLun: Start to initialize No.%d logic unit\n", Index));\r
-    \r
+\r
     UsbIo   = NULL;\r
     UsbMass = AllocateZeroPool (sizeof (USB_MASS_DEVICE));\r
-    if (UsbMass == NULL) {\r
-      Status = EFI_OUT_OF_RESOURCES;\r
-      goto ON_ERROR;\r
-    }\r
-      \r
+    ASSERT (UsbMass != NULL);\r
+\r
     UsbMass->Signature            = USB_MASS_SIGNATURE;\r
     UsbMass->UsbIo                = UsbIo;\r
     UsbMass->BlockIo.Media        = &UsbMass->BlockIoMedia;\r
@@ -510,45 +514,39 @@ UsbMassInitMultiLun (
     UsbMass->Transport            = Transport;\r
     UsbMass->Context              = Context;\r
     UsbMass->Lun                  = Index;\r
-    \r
+\r
     //\r
-    // Get the storage's parameters, such as last block number.\r
-    // then install the BLOCK_IO\r
+    // Initialize the media parameter data for EFI_BLOCK_IO_MEDIA of Block I/O Protocol.\r
     //\r
     Status = UsbMassInitMedia (UsbMass);\r
-    if (!EFI_ERROR (Status)) {\r
-      if ((UsbMass->Pdt != USB_PDT_DIRECT_ACCESS) && \r
-           (UsbMass->Pdt != USB_PDT_CDROM) &&\r
-           (UsbMass->Pdt != USB_PDT_OPTICAL) && \r
-           (UsbMass->Pdt != USB_PDT_SIMPLE_DIRECT)) {\r
-        DEBUG ((EFI_D_ERROR, "UsbMassInitMultiLun: Found an unsupported peripheral type[%d]\n", UsbMass->Pdt));\r
-        goto ON_ERROR;\r
-      }\r
-    } else if (Status != EFI_NO_MEDIA){\r
+    if ((EFI_ERROR (Status)) && (Status != EFI_NO_MEDIA)) {\r
       DEBUG ((EFI_D_ERROR, "UsbMassInitMultiLun: UsbMassInitMedia (%r)\n", Status));\r
-      goto ON_ERROR;\r
+      FreePool (UsbMass);\r
+      continue;\r
     }\r
 \r
     //\r
-    // Create a device path node of device logic unit, and append it \r
+    // Create a device path node for device logic unit, and append it.\r
     //\r
     LunNode.Header.Type    = MESSAGING_DEVICE_PATH;\r
     LunNode.Header.SubType = MSG_DEVICE_LOGICAL_UNIT_DP;\r
     LunNode.Lun            = UsbMass->Lun;\r
-  \r
+\r
     SetDevicePathNodeLength (&LunNode.Header, sizeof (LunNode));\r
-  \r
+\r
     UsbMass->DevicePath = AppendDevicePathNode (DevicePath, &LunNode.Header);\r
-  \r
+\r
     if (UsbMass->DevicePath == NULL) {\r
       DEBUG ((EFI_D_ERROR, "UsbMassInitMultiLun: failed to create device logic unit device path\n"));\r
-  \r
       Status = EFI_OUT_OF_RESOURCES;\r
-      goto ON_ERROR;\r
+      FreePool (UsbMass);\r
+      continue;\r
     }\r
 \r
+    InitializeDiskInfo (UsbMass);\r
+\r
     //\r
-    // Create a UsbMass handle for each lun, and install blockio and devicepath protocols.\r
+    // Create a new handle for each LUN, and install Block I/O Protocol and Device Path Protocol.\r
     //\r
     Status = gBS->InstallMultipleProtocolInterfaces (\r
                     &UsbMass->Controller,\r
@@ -556,16 +554,20 @@ UsbMassInitMultiLun (
                     UsbMass->DevicePath,\r
                     &gEfiBlockIoProtocolGuid,\r
                     &UsbMass->BlockIo,\r
+                    &gEfiDiskInfoProtocolGuid,\r
+                    &UsbMass->DiskInfo,\r
                     NULL\r
                     );\r
-    \r
+\r
     if (EFI_ERROR (Status)) {\r
       DEBUG ((EFI_D_ERROR, "UsbMassInitMultiLun: InstallMultipleProtocolInterfaces (%r)\n", Status));\r
-      goto ON_ERROR;\r
+      FreePool (UsbMass->DevicePath);\r
+      FreePool (UsbMass);\r
+      continue;\r
     }\r
 \r
     //\r
-    // Open UsbIo protocol by child to setup a parent-child relationship.\r
+    // Open USB I/O Protocol by child to setup a parent-child relationship.\r
     //\r
     Status = gBS->OpenProtocol (\r
                     Controller,\r
@@ -584,51 +586,30 @@ UsbMassInitMultiLun (
              UsbMass->DevicePath,\r
              &gEfiBlockIoProtocolGuid,\r
              &UsbMass->BlockIo,\r
+             &gEfiDiskInfoProtocolGuid,\r
+             &UsbMass->DiskInfo,\r
              NULL\r
              );\r
-      goto ON_ERROR;\r
+      FreePool (UsbMass->DevicePath);\r
+      FreePool (UsbMass);\r
+      continue;\r
     }\r
-    \r
+    ReturnStatus = EFI_SUCCESS;\r
     DEBUG ((EFI_D_INFO, "UsbMassInitMultiLun: Success to initialize No.%d logic unit\n", Index));\r
   }\r
-  \r
-  return EFI_SUCCESS;\r
-\r
-ON_ERROR:\r
-  if (UsbMass->DevicePath != NULL) {\r
-    gBS->FreePool (UsbMass->DevicePath);\r
-  }\r
-  if (UsbMass != NULL) {\r
-    gBS->FreePool (UsbMass);\r
-  }\r
-  if (UsbIo != NULL) {\r
-    gBS->CloseProtocol (\r
-           Controller,\r
-           &gEfiUsbIoProtocolGuid,\r
-           This->DriverBindingHandle,\r
-           UsbMass->Controller\r
-           );\r
-  }\r
 \r
-  //\r
-  // If only success to initialize one lun, return success, or else return error\r
-  //\r
-  if (Index > 0) {\r
-    return EFI_SUCCESS; \r
-  } else {\r
-    return Status;\r
-  } \r
+  return ReturnStatus;\r
 }\r
 \r
 /**\r
-  Initialize No/Unsupported LUN device.\r
+  Initialize data for device that does not support multiple LUNSs.\r
 \r
-  @param This             The USB mass driver's driver binding.\r
-  @param Controller       The device to test.\r
-  @param Transport        The pointer to USB_MASS_TRANSPORT.\r
-  @param Context          The passing parameter.\r
+  @param  This            The Driver Binding Protocol instance.\r
+  @param  Controller      The device to initialize.\r
+  @param  Transport       Pointer to USB_MASS_TRANSPORT.\r
+  @param  Context         Parameter for USB_MASS_DEVICE.Context.\r
 \r
-  @retval EFI_SUCCESS     Initialization is success.\r
+  @retval EFI_SUCCESS     Initialization succeeds.\r
   @retval Other           Initialization fails.\r
 \r
 **/\r
@@ -646,9 +627,8 @@ UsbMassInitNonLun (
 \r
   UsbIo   = NULL;\r
   UsbMass = AllocateZeroPool (sizeof (USB_MASS_DEVICE));\r
-  if (UsbMass == NULL) {\r
-    return EFI_OUT_OF_RESOURCES;\r
-  }\r
+  ASSERT (UsbMass != NULL);\r
+\r
   Status = gBS->OpenProtocol (\r
                   Controller,\r
                   &gEfiUsbIoProtocolGuid,\r
@@ -662,7 +642,7 @@ UsbMassInitNonLun (
     DEBUG ((EFI_D_ERROR, "UsbMassInitNonLun: OpenUsbIoProtocol By Driver (%r)\n", Status));\r
     goto ON_ERROR;\r
   }\r
-  \r
+\r
   UsbMass->Signature            = USB_MASS_SIGNATURE;\r
   UsbMass->Controller           = Controller;\r
   UsbMass->UsbIo                = UsbIo;\r
@@ -674,30 +654,25 @@ UsbMassInitNonLun (
   UsbMass->OpticalStorage       = FALSE;\r
   UsbMass->Transport            = Transport;\r
   UsbMass->Context              = Context;\r
-  \r
+\r
   //\r
-  // Get the storage's parameters, such as last block number.\r
-  // then install the BLOCK_IO\r
+  // Initialize the media parameter data for EFI_BLOCK_IO_MEDIA of Block I/O Protocol.\r
   //\r
   Status = UsbMassInitMedia (UsbMass);\r
-  if (!EFI_ERROR (Status)) {\r
-    if ((UsbMass->Pdt != USB_PDT_DIRECT_ACCESS) && \r
-         (UsbMass->Pdt != USB_PDT_CDROM) &&\r
-         (UsbMass->Pdt != USB_PDT_OPTICAL) && \r
-         (UsbMass->Pdt != USB_PDT_SIMPLE_DIRECT)) {\r
-      DEBUG ((EFI_D_ERROR, "UsbMassInitNonLun: Found an unsupported peripheral type[%d]\n", UsbMass->Pdt));\r
-      goto ON_ERROR;\r
-    }\r
-  } else if (Status != EFI_NO_MEDIA){\r
+  if ((EFI_ERROR (Status)) && (Status != EFI_NO_MEDIA)) {\r
     DEBUG ((EFI_D_ERROR, "UsbMassInitNonLun: UsbMassInitMedia (%r)\n", Status));\r
     goto ON_ERROR;\r
   }\r
-    \r
-  Status = gBS->InstallProtocolInterface (\r
+\r
+  InitializeDiskInfo (UsbMass);\r
+\r
+  Status = gBS->InstallMultipleProtocolInterfaces (\r
                   &Controller,\r
                   &gEfiBlockIoProtocolGuid,\r
-                  EFI_NATIVE_INTERFACE,\r
-                  &UsbMass->BlockIo\r
+                  &UsbMass->BlockIo,\r
+                  &gEfiDiskInfoProtocolGuid,\r
+                  &UsbMass->DiskInfo,\r
+                  NULL\r
                   );\r
   if (EFI_ERROR (Status)) {\r
     goto ON_ERROR;\r
@@ -707,15 +682,17 @@ UsbMassInitNonLun (
 \r
 ON_ERROR:\r
   if (UsbMass != NULL) {\r
-    gBS->FreePool (UsbMass);\r
+    FreePool (UsbMass);\r
   }\r
-  gBS->CloseProtocol (\r
-         Controller,\r
-         &gEfiUsbIoProtocolGuid,\r
-         This->DriverBindingHandle,\r
-         Controller\r
-         );\r
-  return Status;  \r
+  if (UsbIo != NULL) {\r
+    gBS->CloseProtocol (\r
+           Controller,\r
+           &gEfiUsbIoProtocolGuid,\r
+           This->DriverBindingHandle,\r
+           Controller\r
+           );\r
+  }\r
+  return Status;\r
 }\r
 \r
 \r
@@ -777,7 +754,7 @@ USBMassDriverBindingSupported (
   // If not found, return EFI_UNSUPPORTED.\r
   // If found, execute USB_MASS_TRANSPORT.Init() to initialize the transport context.\r
   //\r
-  for (Index = 0; mUsbMassTransport[Index] != NULL; Index++) {\r
+  for (Index = 0; Index < USB_MASS_TRANSPORT_COUNT; Index++) {\r
     Transport = mUsbMassTransport[Index];\r
     if (Interface.InterfaceProtocol == Transport->Protocol) {\r
       Status = Transport->Init (UsbIo, NULL);\r
@@ -796,7 +773,6 @@ ON_EXIT:
   return Status;\r
 }\r
 \r
-\r
 /**\r
   Starts the USB mass storage device with this driver.\r
 \r
@@ -804,9 +780,9 @@ ON_EXIT:
   installs Block I/O Protocol, and submits Asynchronous Interrupt\r
   Transfer to manage the USB mass storage device.\r
 \r
-  @param  This                   The USB mass storage driver binding protocol.\r
-  @param  Controller             The USB mass storage device to start on\r
-  @param  RemainingDevicePath    The remaining device path.\r
+  @param  This                  The USB mass storage driver binding protocol.\r
+  @param  Controller            The USB mass storage device to start on\r
+  @param  RemainingDevicePath   The remaining device path.\r
 \r
   @retval EFI_SUCCESS           This driver supports this device.\r
   @retval EFI_UNSUPPORTED       This driver does not support this device.\r
@@ -828,31 +804,32 @@ USBMassDriverBindingStart (
   VOID                          *Context;\r
   UINT8                         MaxLun;\r
   EFI_STATUS                    Status;\r
-  \r
+  EFI_USB_IO_PROTOCOL           *UsbIo;\r
+  EFI_TPL                       OldTpl;\r
+\r
+  OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
+\r
   Transport = NULL;\r
   Context   = NULL;\r
   MaxLun    = 0;\r
 \r
-  //\r
-  // Get interface and protocols, initialize transport\r
-  //\r
   Status = UsbMassInitTransport (This, Controller, &Transport, &Context, &MaxLun);\r
 \r
   if (EFI_ERROR (Status)) {\r
     DEBUG ((EFI_D_ERROR, "USBMassDriverBindingStart: UsbMassInitTransport (%r)\n", Status));\r
-    return Status;\r
+    goto Exit;\r
   }\r
   if (MaxLun == 0) {\r
     //\r
-    // Initialize No/Unsupported LUN device\r
+    // Initialize data for device that does not support multiple LUNSs.\r
     //\r
-    Status = UsbMassInitNonLun(This, Controller, Transport, Context);\r
-    if (EFI_ERROR (Status)) { \r
+    Status = UsbMassInitNonLun (This, Controller, Transport, Context);\r
+    if (EFI_ERROR (Status)) {\r
       DEBUG ((EFI_D_ERROR, "USBMassDriverBindingStart: UsbMassInitNonLun (%r)\n", Status));\r
     }\r
   } else {\r
     //\r
-    // Open device path to prepare append Device Logic Unit node.\r
+    // Open device path to prepare for appending Device Logic Unit node.\r
     //\r
     Status = gBS->OpenProtocol (\r
                     Controller,\r
@@ -862,26 +839,55 @@ USBMassDriverBindingStart (
                     Controller,\r
                     EFI_OPEN_PROTOCOL_BY_DRIVER\r
                     );\r
-  \r
+\r
     if (EFI_ERROR (Status)) {\r
       DEBUG ((EFI_D_ERROR, "USBMassDriverBindingStart: OpenDevicePathProtocol By Driver (%r)\n", Status));\r
-      return Status;\r
+      goto Exit;\r
+    }\r
+\r
+    Status = gBS->OpenProtocol (\r
+                    Controller,\r
+                    &gEfiUsbIoProtocolGuid,\r
+                    (VOID **) &UsbIo,\r
+                    This->DriverBindingHandle,\r
+                    Controller,\r
+                    EFI_OPEN_PROTOCOL_BY_DRIVER\r
+                    );\r
+\r
+    if (EFI_ERROR (Status)) {\r
+      DEBUG ((EFI_D_ERROR, "USBMassDriverBindingStart: OpenUsbIoProtocol By Driver (%r)\n", Status));\r
+      gBS->CloseProtocol (\r
+             Controller,\r
+             &gEfiDevicePathProtocolGuid,\r
+             This->DriverBindingHandle,\r
+             Controller\r
+             );\r
+      goto Exit;\r
     }\r
 \r
     //\r
-    // Try best to initialize all LUNs, and return success only if one of LUNs successed to initialized.\r
+    // Initialize data for device that supports multiple LUNs.\r
+    // EFI_SUCCESS is returned if at least 1 LUN is initialized successfully.\r
     //\r
     Status = UsbMassInitMultiLun (This, Controller, Transport, Context, DevicePath, MaxLun);\r
     if (EFI_ERROR (Status)) {\r
-     gBS->CloseProtocol (\r
-            Controller,\r
-            &gEfiDevicePathProtocolGuid,\r
-            This->DriverBindingHandle,\r
-            Controller\r
-            );\r
+      gBS->CloseProtocol (\r
+              Controller,\r
+              &gEfiDevicePathProtocolGuid,\r
+              This->DriverBindingHandle,\r
+              Controller\r
+              );\r
+      gBS->CloseProtocol (\r
+              Controller,\r
+              &gEfiUsbIoProtocolGuid,\r
+              This->DriverBindingHandle,\r
+              Controller\r
+              );\r
       DEBUG ((EFI_D_ERROR, "USBMassDriverBindingStart: UsbMassInitMultiLun (%r) with Maxlun=%d\n", Status, MaxLun));\r
     }\r
   }\r
+Exit:\r
+  gBS->RestoreTPL (OldTpl);\r
   return Status;\r
 }\r
 \r
@@ -895,6 +901,8 @@ USBMassDriverBindingStart (
   @param  ChildHandleBuffer      The buffer of children handle.\r
 \r
   @retval EFI_SUCCESS            The driver stopped from controlling the device.\r
+  @retval EFI_DEVICE_ERROR       The device could not be stopped due to a device error.\r
+  @retval EFI_UNSUPPORTED        Block I/O Protocol is not installed on Controller.\r
   @retval Others                 Failed to stop the driver\r
 \r
 **/\r
@@ -909,17 +917,17 @@ USBMassDriverBindingStop (
 {\r
   EFI_STATUS            Status;\r
   USB_MASS_DEVICE       *UsbMass;\r
-  EFI_USB_IO_PROTOCOL     *UsbIo;\r
+  EFI_USB_IO_PROTOCOL   *UsbIo;\r
   EFI_BLOCK_IO_PROTOCOL *BlockIo;\r
-  UINTN                   Index;\r
-  BOOLEAN                 AllChildrenStopped;\r
+  UINTN                 Index;\r
+  BOOLEAN               AllChildrenStopped;\r
 \r
   //\r
-  // This a bus driver stop function since multi-lun supported. There are three \r
-  // kinds of device handle might be passed, 1st is a handle with devicepath/\r
-  // usbio/blockio installed(non-multi-lun), 2nd is a handle with devicepath/\r
-  // usbio installed(multi-lun root), 3rd is a handle with devicepath/blockio\r
-  // installed(multi-lun).\r
+  // This is a bus driver stop function since multi-lun is supported.\r
+  // There are three kinds of device handles that might be passed:\r
+  // 1st is a handle with USB I/O & Block I/O installed (non-multi-lun)\r
+  // 2nd is a handle with Device Path & USB I/O installed (multi-lun root)\r
+  // 3rd is a handle with Device Path & USB I/O & Block I/O installed (multi-lun).\r
   //\r
   if (NumberOfChildren == 0) {\r
     //\r
@@ -933,11 +941,11 @@ USBMassDriverBindingStop (
                     Controller,\r
                     EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
                     );\r
-  \r
+\r
     if (EFI_ERROR(Status)) {\r
       //\r
-      // This is a 2nd type handle(multi-lun root), which only needs close \r
-      // devicepath protocol.\r
+      // This is a 2nd type handle(multi-lun root), it needs to close devicepath\r
+      // and usbio protocol.\r
       //\r
       gBS->CloseProtocol (\r
             Controller,\r
@@ -945,47 +953,56 @@ USBMassDriverBindingStop (
             This->DriverBindingHandle,\r
             Controller\r
             );\r
+      gBS->CloseProtocol (\r
+            Controller,\r
+            &gEfiUsbIoProtocolGuid,\r
+            This->DriverBindingHandle,\r
+            Controller\r
+            );\r
       DEBUG ((EFI_D_INFO, "Success to stop multi-lun root handle\n"));\r
       return EFI_SUCCESS;\r
     }\r
-    \r
+\r
     //\r
-    // This is a 1st type handle(non-multi-lun), which only needs uninstall\r
-    // blockio protocol, close usbio protocol and free mass device.\r
+    // This is a 1st type handle(non-multi-lun), which only needs to uninstall\r
+    // Block I/O Protocol, close USB I/O Protocol and free mass device.\r
     //\r
     UsbMass = USB_MASS_DEVICE_FROM_BLOCK_IO (BlockIo);\r
-  \r
+\r
     //\r
     // Uninstall Block I/O protocol from the device handle,\r
     // then call the transport protocol to stop itself.\r
     //\r
-    Status = gBS->UninstallProtocolInterface (\r
+    Status = gBS->UninstallMultipleProtocolInterfaces (\r
                     Controller,\r
                     &gEfiBlockIoProtocolGuid,\r
-                    &UsbMass->BlockIo\r
+                    &UsbMass->BlockIo,\r
+                    &gEfiDiskInfoProtocolGuid,\r
+                    &UsbMass->DiskInfo,\r
+                    NULL\r
                     );\r
     if (EFI_ERROR (Status)) {\r
       return Status;\r
     }\r
-  \r
+\r
     gBS->CloseProtocol (\r
           Controller,\r
           &gEfiUsbIoProtocolGuid,\r
           This->DriverBindingHandle,\r
           Controller\r
           );\r
-  \r
+\r
     UsbMass->Transport->CleanUp (UsbMass->Context);\r
-    gBS->FreePool (UsbMass);\r
-    \r
+    FreePool (UsbMass);\r
+\r
     DEBUG ((EFI_D_INFO, "Success to stop non-multi-lun root handle\n"));\r
     return EFI_SUCCESS;\r
-  } \r
+  }\r
 \r
   //\r
   // This is a 3rd type handle(multi-lun), which needs uninstall\r
-  // blockio and devicepath protocol, close usbio protocol and \r
-  // free mass device.\r
+  // Block I/O Protocol and Device Path Protocol, close USB I/O Protocol and\r
+  // free mass device for all children.\r
   //\r
   AllChildrenStopped = TRUE;\r
 \r
@@ -1008,28 +1025,30 @@ USBMassDriverBindingStop (
     UsbMass = USB_MASS_DEVICE_FROM_BLOCK_IO (BlockIo);\r
 \r
     gBS->CloseProtocol (\r
-          Controller,\r
-          &gEfiUsbIoProtocolGuid,\r
-          This->DriverBindingHandle,\r
-          ChildHandleBuffer[Index]\r
-          );\r
-  \r
+           Controller,\r
+           &gEfiUsbIoProtocolGuid,\r
+           This->DriverBindingHandle,\r
+           ChildHandleBuffer[Index]\r
+           );\r
+\r
     Status = gBS->UninstallMultipleProtocolInterfaces (\r
                     ChildHandleBuffer[Index],\r
                     &gEfiDevicePathProtocolGuid,\r
                     UsbMass->DevicePath,\r
                     &gEfiBlockIoProtocolGuid,\r
                     &UsbMass->BlockIo,\r
+                    &gEfiDiskInfoProtocolGuid,\r
+                    &UsbMass->DiskInfo,\r
                     NULL\r
                     );\r
-    \r
+\r
     if (EFI_ERROR (Status)) {\r
       //\r
-      // Fail to uninstall blockio and devicepath protocol, so re-open usbio by child.\r
+      // Fail to uninstall Block I/O Protocol and Device Path Protocol, so re-open USB I/O Protocol by child.\r
       //\r
       AllChildrenStopped = FALSE;\r
       DEBUG ((EFI_D_ERROR, "Fail to stop No.%d multi-lun child handle when uninstalling blockio and devicepath\n", (UINT32)Index));\r
-      \r
+\r
       gBS->OpenProtocol (\r
              Controller,\r
              &gEfiUsbIoProtocolGuid,\r
@@ -1040,32 +1059,33 @@ USBMassDriverBindingStop (
              );\r
     } else {\r
       //\r
-      // Success to stop this multi-lun handle, so go on next child.\r
+      // Succeed to stop this multi-lun handle, so go on with next child.\r
       //\r
       if (((Index + 1) == NumberOfChildren) && AllChildrenStopped) {\r
         UsbMass->Transport->CleanUp (UsbMass->Context);\r
       }\r
-      gBS->FreePool (UsbMass);\r
+      FreePool (UsbMass);\r
     }\r
   }\r
 \r
   if (!AllChildrenStopped) {\r
     return EFI_DEVICE_ERROR;\r
   }\r
-  \r
-  DEBUG ((EFI_D_INFO, "Success to stop all %d multi-lun children handles\n", (UINT32)NumberOfChildren));\r
+\r
+  DEBUG ((EFI_D_INFO, "Success to stop all %d multi-lun children handles\n", (UINT32) NumberOfChildren));\r
   return EFI_SUCCESS;\r
 }\r
 \r
 /**\r
-  The entry point for the driver, which will install the driver binding and\r
-  component name protocol.\r
+  Entrypoint of USB Mass Storage Driver.\r
+\r
+  This function is the entrypoint of USB Mass Storage Driver. It installs Driver Binding\r
+  Protocol together with Component Name Protocols.\r
 \r
-  @param  ImageHandle       The image handle of this driver.\r
-  @param  SystemTable       The system table.\r
+  @param  ImageHandle       The firmware allocated handle for the EFI image.\r
+  @param  SystemTable       A pointer to the EFI System Table.\r
 \r
-  @retval EFI_SUCCESS       The protocols are installed OK.\r
-  @retval Others            Failed to install protocols.\r
+  @retval EFI_SUCCESS       The entry point is executed successfully.\r
 \r
 **/\r
 EFI_STATUS\r