]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Bus/Usb/UsbKbDxe/EfiKey.c
Code Scrub for UsbKbDxe module.
[mirror_edk2.git] / MdeModulePkg / Bus / Usb / UsbKbDxe / EfiKey.c
index b971903a2dd85e1c170a2c191e6e0d9f8dd38ebf..79f163301c51ac938d1741cb427ddea29839648d 100644 (file)
@@ -1,5 +1,4 @@
 /** @file\r
-\r
   USB Keyboard Driver that includes the implementation of interface.\r
 \r
 Copyright (c) 2004 - 2008, Intel Corporation\r
@@ -16,185 +15,6 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 #include "EfiKey.h"\r
 #include "KeyBoard.h"\r
 \r
-/**\r
-  The Usb Keyboard Driver Entry Point.\r
-\r
-  @param  ImageHandle       The driver image handle.\r
-  @param  SystemTable       The system table.\r
-\r
-  @return EFI_SUCCESS      The component name protocol is installed.\r
-  @return Others           Failed to install.\r
-\r
-**/\r
-EFI_STATUS\r
-EFIAPI\r
-USBKeyboardDriverBindingEntryPoint (\r
-  IN EFI_HANDLE           ImageHandle,\r
-  IN EFI_SYSTEM_TABLE     *SystemTable\r
-  );\r
-\r
-/**\r
-  Check whether USB keyboard driver support this device.\r
-\r
-  @param  This                   The USB keyboard driver binding protocol.\r
-  @param  Controller             The controller handle to check.\r
-  @param  RemainingDevicePath    The remaining device path.\r
-\r
-  @retval EFI_SUCCESS            The driver supports this controller.\r
-  @retval EFI_UNSUPPORTED        This device isn't supported.\r
-**/\r
-EFI_STATUS\r
-EFIAPI\r
-USBKeyboardDriverBindingSupported (\r
-  IN EFI_DRIVER_BINDING_PROTOCOL    *This,\r
-  IN EFI_HANDLE                     Controller,\r
-  IN EFI_DEVICE_PATH_PROTOCOL       *RemainingDevicePath\r
-  );\r
-\r
-/**\r
-  Start running driver on the controller.\r
-\r
-  @param  This                   The USB keyboard driver binding instance.\r
-  @param  Controller             The controller to check.\r
-  @param  RemainingDevicePath    The remaining device patch.\r
-\r
-  @retval EFI_SUCCESS            The controller is controlled by the usb keyboard driver.\r
-  @return Other                  The keyboard driver doesn't support this controller.\r
-\r
-**/\r
-EFI_STATUS\r
-EFIAPI\r
-USBKeyboardDriverBindingStart (\r
-  IN EFI_DRIVER_BINDING_PROTOCOL    *This,\r
-  IN EFI_HANDLE                     Controller,\r
-  IN EFI_DEVICE_PATH_PROTOCOL       *RemainingDevicePath\r
-  );\r
-\r
-/**\r
-  Stop handle the controller by this USB keyboard driver.\r
-\r
-  @param  This                   The USB keyboard driver binding protocol.\r
-  @param  Controller             The controller to release.\r
-  @param  NumberOfChildren       The number of handles in ChildHandleBuffer.\r
-  @param  ChildHandleBuffer      The array of child handle.\r
-\r
-  @retval EFI_SUCCESS            The controller or children are stopped.\r
-  @retval EFI_DEVICE_ERROR       Failed to stop the driver.\r
-\r
-**/\r
-EFI_STATUS\r
-EFIAPI\r
-USBKeyboardDriverBindingStop (\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
-  Reset Usb Keyboard.\r
-\r
-  @param  This                  The protocol instance of EFI_SIMPLE_TEXT_INPUT_PROTOCOL.\r
-  @param  ExtendedVerification  Whether completely reset keyboard or not.\r
-\r
-  @retval EFI_SUCCESS           Reset keyboard successfully.\r
-  @retval EFI_DEVICE_ERROR      Reset keyboard failed.\r
-\r
-**/\r
-EFI_STATUS\r
-EFIAPI\r
-USBKeyboardReset (\r
-  IN  EFI_SIMPLE_TEXT_INPUT_PROTOCOL  *This,\r
-  IN  BOOLEAN                      ExtendedVerification\r
-  );\r
-\r
-/**\r
-  Implements EFI_SIMPLE_TEXT_INPUT_PROTOCOL.ReadKeyStroke() function.\r
-\r
-  @param  This                 The EFI_SIMPLE_TEXT_INPUT_PROTOCOL instance.\r
-  @param  Key                  A pointer to a buffer that is filled in with the keystroke\r
-                               information for the key that was pressed.\r
-\r
-  @retval EFI_SUCCESS          Read key stroke successfully.\r
-  @retval Other                Read key stroke failed.\r
-\r
-**/\r
-EFI_STATUS\r
-EFIAPI\r
-USBKeyboardReadKeyStroke (\r
-  IN  EFI_SIMPLE_TEXT_INPUT_PROTOCOL   *This,\r
-  OUT EFI_INPUT_KEY                 *Key\r
-  );\r
-\r
-/**\r
-  Handler function for WaitForKey event.\r
-\r
-  @param  Event        Event to be signaled when a key is pressed.\r
-  @param  Context      Points to USB_KB_DEV instance.\r
-\r
-  @return None.\r
-**/\r
-VOID\r
-EFIAPI\r
-USBKeyboardWaitForKey (\r
-  IN  EFI_EVENT               Event,\r
-  IN  VOID                    *Context\r
-  );\r
-\r
-/**\r
-  Check whether there is key pending.\r
-\r
-  @param  UsbKeyboardDevice    The USB_KB_DEV instance.\r
-\r
-  @retval EFI_SUCCESS          Have key pending to read.\r
-  @retval Other                Parse key failed.\r
-\r
-**/\r
-EFI_STATUS\r
-EFIAPI\r
-USBKeyboardCheckForKey (\r
-  IN  USB_KB_DEV      *UsbKeyboardDevice\r
-  );\r
-\r
-EFI_GUID  gEfiUsbKeyboardDriverGuid = {\r
-  0xa05f5f78, 0xfb3, 0x4d10, {0x90, 0x90, 0xac, 0x4, 0x6e, 0xeb, 0x7c, 0x3c}\r
-};\r
-\r
-/**\r
-  Free keyboard notify list.\r
-\r
-  @param  ListHead                The list head.\r
-\r
-  @retval EFI_SUCCESS             Free the notify list successfully.\r
-  @retval EFI_INVALID_PARAMETER   ListHead is invalid.\r
-\r
-**/\r
-EFI_STATUS\r
-EFIAPI\r
-KbdFreeNotifyList (\r
-  IN OUT LIST_ENTRY           *ListHead\r
-  );  \r
-\r
-/**\r
-  Whether the pressed key matches a registered key or not.\r
-\r
-  @param  RegsiteredData    A pointer to a buffer that is filled in with the keystroke\r
-                            state data for the key that was registered.\r
-  @param  InputData         A pointer to a buffer that is filled in with the keystroke\r
-                            state data for the key that was pressed.\r
-\r
-  @retval TRUE              Key pressed matches a registered key.\r
-  @retval FLASE             Match failed.\r
-\r
-**/\r
-BOOLEAN\r
-EFIAPI\r
-IsKeyRegistered (\r
-  IN EFI_KEY_DATA  *RegsiteredData,\r
-  IN EFI_KEY_DATA  *InputData\r
-  );\r
-\r
-\r
 //\r
 // USB Keyboard Driver Global Variables\r
 //\r
@@ -208,13 +28,15 @@ EFI_DRIVER_BINDING_PROTOCOL gUsbKeyboardDriverBinding = {
 };\r
 \r
 /**\r
-  The Usb Keyboard Driver Entry Point.\r
+  Entrypoint of USB Keyboard Driver.\r
+\r
+  This function is the entrypoint of USB Keyboard Driver. It installs Driver Binding\r
+  Protocols together with Component Name Protocols.\r
 \r
-  @param  ImageHandle       The driver image handle.\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
-  @return EFI_SUCCESS      The component name protocol is installed.\r
-  @return Others           Failed to install.\r
+  @retval EFI_SUCCESS       The entry point is executed successfully.\r
 \r
 **/\r
 EFI_STATUS\r
@@ -224,25 +46,31 @@ USBKeyboardDriverBindingEntryPoint (
   IN EFI_SYSTEM_TABLE     *SystemTable\r
   )\r
 {\r
-  return EfiLibInstallDriverBindingComponentName2 (\r
-           ImageHandle,\r
-           SystemTable,\r
-           &gUsbKeyboardDriverBinding,\r
-           ImageHandle,\r
-           &gUsbKeyboardComponentName,\r
-           &gUsbKeyboardComponentName2\r
-           );\r
+  EFI_STATUS              Status;\r
+\r
+  Status = EfiLibInstallDriverBindingComponentName2 (\r
+             ImageHandle,\r
+             SystemTable,\r
+             &gUsbKeyboardDriverBinding,\r
+             ImageHandle,\r
+             &gUsbKeyboardComponentName,\r
+             &gUsbKeyboardComponentName2\r
+             );\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
+  return EFI_SUCCESS;\r
 }\r
 \r
 /**\r
-  Check whether USB keyboard driver support this device.\r
+  Check whether USB keyboard driver supports this device.\r
 \r
   @param  This                   The USB keyboard driver binding protocol.\r
   @param  Controller             The controller handle to check.\r
   @param  RemainingDevicePath    The remaining device path.\r
 \r
   @retval EFI_SUCCESS            The driver supports this controller.\r
-  @retval EFI_UNSUPPORTED        This device isn't supported.\r
+  @retval other                  This device isn't supported.\r
+\r
 **/\r
 EFI_STATUS\r
 EFIAPI\r
@@ -252,27 +80,26 @@ USBKeyboardDriverBindingSupported (
   IN EFI_DEVICE_PATH_PROTOCOL       *RemainingDevicePath\r
   )\r
 {\r
-  EFI_STATUS          OpenStatus;\r
-  EFI_USB_IO_PROTOCOL *UsbIo;\r
   EFI_STATUS          Status;\r
+  EFI_USB_IO_PROTOCOL *UsbIo;\r
 \r
   //\r
-  // Check if USB_IO protocol is attached on the controller handle.\r
+  // Check if USB I/O Protocol is attached on the controller handle.\r
   //\r
-  OpenStatus = gBS->OpenProtocol (\r
-                      Controller,\r
-                      &gEfiUsbIoProtocolGuid,\r
-                      (VOID **) &UsbIo,\r
-                      This->DriverBindingHandle,\r
-                      Controller,\r
-                      EFI_OPEN_PROTOCOL_BY_DRIVER\r
-                      );\r
-  if (EFI_ERROR (OpenStatus)) {\r
-    return OpenStatus;\r
+  Status = gBS->OpenProtocol (\r
+                  Controller,\r
+                  &gEfiUsbIoProtocolGuid,\r
+                  (VOID **) &UsbIo,\r
+                  This->DriverBindingHandle,\r
+                  Controller,\r
+                  EFI_OPEN_PROTOCOL_BY_DRIVER\r
+                  );\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
   }\r
 \r
   //\r
-  // Use the USB I/O protocol interface to check whether the Controller is\r
+  // Use the USB I/O Protocol interface to check whether the Controller is\r
   // the Keyboard controller that can be managed by this driver.\r
   //\r
   Status = EFI_SUCCESS;\r
@@ -282,11 +109,11 @@ USBKeyboardDriverBindingSupported (
   }\r
 \r
   gBS->CloseProtocol (\r
-        Controller,\r
-        &gEfiUsbIoProtocolGuid,\r
-        This->DriverBindingHandle,\r
-        Controller\r
-        );\r
+         Controller,\r
+         &gEfiUsbIoProtocolGuid,\r
+         This->DriverBindingHandle,\r
+         Controller\r
+         );\r
 \r
   return Status;\r
 }\r
