]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Bus/Usb/UsbKbDxe/EfiKey.c
Install default keyboard layout package in USB keyboard driver.
[mirror_edk2.git] / MdeModulePkg / Bus / Usb / UsbKbDxe / EfiKey.c
index d889b974d6d8e221c26f02ff69086ff4f806bd60..da6ce75b3a8daf59a64d4e3ad453b39ece28cb17 100644 (file)
@@ -120,7 +120,7 @@ USBKeyboardDriverBindingSupported (
 }\r
 \r
 /**\r
-  Starts the device with this driver.\r
+  Starts the keyboard device with this driver.\r
 \r
   This function produces Simple Text Input Protocol and Simple Text Input Ex Protocol,\r
   initializes the keyboard device, and submit Asynchronous Interrupt Transfer to manage\r
@@ -219,7 +219,7 @@ USBKeyboardDriverBindingStart (
   EndpointNumber = UsbKeyboardDevice->InterfaceDescriptor.NumEndpoints;\r
 \r
   //\r
-  // Traverse endpoints to find interrupt endpoints\r
+  // Traverse endpoints to find interrupt endpoint\r
   //\r
   Found = FALSE;\r
   for (Index = 0; Index < EndpointNumber; Index++) {\r
@@ -230,7 +230,7 @@ USBKeyboardDriverBindingStart (
              &EndpointDescriptor\r
              );\r
 \r
-    if ((EndpointDescriptor.Attributes & 0x03) == USB_ENDPOINT_INTERRUPT) {\r
+    if ((EndpointDescriptor.Attributes & (BIT0 | BIT1)) == USB_ENDPOINT_INTERRUPT) {\r
       //\r
       // We only care interrupt endpoint here\r
       //\r
@@ -256,7 +256,7 @@ USBKeyboardDriverBindingStart (
   UsbKeyboardDevice->SimpleInputEx.ReadKeyStrokeEx     = USBKeyboardReadKeyStrokeEx;\r
   UsbKeyboardDevice->SimpleInputEx.SetState            = USBKeyboardSetState;\r
   UsbKeyboardDevice->SimpleInputEx.RegisterKeyNotify   = USBKeyboardRegisterKeyNotify;\r
-  UsbKeyboardDevice->SimpleInputEx.UnregisterKeyNotify = USBKeyboardUnregisterKeyNotify; \r
+  UsbKeyboardDevice->SimpleInputEx.UnregisterKeyNotify = USBKeyboardUnregisterKeyNotify;\r
   \r
   InitializeListHead (&UsbKeyboardDevice->NotifyList);\r
   \r
@@ -279,12 +279,6 @@ USBKeyboardDriverBindingStart (
                   UsbKeyboardDevice,\r
                   &(UsbKeyboardDevice->SimpleInput.WaitForKey)\r
                   );\r
-\r
-  if (EFI_ERROR (Status)) {\r
-    goto ErrorExit;\r
-  }\r
-\r
-  Status = InitKeyboardLayout (UsbKeyboardDevice);\r
   if (EFI_ERROR (Status)) {\r
     goto ErrorExit;\r
   }\r
@@ -293,9 +287,8 @@ USBKeyboardDriverBindingStart (
   // 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
+  // when plugging into system, other conventional console devices could\r
+  // distinguish it by its device path.\r
   //\r
   Status = gBS->InstallMultipleProtocolInterfaces (\r
                   &Controller,\r
@@ -303,14 +296,27 @@ USBKeyboardDriverBindingStart (
                   &UsbKeyboardDevice->SimpleInput,\r
                   &gEfiSimpleTextInputExProtocolGuid,\r
                   &UsbKeyboardDevice->SimpleInputEx,\r
-                  &gEfiHotPlugDeviceGuid,\r
-                  NULL,\r
                   NULL\r
                   );\r
   if (EFI_ERROR (Status)) {\r
     goto ErrorExit;\r
   }\r
 \r
+  UsbKeyboardDevice->ControllerHandle = Controller;\r
+  Status = InitKeyboardLayout (UsbKeyboardDevice);\r
+  if (EFI_ERROR (Status)) {\r
+    gBS->UninstallMultipleProtocolInterfaces (\r
+      Controller,\r
+      &gEfiSimpleTextInProtocolGuid,\r
+      &UsbKeyboardDevice->SimpleInput,\r
+      &gEfiSimpleTextInputExProtocolGuid,\r
+      &UsbKeyboardDevice->SimpleInputEx,\r
+      NULL\r
+      );\r
+    goto ErrorExit;\r
+  }\r
+\r
+\r
   //\r
   // Reset USB Keyboard Device exhaustively.\r
   //\r
@@ -325,8 +331,6 @@ USBKeyboardDriverBindingStart (
            &UsbKeyboardDevice->SimpleInput,\r
            &gEfiSimpleTextInputExProtocolGuid,\r
            &UsbKeyboardDevice->SimpleInputEx,\r
-           &gEfiHotPlugDeviceGuid,\r
-           NULL,\r
            NULL\r
            );\r
     goto ErrorExit;\r
@@ -356,8 +360,6 @@ USBKeyboardDriverBindingStart (
            &UsbKeyboardDevice->SimpleInput,\r
            &gEfiSimpleTextInputExProtocolGuid,\r
            &UsbKeyboardDevice->SimpleInputEx,\r
-           &gEfiHotPlugDeviceGuid,\r
-           NULL,\r
            NULL\r
            );\r
     goto ErrorExit;\r
@@ -392,7 +394,7 @@ ErrorExit:
     if (UsbKeyboardDevice->SimpleInputEx.WaitForKeyEx != NULL) {\r
       gBS->CloseEvent (UsbKeyboardDevice->SimpleInputEx.WaitForKeyEx);\r
     }\r
-    gBS->FreePool (UsbKeyboardDevice);\r
+    FreePool (UsbKeyboardDevice);\r
     UsbKeyboardDevice = NULL;\r
   }\r
   gBS->CloseProtocol (\r
@@ -495,8 +497,6 @@ USBKeyboardDriverBindingStop (
                   &UsbKeyboardDevice->SimpleInput,\r
                   &gEfiSimpleTextInputExProtocolGuid,\r
                   &UsbKeyboardDevice->SimpleInputEx,\r
-                  &gEfiHotPlugDeviceGuid,\r
-                  NULL,\r
                   NULL\r
                   );\r
   //\r
@@ -515,7 +515,7 @@ USBKeyboardDriverBindingStop (
     FreeUnicodeStringTable (UsbKeyboardDevice->ControllerNameTable);\r
   }\r
 \r
-  gBS->FreePool (UsbKeyboardDevice);\r
+  FreePool (UsbKeyboardDevice);\r
 \r
   return Status;\r
 }\r
@@ -545,6 +545,7 @@ USBKeyboardReadKeyStrokeWorker (
   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
@@ -609,9 +610,10 @@ USBKeyboardReadKeyStrokeWorker (
   //\r
   // Invoke notification functions if the key is registered.\r
   //\r
-  for (Link = GetFirstNode (&UsbKeyboardDevice->NotifyList);\r
-       !IsNull (&UsbKeyboardDevice->NotifyList, Link);\r
-       Link = GetNextNode (&UsbKeyboardDevice->NotifyList, Link)) {\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
@@ -622,7 +624,7 @@ USBKeyboardReadKeyStrokeWorker (
 }\r
 \r
 /**\r
-  Reset the input device and optionaly run diagnostics\r
+  Reset the input device and optionally run diagnostics\r
 \r
   There are 2 types of reset for USB keyboard.\r
   For non-exhaustive reset, only keyboard buffer is cleared.\r
@@ -695,7 +697,7 @@ USBKeyboardReset (
 \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 keydtroke information was not returned due to\r
+  @retval EFI_DEVICE_ERROR     The keystroke information was not returned due to\r
                                hardware errors.\r
 \r
 **/\r
