]> git.proxmox.com Git - mirror_edk2.git/commitdiff
Code Scrub for UsbKbDxe module.
authorxli24 <xli24@6f19259b-4bc3-4df7-8a09-765794883524>
Fri, 5 Dec 2008 09:19:10 +0000 (09:19 +0000)
committerxli24 <xli24@6f19259b-4bc3-4df7-8a09-765794883524>
Fri, 5 Dec 2008 09:19:10 +0000 (09:19 +0000)
git-svn-id: https://edk2.svn.sourceforge.net/svnroot/edk2/trunk/edk2@6871 6f19259b-4bc3-4df7-8a09-765794883524

MdeModulePkg/Bus/Usb/UsbKbDxe/EfiKey.c
MdeModulePkg/Bus/Usb/UsbKbDxe/EfiKey.h
MdeModulePkg/Bus/Usb/UsbKbDxe/KeyBoard.c
MdeModulePkg/Bus/Usb/UsbKbDxe/KeyBoard.h

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
index 8edd34d94cc45744f1d997962430af48bc005832..506a08ce24f699caba72afabb3fdbd50568a754a 100644 (file)
@@ -1,6 +1,5 @@
 /** @file\r
-\r
-    Header file for USB Keyboard Driver's Data Structures.\r
+  Header file for USB Keyboard Driver's Data Structures.\r
 \r
 Copyright (c) 2004 - 2008, Intel Corporation\r
 All rights reserved. This program and the accompanying materials\r
@@ -37,6 +36,7 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 #include <Library/PcdLib.h>\r
 #include <Library/UefiUsbLib.h>\r
 #include <Library/BaseLib.h>\r
+#include <Library/UefiUsbLib.h>\r
 \r
 #include <IndustryStandard/Usb.h>\r
 \r
@@ -59,9 +59,9 @@ typedef struct {
 } USB_KEY;\r
 \r
 typedef struct {\r
-  USB_KEY buffer[MAX_KEY_ALLOWED + 1];\r
-  UINT8   bHead;\r
-  UINT8   bTail;\r
+  USB_KEY Buffer[MAX_KEY_ALLOWED + 1];\r
+  UINT8   BufferHead;\r
+  UINT8   BufferTail;\r
 } USB_KB_BUFFER;\r
 \r
 #define USB_KB_DEV_SIGNATURE  EFI_SIGNATURE_32 ('u', 'k', 'b', 'd')\r
@@ -95,57 +95,60 @@ typedef struct {
 \r
 #define USB_NS_KEY_FORM_FROM_LINK(a)  CR (a, USB_NS_KEY, Link, USB_NS_KEY_SIGNATURE)\r
 \r
+///\r
+/// Structure to describe USB keyboard device\r
+///\r
 typedef struct {\r
-  UINTN                          Signature;\r
-  EFI_DEVICE_PATH_PROTOCOL       *DevicePath;\r
-  EFI_EVENT                      DelayedRecoveryEvent;\r
-  EFI_SIMPLE_TEXT_INPUT_PROTOCOL SimpleInput;\r
+  UINTN                             Signature;\r
+  EFI_DEVICE_PATH_PROTOCOL          *DevicePath;\r
+  EFI_EVENT                         DelayedRecoveryEvent;\r
+  EFI_SIMPLE_TEXT_INPUT_PROTOCOL    SimpleInput;\r
   EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL SimpleInputEx;\r
-  EFI_USB_IO_PROTOCOL           *UsbIo;\r
+  EFI_USB_IO_PROTOCOL               *UsbIo;\r
 \r
-  EFI_USB_INTERFACE_DESCRIPTOR  InterfaceDescriptor;\r
-  EFI_USB_ENDPOINT_DESCRIPTOR   IntEndpointDescriptor;\r
+  EFI_USB_INTERFACE_DESCRIPTOR      InterfaceDescriptor;\r
+  EFI_USB_ENDPOINT_DESCRIPTOR       IntEndpointDescriptor;\r
 \r
-  USB_KB_BUFFER                 KeyboardBuffer;\r
-  UINT8                         CtrlOn;\r
-  UINT8                         AltOn;\r
-  UINT8                         ShiftOn;\r
-  UINT8                         NumLockOn;\r
-  UINT8                         CapsOn;\r
-  UINT8                         ScrollOn;\r
-  UINT8                         LastKeyCodeArray[8];\r
-  UINT8                         CurKeyChar;\r
+  USB_KB_BUFFER                     KeyboardBuffer;\r
+  UINT8                             CtrlOn;\r
+  UINT8                             AltOn;\r
+  UINT8                             ShiftOn;\r
+  UINT8                             NumLockOn;\r
+  UINT8                             CapsOn;\r
+  UINT8                             ScrollOn;\r
+  UINT8                             LastKeyCodeArray[8];\r
+  UINT8                             CurKeyChar;\r
 \r
-  UINT8                         RepeatKey;\r
-  EFI_EVENT                     RepeatTimer;\r
+  UINT8                             RepeatKey;\r
+  EFI_EVENT                         RepeatTimer;\r
 \r
-  EFI_UNICODE_STRING_TABLE      *ControllerNameTable;\r
+  EFI_UNICODE_STRING_TABLE          *ControllerNameTable;\r
   \r
-  UINT8                         LeftCtrlOn;\r
-  UINT8                         LeftAltOn;\r
-  UINT8                         LeftShiftOn;\r
-  UINT8                         LeftLogoOn;\r
-  UINT8                         RightCtrlOn;\r
-  UINT8                         RightAltOn;\r
-  UINT8                         RightShiftOn;\r
-  UINT8                         RightLogoOn;  \r
-  UINT8                         MenuKeyOn;\r
-  UINT8                         SysReqOn;\r
-  UINT8                         AltGrOn;\r
-\r
-  EFI_KEY_STATE                 KeyState;\r
+  UINT8                             LeftCtrlOn;\r
+  UINT8                             LeftAltOn;\r
+  UINT8                             LeftShiftOn;\r
+  UINT8                             LeftLogoOn;\r
+  UINT8                             RightCtrlOn;\r
+  UINT8                             RightAltOn;\r
+  UINT8                             RightShiftOn;\r
+  UINT8                             RightLogoOn;  \r
+  UINT8                             MenuKeyOn;\r
+  UINT8                             SysReqOn;\r
+  UINT8                             AltGrOn;\r
+\r
+  EFI_KEY_STATE                     KeyState;\r
   //\r
   // Notification function list\r
   //\r
-  LIST_ENTRY                    NotifyList;\r
+  LIST_ENTRY                        NotifyList;\r
 \r
   //\r
   // Non-spacing key list\r
   //\r
-  LIST_ENTRY                    NsKeyList;\r
-  USB_NS_KEY                    *CurrentNsKey;\r
-  EFI_KEY_DESCRIPTOR            *KeyConvertionTable;\r
-  EFI_EVENT                     KeyboardLayoutEvent;\r
+  LIST_ENTRY                        NsKeyList;\r
+  USB_NS_KEY                        *CurrentNsKey;\r
+  EFI_KEY_DESCRIPTOR                *KeyConvertionTable;\r
+  EFI_EVENT                         KeyboardLayoutEvent;\r
 } USB_KB_DEV;\r
 \r
 //\r
@@ -154,27 +157,8 @@ typedef struct {
 extern EFI_DRIVER_BINDING_PROTOCOL   gUsbKeyboardDriverBinding;\r
 extern EFI_COMPONENT_NAME_PROTOCOL   gUsbKeyboardComponentName;\r
 extern EFI_COMPONENT_NAME2_PROTOCOL  gUsbKeyboardComponentName2;\r
-extern EFI_GUID                      gEfiUsbKeyboardDriverGuid;\r
 extern EFI_GUID                      gSimpleTextInExNotifyGuid;\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
 #define USB_KB_DEV_FROM_THIS(a) \\r
     CR(a, USB_KB_DEV, SimpleInput, USB_KB_DEV_SIGNATURE)\r
 #define TEXT_INPUT_EX_USB_KB_DEV_FROM_THIS(a) \\r
@@ -206,6 +190,117 @@ typedef struct {
   UINT8 Resrvd : 5;\r
 } LED_MAP;\r
 \r
+//\r
+// Functions of Driver Binding Protocol\r
+//\r
+/**\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 other                  This device isn't supported.\r
+\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             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
+  @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
+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 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
+  @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_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
+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
+// Functions of Simple Text Input Protocol\r
+//\r
+/**\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  Indicates if exhaustive reset is used.\r
+                                TRUE for exhaustive reset.\r
+                                FALSE for non-exhaustive reset.\r
+\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
+  );\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
 // Simple Text Input Ex protocol functions\r
 //\r
@@ -309,5 +404,68 @@ USBKeyboardUnregisterKeyNotify (
   IN EFI_HANDLE                         NotificationHandle\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
+**/\r
+VOID\r
+EFIAPI\r
+USBKeyboardWaitForKey (\r
+  IN  EFI_EVENT               Event,\r
+  IN  VOID                    *Context\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
+  Check whether there is key pending.\r
+\r
+  @param  UsbKeyboardDevice    The USB_KB_DEV instance.\r
+\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
+EFIAPI\r
+USBKeyboardCheckForKey (\r
+  IN  USB_KB_DEV    *UsbKeyboardDevice\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
 #endif\r
 \r
index 9af221daaddeaf0a98e0e7b7635953603b95baec..0d7d3d1bad2d6ccc7e189ffcf1ebdfce73d0a18f 100644 (file)
@@ -1,5 +1,4 @@
 /** @file\r
-\r
   Helper functions for USB Keyboard Driver.\r
 \r
 Copyright (c) 2004 - 2008, Intel Corporation\r
@@ -14,7 +13,6 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 **/\r
 \r
 #include "KeyBoard.h"\r
-#include <Library/UefiUsbLib.h>\r
 \r
 //\r
 // Static English keyboard layout\r
@@ -295,12 +293,10 @@ KB_MODIFIER  KB_Mod[8] = {
   { MOD_WIN_R,      0xe7 }, // 11100111 \r
 };\r
 \r
-\r
 /**\r
   Initialize KeyConvertionTable by using default keyboard layout.\r
 \r
   @param  UsbKeyboardDevice    The USB_KB_DEV instance.\r
-  @retval None.\r
 \r
 **/\r
 VOID\r
@@ -330,12 +326,10 @@ LoadDefaultKeyboardLayout (
   }\r
 }\r
 \r
-\r
 /**\r
   Uses USB I/O to check whether the device is a USB Keyboard device.\r
 \r
   @param  UsbIo    Points to a USB I/O protocol instance.\r
-  @retval None\r
 \r
 **/\r
 BOOLEAN\r
@@ -374,7 +368,7 @@ IsUSBKeyboard (
 /**\r
   Get current keyboard layout from HII database.\r
 \r
-  @retval Pointer to EFI_HII_KEYBOARD_LAYOUT.\r
+  @return Pointer to EFI_HII_KEYBOARD_LAYOUT.\r
 \r
 **/\r
 EFI_HII_KEYBOARD_LAYOUT *\r
@@ -437,6 +431,7 @@ GetCurrentKeyboardLayout (
   @param  ScanCode            USB scan code.\r
 \r
   @return The Key descriptor in KeyConvertionTable.\r
+          NULL means not found.\r
 \r
 **/\r
 EFI_KEY_DESCRIPTOR *\r
@@ -467,8 +462,8 @@ GetKeyDescriptor (
   @param  UsbKeyboardDevice    The USB_KB_DEV instance.\r
   @param  KeyDescriptor        Key descriptor.\r
 \r
-  @retval NULL                 Key list is empty.\r
-  @return Other                The Non-Spacing key.\r
+  @return The Non-Spacing key corresponding to KeyDescriptor\r
+          NULL means not found.\r
 \r
 **/\r
 USB_NS_KEY *\r
@@ -566,12 +561,15 @@ SetKeyboardLayoutEvent (
   }\r
 \r
   //\r
-  // Allocate resource for KeyConvertionTable\r
+  // Re-allocate resource for KeyConvertionTable\r
   //\r
   ReleaseKeyboardLayoutResources (UsbKeyboardDevice);\r
   UsbKeyboardDevice->KeyConvertionTable = AllocateZeroPool ((USB_KEYCODE_MAX_MAKE + 8) * sizeof (EFI_KEY_DESCRIPTOR));\r
   ASSERT (UsbKeyboardDevice->KeyConvertionTable != NULL);\r
 \r
+  //\r
+  // Traverse the list of key descriptors following the header of EFI_HII_KEYBOARD_LAYOUT\r
+  //\r
   KeyDescriptor = (EFI_KEY_DESCRIPTOR *) (((UINT8 *) KeyboardLayout) + sizeof (EFI_HII_KEYBOARD_LAYOUT));\r
   for (Index = 0; Index < KeyboardLayout->DescriptorCount; Index++) {\r
     //\r
@@ -580,7 +578,7 @@ SetKeyboardLayoutEvent (
     CopyMem (&TempKey, KeyDescriptor, sizeof (EFI_KEY_DESCRIPTOR));\r
 \r
     //\r
-    // Fill the key into KeyConvertionTable (which use USB Scan Code as index)\r
+    // Fill the key into KeyConvertionTable, whose index is calculated from USB scan code.\r
     //\r
     ScanCode = UsbScanCodeConvertionTable [(UINT8) (TempKey.Key)];\r
     TableEntry = GetKeyDescriptor (UsbKeyboardDevice, ScanCode);\r
@@ -588,7 +586,7 @@ SetKeyboardLayoutEvent (
 \r
     if (TempKey.Modifier == EFI_NS_KEY_MODIFIER) {\r
       //\r
-      // Non-spacing key\r
+      // For non-spacing key, create the list with a non-spacing key followed by physical keys.\r
       //\r
       UsbNsKey = AllocatePool (sizeof (USB_NS_KEY));\r
       ASSERT (UsbNsKey != NULL);\r
@@ -692,7 +690,8 @@ InitKeyboardLayout (
   UsbKeyboardDevice->KeyboardLayoutEvent = NULL;\r
 \r
   //\r
-  // Register SET_KEYBOARD_LAYOUT_EVENT notification\r
+  // Register event to EFI_HII_SET_KEYBOARD_LAYOUT_EVENT_GUID group,\r
+  // which will be triggered by EFI_HII_DATABASE_PROTOCOL.SetKeyboardLayout.\r
   //\r
   Status = gBS->CreateEventEx (\r
                   EVT_NOTIFY_SIGNAL,\r
@@ -706,26 +705,26 @@ InitKeyboardLayout (
     return Status;\r
   }\r
 \r
-  //\r
-  // Try to get current keyboard layout from HII database\r
-  //\r
   KeyboardLayout = GetCurrentKeyboardLayout ();\r
   if (KeyboardLayout != NULL) {\r
     //\r
-    // Force to initialize the keyboard layout\r
+    // If current keyboard layout is successfully retrieved from HII database,\r
+    // force to initialize the keyboard layout.\r
     //\r
     gBS->SignalEvent (UsbKeyboardDevice->KeyboardLayoutEvent);\r
   } else {\r
     if (FeaturePcdGet (PcdDisableDefaultKeyboardLayoutInUsbKbDriver)) {\r
-      return EFI_NOT_READY;\r
-    } else {\r
-\r
       //\r
-      // Fail to get keyboard layout from HII database,\r
-      // use default keyboard layout\r
+      // If no keyboard layout can be retrieved from HII database, and default layout\r
+      // is disabled, then return EFI_NOT_READY.\r
       //\r
-      LoadDefaultKeyboardLayout (UsbKeyboardDevice);\r
+      return EFI_NOT_READY;\r
     }\r
+    //\r
+    // If no keyboard layout can be retrieved from HII database, and default layout\r
+    // is enabled, then load the default keyboard layout.\r
+    //\r
+    LoadDefaultKeyboardLayout (UsbKeyboardDevice);\r
   }\r
   \r
   return EFI_SUCCESS;\r
@@ -754,10 +753,10 @@ InitUSBKeyboard (
   EFI_STATUS          Status;\r
   UINT32              TransferResult;\r
 \r
-  KbdReportStatusCode (\r
-    UsbKeyboardDevice->DevicePath,\r
+  REPORT_STATUS_CODE_WITH_DEVICE_PATH (\r
     EFI_PROGRESS_CODE,\r
-    PcdGet32 (PcdStatusCodeValueKeyboardSelfTest)\r
+    PcdGet32 (PcdStatusCodeValueKeyboardSelfTest),\r
+    UsbKeyboardDevice->DevicePath\r
     );\r
 \r
   InitUSBKeyBuffer (&(UsbKeyboardDevice->KeyboardBuffer));\r
@@ -771,20 +770,20 @@ InitUSBKeyboard (
   // Uses default configuration to configure the USB Keyboard device.\r
   //\r
   Status = UsbSetConfiguration (\r
-            UsbKeyboardDevice->UsbIo,\r
-            (UINT16) ConfigValue,\r
-            &TransferResult\r
-            );\r
+             UsbKeyboardDevice->UsbIo,\r
+             (UINT16) ConfigValue,\r
+             &TransferResult\r
+             );\r
   if (EFI_ERROR (Status)) {\r
     //\r
     // If configuration could not be set here, it means\r
     // the keyboard interface has some errors and could\r
     // not be initialized\r
     //\r
-    KbdReportStatusCode (\r
-      UsbKeyboardDevice->DevicePath,\r
+    REPORT_STATUS_CODE_WITH_DEVICE_PATH (\r
       EFI_ERROR_CODE | EFI_ERROR_MINOR,\r
-      PcdGet32 (PcdStatusCodeValueKeyboardInterfaceError)\r
+      PcdGet32 (PcdStatusCodeValueKeyboardInterfaceError),\r
+      UsbKeyboardDevice->DevicePath\r
       );\r
 \r
     return EFI_DEVICE_ERROR;\r
@@ -796,9 +795,9 @@ InitUSBKeyboard (
     &Protocol\r
     );\r
   //\r
-  // Sets boot protocol for the USB Keyboard.\r
+  // Set boot protocol for the USB Keyboard.\r
   // This driver only supports boot protocol.\r
-  // !!BugBug: How about the device that does not support boot protocol?\r
+  // The device that does not support boot protocol is not supported.\r
   //\r
   if (Protocol != BOOT_PROTOCOL) {\r
     UsbSetProtocolRequest (\r
@@ -807,17 +806,15 @@ InitUSBKeyboard (
       BOOT_PROTOCOL\r
       );\r
   }\r
-  //\r
-  // the duration is indefinite, so the endpoint will inhibit reporting forever,\r
-  // and only reporting when a change is detected in the report data.\r
-  //\r
 \r
   //\r
-  // idle value for all report ID\r
+  // ReportId is zero, which means the idle rate applies to all input reports.\r
   //\r
   ReportId = 0;\r
   //\r
-  // idle forever until there is a key pressed and released.\r
+  // Duration is zero, which means the duration is indefinite.\r
+  // so the endpoint will inhibit reporting forever,\r
+  // and only reporting when a change is detected in the report data.\r
   //\r
   Duration = 0;\r
   UsbSetIdleRequest (\r
@@ -861,7 +858,7 @@ InitUSBKeyboard (
   //\r
   if (UsbKeyboardDevice->RepeatTimer != NULL) {\r
     gBS->CloseEvent (UsbKeyboardDevice->RepeatTimer);\r
-    UsbKeyboardDevice->RepeatTimer = 0;\r
+    UsbKeyboardDevice->RepeatTimer = NULL;\r
   }\r
 \r
   Status = gBS->CreateEvent (\r
@@ -874,7 +871,7 @@ InitUSBKeyboard (
 \r
   if (UsbKeyboardDevice->DelayedRecoveryEvent != NULL) {\r
     gBS->CloseEvent (UsbKeyboardDevice->DelayedRecoveryEvent);\r
-    UsbKeyboardDevice->DelayedRecoveryEvent = 0;\r
+    UsbKeyboardDevice->DelayedRecoveryEvent = NULL;\r
   }\r
 \r
   Status = gBS->CreateEvent (\r
@@ -928,7 +925,7 @@ KeyboardHandler (
   UINT32              UsbStatus;\r
   EFI_KEY_DESCRIPTOR  *KeyDescriptor;\r
 \r
-  ASSERT (Context);\r
+  ASSERT (Context != NULL);\r
 \r
   NewRepeatKey      = 0;\r
   UsbKeyboardDevice = (USB_KB_DEV *) Context;\r
@@ -941,10 +938,10 @@ KeyboardHandler (
     //\r
     // Some errors happen during the process\r
     //\r
-    KbdReportStatusCode (\r
-      UsbKeyboardDevice->DevicePath,\r
+    REPORT_STATUS_CODE_WITH_DEVICE_PATH (\r
       EFI_ERROR_CODE | EFI_ERROR_MINOR,\r
-      PcdGet32 (PcdStatusCodeValueKeyboardInputError)\r
+      PcdGet32 (PcdStatusCodeValueKeyboardInputError),\r
+      UsbKeyboardDevice->DevicePath\r
       );\r
 \r
     //\r
@@ -968,37 +965,48 @@ KeyboardHandler (
 \r
     //\r
     // Delete & Submit this interrupt again\r
+    // Handler of DelayedRecoveryEvent triggered by timer will re-submit the interrupt. \r
     //\r
-\r
     UsbIo->UsbAsyncInterruptTransfer (\r
-                      UsbIo,\r
-                      UsbKeyboardDevice->IntEndpointDescriptor.EndpointAddress,\r
-                      FALSE,\r
-                      0,\r
-                      0,\r
-                      NULL,\r
-                      NULL\r
-                      );\r
-\r
+             UsbIo,\r
+             UsbKeyboardDevice->IntEndpointDescriptor.EndpointAddress,\r
+             FALSE,\r
+             0,\r
+             0,\r
+             NULL,\r
+             NULL\r
+             );\r
+    //\r
+    // EFI_USB_INTERRUPT_DELAY is defined in USB standard for error handling\r
+    //\r
     gBS->SetTimer (\r
-          UsbKeyboardDevice->DelayedRecoveryEvent,\r
-          TimerRelative,\r
-          EFI_USB_INTERRUPT_DELAY\r
-          );\r
+           UsbKeyboardDevice->DelayedRecoveryEvent,\r
+           TimerRelative,\r
+           EFI_USB_INTERRUPT_DELAY\r
+           );\r
 \r
     return EFI_DEVICE_ERROR;\r
   }\r
 \r
+  //\r
+  // If no error and no data, just return EFI_SUCCESS.\r
+  //\r
   if (DataLength == 0 || Data == NULL) {\r
     return EFI_SUCCESS;\r
   }\r
 \r
+  //\r
+  // Following code checks current keyboard input report against old key code buffer.\r
+  // According to USB HID Firmware Specification, the report consists of 8 bytes.\r
+  // Byte 0 is map of Modifier keys.\r
+  // Byte 1 is reserved.\r
+  // Bytes 2 to 7 are keycodes.\r
+  //\r
   CurKeyCodeBuffer  = (UINT8 *) Data;\r
   OldKeyCodeBuffer  = UsbKeyboardDevice->LastKeyCodeArray;\r
 \r
   //\r
-  // checks for new key stroke.\r
-  // if no new key got, return immediately.\r
+  // Checks for new key stroke.\r
   //\r
   for (Index = 0; Index < 8; Index++) {\r
     if (OldKeyCodeBuffer[Index] != CurKeyCodeBuffer[Index]) {\r
@@ -1006,26 +1014,29 @@ KeyboardHandler (
     }\r
   }\r
 \r
+  //\r
+  // If no new key, return EFI_SUCCESS immediately.\r
+  //\r
   if (Index == 8) {\r
     return EFI_SUCCESS;\r
   }\r
 \r
   //\r
-  // Parse the modifier key\r
+  // Parse the modifier key, which is the first byte of keyboard input report.\r
   //\r
   CurModifierMap  = CurKeyCodeBuffer[0];\r
   OldModifierMap  = OldKeyCodeBuffer[0];\r
 \r
   //\r
-  // handle modifier key's pressing or releasing situation.\r
+  // Handle modifier key's pressing or releasing situation.\r
   //\r
   for (Index = 0; Index < 8; Index++) {\r
 \r
     if ((CurModifierMap & KB_Mod[Index].Mask) != (OldModifierMap & KB_Mod[Index].Mask)) {\r
       //\r
-      // if current modifier key is up, then\r
+      // If current modifier key is up, then\r
       // CurModifierMap & KB_Mod[Index].Mask = 0;\r
-      // otherwize it is a non-zero value.\r
+      // otherwise it is a non-zero value.\r
       // Inserts the pressed modifier key into key buffer.\r
       //\r
       Down = (UINT8) (CurModifierMap & KB_Mod[Index].Mask);\r
@@ -1034,7 +1045,8 @@ KeyboardHandler (
   }\r
 \r
   //\r
-  // handle normal key's releasing situation\r
+  // Handle normal key's releasing situation\r
+  // Bytes 2 to 7 are normal keycodes\r
   //\r
   KeyRelease = FALSE;\r
   for (Index = 2; Index < 8; Index++) {\r
@@ -1042,7 +1054,10 @@ KeyboardHandler (
     if (!USBKBD_VALID_KEYCODE (OldKeyCodeBuffer[Index])) {\r
       continue;\r
     }\r
-\r
+    //\r
+    // For any key in old keycode buffer, if it is not in current keycode buffer,\r
+    // then it is released. Otherwise, it is not released.\r
+    //\r
     KeyRelease = TRUE;\r
     for (Index2 = 2; Index2 < 8; Index2++) {\r
 \r
@@ -1063,7 +1078,7 @@ KeyboardHandler (
         0\r
         );\r
       //\r
-      // the original reapeat key is released.\r
+      // The original repeat key is released.\r
       //\r
       if (OldKeyCodeBuffer[Index] == UsbKeyboardDevice->RepeatKey) {\r
         UsbKeyboardDevice->RepeatKey = 0;\r
@@ -1072,18 +1087,18 @@ KeyboardHandler (
   }\r
 \r
   //\r
-  // original repeat key is released, cancel the repeat timer\r
+  // If original repeat key is released, cancel the repeat timer\r
   //\r
   if (UsbKeyboardDevice->RepeatKey == 0) {\r
     gBS->SetTimer (\r
-          UsbKeyboardDevice->RepeatTimer,\r
-          TimerCancel,\r
-          USBKBD_REPEAT_RATE\r
-          );\r
+           UsbKeyboardDevice->RepeatTimer,\r
+           TimerCancel,\r
+           USBKBD_REPEAT_RATE\r
+           );\r
   }\r
 \r
   //\r
-  // handle normal key's pressing situation\r
+  // Handle normal key's pressing situation\r
   //\r
   KeyPress = FALSE;\r
   for (Index = 2; Index < 8; Index++) {\r
@@ -1091,7 +1106,10 @@ KeyboardHandler (
     if (!USBKBD_VALID_KEYCODE (CurKeyCodeBuffer[Index])) {\r
       continue;\r
     }\r
-\r
+    //\r
+    // For any key in current keycode buffer, if it is not in old keycode buffer,\r
+    // then it is pressed. Otherwise, it is not pressed.\r
+    //\r
     KeyPress = TRUE;\r
     for (Index2 = 2; Index2 < 8; Index2++) {\r
 \r
@@ -1107,17 +1125,21 @@ KeyboardHandler (
 \r
     if (KeyPress) {\r
       InsertKeyCode (&(UsbKeyboardDevice->KeyboardBuffer), CurKeyCodeBuffer[Index], 1);\r
+\r
       //\r
-      // NumLock pressed or CapsLock pressed\r
+      // Handle repeat key\r
       //\r
       KeyDescriptor = GetKeyDescriptor (UsbKeyboardDevice, CurKeyCodeBuffer[Index]);\r
       if (KeyDescriptor->Modifier == EFI_NUM_LOCK_MODIFIER || KeyDescriptor->Modifier == EFI_CAPS_LOCK_MODIFIER) {\r
+        //\r
+        // For NumLock or CapsLock pressed, there is no need to handle repeat key for them.\r
+        //\r
         UsbKeyboardDevice->RepeatKey = 0;\r
       } else {\r
-        NewRepeatKey = CurKeyCodeBuffer[Index];\r
         //\r
-        // do not repeat the original repeated key\r
+        // Prepare new repeat key, and clear the original one.\r
         //\r
+        NewRepeatKey = CurKeyCodeBuffer[Index];\r
         UsbKeyboardDevice->RepeatKey = 0;\r
       }\r
     }\r
@@ -1132,11 +1154,11 @@ KeyboardHandler (
   }\r
 \r
   //\r
-  // pre-process KeyboardBuffer, pop out the ctrl,alt,del key in sequence\r
+  // Pre-process KeyboardBuffer. Pop out the Ctrl, Alt, Del key in sequence\r
   // and judge whether it will invoke reset event.\r
   //\r
-  SavedTail = UsbKeyboardDevice->KeyboardBuffer.bTail;\r
-  Index     = UsbKeyboardDevice->KeyboardBuffer.bHead;\r
+  SavedTail = UsbKeyboardDevice->KeyboardBuffer.BufferTail;\r
+  Index     = UsbKeyboardDevice->KeyboardBuffer.BufferHead;\r
   while (Index != SavedTail) {\r
     RemoveKeyCode (&(UsbKeyboardDevice->KeyboardBuffer), &UsbKey);\r
 \r
@@ -1171,7 +1193,7 @@ KeyboardHandler (
       break;\r
 \r
     //\r
-    // Del Key Code\r
+    // For Del Key, check if Ctrl + Alt + Del occurs for reset.\r
     //\r
     case EFI_DELETE_MODIFIER:\r
       if (UsbKey.Down != 0) {\r
@@ -1186,7 +1208,7 @@ KeyboardHandler (
     }\r
 \r
     //\r
-    // insert the key back to the buffer.\r
+    // Insert the key back to the buffer,\r
     // so the key sequence will not be destroyed.\r
     //\r
     InsertKeyCode (\r
@@ -1194,7 +1216,7 @@ KeyboardHandler (
       UsbKey.KeyCode,\r
       UsbKey.Down\r
       );\r
-    Index = UsbKeyboardDevice->KeyboardBuffer.bHead;\r
+    Index = UsbKeyboardDevice->KeyboardBuffer.BufferHead;\r
 \r
   }\r
   //\r
@@ -1203,7 +1225,7 @@ KeyboardHandler (
   //\r
   if (NewRepeatKey != 0) {\r
     //\r
-    // sets trigger time to "Repeat Delay Time",\r
+    // Sets trigger time to "Repeat Delay Time",\r
     // to trigger the repeat timer when the key is hold long\r
     // enough time.\r
     //\r
@@ -1243,12 +1265,15 @@ USBParseKey (
 \r
   while (!IsUSBKeyboardBufferEmpty (&UsbKeyboardDevice->KeyboardBuffer)) {\r
     //\r
-    // pops one raw data off.\r
+    // Pops one raw data off.\r
     //\r
     RemoveKeyCode (&(UsbKeyboardDevice->KeyboardBuffer), &UsbKey);\r
 \r
     KeyDescriptor = GetKeyDescriptor (UsbKeyboardDevice, UsbKey.KeyCode);\r
     if (UsbKey.Down == 0) {\r
+      //\r
+      // Key is released.\r
+      //\r
       switch (KeyDescriptor->Modifier) {\r
 \r
       //\r
@@ -1417,7 +1442,7 @@ USBParseKey (
     case EFI_NUM_LOCK_MODIFIER:\r
       UsbKeyboardDevice->NumLockOn ^= 1;\r
       //\r
-      // Turn on the NumLock light on KB\r
+      // Set the NumLock light on keyboard\r
       //\r
       SetKeyLED (UsbKeyboardDevice);\r
       continue;\r
@@ -1426,7 +1451,7 @@ USBParseKey (
     case EFI_CAPS_LOCK_MODIFIER:\r
       UsbKeyboardDevice->CapsOn ^= 1;\r
       //\r
-      // Turn on the CapsLock light on KB\r
+      // Set the CapsLock light on keyboard\r
       //\r
       SetKeyLED (UsbKeyboardDevice);\r
       continue;\r
@@ -1435,22 +1460,22 @@ USBParseKey (
     case EFI_SCROLL_LOCK_MODIFIER:\r
       UsbKeyboardDevice->ScrollOn ^= 1;\r
       //\r
-      // Turn on the ScrollLock light on KB\r
+      // Set the ScrollLock light on keyboard\r
       //\r
       SetKeyLED (UsbKeyboardDevice);\r
       continue;\r
       break;\r
 \r
     //\r
-    // F11,F12,PrintScreen,Pause/Break\r
-    // could not be retrieved via SimpleTxtInEx protocol\r
+    // F11, F12, PrintScreen, Pause/Break\r
+    // could not be retrieved via SimpleTextInEx protocol\r
     //\r
     case EFI_FUNCTION_KEY_ELEVEN_MODIFIER:\r
     case EFI_FUNCTION_KEY_TWELVE_MODIFIER:\r
     case EFI_PAUSE_MODIFIER:\r
     case EFI_BREAK_MODIFIER:\r
       //\r
-      // fall through\r
+      // Fall through\r
       //\r
       continue;\r
       break;\r
@@ -1460,7 +1485,7 @@ USBParseKey (
     }\r
 \r
     //\r
-    // When encountered Del Key...\r
+    // When encountering Ctrl + Alt + Del, then warm reset.\r
     //\r
     if (KeyDescriptor->Modifier == EFI_DELETE_MODIFIER) {\r
       if ((UsbKeyboardDevice->CtrlOn != 0) && (UsbKeyboardDevice->AltOn != 0)) {\r
@@ -1477,7 +1502,7 @@ USBParseKey (
 \r
 \r
 /**\r
-  Converts USB Keyboard code to EFI Scan Code.\r
+  Converts USB Keyboard code to EFI_INPUT_KEY.\r
 \r
   @param  UsbKeyboardDevice    The USB_KB_DEV instance.\r
   @param  KeyChar              Indicates the key code that will be interpreted.\r
@@ -1491,7 +1516,7 @@ USBParseKey (
 **/\r
 EFI_STATUS\r
 EFIAPI\r
-USBKeyCodeToEFIScanCode (\r
+UsbKeyCodeToEfiInputKey (\r
   IN  USB_KB_DEV      *UsbKeyboardDevice,\r
   IN  UINT8           KeyChar,\r
   OUT EFI_INPUT_KEY   *Key\r
@@ -1505,7 +1530,7 @@ USBKeyCodeToEFIScanCode (
   }\r
 \r
   //\r
-  // valid USB Key Code starts from 4\r
+  // Valid USB Key Code starts from 4, so it's safe to minus 4.\r
   //\r
   Index = (UINT8) (KeyChar - 4);\r
 \r
@@ -1515,18 +1540,19 @@ USBKeyCodeToEFIScanCode (
 \r
   KeyDescriptor = GetKeyDescriptor (UsbKeyboardDevice, KeyChar);\r
 \r
-  //\r
-  // Check for Non-spacing key\r
-  //\r
   if (KeyDescriptor->Modifier == EFI_NS_KEY_MODIFIER) {\r
+    //\r
+    // If this is a dead key with EFI_NS_KEY_MODIFIER, then record it and return.\r
+    //\r
     UsbKeyboardDevice->CurrentNsKey = FindUsbNsKey (UsbKeyboardDevice, KeyDescriptor);\r
     return EFI_NOT_READY;\r
   }\r
 \r
-  //\r
-  // Check whether this keystroke follows a Non-spacing key\r
-  //\r
   if (UsbKeyboardDevice->CurrentNsKey != NULL) {\r
+    //\r
+    // If this keystroke follows a non-spacing key, then find the descriptor for corresponding\r
+    // physical key.\r
+    //\r
     KeyDescriptor = FindPhysicalKey (UsbKeyboardDevice->CurrentNsKey, KeyDescriptor);\r
     UsbKeyboardDevice->CurrentNsKey = NULL;\r
   }\r
@@ -1534,7 +1560,7 @@ USBKeyCodeToEFIScanCode (
   Key->ScanCode = EfiScanCodeConvertionTable[KeyDescriptor->Modifier];\r
   Key->UnicodeChar = KeyDescriptor->Unicode;\r
 \r
-  if (KeyDescriptor->AffectedAttribute & EFI_AFFECTED_BY_STANDARD_SHIFT) {\r
+  if ((KeyDescriptor->AffectedAttribute & EFI_AFFECTED_BY_STANDARD_SHIFT)!= 0) {\r
     if (UsbKeyboardDevice->ShiftOn != 0) {\r
       Key->UnicodeChar = KeyDescriptor->ShiftedUnicode;\r
 \r
@@ -1542,7 +1568,7 @@ USBKeyCodeToEFIScanCode (
       // Need not return associated shift state if a class of printable characters that\r
       // are normally adjusted by shift modifiers. e.g. Shift Key + 'f' key = 'F'\r
       //\r
-      if (KeyDescriptor->AffectedAttribute & EFI_AFFECTED_BY_CAPS_LOCK) {\r
+      if ((KeyDescriptor->AffectedAttribute & EFI_AFFECTED_BY_CAPS_LOCK) != 0) {\r
         UsbKeyboardDevice->LeftShiftOn = 0;\r
         UsbKeyboardDevice->RightShiftOn = 0;\r
       }\r
@@ -1562,7 +1588,7 @@ USBKeyCodeToEFIScanCode (
     }\r
   }\r
 \r
-  if (KeyDescriptor->AffectedAttribute & EFI_AFFECTED_BY_CAPS_LOCK) {\r
+  if ((KeyDescriptor->AffectedAttribute & EFI_AFFECTED_BY_CAPS_LOCK) != 0) {\r
     if (UsbKeyboardDevice->CapsOn != 0) {\r
 \r
       if (Key->UnicodeChar == KeyDescriptor->Unicode) {\r
@@ -1578,7 +1604,8 @@ USBKeyCodeToEFIScanCode (
   }\r
 \r
   //\r
-  // Translate the CTRL-Alpha characters to their corresponding control value  (ctrl-a = 0x0001 through ctrl-Z = 0x001A)\r
+  // Translate the CTRL-Alpha characters to their corresponding control value\r
+  // (ctrl-a = 0x0001 through ctrl-Z = 0x001A)\r
   //\r
   if (UsbKeyboardDevice->CtrlOn != 0) {\r
     if (Key->UnicodeChar >= 'a' && Key->UnicodeChar <= 'z') {\r
@@ -1589,11 +1616,13 @@ USBKeyCodeToEFIScanCode (
   }\r
 \r
   if (KeyDescriptor->AffectedAttribute & EFI_AFFECTED_BY_NUM_LOCK) {\r
-\r
+    //\r
+    // For key affected by NumLock, if NumLock is on and Shift is not pressed, then it means\r
+    // normal key, instead of original control key. So the ScanCode should be cleaned.\r
+    // Otherwise, it means control key, so preserve the EFI Scan Code and clear the unicode keycode.\r
+    //\r
     if ((UsbKeyboardDevice->NumLockOn != 0) && (UsbKeyboardDevice->ShiftOn == 0)) {\r
-\r
       Key->ScanCode = SCAN_NULL;\r
-\r
     } else {\r
       Key->UnicodeChar = 0x00;\r
     }\r
@@ -1607,6 +1636,9 @@ USBKeyCodeToEFIScanCode (
     Key->UnicodeChar = 0x00;\r
   }\r
 \r
+  //\r
+  // Not valid for key without both unicode key code and EFI Scan Code.\r
+  //\r
   if (Key->UnicodeChar == 0 && Key->ScanCode == SCAN_NULL) {\r
     return EFI_NOT_READY;\r
   }\r
@@ -1666,10 +1698,8 @@ USBKeyCodeToEFIScanCode (
 \r
   @param  KeyboardBuffer     Points to the USB Keyboard Buffer.\r
 \r
-  @retval EFI_SUCCESS        Init key buffer successfully.\r
-\r
 **/\r
-EFI_STATUS\r
+VOID\r
 EFIAPI\r
 InitUSBKeyBuffer (\r
   IN OUT  USB_KB_BUFFER   *KeyboardBuffer\r
@@ -1677,9 +1707,7 @@ InitUSBKeyBuffer (
 {\r
   ZeroMem (KeyboardBuffer, sizeof (USB_KB_BUFFER));\r
 \r
-  KeyboardBuffer->bHead = KeyboardBuffer->bTail;\r
-\r
-  return EFI_SUCCESS;\r
+  KeyboardBuffer->BufferHead = KeyboardBuffer->BufferTail;\r
 }\r
 \r
 \r
@@ -1701,7 +1729,7 @@ IsUSBKeyboardBufferEmpty (
   //\r
   // meet FIFO empty condition\r
   //\r
-  return (BOOLEAN) (KeyboardBuffer->bHead == KeyboardBuffer->bTail);\r
+  return (BOOLEAN) (KeyboardBuffer->BufferHead == KeyboardBuffer->BufferTail);\r
 }\r
 \r
 \r
@@ -1720,8 +1748,7 @@ IsUSBKeyboardBufferFull (
   IN  USB_KB_BUFFER   *KeyboardBuffer\r
   )\r
 {\r
-  return (BOOLEAN)(((KeyboardBuffer->bTail + 1) % (MAX_KEY_ALLOWED + 1)) ==\r
-                                                        KeyboardBuffer->bHead);\r
+  return (BOOLEAN)(((KeyboardBuffer->BufferTail + 1) % (MAX_KEY_ALLOWED + 1)) == KeyboardBuffer->BufferHead);\r
 }\r
 \r
 \r
@@ -1732,10 +1759,8 @@ IsUSBKeyboardBufferFull (
   @param  Key                Key code\r
   @param  Down               Special key\r
 \r
-  @retval EFI_SUCCESS        Success\r
-\r
 **/\r
-EFI_STATUS\r
+VOID\r
 EFIAPI\r
 InsertKeyCode (\r
   IN OUT  USB_KB_BUFFER *KeyboardBuffer,\r
@@ -1753,15 +1778,13 @@ InsertKeyCode (
     RemoveKeyCode (KeyboardBuffer, &UsbKey);\r
   }\r
 \r
-  KeyboardBuffer->buffer[KeyboardBuffer->bTail].KeyCode = Key;\r
-  KeyboardBuffer->buffer[KeyboardBuffer->bTail].Down    = Down;\r
+  KeyboardBuffer->Buffer[KeyboardBuffer->BufferTail].KeyCode = Key;\r
+  KeyboardBuffer->Buffer[KeyboardBuffer->BufferTail].Down    = Down;\r
 \r
   //\r
-  // adjust the tail pointer of the FIFO keyboard buffer.\r
+  // Adjust the tail pointer of the FIFO keyboard buffer.\r
   //\r
-  KeyboardBuffer->bTail = (UINT8) ((KeyboardBuffer->bTail + 1) % (MAX_KEY_ALLOWED + 1));\r
-\r
-  return EFI_SUCCESS;\r
+  KeyboardBuffer->BufferTail = (UINT8) ((KeyboardBuffer->BufferTail + 1) % (MAX_KEY_ALLOWED + 1));\r
 }\r
 \r
 \r
@@ -1771,8 +1794,8 @@ InsertKeyCode (
   @param  KeyboardBuffer     Points to the USB Keyboard Buffer.\r
   @param  UsbKey             Points to the buffer that contains a usb key code.\r
 \r
-  @retval EFI_SUCCESS        Success\r
-  @retval EFI_DEVICE_ERROR   Hardware Error\r
+  @retval EFI_SUCCESS        Key code Successfully poped from keyboard buffer.\r
+  @retval EFI_DEVICE_ERROR   Keyboard buffer is empty.\r
 \r
 **/\r
 EFI_STATUS\r
@@ -1786,13 +1809,13 @@ RemoveKeyCode (
     return EFI_DEVICE_ERROR;\r
   }\r
 \r
-  UsbKey->KeyCode = KeyboardBuffer->buffer[KeyboardBuffer->bHead].KeyCode;\r
-  UsbKey->Down    = KeyboardBuffer->buffer[KeyboardBuffer->bHead].Down;\r
+  UsbKey->KeyCode = KeyboardBuffer->Buffer[KeyboardBuffer->BufferHead].KeyCode;\r
+  UsbKey->Down    = KeyboardBuffer->Buffer[KeyboardBuffer->BufferHead].Down;\r
 \r
   //\r
-  // adjust the head pointer of the FIFO keyboard buffer.\r
+  // Adjust the head pointer of the FIFO keyboard buffer.\r
   //\r
-  KeyboardBuffer->bHead = (UINT8) ((KeyboardBuffer->bHead + 1) % (MAX_KEY_ALLOWED + 1));\r
+  KeyboardBuffer->BufferHead = (UINT8) ((KeyboardBuffer->BufferHead + 1) % (MAX_KEY_ALLOWED + 1));\r
 \r
   return EFI_SUCCESS;\r
 }\r
@@ -1803,10 +1826,8 @@ RemoveKeyCode (
 \r
   @param  UsbKeyboardDevice  The USB_KB_DEV instance.\r
 \r
-  @retval EFI_SUCCESS        Success\r
-\r
 **/\r
-EFI_STATUS\r
+VOID\r
 EFIAPI\r
 SetKeyLED (\r
   IN  USB_KB_DEV    *UsbKeyboardDevice\r
@@ -1825,7 +1846,7 @@ SetKeyLED (
 \r
   ReportId       = 0;\r
   //\r
-  // call Set Report Request to lighten the LED.\r
+  // Call Set_Report Request to lighten the LED.\r
   //\r
   UsbSetReportRequest (\r
     UsbKeyboardDevice->UsbIo,\r
@@ -1835,8 +1856,6 @@ SetKeyLED (
     1,\r
     (UINT8 *) &Led\r
     );\r
-\r
-  return EFI_SUCCESS;\r
 }\r
 \r
 \r
@@ -1846,7 +1865,6 @@ SetKeyLED (
   @param  Event              The Repeat Key event.\r
   @param  Context            Points to the USB_KB_DEV instance.\r
 \r
-\r
 **/\r
 VOID\r
 EFIAPI\r
@@ -1873,14 +1891,13 @@ USBKeyboardRepeatHandler (
       );\r
 \r
     //\r
-    // set repeate rate for repeat key generation.\r
+    // Set repeate rate for next repeat key generation.\r
     //\r
     gBS->SetTimer (\r
-          UsbKeyboardDevice->RepeatTimer,\r
-          TimerRelative,\r
-          USBKBD_REPEAT_RATE\r
-          );\r
-\r
+           UsbKeyboardDevice->RepeatTimer,\r
+           TimerRelative,\r
+           USBKBD_REPEAT_RATE\r
+           );\r
   }\r
 }\r
 \r
@@ -1891,7 +1908,6 @@ USBKeyboardRepeatHandler (
   @param  Event              The Delayed Recovery event.\r
   @param  Context            Points to the USB_KB_DEV instance.\r
 \r
-\r
 **/\r
 VOID\r
 EFIAPI\r
@@ -1911,13 +1927,16 @@ USBKeyboardRecoveryHandler (
 \r
   PacketSize        = (UINT8) (UsbKeyboardDevice->IntEndpointDescriptor.MaxPacketSize);\r
 \r
+  //\r
+  // Re-submit Asynchronous Interrupt Transfer for recovery.\r
+  //\r
   UsbIo->UsbAsyncInterruptTransfer (\r
-          UsbIo,\r
-          UsbKeyboardDevice->IntEndpointDescriptor.EndpointAddress,\r
-          TRUE,\r
-          UsbKeyboardDevice->IntEndpointDescriptor.Interval,\r
-          PacketSize,\r
-          KeyboardHandler,\r
-          UsbKeyboardDevice\r
-          );\r
+           UsbIo,\r
+           UsbKeyboardDevice->IntEndpointDescriptor.EndpointAddress,\r
+           TRUE,\r
+           UsbKeyboardDevice->IntEndpointDescriptor.Interval,\r
+           PacketSize,\r
+           KeyboardHandler,\r
+           UsbKeyboardDevice\r
+           );\r
 }\r
index 22aaf212717ead1f1fd4599147c9205b2812a215..dbf637ff434c0b6527626cb10b4b95402cf195ea 100644 (file)
@@ -1,5 +1,4 @@
 /** @file\r
-\r
   Function prototype for USB Keyboard Driver.\r
 \r
 Copyright (c) 2004 - 2008, Intel Corporation\r
@@ -143,7 +142,7 @@ USBParseKey (
 **/\r
 EFI_STATUS\r
 EFIAPI\r
-USBKeyCodeToEFIScanCode (\r
+UsbKeyCodeToEfiInputKey (\r
   IN  USB_KB_DEV      *UsbKeyboardDevice,\r
   IN  UINT8           KeyChar,\r
   OUT EFI_INPUT_KEY   *Key\r
@@ -154,10 +153,8 @@ USBKeyCodeToEFIScanCode (
 \r
   @param  KeyboardBuffer     Points to the USB Keyboard Buffer.\r
 \r
-  @retval EFI_SUCCESS        Init key buffer successfully.\r
-\r
 **/\r
-EFI_STATUS\r
+VOID\r
 EFIAPI\r
 InitUSBKeyBuffer (\r
   IN OUT  USB_KB_BUFFER   *KeyboardBuffer\r
@@ -200,10 +197,8 @@ IsUSBKeyboardBufferFull (
   @param  Key                Key code\r
   @param  Down               Special key\r
 \r
-  @retval EFI_SUCCESS        Success\r
-\r
 **/\r
-EFI_STATUS\r
+VOID\r
 EFIAPI\r
 InsertKeyCode (\r
   IN OUT  USB_KB_BUFFER *KeyboardBuffer,\r
@@ -248,10 +243,8 @@ USBKeyboardRepeatHandler (
 \r
   @param  UsbKeyboardDevice  The USB_KB_DEV instance.\r
 \r
-  @retval EFI_SUCCESS        Success\r
-\r
 **/\r
-EFI_STATUS\r
+VOID\r
 EFIAPI\r
 SetKeyLED (\r
   IN  USB_KB_DEV    *UsbKeyboardDevice\r