]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Bus/Usb/UsbKbDxe/EfiKey.c
Fix the USB keyboard driver to call hotkey callback even no one is calling ReadKeyStroke
[mirror_edk2.git] / MdeModulePkg / Bus / Usb / UsbKbDxe / EfiKey.c
index 0716830989fe5f4506c9e1df2513e111bb9dafbc..fa1c7e076b9886d8c092fa5ec26c46ddc0a5df0a 100644 (file)
@@ -2,8 +2,8 @@
   USB Keyboard Driver that manages USB keyboard and produces Simple Text Input\r
   Protocol and Simple Text Input Ex Protocol.\r
 \r
-Copyright (c) 2004 - 2010, Intel Corporation\r
-All rights reserved. This program and the accompanying materials\r
+Copyright (c) 2004 - 2011, Intel Corporation. All rights reserved.<BR>\r
+This program and the accompanying materials\r
 are licensed and made available under the terms and conditions of the BSD License\r
 which accompanies this distribution.  The full text of the license may be found at\r
 http://opensource.org/licenses/bsd-license.php\r
@@ -154,7 +154,9 @@ USBKeyboardDriverBindingStart (
   UINT8                         PollingInterval;\r
   UINT8                         PacketSize;\r
   BOOLEAN                       Found;\r
+  EFI_TPL                       OldTpl;\r
 \r
+  OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
   //\r
   // Open USB I/O Protocol\r
   //\r
@@ -167,7 +169,7 @@ USBKeyboardDriverBindingStart (
                   EFI_OPEN_PROTOCOL_BY_DRIVER\r
                   );\r
   if (EFI_ERROR (Status)) {\r
-    return Status;\r
+    goto ErrorExit1;\r
   }\r
 \r
   UsbKeyboardDevice = AllocateZeroPool (sizeof (USB_KB_DEV));\r
@@ -260,6 +262,20 @@ USBKeyboardDriverBindingStart (
   \r
   InitializeListHead (&UsbKeyboardDevice->NotifyList);\r
   \r
+  Status = gBS->CreateEvent (\r
+                  EVT_TIMER | EVT_NOTIFY_SIGNAL,\r
+                  TPL_NOTIFY,\r
+                  USBKeyboardTimerHandler,\r
+                  UsbKeyboardDevice,\r
+                  &UsbKeyboardDevice->TimerEvent\r
+                  );\r
+  if (!EFI_ERROR (Status)) {\r
+    Status = gBS->SetTimer (UsbKeyboardDevice->TimerEvent, TimerPeriodic, KEYBOARD_TIMER_INTERVAL);\r
+  }\r
+  if (EFI_ERROR (Status)) {\r
+    goto ErrorExit;\r
+  }\r
+\r
   Status = gBS->CreateEvent (\r
                   EVT_NOTIFY_WAIT,\r
                   TPL_NOTIFY,\r
@@ -320,8 +336,8 @@ USBKeyboardDriverBindingStart (
   //\r
   // Reset USB Keyboard Device exhaustively.\r
   //\r
-  Status = UsbKeyboardDevice->SimpleInput.Reset (\r
-                                            &UsbKeyboardDevice->SimpleInput,\r
+  Status = UsbKeyboardDevice->SimpleInputEx.Reset (\r
+                                            &UsbKeyboardDevice->SimpleInputEx,\r
                                             TRUE\r
                                             );\r
   if (EFI_ERROR (Status)) {\r
@@ -381,6 +397,7 @@ USBKeyboardDriverBindingStart (
     FALSE\r
     );\r
 \r
+  gBS->RestoreTPL (OldTpl);\r
   return EFI_SUCCESS;\r
 \r
 //\r
@@ -388,6 +405,9 @@ USBKeyboardDriverBindingStart (
 //\r
 ErrorExit:\r
   if (UsbKeyboardDevice != NULL) {\r
+    if (UsbKeyboardDevice->TimerEvent != NULL) {\r
+      gBS->CloseEvent (UsbKeyboardDevice->TimerEvent);\r
+    }\r
     if (UsbKeyboardDevice->SimpleInput.WaitForKey != NULL) {\r
       gBS->CloseEvent (UsbKeyboardDevice->SimpleInput.WaitForKey);\r
     }\r
@@ -395,11 +415,9 @@ ErrorExit:
       gBS->CloseEvent (UsbKeyboardDevice->SimpleInputEx.WaitForKeyEx);\r
     }\r
     if (UsbKeyboardDevice->KeyboardLayoutEvent != NULL) {\r
+      ReleaseKeyboardLayoutResources (UsbKeyboardDevice);\r
       gBS->CloseEvent (UsbKeyboardDevice->KeyboardLayoutEvent);\r
     }\r
-    if (UsbKeyboardDevice->KeyConvertionTable != NULL) {\r
-      FreePool (UsbKeyboardDevice->KeyConvertionTable);\r
-    }\r
     FreePool (UsbKeyboardDevice);\r
     UsbKeyboardDevice = NULL;\r
   }\r
@@ -409,6 +427,10 @@ ErrorExit:
          This->DriverBindingHandle,\r
          Controller\r
          );\r
+\r
+ErrorExit1:\r
+  gBS->RestoreTPL (OldTpl);\r
+\r
   return Status;\r
 \r
 }\r
@@ -508,11 +530,12 @@ USBKeyboardDriverBindingStop (
   //\r
   // Free all resources.\r
   //\r
+  gBS->CloseEvent (UsbKeyboardDevice->TimerEvent);\r
   gBS->CloseEvent (UsbKeyboardDevice->RepeatTimer);\r
   gBS->CloseEvent (UsbKeyboardDevice->DelayedRecoveryEvent);\r
-  gBS->CloseEvent ((UsbKeyboardDevice->SimpleInput).WaitForKey);\r
-  gBS->CloseEvent (UsbKeyboardDevice->SimpleInputEx.WaitForKeyEx);  \r
-  KbdFreeNotifyList (&UsbKeyboardDevice->NotifyList);    \r
+  gBS->CloseEvent (UsbKeyboardDevice->SimpleInput.WaitForKey);\r
+  gBS->CloseEvent (UsbKeyboardDevice->SimpleInputEx.WaitForKeyEx);\r
+  KbdFreeNotifyList (&UsbKeyboardDevice->NotifyList);\r
 \r
   ReleaseKeyboardLayoutResources (UsbKeyboardDevice);\r
   gBS->CloseEvent (UsbKeyboardDevice->KeyboardLayoutEvent);\r
@@ -521,6 +544,9 @@ USBKeyboardDriverBindingStop (
     FreeUnicodeStringTable (UsbKeyboardDevice->ControllerNameTable);\r
   }\r
 \r
+  DestroyQueue (&UsbKeyboardDevice->UsbKeyQueue);\r
+  DestroyQueue (&UsbKeyboardDevice->EfiKeyQueue);\r
+  \r
   FreePool (UsbKeyboardDevice);\r
 \r
   return Status;\r
@@ -542,89 +568,20 @@ USBKeyboardDriverBindingStop (
 \r
 **/\r
 EFI_STATUS\r
-EFIAPI\r
 USBKeyboardReadKeyStrokeWorker (\r
   IN OUT USB_KB_DEV                 *UsbKeyboardDevice,\r
      OUT EFI_KEY_DATA               *KeyData\r
   )\r
 {\r
-  EFI_STATUS                        Status;\r
-  UINT8                             KeyCode;  \r
-  LIST_ENTRY                        *Link;\r
-  LIST_ENTRY                        *NotifyList;\r
-  KEYBOARD_CONSOLE_IN_EX_NOTIFY     *CurrentNotify;  \r
-  EFI_KEY_DATA                      OriginalKeyData;\r
-\r
   if (KeyData == NULL) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
-  //\r
-  // If there is no saved USB keycode, fetch it\r
-  // by calling USBKeyboardCheckForKey().\r
-  //\r
-  if (UsbKeyboardDevice->CurKeyCode == 0) {\r
-    Status = USBKeyboardCheckForKey (UsbKeyboardDevice);\r
-    if (EFI_ERROR (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 current keycode and clear it.\r
-  //\r
-  KeyCode = UsbKeyboardDevice->CurKeyCode;\r
-  UsbKeyboardDevice->CurKeyCode = 0;\r
-\r
-  //\r
-  // Translate saved USB keycode into EFI_INPUT_KEY\r
-  //\r
-  Status = UsbKeyCodeToEfiInputKey (UsbKeyboardDevice, KeyCode, &KeyData->Key);\r
-  if (EFI_ERROR (Status)) {\r
-    return Status;\r
+  if (IsQueueEmpty (&UsbKeyboardDevice->EfiKeyQueue)) {\r
+    return EFI_NOT_READY;\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.\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) {\r
-    if (OriginalKeyData.Key.UnicodeChar >= 0x01 && OriginalKeyData.Key.UnicodeChar <= 0x1A) {\r
-      if (UsbKeyboardDevice->CapsOn) {\r
-        OriginalKeyData.Key.UnicodeChar = (CHAR16)(OriginalKeyData.Key.UnicodeChar + 'A' - 1);\r
-      } else {\r
-        OriginalKeyData.Key.UnicodeChar = (CHAR16)(OriginalKeyData.Key.UnicodeChar + 'a' - 1);\r
-      } \r
-    }\r
-  }\r
-  \r
-  //\r
-  // Invoke notification functions if the key is registered.\r
-  //\r
-  NotifyList = &UsbKeyboardDevice->NotifyList;\r
-  for (Link = GetFirstNode (NotifyList);\r
-       !IsNull (NotifyList, Link);\r
-       Link = GetNextNode (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
-  }\r
+  Dequeue (&UsbKeyboardDevice->EfiKeyQueue, KeyData, sizeof (*KeyData));\r
 \r
   return EFI_SUCCESS;\r
 }\r
@@ -675,8 +632,8 @@ USBKeyboardReset (
     //\r
     // Clear the key buffer of this USB keyboard\r
     //\r
-    InitUSBKeyBuffer (&(UsbKeyboardDevice->KeyboardBuffer));\r
-    UsbKeyboardDevice->CurKeyCode = 0;\r
+    InitQueue (&UsbKeyboardDevice->UsbKeyQueue, sizeof (USB_KEY));\r
+    InitQueue (&UsbKeyboardDevice->EfiKeyQueue, sizeof (EFI_KEY_DATA));\r
 \r
     return EFI_SUCCESS;\r
   }\r
@@ -685,7 +642,6 @@ USBKeyboardReset (
   // Exhaustive reset\r
   //\r
   Status = InitUSBKeyboard (UsbKeyboardDevice);\r
-  UsbKeyboardDevice->CurKeyCode = 0;\r
   if (EFI_ERROR (Status)) {\r
     return EFI_DEVICE_ERROR;\r
   }\r
@@ -750,50 +706,55 @@ USBKeyboardWaitForKey (
 \r
   UsbKeyboardDevice = (USB_KB_DEV *) Context;\r
 \r
-  if (UsbKeyboardDevice->CurKeyCode == 0) {\r
-    if (EFI_ERROR (USBKeyboardCheckForKey (UsbKeyboardDevice))) {\r
-      //\r
-      // If no pending key, simply return.\r
-      //\r
-      return ;\r
-    }\r
+  if (!IsQueueEmpty (&UsbKeyboardDevice->EfiKeyQueue)) {\r
+    //\r
+    // If there is pending key, signal the event.\r
+    //\r
+    gBS->SignalEvent (Event);\r
   }\r
-  //\r
-  // If there is pending key, signal the event.\r
-  //\r
-  gBS->SignalEvent (Event);\r
 }\r
 \r
-\r
 /**\r
-  Check whether there is key pending in the keyboard buffer.\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
+  Timer handler to convert the key from USB.\r
 \r
+  @param  Event                    Indicates the event that invoke this function.\r
+  @param  Context                  Indicates the calling context.\r
 **/\r
-EFI_STATUS\r
+VOID\r
 EFIAPI\r
-USBKeyboardCheckForKey (\r
-  IN OUT  USB_KB_DEV    *UsbKeyboardDevice\r
+USBKeyboardTimerHandler (\r
+  IN  EFI_EVENT                 Event,\r
+  IN  VOID                      *Context\r
   )\r
 {\r
-  EFI_STATUS  Status;\r
-  UINT8       KeyCode;\r
+  EFI_STATUS                    Status;\r
+  USB_KB_DEV                    *UsbKeyboardDevice;\r
+  UINT8                         KeyCode;\r
+  EFI_KEY_DATA                  KeyData;\r
 \r
+  UsbKeyboardDevice = (USB_KB_DEV *) Context;\r
+  \r
   //\r
   // Fetch raw data from the USB keyboard buffer,\r
   // and translate it into USB keycode.\r
   //\r
   Status = USBParseKey (UsbKeyboardDevice, &KeyCode);\r
   if (EFI_ERROR (Status)) {\r
-    return EFI_NOT_READY;\r
+    return ;\r
   }\r
 \r
-  UsbKeyboardDevice->CurKeyCode = KeyCode;\r
-  return EFI_SUCCESS;\r
+  //\r
+  // Translate saved USB keycode into EFI_INPUT_KEY\r
+  //\r
+  Status = UsbKeyCodeToEfiInputKey (UsbKeyboardDevice, KeyCode, &KeyData);\r
+  if (EFI_ERROR (Status)) {\r
+    return ;\r
+  }\r
+\r
+  //\r
+  // Insert to the EFI Key queue\r
+  //\r
+  Enqueue (&UsbKeyboardDevice->EfiKeyQueue, &KeyData, sizeof (KeyData));\r
 }\r
 \r
 /**\r
@@ -806,7 +767,6 @@ USBKeyboardCheckForKey (
 \r
 **/\r
 EFI_STATUS\r
-EFIAPI\r
 KbdFreeNotifyList (\r
   IN OUT LIST_ENTRY           *NotifyList\r
   )\r
@@ -838,7 +798,6 @@ KbdFreeNotifyList (
 \r
 **/\r
 BOOLEAN\r
-EFIAPI\r
 IsKeyRegistered (\r
   IN EFI_KEY_DATA  *RegsiteredData,\r
   IN EFI_KEY_DATA  *InputData\r
@@ -900,8 +859,6 @@ USBKeyboardResetEx (
 {\r
   EFI_STATUS                Status;\r
   USB_KB_DEV                *UsbKeyboardDevice;\r
-  EFI_TPL                   OldTpl;\r
-  \r
 \r
   UsbKeyboardDevice = TEXT_INPUT_EX_USB_KB_DEV_FROM_THIS (This);\r
 \r
@@ -910,11 +867,6 @@ USBKeyboardResetEx (
     return EFI_DEVICE_ERROR;\r
   }\r
 \r
-  OldTpl = gBS->RaiseTPL (TPL_NOTIFY);\r
-  UsbKeyboardDevice->KeyState.KeyShiftState  = EFI_SHIFT_STATE_VALID;\r
-  UsbKeyboardDevice->KeyState.KeyToggleState = EFI_TOGGLE_STATE_VALID;\r
-  gBS->RestoreTPL (OldTpl);\r
-\r
   return EFI_SUCCESS;\r
 \r
 }\r
@@ -981,8 +933,7 @@ USBKeyboardSetState (
 \r
   UsbKeyboardDevice = TEXT_INPUT_EX_USB_KB_DEV_FROM_THIS (This);\r
 \r
-  if (((UsbKeyboardDevice->KeyState.KeyToggleState & EFI_TOGGLE_STATE_VALID) != EFI_TOGGLE_STATE_VALID) ||\r
-      ((*KeyToggleState & EFI_TOGGLE_STATE_VALID) != EFI_TOGGLE_STATE_VALID)) {\r
+  if ((*KeyToggleState & EFI_TOGGLE_STATE_VALID) != EFI_TOGGLE_STATE_VALID) {\r
     return EFI_UNSUPPORTED;\r
   }\r
 \r
@@ -1006,8 +957,6 @@ USBKeyboardSetState (
 \r
   SetKeyLED (UsbKeyboardDevice);\r
 \r
-  UsbKeyboardDevice->KeyState.KeyToggleState = *KeyToggleState;\r
-\r
   return EFI_SUCCESS;\r
   \r
 }\r