@@ -813,7 +815,7 @@ KbdFreeNotifyList (
     Link = GetFirstNode (NotifyList);\r
     NotifyNode = CR (Link, KEYBOARD_CONSOLE_IN_EX_NOTIFY, NotifyEntry, USB_KB_CONSOLE_IN_EX_NOTIFY_SIGNATURE);\r
     RemoveEntryList (Link);\r
-    gBS->FreePool (NotifyNode);\r
+    FreePool (NotifyNode);\r
   }\r
   \r
   return EFI_SUCCESS;\r
@@ -826,7 +828,7 @@ KbdFreeNotifyList (
   @param  InputData         A pointer to keystroke data for the key that was pressed.\r
 \r
   @retval TRUE              Key pressed matches a registered key.\r
-  @retval FLASE             Key pressed does not matche a registered key.\r
+  @retval FLASE             Key pressed does not matches a registered key.\r
 \r
 **/\r
 BOOLEAN\r
@@ -1015,7 +1017,7 @@ USBKeyboardSetState (
   @param  NotifyHandle                Points to the unique handle assigned to the registered notification.\r
 \r
   @retval EFI_SUCCESS                 The notification function was registered successfully.\r
-  @retval EFI_OUT_OF_RESOURCES        Unable to allocate resources for necesssary data structures.\r
+  @retval EFI_OUT_OF_RESOURCES        Unable to allocate resources for necessary data structures.\r
   @retval EFI_INVALID_PARAMETER       KeyData or NotifyHandle or KeyNotificationFunction is NULL.\r
 \r
 **/\r
@@ -1029,9 +1031,9 @@ USBKeyboardRegisterKeyNotify (
   )\r
 {\r
   USB_KB_DEV                        *UsbKeyboardDevice;\r
-  EFI_STATUS                        Status;\r
   KEYBOARD_CONSOLE_IN_EX_NOTIFY     *NewNotify;\r
   LIST_ENTRY                        *Link;\r
+  LIST_ENTRY                        *NotifyList;\r
   KEYBOARD_CONSOLE_IN_EX_NOTIFY     *CurrentNotify;  \r
 \r
   if (KeyData == NULL || NotifyHandle == NULL || KeyNotificationFunction == NULL) {\r
@@ -1043,9 +1045,11 @@ USBKeyboardRegisterKeyNotify (
   //\r
   // Return EFI_SUCCESS if the (KeyData, NotificationFunction) is already registered.\r
   //\r
-  for (Link = GetFirstNode (&UsbKeyboardDevice->NotifyList);\r
-       !IsNull (&UsbKeyboardDevice->NotifyList, Link);\r
-       Link = GetNextNode (&UsbKeyboardDevice->NotifyList, Link)) {\r
+  NotifyList = &UsbKeyboardDevice->NotifyList;\r
+  \r
+  for (Link = GetFirstNode (NotifyList);\r
+       !IsNull (NotifyList, Link);\r
+       Link = GetNextNode (NotifyList, Link)) {\r
     CurrentNotify = CR (\r
                       Link, \r
                       KEYBOARD_CONSOLE_IN_EX_NOTIFY, \r
@@ -1070,20 +1074,11 @@ USBKeyboardRegisterKeyNotify (
 \r
   NewNotify->Signature         = USB_KB_CONSOLE_IN_EX_NOTIFY_SIGNATURE;     \r
   NewNotify->KeyNotificationFn = KeyNotificationFunction;\r
+  NewNotify->NotifyHandle      = (EFI_HANDLE) NewNotify;\r
   CopyMem (&NewNotify->KeyData, KeyData, sizeof (EFI_KEY_DATA));\r
   InsertTailList (&UsbKeyboardDevice->NotifyList, &NewNotify->NotifyEntry);\r
 \r
-  //\r
-  // Use gSimpleTextInExNotifyGuid to get a valid EFI_HANDLE\r
-  //  \r
-  Status = gBS->InstallMultipleProtocolInterfaces (\r
-                  &NewNotify->NotifyHandle,\r
-                  &gSimpleTextInExNotifyGuid,\r
-                  NULL,\r
-                  NULL\r
-                  );\r
-  ASSERT_EFI_ERROR (Status);\r
-  \r
+\r
   *NotifyHandle = NewNotify->NotifyHandle;  \r
   \r
   return EFI_SUCCESS;\r
@@ -1098,7 +1093,6 @@ USBKeyboardRegisterKeyNotify (
 \r
   @retval EFI_SUCCESS              The notification function was unregistered successfully.\r
   @retval EFI_INVALID_PARAMETER    The NotificationHandle is invalid\r
-  @retval EFI_NOT_FOUND            Cannot find the matching entry in database.\r
 \r
 **/\r
 EFI_STATUS\r
@@ -1109,37 +1103,27 @@ USBKeyboardUnregisterKeyNotify (
   )\r
 {\r
   USB_KB_DEV                        *UsbKeyboardDevice;\r
-  EFI_STATUS                        Status;\r
   KEYBOARD_CONSOLE_IN_EX_NOTIFY     *CurrentNotify;\r
   LIST_ENTRY                        *Link;\r
+  LIST_ENTRY                        *NotifyList;\r
 \r
   if (NotificationHandle == NULL) {\r
     return EFI_INVALID_PARAMETER;\r
   }  \r
+\r
+  if (((KEYBOARD_CONSOLE_IN_EX_NOTIFY *) NotificationHandle)->Signature != USB_KB_CONSOLE_IN_EX_NOTIFY_SIGNATURE) {\r
+    return EFI_INVALID_PARAMETER;\r
+  } \r
   \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
-                  NULL,\r
-                  NULL,\r
-                  NULL,\r
-                  EFI_OPEN_PROTOCOL_TEST_PROTOCOL\r
-                  );\r
-  if (EFI_ERROR (Status)) {\r
-    return EFI_INVALID_PARAMETER;\r
-  }\r
-\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
+  NotifyList = &UsbKeyboardDevice->NotifyList;\r
+  for (Link = GetFirstNode (NotifyList);\r
+       !IsNull (NotifyList, Link);\r
+       Link = GetNextNode (NotifyList, Link)) {\r
     CurrentNotify = CR (\r
                       Link, \r
                       KEYBOARD_CONSOLE_IN_EX_NOTIFY, \r
@@ -1151,18 +1135,15 @@ USBKeyboardUnregisterKeyNotify (
       // Remove the notification function from NotifyList and free resources\r
       //\r
       RemoveEntryList (&CurrentNotify->NotifyEntry);      \r
-      Status = gBS->UninstallMultipleProtocolInterfaces (\r
-                      CurrentNotify->NotifyHandle,\r
-                      &gSimpleTextInExNotifyGuid,\r
-                      NULL,\r
-                      NULL\r
-                      );\r
-      ASSERT_EFI_ERROR (Status);\r
-      gBS->FreePool (CurrentNotify);            \r
+\r
+      FreePool (CurrentNotify);            \r
       return EFI_SUCCESS;\r
     }\r
   }\r
 \r
-  return EFI_NOT_FOUND;  \r
+  //\r
+  // Cannot find the matching entry in database.\r
+  //\r
+  return EFI_INVALID_PARAMETER;  \r
 }\r
 \r