@@ -295,11 +122,13 @@ USBKeyboardDriverBindingSupported (
   Start running driver on the controller.\r
 \r
   @param  This                   The USB keyboard driver binding instance.\r
-  @param  Controller             The controller to check.\r
-  @param  RemainingDevicePath    The remaining device patch.\r
+  @param  Controller             Handle of device to bind driver to.\r
+  @param  RemainingDevicePath    Optional parameter use to pick a specific child\r
+                                 device to start.\r
 \r
   @retval EFI_SUCCESS            The controller is controlled by the usb keyboard driver.\r
-  @return Other                  The keyboard driver doesn't support this controller.\r
+  @retval EFI_UNSUPPORTED        No interrupt endpoint can be found.\r
+  @retval Other                  The keyboard driver cannot support this controller.\r
 \r
 **/\r
 EFI_STATUS\r
@@ -321,11 +150,8 @@ USBKeyboardDriverBindingStart (
   UINT8                         PacketSize;\r
   BOOLEAN                       Found;\r
 \r
-  UsbKeyboardDevice = NULL;\r
-  Found             = FALSE;\r
-\r
   //\r
-  // Open USB_IO Protocol\r
+  // Open USB I/O Protocol\r
   //\r
   Status = gBS->OpenProtocol (\r
                   Controller,\r
@@ -340,15 +166,8 @@ USBKeyboardDriverBindingStart (
   }\r
 \r
   UsbKeyboardDevice = AllocateZeroPool (sizeof (USB_KB_DEV));\r
-  if (UsbKeyboardDevice == NULL) {\r
-    gBS->CloseProtocol (\r
-          Controller,\r
-          &gEfiUsbIoProtocolGuid,\r
-          This->DriverBindingHandle,\r
-          Controller\r
-          );\r
-    return EFI_OUT_OF_RESOURCES;\r
-  }\r
+  ASSERT (UsbKeyboardDevice != NULL);\r
+\r
   //\r
   // Get the Device Path Protocol on Controller's handle\r
   //\r
@@ -362,31 +181,24 @@ USBKeyboardDriverBindingStart (
                   );\r
 \r
   if (EFI_ERROR (Status)) {\r
-    gBS->FreePool (UsbKeyboardDevice);\r
-    gBS->CloseProtocol (\r
-          Controller,\r
-          &gEfiUsbIoProtocolGuid,\r
-          This->DriverBindingHandle,\r
-          Controller\r
-          );\r
-    return Status;\r
+    goto ErrorExit;\r
   }\r
   //\r
-  // Report that the usb keyboard is being enabled\r
+  // Report that the USB keyboard is being enabled\r
   //\r
-  KbdReportStatusCode (\r
-    UsbKeyboardDevice->DevicePath,\r
+  REPORT_STATUS_CODE_WITH_DEVICE_PATH (\r
     EFI_PROGRESS_CODE,\r
-    PcdGet32 (PcdStatusCodeValueKeyboardEnable)\r
+    PcdGet32 (PcdStatusCodeValueKeyboardEnable),\r
+    UsbKeyboardDevice->DevicePath\r
     );\r
 \r
   //\r
   // This is pretty close to keyboard detection, so log progress\r
   //\r
-  KbdReportStatusCode (\r
-    UsbKeyboardDevice->DevicePath,\r
+  REPORT_STATUS_CODE_WITH_DEVICE_PATH (\r
     EFI_PROGRESS_CODE,\r
-    PcdGet32 (PcdStatusCodeValueKeyboardPresenceDetect)\r
+    PcdGet32 (PcdStatusCodeValueKeyboardPresenceDetect),\r
+    UsbKeyboardDevice->DevicePath\r
     );\r
 \r
   //\r
@@ -398,26 +210,31 @@ USBKeyboardDriverBindingStart (
   // Get interface & endpoint descriptor\r
   //\r
   UsbIo->UsbGetInterfaceDescriptor (\r
-          UsbIo,\r
-          &UsbKeyboardDevice->InterfaceDescriptor\r
-          );\r
+           UsbIo,\r
+           &UsbKeyboardDevice->InterfaceDescriptor\r
+           );\r
 \r
   EndpointNumber = UsbKeyboardDevice->InterfaceDescriptor.NumEndpoints;\r
 \r
+  //\r
+  // Traverse endpoints to find interrupt endpoints\r
+  //\r
+  Found = FALSE;\r
   for (Index = 0; Index < EndpointNumber; Index++) {\r
 \r
     UsbIo->UsbGetEndpointDescriptor (\r
-            UsbIo,\r
-            Index,\r
-            &EndpointDescriptor\r
-            );\r
+             UsbIo,\r
+             Index,\r
+             &EndpointDescriptor\r
+             );\r
 \r
-    if ((EndpointDescriptor.Attributes & 0x03) == 0x03) {\r
+    if ((EndpointDescriptor.Attributes & 0x03) == USB_ENDPOINT_INTERRUPT) {\r
       //\r
       // We only care interrupt endpoint here\r
       //\r
       CopyMem(&UsbKeyboardDevice->IntEndpointDescriptor, &EndpointDescriptor, sizeof(EndpointDescriptor));\r
       Found = TRUE;\r
+      break;\r
     }\r
   }\r
 \r
@@ -425,14 +242,8 @@ USBKeyboardDriverBindingStart (
     //\r
     // No interrupt endpoint found, then return unsupported.\r
     //\r
-    gBS->FreePool (UsbKeyboardDevice);\r
-    gBS->CloseProtocol (\r
-          Controller,\r
-          &gEfiUsbIoProtocolGuid,\r
-          This->DriverBindingHandle,\r
-          Controller\r
-          );\r
-    return EFI_UNSUPPORTED;\r
+    Status = EFI_UNSUPPORTED;\r
+    goto ErrorExit;\r
   }\r
 \r
   UsbKeyboardDevice->Signature                  = USB_KB_DEV_SIGNATURE;\r
@@ -459,11 +270,6 @@ USBKeyboardDriverBindingStart (
     goto ErrorExit;\r
   }\r
 \r
-  Status = InitKeyboardLayout (UsbKeyboardDevice);\r
-  if (EFI_ERROR (Status)) {\r
-    goto ErrorExit;\r
-  }\r
-\r
   Status = gBS->CreateEvent (\r
                   EVT_NOTIFY_WAIT,\r
                   TPL_NOTIFY,\r
@@ -473,20 +279,18 @@ USBKeyboardDriverBindingStart (
                   );\r
 \r
   if (EFI_ERROR (Status)) {\r
-    gBS->FreePool (UsbKeyboardDevice);\r
-    gBS->CloseProtocol (\r
-          Controller,\r
-          &gEfiUsbIoProtocolGuid,\r
-          This->DriverBindingHandle,\r
-          Controller\r
-          );\r
-    return Status;\r
+    goto ErrorExit;\r
+  }\r
+\r
+  Status = InitKeyboardLayout (UsbKeyboardDevice);\r
+  if (EFI_ERROR (Status)) {\r
+    goto ErrorExit;\r
   }\r
 \r
   //\r
-  // Install simple txt in protocol interface\r
-  // for the usb keyboard device.\r
-  // Usb keyboard is a hot plug device, and expected to work immediately\r
+  // Install Simple Text Input Protocol and Simple Text Input Ex Protocol\r
+  // for the USB keyboard device.\r
+  // USB keyboard is a hot plug device, and expected to work immediately\r
   // when plugging into system, so a HotPlugDeviceGuid is installed onto\r
   // the usb keyboard device handle, to distinguish it from other conventional\r
   // console devices.\r
@@ -502,19 +306,11 @@ USBKeyboardDriverBindingStart (
                   NULL\r
                   );\r
   if (EFI_ERROR (Status)) {\r
-    gBS->CloseEvent (UsbKeyboardDevice->SimpleInput.WaitForKey);\r
-    gBS->FreePool (UsbKeyboardDevice);\r
-    gBS->CloseProtocol (\r
-          Controller,\r
-          &gEfiUsbIoProtocolGuid,\r
-          This->DriverBindingHandle,\r
-          Controller\r
-          );\r
-    return Status;\r
+    goto ErrorExit;\r
   }\r
 \r
   //\r
-  // Reset USB Keyboard Device\r
+  // Reset USB Keyboard Device exhaustively.\r
   //\r
   Status = UsbKeyboardDevice->SimpleInput.Reset (\r
                                             &UsbKeyboardDevice->SimpleInput,\r
@@ -531,18 +327,11 @@ USBKeyboardDriverBindingStart (
            NULL,\r
            NULL\r
            );\r
-    gBS->CloseEvent (UsbKeyboardDevice->SimpleInput.WaitForKey);\r
-    gBS->FreePool (UsbKeyboardDevice);\r
-    gBS->CloseProtocol (\r
-          Controller,\r
-          &gEfiUsbIoProtocolGuid,\r
-          This->DriverBindingHandle,\r
-          Controller\r
-          );\r
-    return Status;\r
+    goto ErrorExit;\r
   }\r
+\r
   //\r
-  // submit async interrupt transfer\r
+  // Submit Asynchronous Interrupt Transfer to manage this device.\r
   //\r
   EndpointAddr    = UsbKeyboardDevice->IntEndpointDescriptor.EndpointAddress;\r
   PollingInterval = UsbKeyboardDevice->IntEndpointDescriptor.Interval;\r
@@ -559,7 +348,6 @@ USBKeyboardDriverBindingStart (
                     );\r
 \r
   if (EFI_ERROR (Status)) {\r
-\r
     gBS->UninstallMultipleProtocolInterfaces (\r
            Controller,\r
            &gEfiSimpleTextInProtocolGuid,\r
@@ -570,15 +358,7 @@ USBKeyboardDriverBindingStart (
            NULL,\r
            NULL\r
            );\r
-    gBS->CloseEvent (UsbKeyboardDevice->SimpleInput.WaitForKey);\r
-    gBS->FreePool (UsbKeyboardDevice);\r
-    gBS->CloseProtocol (\r
-          Controller,\r
-          &gEfiUsbIoProtocolGuid,\r
-          This->DriverBindingHandle,\r
-          Controller\r
-          );\r
-    return Status;\r
+    goto ErrorExit;\r
   }\r
 \r
   UsbKeyboardDevice->ControllerNameTable = NULL;\r
@@ -597,9 +377,11 @@ USBKeyboardDriverBindingStart (
     FALSE\r
     );\r
 \r
-\r
   return EFI_SUCCESS;\r
 \r
+//\r
+// Error handler\r
+//\r
 ErrorExit:\r
   if (UsbKeyboardDevice != NULL) {\r
     if (UsbKeyboardDevice->SimpleInput.WaitForKey != NULL) {\r
@@ -608,7 +390,6 @@ ErrorExit:
     if (UsbKeyboardDevice->SimpleInputEx.WaitForKeyEx != NULL) {\r
       gBS->CloseEvent (UsbKeyboardDevice->SimpleInputEx.WaitForKeyEx);\r
     }\r
-    KbdFreeNotifyList (&UsbKeyboardDevice->NotifyList);    \r
     gBS->FreePool (UsbKeyboardDevice);\r
     UsbKeyboardDevice = NULL;\r
   }\r
@@ -624,7 +405,7 @@ ErrorExit:
 \r
 \r
 /**\r
-  Stop handle the controller by this USB keyboard driver.\r
+  Stop handling the controller by this USB keyboard driver.\r
 \r
   @param  This                   The USB keyboard driver binding protocol.\r
   @param  Controller             The controller to release.\r
@@ -632,6 +413,8 @@ ErrorExit:
   @param  ChildHandleBuffer      The array of child handle.\r
 \r
   @retval EFI_SUCCESS            The controller or children are stopped.\r
+  @retval EFI_UNSUPPORTED        Simple Text In Protocol or Simple Text In Ex Protocol\r
+                                 is not installed on Controller.\r
   @retval EFI_DEVICE_ERROR       Failed to stop the driver.\r
 \r
 **/\r
@@ -644,9 +427,9 @@ USBKeyboardDriverBindingStop (
   IN  EFI_HANDLE                     *ChildHandleBuffer\r
   )\r
 {\r
-  EFI_STATUS                  Status;\r
+  EFI_STATUS                     Status;\r
   EFI_SIMPLE_TEXT_INPUT_PROTOCOL *SimpleInput;\r
-  USB_KB_DEV                  *UsbKeyboardDevice;\r
+  USB_KB_DEV                     *UsbKeyboardDevice;\r
 \r
   Status = gBS->OpenProtocol (\r
                   Controller,\r
@@ -659,6 +442,7 @@ USBKeyboardDriverBindingStop (
   if (EFI_ERROR (Status)) {\r
     return EFI_UNSUPPORTED;\r
   }\r
+\r
   Status = gBS->OpenProtocol (\r
                   Controller,\r
                   &gEfiSimpleTextInputExProtocolGuid,\r
@@ -675,25 +459,17 @@ USBKeyboardDriverBindingStop (
   //\r
   UsbKeyboardDevice = USB_KB_DEV_FROM_THIS (SimpleInput);\r
 \r
-  gBS->CloseProtocol (\r
-        Controller,\r
-        &gEfiSimpleTextInProtocolGuid,\r
-        This->DriverBindingHandle,\r
-        Controller\r
-        );\r
-\r
   //\r
-  // Uninstall the Asyn Interrupt Transfer from this device\r
-  // will disable the key data input from this device\r
+  // The key data input from this device will be disabled.\r
   //\r
-  KbdReportStatusCode (\r
-    UsbKeyboardDevice->DevicePath,\r
+  REPORT_STATUS_CODE_WITH_DEVICE_PATH (\r
     EFI_PROGRESS_CODE,\r
-    PcdGet32 (PcdStatusCodeValueKeyboardDisable)\r
+    PcdGet32 (PcdStatusCodeValueKeyboardDisable),\r
+    UsbKeyboardDevice->DevicePath\r
     );\r
 \r
   //\r
-  // Destroy asynchronous interrupt transfer\r
+  // Delete the Asynchronous Interrupt Transfer from this device\r
   //\r
   UsbKeyboardDevice->UsbIo->UsbAsyncInterruptTransfer (\r
                               UsbKeyboardDevice->UsbIo,\r
@@ -706,11 +482,11 @@ USBKeyboardDriverBindingStop (
                               );\r
 \r
   gBS->CloseProtocol (\r
-        Controller,\r
-        &gEfiUsbIoProtocolGuid,\r
-        This->DriverBindingHandle,\r
-        Controller\r
-        );\r
+         Controller,\r
+         &gEfiUsbIoProtocolGuid,\r
+         This->DriverBindingHandle,\r
+         Controller\r
+         );\r
 \r
   Status = gBS->UninstallMultipleProtocolInterfaces (\r
                   Controller,\r
@@ -723,7 +499,7 @@ USBKeyboardDriverBindingStop (
                   NULL\r
                   );\r
   //\r
-  // free all the resources.\r
+  // Free all resources.\r
   //\r
   gBS->CloseEvent (UsbKeyboardDevice->RepeatTimer);\r
   gBS->CloseEvent (UsbKeyboardDevice->DelayedRecoveryEvent);\r
@@ -741,12 +517,10 @@ USBKeyboardDriverBindingStop (
   gBS->FreePool (UsbKeyboardDevice);\r
 \r
   return Status;\r
-\r
 }\r
 \r
 /**\r
-  Reads the next keystroke from the input device. The WaitForKey Event can\r
-  be used to test for existance of a keystroke via WaitForEvent () call.\r
+  Internal function to read the next keystroke from the input device.\r
 \r
   @param  UsbKeyboardDevice       Usb keyboard's private structure.\r
   @param  KeyData                 A pointer to a buffer that is filled in with the keystroke\r
@@ -766,7 +540,6 @@ USBKeyboardReadKeyStrokeWorker (
   OUT EFI_KEY_DATA                      *KeyData\r
   )\r
 {\r
-\r
   EFI_STATUS                        Status;\r
   UINT8                             KeyChar;  \r
   LIST_ENTRY                        *Link;\r
@@ -778,39 +551,47 @@ USBKeyboardReadKeyStrokeWorker (
   }\r
 \r
   //\r
-  // if there is no saved ASCII byte, fetch it\r
+  // If there is no saved ASCII byte, fetch it\r
   // by calling USBKeyboardCheckForKey().\r
   //\r
   if (UsbKeyboardDevice->CurKeyChar == 0) {\r
     Status = USBKeyboardCheckForKey (UsbKeyboardDevice);\r
     if (EFI_ERROR (Status)) {\r
-      return Status;\r
+      return EFI_NOT_READY;\r
     }\r
   }\r
 \r
   KeyData->Key.UnicodeChar = 0;\r
   KeyData->Key.ScanCode    = SCAN_NULL;\r
 \r
+  //\r
+  // Store the key char read by USBKeyboardCheckForKey() and clear it.\r
+  //\r
   KeyChar = UsbKeyboardDevice->CurKeyChar;\r
-\r
   UsbKeyboardDevice->CurKeyChar = 0;\r
 \r
   //\r
   // Translate saved ASCII byte into EFI_INPUT_KEY\r
   //\r
-  Status = USBKeyCodeToEFIScanCode (UsbKeyboardDevice, KeyChar, &KeyData->Key);\r
+  Status = UsbKeyCodeToEfiInputKey (UsbKeyboardDevice, KeyChar, &KeyData->Key);\r
   if (EFI_ERROR (Status)) {\r
     return Status;\r
   }\r
 \r
+  //\r
+  // Get current state of various toggled attributes as well as input modifier values,\r
+  // and set them as valid.\r
+  //\r
   CopyMem (&KeyData->KeyState, &UsbKeyboardDevice->KeyState, sizeof (KeyData->KeyState));\r
   \r
   UsbKeyboardDevice->KeyState.KeyShiftState  = EFI_SHIFT_STATE_VALID;\r
   UsbKeyboardDevice->KeyState.KeyToggleState = EFI_TOGGLE_STATE_VALID;\r
 \r
   //\r
-  //Switch the control value to their original characters. In USBKeyCodeToEFIScanCode() the  CTRL-Alpha characters have been switched to \r
-  // their corresponding control value (ctrl-a = 0x0001 through ctrl-Z = 0x001A), here switch them back for notification function.\r
+  // Switch the control value to their original characters.\r
+  // In UsbKeyCodeToEfiInputKey() the  CTRL-Alpha characters have been switched to \r
+  // their corresponding control value (ctrl-a = 0x0001 through ctrl-Z = 0x001A),\r
+  // here switch them back for notification function.\r
   //\r
   CopyMem (&OriginalKeyData, KeyData, sizeof (EFI_KEY_DATA));\r
   if (UsbKeyboardDevice->CtrlOn != 0) {\r
@@ -824,15 +605,12 @@ USBKeyboardReadKeyStrokeWorker (
   }\r
   \r
   //\r
-  // Invoke notification functions if exist\r
+  // Invoke notification functions if the key is registered.\r
   //\r
-  for (Link = UsbKeyboardDevice->NotifyList.ForwardLink; Link != &UsbKeyboardDevice->NotifyList; Link = Link->ForwardLink) {\r
-    CurrentNotify = CR (\r
-                      Link, \r
-                      KEYBOARD_CONSOLE_IN_EX_NOTIFY, \r
-                      NotifyEntry, \r
-                      USB_KB_CONSOLE_IN_EX_NOTIFY_SIGNATURE\r
-                      );\r
+  for (Link = GetFirstNode (&UsbKeyboardDevice->NotifyList);\r
+       !IsNull (&UsbKeyboardDevice->NotifyList, Link);\r
+       Link = GetNextNode (&UsbKeyboardDevice->NotifyList, Link)) {\r
+    CurrentNotify = CR (Link, KEYBOARD_CONSOLE_IN_EX_NOTIFY, NotifyEntry, USB_KB_CONSOLE_IN_EX_NOTIFY_SIGNATURE);\r
     if (IsKeyRegistered (&CurrentNotify->KeyData, &OriginalKeyData)) { \r
       CurrentNotify->KeyNotificationFn (&OriginalKeyData);\r
     }\r
@@ -843,20 +621,27 @@ USBKeyboardReadKeyStrokeWorker (
 }\r
 \r
 /**\r
-  Reset Usb Keyboard.\r
+  Reset USB Keyboard.\r
+\r
+  There are 2 types of reset for USB keyboard.\r
+  For non-exhaustive reset, only keyboard buffer is cleared.\r
+  For exhaustive reset, in addition to clearance of keyboard buffer, the hardware status\r
+  is also re-initialized.\r
 \r
   @param  This                  The protocol instance of EFI_SIMPLE_TEXT_INPUT_PROTOCOL.\r
-  @param  ExtendedVerification  Whether completely reset keyboard or not.\r
+  @param  ExtendedVerification  Indicates if exhaustive reset is used.\r
+                                TRUE for exhaustive reset.\r
+                                FALSE for non-exhaustive reset.\r
 \r
-  @retval EFI_SUCCESS           Reset keyboard successfully.\r
-  @retval EFI_DEVICE_ERROR      Reset keyboard failed.\r
+  @retval EFI_SUCCESS           Keyboard is reset successfully.\r
+  @retval EFI_DEVICE_ERROR      Failed to reset keyboard.\r
 \r
 **/\r
 EFI_STATUS\r
 EFIAPI\r
 USBKeyboardReset (\r
   IN  EFI_SIMPLE_TEXT_INPUT_PROTOCOL   *This,\r
-  IN  BOOLEAN                       ExtendedVerification\r
+  IN  BOOLEAN                          ExtendedVerification\r
   )\r
 {\r
   EFI_STATUS          Status;\r
@@ -864,35 +649,35 @@ USBKeyboardReset (
 \r
   UsbKeyboardDevice = USB_KB_DEV_FROM_THIS (This);\r
 \r
-  KbdReportStatusCode (\r
-    UsbKeyboardDevice->DevicePath,\r
+  REPORT_STATUS_CODE_WITH_DEVICE_PATH (\r
     EFI_PROGRESS_CODE,\r
-        PcdGet32 (PcdStatusCodeValueKeyboardReset)\r
+    PcdGet32 (PcdStatusCodeValueKeyboardReset),\r
+    UsbKeyboardDevice->DevicePath\r
     );\r
 \r
   //\r
-  // Non Exhaustive reset:\r
+  // Non-exhaustive reset:\r
   // only reset private data structures.\r
   //\r
   if (!ExtendedVerification) {\r
-    //\r
-    // Clear the key buffer of this Usb keyboard\r
-    //\r
-    KbdReportStatusCode (\r
-      UsbKeyboardDevice->DevicePath,\r
+    REPORT_STATUS_CODE_WITH_DEVICE_PATH (\r
       EFI_PROGRESS_CODE,\r
-      PcdGet32 (PcdStatusCodeValueKeyboardClearBuffer)\r
+      PcdGet32 (PcdStatusCodeValueKeyboardClearBuffer),\r
+      UsbKeyboardDevice->DevicePath\r
       );\r
-\r
+    //\r
+    // Clear the key buffer of this USB keyboard\r
+    //\r
     InitUSBKeyBuffer (&(UsbKeyboardDevice->KeyboardBuffer));\r
     UsbKeyboardDevice->CurKeyChar = 0;\r
+\r
     return EFI_SUCCESS;\r
   }\r
 \r
   //\r
   // Exhaustive reset\r
   //\r
-  Status                        = InitUSBKeyboard (UsbKeyboardDevice);\r
+  Status = InitUSBKeyboard (UsbKeyboardDevice);\r
   UsbKeyboardDevice->CurKeyChar = 0;\r
   if (EFI_ERROR (Status)) {\r
     return EFI_DEVICE_ERROR;\r
@@ -917,7 +702,7 @@ EFI_STATUS
 EFIAPI\r
 USBKeyboardReadKeyStroke (\r
   IN  EFI_SIMPLE_TEXT_INPUT_PROTOCOL   *This,\r
-  OUT EFI_INPUT_KEY                 *Key\r
+  OUT EFI_INPUT_KEY                    *Key\r
   )\r
 {\r
   USB_KB_DEV   *UsbKeyboardDevice;\r
@@ -944,7 +729,6 @@ USBKeyboardReadKeyStroke (
   @param  Event        Event to be signaled when a key is pressed.\r
   @param  Context      Points to USB_KB_DEV instance.\r
 \r
-  @return None.\r
 **/\r
 VOID\r
 EFIAPI\r
@@ -958,13 +742,15 @@ USBKeyboardWaitForKey (
   UsbKeyboardDevice = (USB_KB_DEV *) Context;\r
 \r
   if (UsbKeyboardDevice->CurKeyChar == 0) {\r
-\r
     if (EFI_ERROR (USBKeyboardCheckForKey (UsbKeyboardDevice))) {\r
+      //\r
+      // If no pending key, simply return.\r
+      //\r
       return ;\r
     }\r
   }\r
   //\r
-  // If has key pending, signal the event.\r
+  // If there is pending key, signal the event.\r
   //\r
   gBS->SignalEvent (Event);\r
 }\r
@@ -975,8 +761,8 @@ USBKeyboardWaitForKey (
 \r
   @param  UsbKeyboardDevice    The USB_KB_DEV instance.\r
 \r
-  @retval EFI_SUCCESS          Have key pending to read.\r
-  @retval Other                Parse key failed.\r
+  @retval EFI_SUCCESS          There is pending key to read.\r
+  @retval EFI_NOT_READY        No pending key to read.\r
 \r
 **/\r
 EFI_STATUS\r
@@ -994,39 +780,13 @@ USBKeyboardCheckForKey (
   //\r
   Status = USBParseKey (UsbKeyboardDevice, &KeyChar);\r
   if (EFI_ERROR (Status)) {\r
-    return Status;\r
+    return EFI_NOT_READY;\r
   }\r
 \r
   UsbKeyboardDevice->CurKeyChar = KeyChar;\r
   return EFI_SUCCESS;\r
 }\r
 \r
-/**\r
-  Report Status Code in Usb Keyboard Driver.\r
-\r
-  @param  DevicePath            Use this to get Device Path.\r
-  @param  CodeType              Status Code Type.\r
-  @param  CodeValue             Status Code Value.\r
-\r
-  @return None.\r
-\r
-**/\r
-VOID\r
-EFIAPI\r
-KbdReportStatusCode (\r
-  IN EFI_DEVICE_PATH_PROTOCOL  *DevicePath,\r
-  IN EFI_STATUS_CODE_TYPE      CodeType,\r
-  IN EFI_STATUS_CODE_VALUE     Value\r
-  )\r
-{\r
-\r
-  REPORT_STATUS_CODE_WITH_DEVICE_PATH (\r
-    CodeType,\r
-    Value,\r
-    DevicePath\r
-    );\r
-}\r
-\r
 /**\r
   Free keyboard notify list.\r
 \r
@@ -1274,7 +1034,9 @@ USBKeyboardRegisterKeyNotify (
   //\r
   // Return EFI_SUCCESS if the (KeyData, NotificationFunction) is already registered.\r
   //\r
-  for (Link = UsbKeyboardDevice->NotifyList.ForwardLink; Link != &UsbKeyboardDevice->NotifyList; Link = Link->ForwardLink) {\r
+  for (Link = GetFirstNode (&UsbKeyboardDevice->NotifyList);\r
+       !IsNull (&UsbKeyboardDevice->NotifyList, Link);\r
+       Link = GetNextNode (&UsbKeyboardDevice->NotifyList, Link)) {\r
     CurrentNotify = CR (\r
                       Link, \r
                       KEYBOARD_CONSOLE_IN_EX_NOTIFY, \r
@@ -1349,6 +1111,9 @@ USBKeyboardUnregisterKeyNotify (
   \r
   UsbKeyboardDevice = TEXT_INPUT_EX_USB_KB_DEV_FROM_THIS (This);\r
   \r
+  //\r
+  // Check if NotificationHandle is returned from RegisterKeyNotify().\r
+  //\r
   Status = gBS->OpenProtocol (\r
                   NotificationHandle,\r
                   &gSimpleTextInExNotifyGuid,\r
@@ -1361,7 +1126,12 @@ USBKeyboardUnregisterKeyNotify (
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
-  for (Link = UsbKeyboardDevice->NotifyList.ForwardLink; Link != &UsbKeyboardDevice->NotifyList; Link = Link->ForwardLink) {\r
+  //\r
+  // Traverse notify list of USB keyboard and remove the entry of NotificationHandle.\r
+  //\r
+  for (Link = GetFirstNode (&UsbKeyboardDevice->NotifyList);\r
+       !IsNull (&UsbKeyboardDevice->NotifyList, Link);\r
+       Link = GetNextNode (&UsbKeyboardDevice->NotifyList, Link)) {\r
     CurrentNotify = CR (\r
                       Link, \r
                       KEYBOARD_CONSOLE_IN_EX_NOTIFY, \r