]> git.proxmox.com Git - mirror_edk2.git/blobdiff - EmbeddedPkg/Drivers/VirtualKeyboardDxe/VirtualKeyboard.c
EmbeddedPkg/Drivers: add virtual keyboard driver
[mirror_edk2.git] / EmbeddedPkg / Drivers / VirtualKeyboardDxe / VirtualKeyboard.c
diff --git a/EmbeddedPkg/Drivers/VirtualKeyboardDxe/VirtualKeyboard.c b/EmbeddedPkg/Drivers/VirtualKeyboardDxe/VirtualKeyboard.c
new file mode 100644 (file)
index 0000000..6609bc8
--- /dev/null
@@ -0,0 +1,1149 @@
+/** @file\r
+  VirtualKeyboard driver\r
+\r
+Copyright (c) 2006 - 2016, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2018, Linaro Ltd. All rights reserved.<BR>\r
+\r
+This program and the accompanying materials\r
+are licensed and made available under the terms and conditions\r
+of the BSD License which accompanies this distribution.  The\r
+full text of the license may be found at\r
+http://opensource.org/licenses/bsd-license.php\r
+\r
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+\r
+**/\r
+\r
+#include "VirtualKeyboard.h"\r
+\r
+//\r
+// RAM Keyboard Driver Binding Protocol Instance\r
+//\r
+EFI_DRIVER_BINDING_PROTOCOL gVirtualKeyboardDriverBinding = {\r
+  VirtualKeyboardDriverBindingSupported,\r
+  VirtualKeyboardDriverBindingStart,\r
+  VirtualKeyboardDriverBindingStop,\r
+  0x10,\r
+  NULL,\r
+  NULL\r
+};\r
+\r
+//\r
+// EFI Driver Binding Protocol Functions\r
+//\r
+\r
+/**\r
+  Check whether the driver supports this device.\r
+\r
+  @param  This                   The Udriver 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
+VirtualKeyboardDriverBindingSupported (\r
+  IN EFI_DRIVER_BINDING_PROTOCOL  *This,\r
+  IN EFI_HANDLE                   Controller,\r
+  IN EFI_DEVICE_PATH_PROTOCOL     *RemainingDevicePath\r
+  )\r
+{\r
+  EFI_STATUS                      Status;\r
+  PLATFORM_VIRTUAL_KBD_PROTOCOL   *PlatformVirtual;\r
+\r
+  Status = gBS->OpenProtocol (\r
+                  Controller,\r
+                  &gPlatformVirtualKeyboardProtocolGuid,\r
+                  (VOID **) &PlatformVirtual,\r
+                  This->DriverBindingHandle,\r
+                  Controller,\r
+                  EFI_OPEN_PROTOCOL_BY_DRIVER\r
+                  );\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
+  gBS->CloseProtocol (\r
+         Controller,\r
+         &gPlatformVirtualKeyboardProtocolGuid,\r
+         This->DriverBindingHandle,\r
+         Controller\r
+         );\r
+  return Status;\r
+}\r
+\r
+/**\r
+  Starts the device with this driver.\r
+\r
+  @param  This                   The 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 driver.\r
+  @retval Other                  This controller cannot be started.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+VirtualKeyboardDriverBindingStart (\r
+  IN EFI_DRIVER_BINDING_PROTOCOL  *This,\r
+  IN EFI_HANDLE                   Controller,\r
+  IN EFI_DEVICE_PATH_PROTOCOL     *RemainingDevicePath\r
+  )\r
+{\r
+  EFI_STATUS                                Status;\r
+  VIRTUAL_KEYBOARD_DEV                      *VirtualKeyboardPrivate;\r
+  PLATFORM_VIRTUAL_KBD_PROTOCOL             *PlatformVirtual;\r
+\r
+  Status = gBS->OpenProtocol (\r
+                  Controller,\r
+                  &gPlatformVirtualKeyboardProtocolGuid,\r
+                  (VOID **) &PlatformVirtual,\r
+                  This->DriverBindingHandle,\r
+                  Controller,\r
+                  EFI_OPEN_PROTOCOL_BY_DRIVER\r
+                  );\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
+\r
+  //\r
+  // Allocate the private device structure\r
+  //\r
+  VirtualKeyboardPrivate = (VIRTUAL_KEYBOARD_DEV *) AllocateZeroPool (sizeof (VIRTUAL_KEYBOARD_DEV));\r
+  if (VirtualKeyboardPrivate == NULL) {\r
+    Status = EFI_OUT_OF_RESOURCES;\r
+    goto Done;\r
+  }\r
+\r
+  //\r
+  // Initialize the private device structure\r
+  //\r
+  VirtualKeyboardPrivate->Signature                  = VIRTUAL_KEYBOARD_DEV_SIGNATURE;\r
+  VirtualKeyboardPrivate->Handle                     = Controller;\r
+  VirtualKeyboardPrivate->PlatformVirtual            = PlatformVirtual;\r
+  VirtualKeyboardPrivate->Queue.Front                = 0;\r
+  VirtualKeyboardPrivate->Queue.Rear                 = 0;\r
+  VirtualKeyboardPrivate->QueueForNotify.Front       = 0;\r
+  VirtualKeyboardPrivate->QueueForNotify.Rear        = 0;\r
+\r
+  VirtualKeyboardPrivate->SimpleTextIn.Reset         = VirtualKeyboardReset;\r
+  VirtualKeyboardPrivate->SimpleTextIn.ReadKeyStroke = VirtualKeyboardReadKeyStroke;\r
+\r
+  VirtualKeyboardPrivate->SimpleTextInputEx.Reset               = VirtualKeyboardResetEx;\r
+  VirtualKeyboardPrivate->SimpleTextInputEx.ReadKeyStrokeEx     = VirtualKeyboardReadKeyStrokeEx;\r
+  VirtualKeyboardPrivate->SimpleTextInputEx.SetState            = VirtualKeyboardSetState;\r
+\r
+  VirtualKeyboardPrivate->SimpleTextInputEx.RegisterKeyNotify   = VirtualKeyboardRegisterKeyNotify;\r
+  VirtualKeyboardPrivate->SimpleTextInputEx.UnregisterKeyNotify = VirtualKeyboardUnregisterKeyNotify;\r
+  InitializeListHead (&VirtualKeyboardPrivate->NotifyList);\r
+\r
+  Status = PlatformVirtual->Register ();\r
+  if (EFI_ERROR (Status)) {\r
+    goto Done;\r
+  }\r
+\r
+  //\r
+  // Report that the keyboard is being enabled\r
+  //\r
+  REPORT_STATUS_CODE (\r
+    EFI_PROGRESS_CODE,\r
+    EFI_PERIPHERAL_KEYBOARD | EFI_P_PC_ENABLE\r
+    );\r
+\r
+  //\r
+  // Setup the WaitForKey event\r
+  //\r
+  Status = gBS->CreateEvent (\r
+                  EVT_NOTIFY_WAIT,\r
+                  TPL_NOTIFY,\r
+                  VirtualKeyboardWaitForKey,\r
+                  &(VirtualKeyboardPrivate->SimpleTextIn),\r
+                  &((VirtualKeyboardPrivate->SimpleTextIn).WaitForKey)\r
+                  );\r
+  if (EFI_ERROR (Status)) {\r
+    (VirtualKeyboardPrivate->SimpleTextIn).WaitForKey = NULL;\r
+    goto Done;\r
+  }\r
+  Status = gBS->CreateEvent (\r
+                  EVT_NOTIFY_WAIT,\r
+                  TPL_NOTIFY,\r
+                  VirtualKeyboardWaitForKeyEx,\r
+                  &(VirtualKeyboardPrivate->SimpleTextInputEx),\r
+                  &(VirtualKeyboardPrivate->SimpleTextInputEx.WaitForKeyEx)\r
+                  );\r
+  if (EFI_ERROR (Status)) {\r
+    VirtualKeyboardPrivate->SimpleTextInputEx.WaitForKeyEx = NULL;\r
+    goto Done;\r
+  }\r
+\r
+  //\r
+  // Setup a periodic timer, used for reading keystrokes at a fixed interval\r
+  //\r
+  Status = gBS->CreateEvent (\r
+                  EVT_TIMER | EVT_NOTIFY_SIGNAL,\r
+                  TPL_NOTIFY,\r
+                  VirtualKeyboardTimerHandler,\r
+                  VirtualKeyboardPrivate,\r
+                  &VirtualKeyboardPrivate->TimerEvent\r
+                  );\r
+  if (EFI_ERROR (Status)) {\r
+    Status = EFI_OUT_OF_RESOURCES;\r
+    goto Done;\r
+  }\r
+\r
+  Status = gBS->SetTimer (\r
+                  VirtualKeyboardPrivate->TimerEvent,\r
+                  TimerPeriodic,\r
+                  KEYBOARD_TIMER_INTERVAL\r
+                  );\r
+  if (EFI_ERROR (Status)) {\r
+    Status = EFI_OUT_OF_RESOURCES;\r
+    goto Done;\r
+  }\r
+\r
+  Status = gBS->CreateEvent (\r
+                  EVT_NOTIFY_SIGNAL,\r
+                  TPL_CALLBACK,\r
+                  KeyNotifyProcessHandler,\r
+                  VirtualKeyboardPrivate,\r
+                  &VirtualKeyboardPrivate->KeyNotifyProcessEvent\r
+                  );\r
+  if (EFI_ERROR (Status)) {\r
+    Status = EFI_OUT_OF_RESOURCES;\r
+    goto Done;\r
+  }\r
+\r
+  //\r
+  // Reset the keyboard device\r
+  //\r
+  Status = VirtualKeyboardPrivate->SimpleTextInputEx.Reset (\r
+                                     &VirtualKeyboardPrivate->SimpleTextInputEx,\r
+                                     FALSE\r
+                                     );\r
+  if (EFI_ERROR (Status)) {\r
+    DEBUG ((DEBUG_ERROR, "[KBD]Reset Failed. Status - %r\n", Status));\r
+    goto Done;\r
+  }\r
+  //\r
+  // Install protocol interfaces for the keyboard device.\r
+  //\r
+  Status = gBS->InstallMultipleProtocolInterfaces (\r
+                  &Controller,\r
+                  &gEfiSimpleTextInProtocolGuid,\r
+                  &VirtualKeyboardPrivate->SimpleTextIn,\r
+                  &gEfiSimpleTextInputExProtocolGuid,\r
+                  &VirtualKeyboardPrivate->SimpleTextInputEx,\r
+                  NULL\r
+                  );\r
+\r
+Done:\r
+  if (EFI_ERROR (Status)) {\r
+    if (VirtualKeyboardPrivate != NULL) {\r
+      if ((VirtualKeyboardPrivate->SimpleTextIn).WaitForKey != NULL) {\r
+        gBS->CloseEvent ((VirtualKeyboardPrivate->SimpleTextIn).WaitForKey);\r
+      }\r
+\r
+      if ((VirtualKeyboardPrivate->SimpleTextInputEx).WaitForKeyEx != NULL) {\r
+        gBS->CloseEvent (\r
+               (VirtualKeyboardPrivate->SimpleTextInputEx).WaitForKeyEx\r
+               );\r
+      }\r
+\r
+      if (VirtualKeyboardPrivate->KeyNotifyProcessEvent != NULL) {\r
+        gBS->CloseEvent (VirtualKeyboardPrivate->KeyNotifyProcessEvent);\r
+      }\r
+\r
+      VirtualKeyboardFreeNotifyList (&VirtualKeyboardPrivate->NotifyList);\r
+\r
+      if (VirtualKeyboardPrivate->TimerEvent != NULL) {\r
+        gBS->CloseEvent (VirtualKeyboardPrivate->TimerEvent);\r
+      }\r
+      FreePool (VirtualKeyboardPrivate);\r
+    }\r
+  }\r
+\r
+  gBS->CloseProtocol (\r
+         Controller,\r
+         &gPlatformVirtualKeyboardProtocolGuid,\r
+         This->DriverBindingHandle,\r
+         Controller\r
+         );\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  Stop the device handled by this driver.\r
+\r
+  @param  This                   The 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 device was stopped.\r
+  @retval EFI_DEVICE_ERROR       The device could not be stopped due to a\r
+                                 device error.\r
+  @retval Others                 Fail to uninstall protocols attached on the\r
+                                 device.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+VirtualKeyboardDriverBindingStop (\r
+  IN  EFI_DRIVER_BINDING_PROTOCOL  *This,\r
+  IN  EFI_HANDLE                   Controller,\r
+  IN  UINTN                        NumberOfChildren,\r
+  IN  EFI_HANDLE                   *ChildHandleBuffer\r
+  )\r
+{\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+\r
+/**\r
+  Enqueue the key.\r
+\r
+  @param  Queue                 The queue to be enqueued.\r
+  @param  KeyData               The key data to be enqueued.\r
+\r
+  @retval EFI_NOT_READY         The queue is full.\r
+  @retval EFI_SUCCESS           Successfully enqueued the key data.\r
+\r
+**/\r
+EFI_STATUS\r
+Enqueue (\r
+  IN SIMPLE_QUEUE         *Queue,\r
+  IN EFI_KEY_DATA         *KeyData\r
+  )\r
+{\r
+  if ((Queue->Rear + 1) % QUEUE_MAX_COUNT == Queue->Front) {\r
+    return EFI_NOT_READY;\r
+  }\r
+\r
+  CopyMem (&Queue->Buffer[Queue->Rear], KeyData, sizeof (EFI_KEY_DATA));\r
+  Queue->Rear = (Queue->Rear + 1) % QUEUE_MAX_COUNT;\r
+\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+/**\r
+  Dequeue the key.\r
+\r
+  @param  Queue                 The queue to be dequeued.\r
+  @param  KeyData               The key data to be dequeued.\r
+\r
+  @retval EFI_NOT_READY         The queue is empty.\r
+  @retval EFI_SUCCESS           Successfully dequeued the key data.\r
+\r
+**/\r
+EFI_STATUS\r
+Dequeue (\r
+  IN SIMPLE_QUEUE         *Queue,\r
+  IN EFI_KEY_DATA         *KeyData\r
+  )\r
+{\r
+  if (Queue->Front == Queue->Rear) {\r
+    return EFI_NOT_READY;\r
+  }\r
+\r
+  CopyMem (KeyData, &Queue->Buffer[Queue->Front], sizeof (EFI_KEY_DATA));\r
+  Queue->Front  = (Queue->Front + 1) % QUEUE_MAX_COUNT;\r
+\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+/**\r
+  Check whether the queue is empty.\r
+\r
+  @param  Queue                 The queue to be checked.\r
+\r
+  @retval EFI_NOT_READY         The queue is empty.\r
+  @retval EFI_SUCCESS           The queue is not empty.\r
+\r
+**/\r
+EFI_STATUS\r
+CheckQueue (\r
+  IN SIMPLE_QUEUE         *Queue\r
+  )\r
+{\r
+  if (Queue->Front == Queue->Rear) {\r
+    return EFI_NOT_READY;\r
+  }\r
+\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+/**\r
+  Check key buffer to get the key stroke status.\r
+\r
+  @param  This         Pointer of the protocol EFI_SIMPLE_TEXT_IN_PROTOCOL.\r
+\r
+  @retval EFI_SUCCESS  A key is being pressed now.\r
+  @retval Other        No key is now pressed.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+VirtualKeyboardCheckForKey (\r
+  IN  EFI_SIMPLE_TEXT_INPUT_PROTOCOL  *This\r
+  )\r
+{\r
+  VIRTUAL_KEYBOARD_DEV     *VirtualKeyboardPrivate;\r
+\r
+  VirtualKeyboardPrivate = VIRTUAL_KEYBOARD_DEV_FROM_THIS (This);\r
+\r
+  return CheckQueue (&VirtualKeyboardPrivate->Queue);\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
+VirtualKeyboardFreeNotifyList (\r
+  IN OUT LIST_ENTRY           *ListHead\r
+  )\r
+{\r
+  VIRTUAL_KEYBOARD_CONSOLE_IN_EX_NOTIFY *NotifyNode;\r
+\r
+  if (ListHead == NULL) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+  while (!IsListEmpty (ListHead)) {\r
+    NotifyNode = CR (\r
+                   ListHead->ForwardLink,\r
+                   VIRTUAL_KEYBOARD_CONSOLE_IN_EX_NOTIFY,\r
+                   NotifyEntry,\r
+                   VIRTUAL_KEYBOARD_CONSOLE_IN_EX_NOTIFY_SIGNATURE\r
+                   );\r
+    RemoveEntryList (ListHead->ForwardLink);\r
+    gBS->FreePool (NotifyNode);\r
+  }\r
+\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+/**\r
+  Judge whether is a registed key\r
+\r
+  @param RegsiteredData       A pointer to a buffer that is filled in with\r
+                              the keystroke state data for the key that was\r
+                              registered.\r
+  @param InputData            A pointer to a buffer that is filled in with\r
+                              the keystroke state data for the key that was\r
+                              pressed.\r
+\r
+  @retval TRUE                Key be pressed matches a registered key.\r
+  @retval FLASE               Match failed.\r
+\r
+**/\r
+BOOLEAN\r
+IsKeyRegistered (\r
+  IN EFI_KEY_DATA  *RegsiteredData,\r
+  IN EFI_KEY_DATA  *InputData\r
+  )\r
+\r
+{\r
+  ASSERT (RegsiteredData != NULL && InputData != NULL);\r
+\r
+  if ((RegsiteredData->Key.ScanCode    != InputData->Key.ScanCode) ||\r
+      (RegsiteredData->Key.UnicodeChar != InputData->Key.UnicodeChar)) {\r
+    return FALSE;\r
+  }\r
+\r
+  //\r
+  // Assume KeyShiftState/KeyToggleState = 0 in Registered key data means\r
+  // these state could be ignored.\r
+  //\r
+  if ((RegsiteredData->KeyState.KeyShiftState != 0) &&\r
+      (RegsiteredData->KeyState.KeyShiftState != InputData->KeyState.KeyShiftState)) {\r
+    return FALSE;\r
+  }\r
+  if ((RegsiteredData->KeyState.KeyToggleState != 0) &&\r
+      (RegsiteredData->KeyState.KeyToggleState != InputData->KeyState.KeyToggleState)) {\r
+    return FALSE;\r
+  }\r
+\r
+  return TRUE;\r
+\r
+}\r
+\r
+/**\r
+  Event notification function for SIMPLE_TEXT_IN.WaitForKey event\r
+  Signal the event if there is key available\r
+\r
+  @param Event    the event object\r
+  @param Context  waitting context\r
+\r
+**/\r
+VOID\r
+EFIAPI\r
+VirtualKeyboardWaitForKey (\r
+  IN  EFI_EVENT               Event,\r
+  IN  VOID                    *Context\r
+  )\r
+{\r
+  //\r
+  // Stall 1ms to give a chance to let other driver interrupt this routine\r
+  // for their timer event.\r
+  // e.g. UI setup or Shell, other drivers which are driven by timer event\r
+  // will have a bad performance during this period,\r
+  // e.g. usb keyboard driver.\r
+  // Add a stall period can greatly increate other driver performance during\r
+  // the WaitForKey is recursivly invoked. 1ms delay will make little impact\r
+  // to the thunk keyboard driver, and user can not feel the delay at all when\r
+  // input.\r
+  //\r
+  gBS->Stall (1000);\r
+  //\r
+  // Use TimerEvent callback function to check whether there's any key pressed\r
+  //\r
+  VirtualKeyboardTimerHandler (NULL, VIRTUAL_KEYBOARD_DEV_FROM_THIS (Context));\r
+\r
+  if (!EFI_ERROR (VirtualKeyboardCheckForKey (Context))) {\r
+    gBS->SignalEvent (Event);\r
+  }\r
+}\r
+\r
+/**\r
+  Event notification function for SIMPLE_TEXT_INPUT_EX_PROTOCOL.WaitForKeyEx\r
+  event. Signal the event if there is key available\r
+\r
+  @param Event    event object\r
+  @param Context  waiting context\r
+\r
+**/\r
+VOID\r
+EFIAPI\r
+VirtualKeyboardWaitForKeyEx (\r
+  IN  EFI_EVENT               Event,\r
+  IN  VOID                    *Context\r
+  )\r
+\r
+{\r
+  VIRTUAL_KEYBOARD_DEV                     *VirtualKeyboardPrivate;\r
+\r
+  VirtualKeyboardPrivate = TEXT_INPUT_EX_VIRTUAL_KEYBOARD_DEV_FROM_THIS (Context);\r
+  VirtualKeyboardWaitForKey (Event, &VirtualKeyboardPrivate->SimpleTextIn);\r
+\r
+}\r
+\r
+//\r
+// EFI Simple Text In Protocol Functions\r
+//\r
+/**\r
+  Reset the Keyboard and do BAT test for it, if (ExtendedVerification == TRUE)\r
+  then do some extra keyboard validations.\r
+\r
+  @param  This                  Pointer of simple text Protocol.\r
+  @param  ExtendedVerification  Whether perform the extra validation of\r
+                                keyboard. True: perform; FALSE: skip.\r
+\r
+  @retval EFI_SUCCESS           The command byte is written successfully.\r
+  @retval EFI_DEVICE_ERROR      Errors occurred during resetting keyboard.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+VirtualKeyboardReset (\r
+  IN  EFI_SIMPLE_TEXT_INPUT_PROTOCOL  *This,\r
+  IN  BOOLEAN                         ExtendedVerification\r
+  )\r
+{\r
+  VIRTUAL_KEYBOARD_DEV *VirtualKeyboardPrivate;\r
+  EFI_STATUS           Status;\r
+  EFI_TPL              OldTpl;\r
+\r
+  VirtualKeyboardPrivate = VIRTUAL_KEYBOARD_DEV_FROM_THIS (This);\r
+\r
+  //\r
+  // Raise TPL to avoid mouse operation impact\r
+  //\r
+  OldTpl = gBS->RaiseTPL (TPL_NOTIFY);\r
+\r
+  if (VirtualKeyboardPrivate->PlatformVirtual &&\r
+      VirtualKeyboardPrivate->PlatformVirtual->Reset) {\r
+    Status = VirtualKeyboardPrivate->PlatformVirtual->Reset ();\r
+  } else {\r
+    Status = EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  //\r
+  // resume priority of task level\r
+  //\r
+  gBS->RestoreTPL (OldTpl);\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  Reset the input device and optionaly run diagnostics\r
+\r
+  @param  This                  Protocol instance pointer.\r
+  @param  ExtendedVerification  Driver may perform diagnostics on reset.\r
+\r
+  @retval EFI_SUCCESS           The device was reset.\r
+  @retval EFI_DEVICE_ERROR      The device is not functioning properly and\r
+                                could not be reset.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+VirtualKeyboardResetEx (\r
+  IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL  *This,\r
+  IN BOOLEAN                            ExtendedVerification\r
+  )\r
+{\r
+  VIRTUAL_KEYBOARD_DEV                  *VirtualKeyboardPrivate;\r
+  EFI_STATUS                            Status;\r
+  EFI_TPL                               OldTpl;\r
+\r
+  VirtualKeyboardPrivate = TEXT_INPUT_EX_VIRTUAL_KEYBOARD_DEV_FROM_THIS (This);\r
+\r
+  Status = VirtualKeyboardPrivate->SimpleTextIn.Reset (\r
+                                     &VirtualKeyboardPrivate->SimpleTextIn,\r
+                                     ExtendedVerification\r
+                                     );\r
+  if (EFI_ERROR (Status)) {\r
+    return EFI_DEVICE_ERROR;\r
+  }\r
+\r
+  OldTpl = gBS->RaiseTPL (TPL_NOTIFY);\r
+\r
+  gBS->RestoreTPL (OldTpl);\r
+\r
+  return EFI_SUCCESS;\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
+\r
+  @param  VirtualKeyboardPrivate   Virtualkeyboard driver private structure.\r
+  @param  KeyData                  A pointer to a buffer that is filled in\r
+                                   with the keystroke state data for the key\r
+                                   that was pressed.\r
+\r
+  @retval EFI_SUCCESS              The keystroke information was returned.\r
+  @retval EFI_NOT_READY            There was no keystroke data availiable.\r
+  @retval EFI_DEVICE_ERROR         The keystroke information was not returned\r
+                                   due to hardware errors.\r
+  @retval EFI_INVALID_PARAMETER    KeyData is NULL.\r
+\r
+**/\r
+EFI_STATUS\r
+KeyboardReadKeyStrokeWorker (\r
+  IN VIRTUAL_KEYBOARD_DEV  *VirtualKeyboardPrivate,\r
+  OUT EFI_KEY_DATA      *KeyData\r
+  )\r
+{\r
+  EFI_STATUS                            Status;\r
+  EFI_TPL                               OldTpl;\r
+  if (KeyData == NULL) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  //\r
+  // Use TimerEvent callback function to check whether there's any key pressed\r
+  //\r
+\r
+  //\r
+  // Stall 1ms to give a chance to let other driver interrupt this routine for\r
+  // their timer event.\r
+  // e.g. OS loader, other drivers which are driven by timer event will have a\r
+  // bad performance during this period,\r
+  // e.g. usb keyboard driver.\r
+  // Add a stall period can greatly increate other driver performance during\r
+  // the WaitForKey is recursivly invoked. 1ms delay will make little impact\r
+  // to the thunk keyboard driver, and user can not feel the delay at all when\r
+  // input.\r
+  //\r
+  gBS->Stall (1000);\r
+\r
+  OldTpl = gBS->RaiseTPL (TPL_NOTIFY);\r
+\r
+  VirtualKeyboardTimerHandler (NULL, VirtualKeyboardPrivate);\r
+  //\r
+  // If there's no key, just return\r
+  //\r
+  Status = CheckQueue (&VirtualKeyboardPrivate->Queue);\r
+  if (EFI_ERROR (Status)) {\r
+    gBS->RestoreTPL (OldTpl);\r
+    return EFI_NOT_READY;\r
+  }\r
+\r
+  Status = Dequeue (&VirtualKeyboardPrivate->Queue, KeyData);\r
+\r
+  gBS->RestoreTPL (OldTpl);\r
+\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+/**\r
+  Read out the scan code of the key that has just been stroked.\r
+\r
+  @param  This        Pointer of simple text Protocol.\r
+  @param  Key         Pointer for store the key that read out.\r
+\r
+  @retval EFI_SUCCESS The key is read out successfully.\r
+  @retval other       The key reading failed.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+VirtualKeyboardReadKeyStroke (\r
+  IN  EFI_SIMPLE_TEXT_INPUT_PROTOCOL  *This,\r
+  OUT EFI_INPUT_KEY                   *Key\r
+  )\r
+{\r
+  VIRTUAL_KEYBOARD_DEV     *VirtualKeyboardPrivate;\r
+  EFI_STATUS               Status;\r
+  EFI_KEY_DATA             KeyData;\r
+\r
+  VirtualKeyboardPrivate = VIRTUAL_KEYBOARD_DEV_FROM_THIS (This);\r
+\r
+  Status = KeyboardReadKeyStrokeWorker (VirtualKeyboardPrivate, &KeyData);\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
+\r
+  //\r
+  // Convert the Ctrl+[a-z] to Ctrl+[1-26]\r
+  //\r
+  if ((KeyData.KeyState.KeyShiftState & (EFI_LEFT_CONTROL_PRESSED | EFI_RIGHT_CONTROL_PRESSED)) != 0) {\r
+    if (KeyData.Key.UnicodeChar >= L'a' &&\r
+        KeyData.Key.UnicodeChar <= L'z') {\r
+      KeyData.Key.UnicodeChar = (CHAR16) (KeyData.Key.UnicodeChar - L'a' + 1);\r
+    } else if (KeyData.Key.UnicodeChar >= L'A' &&\r
+               KeyData.Key.UnicodeChar <= L'Z') {\r
+      KeyData.Key.UnicodeChar = (CHAR16) (KeyData.Key.UnicodeChar - L'A' + 1);\r
+    }\r
+  }\r
+\r
+  CopyMem (Key, &KeyData.Key, sizeof (EFI_INPUT_KEY));\r
+\r
+  return EFI_SUCCESS;\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
+\r
+  @param  This         Protocol instance pointer.\r
+  @param  KeyData      A pointer to a buffer that is filled in with the\r
+                       keystroke state data for the key that was pressed.\r
+\r
+  @retval  EFI_SUCCESS           The keystroke information was returned.\r
+  @retval  EFI_NOT_READY         There was no keystroke data availiable.\r
+  @retval  EFI_DEVICE_ERROR      The keystroke information was not returned\r
+                                 due to hardware errors.\r
+  @retval  EFI_INVALID_PARAMETER KeyData is NULL.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+VirtualKeyboardReadKeyStrokeEx (\r
+  IN  EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *This,\r
+  OUT EFI_KEY_DATA                      *KeyData\r
+  )\r
+{\r
+  VIRTUAL_KEYBOARD_DEV                  *VirtualKeyboardPrivate;\r
+\r
+  if (KeyData == NULL) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  VirtualKeyboardPrivate = TEXT_INPUT_EX_VIRTUAL_KEYBOARD_DEV_FROM_THIS (This);\r
+\r
+  return KeyboardReadKeyStrokeWorker (VirtualKeyboardPrivate, KeyData);\r
+\r
+}\r
+\r
+/**\r
+  Set certain state for the input device.\r
+\r
+  @param  This              Protocol instance pointer.\r
+  @param  KeyToggleState    A pointer to the EFI_KEY_TOGGLE_STATE to set the\r
+                            state for the input device.\r
+\r
+  @retval EFI_SUCCESS           The device state was set successfully.\r
+  @retval EFI_DEVICE_ERROR      The device is not functioning correctly and\r
+                                could not have the setting adjusted.\r
+  @retval EFI_UNSUPPORTED       The device does not have the ability to set\r
+                                its state.\r
+  @retval EFI_INVALID_PARAMETER KeyToggleState is NULL.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+VirtualKeyboardSetState (\r
+  IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL  *This,\r
+  IN EFI_KEY_TOGGLE_STATE               *KeyToggleState\r
+  )\r
+{\r
+  if (KeyToggleState == NULL) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+/**\r
+  Register a notification function for a particular keystroke for the\r
+  input device.\r
+\r
+  @param  This                    Protocol instance pointer.\r
+  @param  KeyData                 A pointer to a buffer that is filled in with\r
+                                  the keystroke information data for the key\r
+                                  that was pressed.\r
+  @param  KeyNotificationFunction Points to the function to be called when the\r
+                                  key sequence is typed specified by KeyData.\r
+  @param  NotifyHandle            Points to the unique handle assigned to the\r
+                                  registered notification.\r
+\r
+\r
+  @retval EFI_SUCCESS             The notification function was registered\r
+                                  successfully.\r
+  @retval EFI_OUT_OF_RESOURCES    Unable to allocate resources for necesssary\r
+                                  data structures.\r
+  @retval EFI_INVALID_PARAMETER   KeyData or NotifyHandle is NULL.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+VirtualKeyboardRegisterKeyNotify (\r
+  IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL  *This,\r
+  IN EFI_KEY_DATA                       *KeyData,\r
+  IN EFI_KEY_NOTIFY_FUNCTION            KeyNotificationFunction,\r
+  OUT VOID                              **NotifyHandle\r
+  )\r
+{\r
+  EFI_STATUS                            Status;\r
+  VIRTUAL_KEYBOARD_DEV                  *VirtualKeyboardPrivate;\r
+  EFI_TPL                               OldTpl;\r
+  VIRTUAL_KEYBOARD_CONSOLE_IN_EX_NOTIFY *NewNotify;\r
+  LIST_ENTRY                            *Link;\r
+  VIRTUAL_KEYBOARD_CONSOLE_IN_EX_NOTIFY *CurrentNotify;\r
+\r
+  if (KeyData == NULL ||\r
+      NotifyHandle == NULL ||\r
+      KeyNotificationFunction == NULL) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  VirtualKeyboardPrivate = TEXT_INPUT_EX_VIRTUAL_KEYBOARD_DEV_FROM_THIS (This);\r
+\r
+  //\r
+  // Enter critical section\r
+  //\r
+  OldTpl = gBS->RaiseTPL (TPL_NOTIFY);\r
+\r
+  //\r
+  // Return EFI_SUCCESS if the (KeyData, NotificationFunction) is already\r
+  // registered.\r
+  //\r
+  for (Link = VirtualKeyboardPrivate->NotifyList.ForwardLink;\r
+       Link != &VirtualKeyboardPrivate->NotifyList;\r
+       Link = Link->ForwardLink) {\r
+    CurrentNotify = CR (\r
+                      Link,\r
+                      VIRTUAL_KEYBOARD_CONSOLE_IN_EX_NOTIFY,\r
+                      NotifyEntry,\r
+                      VIRTUAL_KEYBOARD_CONSOLE_IN_EX_NOTIFY_SIGNATURE\r
+                      );\r
+    if (IsKeyRegistered (&CurrentNotify->KeyData, KeyData)) {\r
+      if (CurrentNotify->KeyNotificationFn == KeyNotificationFunction) {\r
+        *NotifyHandle = CurrentNotify;\r
+        Status = EFI_SUCCESS;\r
+        goto Exit;\r
+      }\r
+    }\r
+  }\r
+\r
+  //\r
+  // Allocate resource to save the notification function\r
+  //\r
+\r
+  NewNotify = (VIRTUAL_KEYBOARD_CONSOLE_IN_EX_NOTIFY *) AllocateZeroPool (sizeof (VIRTUAL_KEYBOARD_CONSOLE_IN_EX_NOTIFY));\r
+  if (NewNotify == NULL) {\r
+    Status = EFI_OUT_OF_RESOURCES;\r
+    goto Exit;\r
+  }\r
+\r
+  NewNotify->Signature         = VIRTUAL_KEYBOARD_CONSOLE_IN_EX_NOTIFY_SIGNATURE;\r
+  NewNotify->KeyNotificationFn = KeyNotificationFunction;\r
+  CopyMem (&NewNotify->KeyData, KeyData, sizeof (EFI_KEY_DATA));\r
+  InsertTailList (&VirtualKeyboardPrivate->NotifyList, &NewNotify->NotifyEntry);\r
+\r
+  *NotifyHandle                = NewNotify;\r
+  Status                       = EFI_SUCCESS;\r
+\r
+Exit:\r
+  //\r
+  // Leave critical section and return\r
+  //\r
+  gBS->RestoreTPL (OldTpl);\r
+  return Status;\r
+\r
+}\r
+\r
+/**\r
+  Remove a registered notification function from a particular keystroke.\r
+\r
+  @param  This                 Protocol instance pointer.\r
+  @param  NotificationHandle   The handle of the notification function\r
+                               being unregistered.\r
+\r
+  @retval EFI_SUCCESS             The notification function was unregistered\r
+                                  successfully.\r
+  @retval EFI_INVALID_PARAMETER   The NotificationHandle is invalid.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+VirtualKeyboardUnregisterKeyNotify (\r
+  IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL  *This,\r
+  IN VOID                               *NotificationHandle\r
+  )\r
+{\r
+  EFI_STATUS                            Status;\r
+  VIRTUAL_KEYBOARD_DEV                  *VirtualKeyboardPrivate;\r
+  EFI_TPL                               OldTpl;\r
+  LIST_ENTRY                            *Link;\r
+  VIRTUAL_KEYBOARD_CONSOLE_IN_EX_NOTIFY *CurrentNotify;\r
+\r
+  //\r
+  // Check incoming notification handle\r
+  //\r
+  if (NotificationHandle == NULL) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  if (((VIRTUAL_KEYBOARD_CONSOLE_IN_EX_NOTIFY *) NotificationHandle)->Signature !=\r
+      VIRTUAL_KEYBOARD_CONSOLE_IN_EX_NOTIFY_SIGNATURE) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  VirtualKeyboardPrivate = TEXT_INPUT_EX_VIRTUAL_KEYBOARD_DEV_FROM_THIS (This);\r
+\r
+  //\r
+  // Enter critical section\r
+  //\r
+  OldTpl = gBS->RaiseTPL (TPL_NOTIFY);\r
+\r
+  for (Link = VirtualKeyboardPrivate->NotifyList.ForwardLink;\r
+       Link != &VirtualKeyboardPrivate->NotifyList;\r
+       Link = Link->ForwardLink) {\r
+    CurrentNotify = CR (\r
+                      Link,\r
+                      VIRTUAL_KEYBOARD_CONSOLE_IN_EX_NOTIFY,\r
+                      NotifyEntry,\r
+                      VIRTUAL_KEYBOARD_CONSOLE_IN_EX_NOTIFY_SIGNATURE\r
+                      );\r
+    if (CurrentNotify == NotificationHandle) {\r
+      //\r
+      // Remove the notification function from NotifyList and free resources\r
+      //\r
+      RemoveEntryList (&CurrentNotify->NotifyEntry);\r
+\r
+      Status = EFI_SUCCESS;\r
+      goto Exit;\r
+    }\r
+  }\r
+\r
+  //\r
+  // Can not find the specified Notification Handle\r
+  //\r
+  Status = EFI_INVALID_PARAMETER;\r
+\r
+Exit:\r
+  //\r
+  // Leave critical section and return\r
+  //\r
+  gBS->RestoreTPL (OldTpl);\r
+  return Status;\r
+}\r
+\r
+/**\r
+  Timer event handler: read a series of scancodes from 8042\r
+  and put them into memory scancode buffer.\r
+  it read as much scancodes to either fill\r
+  the memory buffer or empty the keyboard buffer.\r
+  It is registered as running under TPL_NOTIFY\r
+\r
+  @param Event       The timer event\r
+  @param Context     A KEYBOARD_CONSOLE_IN_DEV pointer\r
+\r
+**/\r
+VOID\r
+EFIAPI\r
+VirtualKeyboardTimerHandler (\r
+  IN EFI_EVENT    Event,\r
+  IN VOID         *Context\r
+  )\r
+{\r
+  EFI_TPL                                OldTpl;\r
+  LIST_ENTRY                             *Link;\r
+  EFI_KEY_DATA                           KeyData;\r
+  VIRTUAL_KEYBOARD_CONSOLE_IN_EX_NOTIFY  *CurrentNotify;\r
+  VIRTUAL_KEYBOARD_DEV                   *VirtualKeyboardPrivate;\r
+  VIRTUAL_KBD_KEY                        VirtualKey;\r
+\r
+  VirtualKeyboardPrivate = Context;\r
+\r
+  //\r
+  // Enter critical section\r
+  //\r
+  OldTpl = gBS->RaiseTPL (TPL_NOTIFY);\r
+\r
+  if (VirtualKeyboardPrivate->PlatformVirtual &&\r
+      VirtualKeyboardPrivate->PlatformVirtual->Query) {\r
+    if (VirtualKeyboardPrivate->PlatformVirtual->Query (&VirtualKey) ==\r
+        FALSE) {\r
+      goto Exit;\r
+    }\r
+    // Found key\r
+    KeyData.Key.ScanCode = VirtualKey.Key.ScanCode;\r
+    KeyData.Key.UnicodeChar = VirtualKey.Key.UnicodeChar;\r
+    KeyData.KeyState.KeyShiftState  = EFI_SHIFT_STATE_VALID;\r
+    KeyData.KeyState.KeyToggleState = EFI_TOGGLE_STATE_VALID;\r
+    if (VirtualKeyboardPrivate->PlatformVirtual->Clear) {\r
+      VirtualKeyboardPrivate->PlatformVirtual->Clear (&VirtualKey);\r
+    }\r
+  } else {\r
+    goto Exit;\r
+  }\r
+\r
+  //\r
+  // Signal KeyNotify process event if this key pressed matches any key registered.\r
+  //\r
+  for (Link = VirtualKeyboardPrivate->NotifyList.ForwardLink;\r
+       Link != &VirtualKeyboardPrivate->NotifyList;\r
+       Link = Link->ForwardLink) {\r
+    CurrentNotify = CR (\r
+                      Link,\r
+                      VIRTUAL_KEYBOARD_CONSOLE_IN_EX_NOTIFY,\r
+                      NotifyEntry,\r
+                      VIRTUAL_KEYBOARD_CONSOLE_IN_EX_NOTIFY_SIGNATURE\r
+                      );\r
+    if (IsKeyRegistered (&CurrentNotify->KeyData, &KeyData)) {\r
+      //\r
+      // The key notification function needs to run at TPL_CALLBACK\r
+      // while current TPL is TPL_NOTIFY. It will be invoked in\r
+      // KeyNotifyProcessHandler() which runs at TPL_CALLBACK.\r
+      //\r
+      Enqueue (&VirtualKeyboardPrivate->QueueForNotify, &KeyData);\r
+      gBS->SignalEvent (VirtualKeyboardPrivate->KeyNotifyProcessEvent);\r
+    }\r
+  }\r
+\r
+  Enqueue (&VirtualKeyboardPrivate->Queue, &KeyData);\r
+\r
+Exit:\r
+  //\r
+  // Leave critical section and return\r
+  //\r
+  gBS->RestoreTPL (OldTpl);\r
+}\r
+\r
+/**\r
+  Process key notify.\r
+\r
+  @param  Event                 Indicates the event that invoke this function.\r
+  @param  Context               Indicates the calling context.\r
+**/\r
+VOID\r
+EFIAPI\r
+KeyNotifyProcessHandler (\r
+  IN  EFI_EVENT                 Event,\r
+  IN  VOID                      *Context\r
+  )\r
+{\r
+  EFI_STATUS                            Status;\r
+  VIRTUAL_KEYBOARD_DEV                  *VirtualKeyboardPrivate;\r
+  EFI_KEY_DATA                          KeyData;\r
+  LIST_ENTRY                            *Link;\r
+  LIST_ENTRY                            *NotifyList;\r
+  VIRTUAL_KEYBOARD_CONSOLE_IN_EX_NOTIFY *CurrentNotify;\r
+  EFI_TPL                               OldTpl;\r
+\r
+  VirtualKeyboardPrivate = (VIRTUAL_KEYBOARD_DEV *) Context;\r
+\r
+  //\r
+  // Invoke notification functions.\r
+  //\r
+  NotifyList = &VirtualKeyboardPrivate->NotifyList;\r
+  while (TRUE) {\r
+    //\r
+    // Enter critical section\r
+    //\r
+    OldTpl = gBS->RaiseTPL (TPL_NOTIFY);\r
+    Status = Dequeue (&VirtualKeyboardPrivate->QueueForNotify, &KeyData);\r
+    //\r
+    // Leave critical section\r
+    //\r
+    gBS->RestoreTPL (OldTpl);\r
+    if (EFI_ERROR (Status)) {\r
+      break;\r
+    }\r
+    for (Link = GetFirstNode (NotifyList);\r
+         !IsNull (NotifyList, Link);\r
+         Link = GetNextNode (NotifyList, Link)) {\r
+      CurrentNotify = CR (Link,\r
+                        VIRTUAL_KEYBOARD_CONSOLE_IN_EX_NOTIFY,\r
+                        NotifyEntry,\r
+                        VIRTUAL_KEYBOARD_CONSOLE_IN_EX_NOTIFY_SIGNATURE\r
+                        );\r
+      if (IsKeyRegistered (&CurrentNotify->KeyData, &KeyData)) {\r
+        CurrentNotify->KeyNotificationFn (&KeyData);\r
+      }\r
+    }\r
+  }\r
+}\r
+\r
+/**\r
+  The user Entry Point for module VirtualKeyboard. The user code starts with\r
+  this function.\r
+\r
+  @param[in] ImageHandle    The firmware allocated handle for the EFI image.\r
+  @param[in] SystemTable    A pointer to the EFI System Table.\r
+\r
+  @retval EFI_SUCCESS       The entry point is executed successfully.\r
+  @retval other             Some error occurs when executing this entry point.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+InitializeVirtualKeyboard(\r
+  IN EFI_HANDLE           ImageHandle,\r
+  IN EFI_SYSTEM_TABLE     *SystemTable\r
+  )\r
+{\r
+  EFI_STATUS              Status;\r
+\r
+  //\r
+  // Install driver model protocol(s).\r
+  //\r
+  Status = EfiLibInstallDriverBindingComponentName2 (\r
+             ImageHandle,\r
+             SystemTable,\r
+             &gVirtualKeyboardDriverBinding,\r
+             ImageHandle,\r
+             &gVirtualKeyboardComponentName,\r
+             &gVirtualKeyboardComponentName2\r
+             );\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
+  return Status;\r
+}\r