From a7022cec4e8b46d7f2bfc1ee84333cf6e71e633b Mon Sep 17 00:00:00 2001 From: eric_tian Date: Wed, 23 Jul 2008 06:29:46 +0000 Subject: modify coding style to pass ecc tool git-svn-id: https://edk2.svn.sourceforge.net/svnroot/edk2/trunk/edk2@5551 6f19259b-4bc3-4df7-8a09-765794883524 --- MdeModulePkg/Bus/Usb/UsbKbDxe/ComponentName.c | 13 +- MdeModulePkg/Bus/Usb/UsbKbDxe/EfiKey.c | 1390 ++++++++++++++++++ MdeModulePkg/Bus/Usb/UsbKbDxe/EfiKey.h | 318 ++++ MdeModulePkg/Bus/Usb/UsbKbDxe/KeyBoard.c | 1924 ++++++++++++++++++++++++ MdeModulePkg/Bus/Usb/UsbKbDxe/KeyBoard.h | 260 ++++ MdeModulePkg/Bus/Usb/UsbKbDxe/UsbKbDxe.inf | 8 +- MdeModulePkg/Bus/Usb/UsbKbDxe/efikey.c | 1340 ----------------- MdeModulePkg/Bus/Usb/UsbKbDxe/efikey.h | 337 ----- MdeModulePkg/Bus/Usb/UsbKbDxe/keyboard.c | 1951 ------------------------- MdeModulePkg/Bus/Usb/UsbKbDxe/keyboard.h | 118 -- 10 files changed, 3900 insertions(+), 3759 deletions(-) create mode 100644 MdeModulePkg/Bus/Usb/UsbKbDxe/EfiKey.c create mode 100644 MdeModulePkg/Bus/Usb/UsbKbDxe/EfiKey.h create mode 100644 MdeModulePkg/Bus/Usb/UsbKbDxe/KeyBoard.c create mode 100644 MdeModulePkg/Bus/Usb/UsbKbDxe/KeyBoard.h delete mode 100644 MdeModulePkg/Bus/Usb/UsbKbDxe/efikey.c delete mode 100644 MdeModulePkg/Bus/Usb/UsbKbDxe/efikey.h delete mode 100644 MdeModulePkg/Bus/Usb/UsbKbDxe/keyboard.c delete mode 100644 MdeModulePkg/Bus/Usb/UsbKbDxe/keyboard.h (limited to 'MdeModulePkg') diff --git a/MdeModulePkg/Bus/Usb/UsbKbDxe/ComponentName.c b/MdeModulePkg/Bus/Usb/UsbKbDxe/ComponentName.c index b95b70335..e40a79b9f 100644 --- a/MdeModulePkg/Bus/Usb/UsbKbDxe/ComponentName.c +++ b/MdeModulePkg/Bus/Usb/UsbKbDxe/ComponentName.c @@ -1,6 +1,8 @@ /** @file -Copyright (c) 2004 - 2007, Intel Corporation + UEFI Component Name(2) protocol implementation for Usb Keyboard driver. + +Copyright (c) 2004 - 2008, Intel Corporation All rights reserved. This program and the accompanying materials are licensed and made available under the terms and conditions of the BSD License which accompanies this distribution. The full text of the license may be found at @@ -9,17 +11,10 @@ http://opensource.org/licenses/bsd-license.php THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. -Module Name: - - ComponentName.c - -Abstract: - - **/ -#include "keyboard.h" +#include "KeyBoard.h" // // EFI Component Name Functions diff --git a/MdeModulePkg/Bus/Usb/UsbKbDxe/EfiKey.c b/MdeModulePkg/Bus/Usb/UsbKbDxe/EfiKey.c new file mode 100644 index 000000000..b971903a2 --- /dev/null +++ b/MdeModulePkg/Bus/Usb/UsbKbDxe/EfiKey.c @@ -0,0 +1,1390 @@ +/** @file + + USB Keyboard Driver that includes the implementation of interface. + +Copyright (c) 2004 - 2008, Intel Corporation +All rights reserved. This program and the accompanying materials +are licensed and made available under the terms and conditions of the BSD License +which accompanies this distribution. The full text of the license may be found at +http://opensource.org/licenses/bsd-license.php + +THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, +WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. + +**/ + +#include "EfiKey.h" +#include "KeyBoard.h" + +/** + The Usb Keyboard Driver Entry Point. + + @param ImageHandle The driver image handle. + @param SystemTable The system table. + + @return EFI_SUCCESS The component name protocol is installed. + @return Others Failed to install. + +**/ +EFI_STATUS +EFIAPI +USBKeyboardDriverBindingEntryPoint ( + IN EFI_HANDLE ImageHandle, + IN EFI_SYSTEM_TABLE *SystemTable + ); + +/** + Check whether USB keyboard driver support this device. + + @param This The USB keyboard driver binding protocol. + @param Controller The controller handle to check. + @param RemainingDevicePath The remaining device path. + + @retval EFI_SUCCESS The driver supports this controller. + @retval EFI_UNSUPPORTED This device isn't supported. +**/ +EFI_STATUS +EFIAPI +USBKeyboardDriverBindingSupported ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE Controller, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath + ); + +/** + Start running driver on the controller. + + @param This The USB keyboard driver binding instance. + @param Controller The controller to check. + @param RemainingDevicePath The remaining device patch. + + @retval EFI_SUCCESS The controller is controlled by the usb keyboard driver. + @return Other The keyboard driver doesn't support this controller. + +**/ +EFI_STATUS +EFIAPI +USBKeyboardDriverBindingStart ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE Controller, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath + ); + +/** + Stop handle the controller by this USB keyboard driver. + + @param This The USB keyboard driver binding protocol. + @param Controller The controller to release. + @param NumberOfChildren The number of handles in ChildHandleBuffer. + @param ChildHandleBuffer The array of child handle. + + @retval EFI_SUCCESS The controller or children are stopped. + @retval EFI_DEVICE_ERROR Failed to stop the driver. + +**/ +EFI_STATUS +EFIAPI +USBKeyboardDriverBindingStop ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE Controller, + IN UINTN NumberOfChildren, + IN EFI_HANDLE *ChildHandleBuffer + ); + +/** + Reset Usb Keyboard. + + @param This The protocol instance of EFI_SIMPLE_TEXT_INPUT_PROTOCOL. + @param ExtendedVerification Whether completely reset keyboard or not. + + @retval EFI_SUCCESS Reset keyboard successfully. + @retval EFI_DEVICE_ERROR Reset keyboard failed. + +**/ +EFI_STATUS +EFIAPI +USBKeyboardReset ( + IN EFI_SIMPLE_TEXT_INPUT_PROTOCOL *This, + IN BOOLEAN ExtendedVerification + ); + +/** + Implements EFI_SIMPLE_TEXT_INPUT_PROTOCOL.ReadKeyStroke() function. + + @param This The EFI_SIMPLE_TEXT_INPUT_PROTOCOL instance. + @param Key A pointer to a buffer that is filled in with the keystroke + information for the key that was pressed. + + @retval EFI_SUCCESS Read key stroke successfully. + @retval Other Read key stroke failed. + +**/ +EFI_STATUS +EFIAPI +USBKeyboardReadKeyStroke ( + IN EFI_SIMPLE_TEXT_INPUT_PROTOCOL *This, + OUT EFI_INPUT_KEY *Key + ); + +/** + Handler function for WaitForKey event. + + @param Event Event to be signaled when a key is pressed. + @param Context Points to USB_KB_DEV instance. + + @return None. +**/ +VOID +EFIAPI +USBKeyboardWaitForKey ( + IN EFI_EVENT Event, + IN VOID *Context + ); + +/** + Check whether there is key pending. + + @param UsbKeyboardDevice The USB_KB_DEV instance. + + @retval EFI_SUCCESS Have key pending to read. + @retval Other Parse key failed. + +**/ +EFI_STATUS +EFIAPI +USBKeyboardCheckForKey ( + IN USB_KB_DEV *UsbKeyboardDevice + ); + +EFI_GUID gEfiUsbKeyboardDriverGuid = { + 0xa05f5f78, 0xfb3, 0x4d10, {0x90, 0x90, 0xac, 0x4, 0x6e, 0xeb, 0x7c, 0x3c} +}; + +/** + Free keyboard notify list. + + @param ListHead The list head. + + @retval EFI_SUCCESS Free the notify list successfully. + @retval EFI_INVALID_PARAMETER ListHead is invalid. + +**/ +EFI_STATUS +EFIAPI +KbdFreeNotifyList ( + IN OUT LIST_ENTRY *ListHead + ); + +/** + Whether the pressed key matches a registered key or not. + + @param RegsiteredData A pointer to a buffer that is filled in with the keystroke + state data for the key that was registered. + @param InputData A pointer to a buffer that is filled in with the keystroke + state data for the key that was pressed. + + @retval TRUE Key pressed matches a registered key. + @retval FLASE Match failed. + +**/ +BOOLEAN +EFIAPI +IsKeyRegistered ( + IN EFI_KEY_DATA *RegsiteredData, + IN EFI_KEY_DATA *InputData + ); + + +// +// USB Keyboard Driver Global Variables +// +EFI_DRIVER_BINDING_PROTOCOL gUsbKeyboardDriverBinding = { + USBKeyboardDriverBindingSupported, + USBKeyboardDriverBindingStart, + USBKeyboardDriverBindingStop, + 0xa, + NULL, + NULL +}; + +/** + The Usb Keyboard Driver Entry Point. + + @param ImageHandle The driver image handle. + @param SystemTable The system table. + + @return EFI_SUCCESS The component name protocol is installed. + @return Others Failed to install. + +**/ +EFI_STATUS +EFIAPI +USBKeyboardDriverBindingEntryPoint ( + IN EFI_HANDLE ImageHandle, + IN EFI_SYSTEM_TABLE *SystemTable + ) +{ + return EfiLibInstallDriverBindingComponentName2 ( + ImageHandle, + SystemTable, + &gUsbKeyboardDriverBinding, + ImageHandle, + &gUsbKeyboardComponentName, + &gUsbKeyboardComponentName2 + ); +} + +/** + Check whether USB keyboard driver support this device. + + @param This The USB keyboard driver binding protocol. + @param Controller The controller handle to check. + @param RemainingDevicePath The remaining device path. + + @retval EFI_SUCCESS The driver supports this controller. + @retval EFI_UNSUPPORTED This device isn't supported. +**/ +EFI_STATUS +EFIAPI +USBKeyboardDriverBindingSupported ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE Controller, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath + ) +{ + EFI_STATUS OpenStatus; + EFI_USB_IO_PROTOCOL *UsbIo; + EFI_STATUS Status; + + // + // Check if USB_IO protocol is attached on the controller handle. + // + OpenStatus = gBS->OpenProtocol ( + Controller, + &gEfiUsbIoProtocolGuid, + (VOID **) &UsbIo, + This->DriverBindingHandle, + Controller, + EFI_OPEN_PROTOCOL_BY_DRIVER + ); + if (EFI_ERROR (OpenStatus)) { + return OpenStatus; + } + + // + // Use the USB I/O protocol interface to check whether the Controller is + // the Keyboard controller that can be managed by this driver. + // + Status = EFI_SUCCESS; + + if (!IsUSBKeyboard (UsbIo)) { + Status = EFI_UNSUPPORTED; + } + + gBS->CloseProtocol ( + Controller, + &gEfiUsbIoProtocolGuid, + This->DriverBindingHandle, + Controller + ); + + return Status; +} + +/** + Start running driver on the controller. + + @param This The USB keyboard driver binding instance. + @param Controller The controller to check. + @param RemainingDevicePath The remaining device patch. + + @retval EFI_SUCCESS The controller is controlled by the usb keyboard driver. + @return Other The keyboard driver doesn't support this controller. + +**/ +EFI_STATUS +EFIAPI +USBKeyboardDriverBindingStart ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE Controller, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath + ) +{ + EFI_STATUS Status; + EFI_USB_IO_PROTOCOL *UsbIo; + USB_KB_DEV *UsbKeyboardDevice; + UINT8 EndpointNumber; + EFI_USB_ENDPOINT_DESCRIPTOR EndpointDescriptor; + UINT8 Index; + UINT8 EndpointAddr; + UINT8 PollingInterval; + UINT8 PacketSize; + BOOLEAN Found; + + UsbKeyboardDevice = NULL; + Found = FALSE; + + // + // Open USB_IO Protocol + // + Status = gBS->OpenProtocol ( + Controller, + &gEfiUsbIoProtocolGuid, + (VOID **) &UsbIo, + This->DriverBindingHandle, + Controller, + EFI_OPEN_PROTOCOL_BY_DRIVER + ); + if (EFI_ERROR (Status)) { + return Status; + } + + UsbKeyboardDevice = AllocateZeroPool (sizeof (USB_KB_DEV)); + if (UsbKeyboardDevice == NULL) { + gBS->CloseProtocol ( + Controller, + &gEfiUsbIoProtocolGuid, + This->DriverBindingHandle, + Controller + ); + return EFI_OUT_OF_RESOURCES; + } + // + // Get the Device Path Protocol on Controller's handle + // + Status = gBS->OpenProtocol ( + Controller, + &gEfiDevicePathProtocolGuid, + (VOID **) &UsbKeyboardDevice->DevicePath, + This->DriverBindingHandle, + Controller, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + + if (EFI_ERROR (Status)) { + gBS->FreePool (UsbKeyboardDevice); + gBS->CloseProtocol ( + Controller, + &gEfiUsbIoProtocolGuid, + This->DriverBindingHandle, + Controller + ); + return Status; + } + // + // Report that the usb keyboard is being enabled + // + KbdReportStatusCode ( + UsbKeyboardDevice->DevicePath, + EFI_PROGRESS_CODE, + PcdGet32 (PcdStatusCodeValueKeyboardEnable) + ); + + // + // This is pretty close to keyboard detection, so log progress + // + KbdReportStatusCode ( + UsbKeyboardDevice->DevicePath, + EFI_PROGRESS_CODE, + PcdGet32 (PcdStatusCodeValueKeyboardPresenceDetect) + ); + + // + // Initialize UsbKeyboardDevice + // + UsbKeyboardDevice->UsbIo = UsbIo; + + // + // Get interface & endpoint descriptor + // + UsbIo->UsbGetInterfaceDescriptor ( + UsbIo, + &UsbKeyboardDevice->InterfaceDescriptor + ); + + EndpointNumber = UsbKeyboardDevice->InterfaceDescriptor.NumEndpoints; + + for (Index = 0; Index < EndpointNumber; Index++) { + + UsbIo->UsbGetEndpointDescriptor ( + UsbIo, + Index, + &EndpointDescriptor + ); + + if ((EndpointDescriptor.Attributes & 0x03) == 0x03) { + // + // We only care interrupt endpoint here + // + CopyMem(&UsbKeyboardDevice->IntEndpointDescriptor, &EndpointDescriptor, sizeof(EndpointDescriptor)); + Found = TRUE; + } + } + + if (!Found) { + // + // No interrupt endpoint found, then return unsupported. + // + gBS->FreePool (UsbKeyboardDevice); + gBS->CloseProtocol ( + Controller, + &gEfiUsbIoProtocolGuid, + This->DriverBindingHandle, + Controller + ); + return EFI_UNSUPPORTED; + } + + UsbKeyboardDevice->Signature = USB_KB_DEV_SIGNATURE; + UsbKeyboardDevice->SimpleInput.Reset = USBKeyboardReset; + UsbKeyboardDevice->SimpleInput.ReadKeyStroke = USBKeyboardReadKeyStroke; + + UsbKeyboardDevice->SimpleInputEx.Reset = USBKeyboardResetEx; + UsbKeyboardDevice->SimpleInputEx.ReadKeyStrokeEx = USBKeyboardReadKeyStrokeEx; + UsbKeyboardDevice->SimpleInputEx.SetState = USBKeyboardSetState; + UsbKeyboardDevice->SimpleInputEx.RegisterKeyNotify = USBKeyboardRegisterKeyNotify; + UsbKeyboardDevice->SimpleInputEx.UnregisterKeyNotify = USBKeyboardUnregisterKeyNotify; + + InitializeListHead (&UsbKeyboardDevice->NotifyList); + + Status = gBS->CreateEvent ( + EVT_NOTIFY_WAIT, + TPL_NOTIFY, + USBKeyboardWaitForKey, + UsbKeyboardDevice, + &(UsbKeyboardDevice->SimpleInputEx.WaitForKeyEx) + ); + + if (EFI_ERROR (Status)) { + goto ErrorExit; + } + + Status = InitKeyboardLayout (UsbKeyboardDevice); + if (EFI_ERROR (Status)) { + goto ErrorExit; + } + + Status = gBS->CreateEvent ( + EVT_NOTIFY_WAIT, + TPL_NOTIFY, + USBKeyboardWaitForKey, + UsbKeyboardDevice, + &(UsbKeyboardDevice->SimpleInput.WaitForKey) + ); + + if (EFI_ERROR (Status)) { + gBS->FreePool (UsbKeyboardDevice); + gBS->CloseProtocol ( + Controller, + &gEfiUsbIoProtocolGuid, + This->DriverBindingHandle, + Controller + ); + return Status; + } + + // + // Install simple txt in protocol interface + // for the usb keyboard device. + // Usb keyboard is a hot plug device, and expected to work immediately + // when plugging into system, so a HotPlugDeviceGuid is installed onto + // the usb keyboard device handle, to distinguish it from other conventional + // console devices. + // + Status = gBS->InstallMultipleProtocolInterfaces ( + &Controller, + &gEfiSimpleTextInProtocolGuid, + &UsbKeyboardDevice->SimpleInput, + &gEfiSimpleTextInputExProtocolGuid, + &UsbKeyboardDevice->SimpleInputEx, + &gEfiHotPlugDeviceGuid, + NULL, + NULL + ); + if (EFI_ERROR (Status)) { + gBS->CloseEvent (UsbKeyboardDevice->SimpleInput.WaitForKey); + gBS->FreePool (UsbKeyboardDevice); + gBS->CloseProtocol ( + Controller, + &gEfiUsbIoProtocolGuid, + This->DriverBindingHandle, + Controller + ); + return Status; + } + + // + // Reset USB Keyboard Device + // + Status = UsbKeyboardDevice->SimpleInput.Reset ( + &UsbKeyboardDevice->SimpleInput, + TRUE + ); + if (EFI_ERROR (Status)) { + gBS->UninstallMultipleProtocolInterfaces ( + Controller, + &gEfiSimpleTextInProtocolGuid, + &UsbKeyboardDevice->SimpleInput, + &gEfiSimpleTextInputExProtocolGuid, + &UsbKeyboardDevice->SimpleInputEx, + &gEfiHotPlugDeviceGuid, + NULL, + NULL + ); + gBS->CloseEvent (UsbKeyboardDevice->SimpleInput.WaitForKey); + gBS->FreePool (UsbKeyboardDevice); + gBS->CloseProtocol ( + Controller, + &gEfiUsbIoProtocolGuid, + This->DriverBindingHandle, + Controller + ); + return Status; + } + // + // submit async interrupt transfer + // + EndpointAddr = UsbKeyboardDevice->IntEndpointDescriptor.EndpointAddress; + PollingInterval = UsbKeyboardDevice->IntEndpointDescriptor.Interval; + PacketSize = (UINT8) (UsbKeyboardDevice->IntEndpointDescriptor.MaxPacketSize); + + Status = UsbIo->UsbAsyncInterruptTransfer ( + UsbIo, + EndpointAddr, + TRUE, + PollingInterval, + PacketSize, + KeyboardHandler, + UsbKeyboardDevice + ); + + if (EFI_ERROR (Status)) { + + gBS->UninstallMultipleProtocolInterfaces ( + Controller, + &gEfiSimpleTextInProtocolGuid, + &UsbKeyboardDevice->SimpleInput, + &gEfiSimpleTextInputExProtocolGuid, + &UsbKeyboardDevice->SimpleInputEx, + &gEfiHotPlugDeviceGuid, + NULL, + NULL + ); + gBS->CloseEvent (UsbKeyboardDevice->SimpleInput.WaitForKey); + gBS->FreePool (UsbKeyboardDevice); + gBS->CloseProtocol ( + Controller, + &gEfiUsbIoProtocolGuid, + This->DriverBindingHandle, + Controller + ); + return Status; + } + + UsbKeyboardDevice->ControllerNameTable = NULL; + AddUnicodeString2 ( + "eng", + gUsbKeyboardComponentName.SupportedLanguages, + &UsbKeyboardDevice->ControllerNameTable, + L"Generic Usb Keyboard", + TRUE + ); + AddUnicodeString2 ( + "en", + gUsbKeyboardComponentName2.SupportedLanguages, + &UsbKeyboardDevice->ControllerNameTable, + L"Generic Usb Keyboard", + FALSE + ); + + + return EFI_SUCCESS; + +ErrorExit: + if (UsbKeyboardDevice != NULL) { + if (UsbKeyboardDevice->SimpleInput.WaitForKey != NULL) { + gBS->CloseEvent (UsbKeyboardDevice->SimpleInput.WaitForKey); + } + if (UsbKeyboardDevice->SimpleInputEx.WaitForKeyEx != NULL) { + gBS->CloseEvent (UsbKeyboardDevice->SimpleInputEx.WaitForKeyEx); + } + KbdFreeNotifyList (&UsbKeyboardDevice->NotifyList); + gBS->FreePool (UsbKeyboardDevice); + UsbKeyboardDevice = NULL; + } + gBS->CloseProtocol ( + Controller, + &gEfiUsbIoProtocolGuid, + This->DriverBindingHandle, + Controller + ); + return Status; + +} + + +/** + Stop handle the controller by this USB keyboard driver. + + @param This The USB keyboard driver binding protocol. + @param Controller The controller to release. + @param NumberOfChildren The number of handles in ChildHandleBuffer. + @param ChildHandleBuffer The array of child handle. + + @retval EFI_SUCCESS The controller or children are stopped. + @retval EFI_DEVICE_ERROR Failed to stop the driver. + +**/ +EFI_STATUS +EFIAPI +USBKeyboardDriverBindingStop ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE Controller, + IN UINTN NumberOfChildren, + IN EFI_HANDLE *ChildHandleBuffer + ) +{ + EFI_STATUS Status; + EFI_SIMPLE_TEXT_INPUT_PROTOCOL *SimpleInput; + USB_KB_DEV *UsbKeyboardDevice; + + Status = gBS->OpenProtocol ( + Controller, + &gEfiSimpleTextInProtocolGuid, + (VOID **) &SimpleInput, + This->DriverBindingHandle, + Controller, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + if (EFI_ERROR (Status)) { + return EFI_UNSUPPORTED; + } + Status = gBS->OpenProtocol ( + Controller, + &gEfiSimpleTextInputExProtocolGuid, + NULL, + This->DriverBindingHandle, + Controller, + EFI_OPEN_PROTOCOL_TEST_PROTOCOL + ); + if (EFI_ERROR (Status)) { + return EFI_UNSUPPORTED; + } + // + // Get USB_KB_DEV instance. + // + UsbKeyboardDevice = USB_KB_DEV_FROM_THIS (SimpleInput); + + gBS->CloseProtocol ( + Controller, + &gEfiSimpleTextInProtocolGuid, + This->DriverBindingHandle, + Controller + ); + + // + // Uninstall the Asyn Interrupt Transfer from this device + // will disable the key data input from this device + // + KbdReportStatusCode ( + UsbKeyboardDevice->DevicePath, + EFI_PROGRESS_CODE, + PcdGet32 (PcdStatusCodeValueKeyboardDisable) + ); + + // + // Destroy asynchronous interrupt transfer + // + UsbKeyboardDevice->UsbIo->UsbAsyncInterruptTransfer ( + UsbKeyboardDevice->UsbIo, + UsbKeyboardDevice->IntEndpointDescriptor.EndpointAddress, + FALSE, + UsbKeyboardDevice->IntEndpointDescriptor.Interval, + 0, + NULL, + NULL + ); + + gBS->CloseProtocol ( + Controller, + &gEfiUsbIoProtocolGuid, + This->DriverBindingHandle, + Controller + ); + + Status = gBS->UninstallMultipleProtocolInterfaces ( + Controller, + &gEfiSimpleTextInProtocolGuid, + &UsbKeyboardDevice->SimpleInput, + &gEfiSimpleTextInputExProtocolGuid, + &UsbKeyboardDevice->SimpleInputEx, + &gEfiHotPlugDeviceGuid, + NULL, + NULL + ); + // + // free all the resources. + // + gBS->CloseEvent (UsbKeyboardDevice->RepeatTimer); + gBS->CloseEvent (UsbKeyboardDevice->DelayedRecoveryEvent); + gBS->CloseEvent ((UsbKeyboardDevice->SimpleInput).WaitForKey); + gBS->CloseEvent (UsbKeyboardDevice->SimpleInputEx.WaitForKeyEx); + KbdFreeNotifyList (&UsbKeyboardDevice->NotifyList); + + ReleaseKeyboardLayoutResources (UsbKeyboardDevice); + gBS->CloseEvent (UsbKeyboardDevice->KeyboardLayoutEvent); + + if (UsbKeyboardDevice->ControllerNameTable != NULL) { + FreeUnicodeStringTable (UsbKeyboardDevice->ControllerNameTable); + } + + gBS->FreePool (UsbKeyboardDevice); + + return Status; + +} + +/** + Reads the next keystroke from the input device. The WaitForKey Event can + be used to test for existance of a keystroke via WaitForEvent () call. + + @param UsbKeyboardDevice Usb keyboard's private structure. + @param KeyData A pointer to a buffer that is filled in with the keystroke + state data for the key that was pressed. + + @return EFI_SUCCESS The keystroke information was returned. + @return EFI_NOT_READY There was no keystroke data availiable. + @return EFI_DEVICE_ERROR The keystroke information was not returned due to + hardware errors. + @return EFI_INVALID_PARAMETER KeyData is NULL. + +**/ +EFI_STATUS +EFIAPI +USBKeyboardReadKeyStrokeWorker ( + IN USB_KB_DEV *UsbKeyboardDevice, + OUT EFI_KEY_DATA *KeyData + ) +{ + + EFI_STATUS Status; + UINT8 KeyChar; + LIST_ENTRY *Link; + KEYBOARD_CONSOLE_IN_EX_NOTIFY *CurrentNotify; + EFI_KEY_DATA OriginalKeyData; + + if (KeyData == NULL) { + return EFI_INVALID_PARAMETER; + } + + // + // if there is no saved ASCII byte, fetch it + // by calling USBKeyboardCheckForKey(). + // + if (UsbKeyboardDevice->CurKeyChar == 0) { + Status = USBKeyboardCheckForKey (UsbKeyboardDevice); + if (EFI_ERROR (Status)) { + return Status; + } + } + + KeyData->Key.UnicodeChar = 0; + KeyData->Key.ScanCode = SCAN_NULL; + + KeyChar = UsbKeyboardDevice->CurKeyChar; + + UsbKeyboardDevice->CurKeyChar = 0; + + // + // Translate saved ASCII byte into EFI_INPUT_KEY + // + Status = USBKeyCodeToEFIScanCode (UsbKeyboardDevice, KeyChar, &KeyData->Key); + if (EFI_ERROR (Status)) { + return Status; + } + + CopyMem (&KeyData->KeyState, &UsbKeyboardDevice->KeyState, sizeof (KeyData->KeyState)); + + UsbKeyboardDevice->KeyState.KeyShiftState = EFI_SHIFT_STATE_VALID; + UsbKeyboardDevice->KeyState.KeyToggleState = EFI_TOGGLE_STATE_VALID; + + // + //Switch the control value to their original characters. In USBKeyCodeToEFIScanCode() the CTRL-Alpha characters have been switched to + // their corresponding control value (ctrl-a = 0x0001 through ctrl-Z = 0x001A), here switch them back for notification function. + // + CopyMem (&OriginalKeyData, KeyData, sizeof (EFI_KEY_DATA)); + if (UsbKeyboardDevice->CtrlOn != 0) { + if (OriginalKeyData.Key.UnicodeChar >= 0x01 && OriginalKeyData.Key.UnicodeChar <= 0x1A) { + if (UsbKeyboardDevice->CapsOn != 0) { + OriginalKeyData.Key.UnicodeChar = (CHAR16)(OriginalKeyData.Key.UnicodeChar + 'A' - 1); + } else { + OriginalKeyData.Key.UnicodeChar = (CHAR16)(OriginalKeyData.Key.UnicodeChar + 'a' - 1); + } + } + } + + // + // Invoke notification functions if exist + // + for (Link = UsbKeyboardDevice->NotifyList.ForwardLink; Link != &UsbKeyboardDevice->NotifyList; Link = Link->ForwardLink) { + CurrentNotify = CR ( + Link, + KEYBOARD_CONSOLE_IN_EX_NOTIFY, + NotifyEntry, + USB_KB_CONSOLE_IN_EX_NOTIFY_SIGNATURE + ); + if (IsKeyRegistered (&CurrentNotify->KeyData, &OriginalKeyData)) { + CurrentNotify->KeyNotificationFn (&OriginalKeyData); + } + } + + return EFI_SUCCESS; + +} + +/** + Reset Usb Keyboard. + + @param This The protocol instance of EFI_SIMPLE_TEXT_INPUT_PROTOCOL. + @param ExtendedVerification Whether completely reset keyboard or not. + + @retval EFI_SUCCESS Reset keyboard successfully. + @retval EFI_DEVICE_ERROR Reset keyboard failed. + +**/ +EFI_STATUS +EFIAPI +USBKeyboardReset ( + IN EFI_SIMPLE_TEXT_INPUT_PROTOCOL *This, + IN BOOLEAN ExtendedVerification + ) +{ + EFI_STATUS Status; + USB_KB_DEV *UsbKeyboardDevice; + + UsbKeyboardDevice = USB_KB_DEV_FROM_THIS (This); + + KbdReportStatusCode ( + UsbKeyboardDevice->DevicePath, + EFI_PROGRESS_CODE, + PcdGet32 (PcdStatusCodeValueKeyboardReset) + ); + + // + // Non Exhaustive reset: + // only reset private data structures. + // + if (!ExtendedVerification) { + // + // Clear the key buffer of this Usb keyboard + // + KbdReportStatusCode ( + UsbKeyboardDevice->DevicePath, + EFI_PROGRESS_CODE, + PcdGet32 (PcdStatusCodeValueKeyboardClearBuffer) + ); + + InitUSBKeyBuffer (&(UsbKeyboardDevice->KeyboardBuffer)); + UsbKeyboardDevice->CurKeyChar = 0; + return EFI_SUCCESS; + } + + // + // Exhaustive reset + // + Status = InitUSBKeyboard (UsbKeyboardDevice); + UsbKeyboardDevice->CurKeyChar = 0; + if (EFI_ERROR (Status)) { + return EFI_DEVICE_ERROR; + } + + return EFI_SUCCESS; +} + + +/** + Implements EFI_SIMPLE_TEXT_INPUT_PROTOCOL.ReadKeyStroke() function. + + @param This The EFI_SIMPLE_TEXT_INPUT_PROTOCOL instance. + @param Key A pointer to a buffer that is filled in with the keystroke + information for the key that was pressed. + + @retval EFI_SUCCESS Read key stroke successfully. + @retval Other Read key stroke failed. + +**/ +EFI_STATUS +EFIAPI +USBKeyboardReadKeyStroke ( + IN EFI_SIMPLE_TEXT_INPUT_PROTOCOL *This, + OUT EFI_INPUT_KEY *Key + ) +{ + USB_KB_DEV *UsbKeyboardDevice; + EFI_STATUS Status; + EFI_KEY_DATA KeyData; + + UsbKeyboardDevice = USB_KB_DEV_FROM_THIS (This); + + Status = USBKeyboardReadKeyStrokeWorker (UsbKeyboardDevice, &KeyData); + if (EFI_ERROR (Status)) { + return Status; + } + + CopyMem (Key, &KeyData.Key, sizeof (EFI_INPUT_KEY)); + + return EFI_SUCCESS; + +} + + +/** + Handler function for WaitForKey event. + + @param Event Event to be signaled when a key is pressed. + @param Context Points to USB_KB_DEV instance. + + @return None. +**/ +VOID +EFIAPI +USBKeyboardWaitForKey ( + IN EFI_EVENT Event, + IN VOID *Context + ) +{ + USB_KB_DEV *UsbKeyboardDevice; + + UsbKeyboardDevice = (USB_KB_DEV *) Context; + + if (UsbKeyboardDevice->CurKeyChar == 0) { + + if (EFI_ERROR (USBKeyboardCheckForKey (UsbKeyboardDevice))) { + return ; + } + } + // + // If has key pending, signal the event. + // + gBS->SignalEvent (Event); +} + + +/** + Check whether there is key pending. + + @param UsbKeyboardDevice The USB_KB_DEV instance. + + @retval EFI_SUCCESS Have key pending to read. + @retval Other Parse key failed. + +**/ +EFI_STATUS +EFIAPI +USBKeyboardCheckForKey ( + IN USB_KB_DEV *UsbKeyboardDevice + ) +{ + EFI_STATUS Status; + UINT8 KeyChar; + + // + // Fetch raw data from the USB keyboard input, + // and translate it into ASCII data. + // + Status = USBParseKey (UsbKeyboardDevice, &KeyChar); + if (EFI_ERROR (Status)) { + return Status; + } + + UsbKeyboardDevice->CurKeyChar = KeyChar; + return EFI_SUCCESS; +} + +/** + Report Status Code in Usb Keyboard Driver. + + @param DevicePath Use this to get Device Path. + @param CodeType Status Code Type. + @param CodeValue Status Code Value. + + @return None. + +**/ +VOID +EFIAPI +KbdReportStatusCode ( + IN EFI_DEVICE_PATH_PROTOCOL *DevicePath, + IN EFI_STATUS_CODE_TYPE CodeType, + IN EFI_STATUS_CODE_VALUE Value + ) +{ + + REPORT_STATUS_CODE_WITH_DEVICE_PATH ( + CodeType, + Value, + DevicePath + ); +} + +/** + Free keyboard notify list. + + @param ListHead The list head. + + @retval EFI_SUCCESS Free the notify list successfully. + @retval EFI_INVALID_PARAMETER ListHead is invalid. + +**/ +EFI_STATUS +EFIAPI +KbdFreeNotifyList ( + IN OUT LIST_ENTRY *ListHead + ) +{ + KEYBOARD_CONSOLE_IN_EX_NOTIFY *NotifyNode; + + if (ListHead == NULL) { + return EFI_INVALID_PARAMETER; + } + while (!IsListEmpty (ListHead)) { + NotifyNode = CR ( + ListHead->ForwardLink, + KEYBOARD_CONSOLE_IN_EX_NOTIFY, + NotifyEntry, + USB_KB_CONSOLE_IN_EX_NOTIFY_SIGNATURE + ); + RemoveEntryList (ListHead->ForwardLink); + gBS->FreePool (NotifyNode); + } + + return EFI_SUCCESS; +} + +/** + Whether the pressed key matches a registered key or not. + + @param RegsiteredData A pointer to a buffer that is filled in with the keystroke + state data for the key that was registered. + @param InputData A pointer to a buffer that is filled in with the keystroke + state data for the key that was pressed. + + @retval TRUE Key pressed matches a registered key. + @retval FLASE Match failed. + +**/ +BOOLEAN +EFIAPI +IsKeyRegistered ( + IN EFI_KEY_DATA *RegsiteredData, + IN EFI_KEY_DATA *InputData + ) +{ + ASSERT (RegsiteredData != NULL && InputData != NULL); + + if ((RegsiteredData->Key.ScanCode != InputData->Key.ScanCode) || + (RegsiteredData->Key.UnicodeChar != InputData->Key.UnicodeChar)) { + return FALSE; + } + + // + // Assume KeyShiftState/KeyToggleState = 0 in Registered key data means these state could be ignored. + // + if (RegsiteredData->KeyState.KeyShiftState != 0 && + RegsiteredData->KeyState.KeyShiftState != InputData->KeyState.KeyShiftState) { + return FALSE; + } + if (RegsiteredData->KeyState.KeyToggleState != 0 && + RegsiteredData->KeyState.KeyToggleState != InputData->KeyState.KeyToggleState) { + return FALSE; + } + + return TRUE; + +} + +// +// Simple Text Input Ex protocol functions +// +/** + The extension routine to reset the input device. + + @param This Protocol instance pointer. + @param ExtendedVerification Driver may perform diagnostics on reset. + + @retval EFI_SUCCESS The device was reset. + @retval EFI_DEVICE_ERROR The device is not functioning properly and could + not be reset. + +**/ +EFI_STATUS +EFIAPI +USBKeyboardResetEx ( + IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *This, + IN BOOLEAN ExtendedVerification + ) +{ + EFI_STATUS Status; + USB_KB_DEV *UsbKeyboardDevice; + EFI_TPL OldTpl; + + + UsbKeyboardDevice = TEXT_INPUT_EX_USB_KB_DEV_FROM_THIS (This); + + Status = UsbKeyboardDevice->SimpleInput.Reset (&UsbKeyboardDevice->SimpleInput, ExtendedVerification); + if (EFI_ERROR (Status)) { + return EFI_DEVICE_ERROR; + } + + OldTpl = gBS->RaiseTPL (TPL_NOTIFY); + UsbKeyboardDevice->KeyState.KeyShiftState = EFI_SHIFT_STATE_VALID; + UsbKeyboardDevice->KeyState.KeyToggleState = EFI_TOGGLE_STATE_VALID; + gBS->RestoreTPL (OldTpl); + + return EFI_SUCCESS; + +} + +/** + Reads the next keystroke from the input device. The WaitForKey Event can + be used to test for existance of a keystroke via WaitForEvent () call. + + @param This Protocol instance pointer. + @param KeyData A pointer to a buffer that is filled in with the keystroke + state data for the key that was pressed. + + @return EFI_SUCCESS The keystroke information was returned successfully. + @retval EFI_INVALID_PARAMETER KeyData is NULL. + @retval Other Read key stroke information failed. + +**/ +EFI_STATUS +EFIAPI +USBKeyboardReadKeyStrokeEx ( + IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *This, + OUT EFI_KEY_DATA *KeyData + ) +{ + USB_KB_DEV *UsbKeyboardDevice; + + if (KeyData == NULL) { + return EFI_INVALID_PARAMETER; + } + + UsbKeyboardDevice = TEXT_INPUT_EX_USB_KB_DEV_FROM_THIS (This); + + return USBKeyboardReadKeyStrokeWorker (UsbKeyboardDevice, KeyData); + +} + +/** + Set certain state for the input device. + + @param This Protocol instance pointer. + @param KeyToggleState A pointer to the EFI_KEY_TOGGLE_STATE to set the + state for the input device. + + @retval EFI_SUCCESS The device state was set successfully. + @retval EFI_UNSUPPORTED The device does not have the ability to set its state. + @retval EFI_INVALID_PARAMETER KeyToggleState is NULL. + +**/ +EFI_STATUS +EFIAPI +USBKeyboardSetState ( + IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *This, + IN EFI_KEY_TOGGLE_STATE *KeyToggleState + ) +{ + USB_KB_DEV *UsbKeyboardDevice; + + if (KeyToggleState == NULL) { + return EFI_INVALID_PARAMETER; + } + + UsbKeyboardDevice = TEXT_INPUT_EX_USB_KB_DEV_FROM_THIS (This); + + if (((UsbKeyboardDevice->KeyState.KeyToggleState & EFI_TOGGLE_STATE_VALID) != EFI_TOGGLE_STATE_VALID) || + ((*KeyToggleState & EFI_TOGGLE_STATE_VALID) != EFI_TOGGLE_STATE_VALID)) { + return EFI_UNSUPPORTED; + } + + // + // Update the status light + // + + UsbKeyboardDevice->ScrollOn = 0; + UsbKeyboardDevice->NumLockOn = 0; + UsbKeyboardDevice->CapsOn = 0; + + if ((*KeyToggleState & EFI_SCROLL_LOCK_ACTIVE) == EFI_SCROLL_LOCK_ACTIVE) { + UsbKeyboardDevice->ScrollOn = 1; + } + if ((*KeyToggleState & EFI_NUM_LOCK_ACTIVE) == EFI_NUM_LOCK_ACTIVE) { + UsbKeyboardDevice->NumLockOn = 1; + } + if ((*KeyToggleState & EFI_CAPS_LOCK_ACTIVE) == EFI_CAPS_LOCK_ACTIVE) { + UsbKeyboardDevice->CapsOn = 1; + } + + SetKeyLED (UsbKeyboardDevice); + + UsbKeyboardDevice->KeyState.KeyToggleState = *KeyToggleState; + + return EFI_SUCCESS; + +} + +/** + Register a notification function for a particular keystroke for the input device. + + @param This Protocol instance pointer. + @param KeyData A pointer to a buffer that is filled in with the keystroke + information data for the key that was pressed. + @param KeyNotificationFunction Points to the function to be called when the key + sequence is typed specified by KeyData. + @param NotifyHandle Points to the unique handle assigned to the registered notification. + + @retval EFI_SUCCESS The notification function was registered successfully. + @retval EFI_OUT_OF_RESOURCES Unable to allocate resources for necesssary data structures. + @retval EFI_INVALID_PARAMETER KeyData or NotifyHandle is NULL. + +**/ +EFI_STATUS +EFIAPI +USBKeyboardRegisterKeyNotify ( + IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *This, + IN EFI_KEY_DATA *KeyData, + IN EFI_KEY_NOTIFY_FUNCTION KeyNotificationFunction, + OUT EFI_HANDLE *NotifyHandle + ) +{ + USB_KB_DEV *UsbKeyboardDevice; + EFI_STATUS Status; + KEYBOARD_CONSOLE_IN_EX_NOTIFY *NewNotify; + LIST_ENTRY *Link; + KEYBOARD_CONSOLE_IN_EX_NOTIFY *CurrentNotify; + + if (KeyData == NULL || NotifyHandle == NULL || KeyNotificationFunction == NULL) { + return EFI_INVALID_PARAMETER; + } + + UsbKeyboardDevice = TEXT_INPUT_EX_USB_KB_DEV_FROM_THIS (This); + + // + // Return EFI_SUCCESS if the (KeyData, NotificationFunction) is already registered. + // + for (Link = UsbKeyboardDevice->NotifyList.ForwardLink; Link != &UsbKeyboardDevice->NotifyList; Link = Link->ForwardLink) { + CurrentNotify = CR ( + Link, + KEYBOARD_CONSOLE_IN_EX_NOTIFY, + NotifyEntry, + USB_KB_CONSOLE_IN_EX_NOTIFY_SIGNATURE + ); + if (IsKeyRegistered (&CurrentNotify->KeyData, KeyData)) { + if (CurrentNotify->KeyNotificationFn == KeyNotificationFunction) { + *NotifyHandle = CurrentNotify->NotifyHandle; + return EFI_SUCCESS; + } + } + } + + // + // Allocate resource to save the notification function + // + NewNotify = (KEYBOARD_CONSOLE_IN_EX_NOTIFY *) AllocateZeroPool (sizeof (KEYBOARD_CONSOLE_IN_EX_NOTIFY)); + if (NewNotify == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + NewNotify->Signature = USB_KB_CONSOLE_IN_EX_NOTIFY_SIGNATURE; + NewNotify->KeyNotificationFn = KeyNotificationFunction; + CopyMem (&NewNotify->KeyData, KeyData, sizeof (EFI_KEY_DATA)); + InsertTailList (&UsbKeyboardDevice->NotifyList, &NewNotify->NotifyEntry); + + // + // Use gSimpleTextInExNotifyGuid to get a valid EFI_HANDLE + // + Status = gBS->InstallMultipleProtocolInterfaces ( + &NewNotify->NotifyHandle, + &gSimpleTextInExNotifyGuid, + NULL, + NULL + ); + ASSERT_EFI_ERROR (Status); + + *NotifyHandle = NewNotify->NotifyHandle; + + return EFI_SUCCESS; + +} + +/** + Remove a registered notification function from a particular keystroke. + + @param This Protocol instance pointer. + @param NotificationHandle The handle of the notification function being unregistered. + + @retval EFI_SUCCESS The notification function was unregistered successfully. + @retval EFI_INVALID_PARAMETER The NotificationHandle is invalid or opening gSimpleTextInExNotifyGuid + on NotificationHandle fails. + @retval EFI_NOT_FOUND Can not find the matching entry in database. + +**/ +EFI_STATUS +EFIAPI +USBKeyboardUnregisterKeyNotify ( + IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *This, + IN EFI_HANDLE NotificationHandle + ) +{ + USB_KB_DEV *UsbKeyboardDevice; + EFI_STATUS Status; + KEYBOARD_CONSOLE_IN_EX_NOTIFY *CurrentNotify; + LIST_ENTRY *Link; + + if (NotificationHandle == NULL) { + return EFI_INVALID_PARAMETER; + } + + UsbKeyboardDevice = TEXT_INPUT_EX_USB_KB_DEV_FROM_THIS (This); + + Status = gBS->OpenProtocol ( + NotificationHandle, + &gSimpleTextInExNotifyGuid, + NULL, + NULL, + NULL, + EFI_OPEN_PROTOCOL_TEST_PROTOCOL + ); + if (EFI_ERROR (Status)) { + return EFI_INVALID_PARAMETER; + } + + for (Link = UsbKeyboardDevice->NotifyList.ForwardLink; Link != &UsbKeyboardDevice->NotifyList; Link = Link->ForwardLink) { + CurrentNotify = CR ( + Link, + KEYBOARD_CONSOLE_IN_EX_NOTIFY, + NotifyEntry, + USB_KB_CONSOLE_IN_EX_NOTIFY_SIGNATURE + ); + if (CurrentNotify->NotifyHandle == NotificationHandle) { + // + // Remove the notification function from NotifyList and free resources + // + RemoveEntryList (&CurrentNotify->NotifyEntry); + Status = gBS->UninstallMultipleProtocolInterfaces ( + CurrentNotify->NotifyHandle, + &gSimpleTextInExNotifyGuid, + NULL, + NULL + ); + ASSERT_EFI_ERROR (Status); + gBS->FreePool (CurrentNotify); + return EFI_SUCCESS; + } + } + + return EFI_NOT_FOUND; +} + diff --git a/MdeModulePkg/Bus/Usb/UsbKbDxe/EfiKey.h b/MdeModulePkg/Bus/Usb/UsbKbDxe/EfiKey.h new file mode 100644 index 000000000..b933201cc --- /dev/null +++ b/MdeModulePkg/Bus/Usb/UsbKbDxe/EfiKey.h @@ -0,0 +1,318 @@ +/** @file + + Header file for USB Keyboard Driver's Data Structures. + +Copyright (c) 2004 - 2008, Intel Corporation +All rights reserved. This program and the accompanying materials +are licensed and made available under the terms and conditions of the BSD License +which accompanies this distribution. The full text of the license may be found at +http://opensource.org/licenses/bsd-license.php + +THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, +WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. + +**/ +#ifndef _EFI_USB_KB_H_ +#define _EFI_USB_KB_H_ + + +#include + +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#define MAX_KEY_ALLOWED 32 + +#define HZ 1000 * 1000 * 10 +#define USBKBD_REPEAT_DELAY ((HZ) / 2) +#define USBKBD_REPEAT_RATE ((HZ) / 50) + +#define CLASS_HID 3 +#define SUBCLASS_BOOT 1 +#define PROTOCOL_KEYBOARD 1 + +#define BOOT_PROTOCOL 0 +#define REPORT_PROTOCOL 1 + +typedef struct { + UINT8 Down; + UINT8 KeyCode; +} USB_KEY; + +typedef struct { + USB_KEY buffer[MAX_KEY_ALLOWED + 1]; + UINT8 bHead; + UINT8 bTail; +} USB_KB_BUFFER; + +#define USB_KB_DEV_SIGNATURE EFI_SIGNATURE_32 ('u', 'k', 'b', 'd') +#define USB_KB_CONSOLE_IN_EX_NOTIFY_SIGNATURE EFI_SIGNATURE_32 ('u', 'k', 'b', 'x') + +typedef struct _KEYBOARD_CONSOLE_IN_EX_NOTIFY { + UINTN Signature; + EFI_HANDLE NotifyHandle; + EFI_KEY_DATA KeyData; + EFI_KEY_NOTIFY_FUNCTION KeyNotificationFn; + LIST_ENTRY NotifyEntry; +} KEYBOARD_CONSOLE_IN_EX_NOTIFY; + +#define USB_NS_KEY_SIGNATURE EFI_SIGNATURE_32 ('u', 'n', 's', 'k') + +typedef struct { + UINTN Signature; + LIST_ENTRY Link; + + // + // The number of EFI_NS_KEY_MODIFIER children definitions + // + UINTN KeyCount; + + // + // NsKey[0] : Non-spacing key + // NsKey[1] ~ NsKey[KeyCount] : Physical keys + // + EFI_KEY_DESCRIPTOR *NsKey; +} USB_NS_KEY; + +#define USB_NS_KEY_FORM_FROM_LINK(a) CR (a, USB_NS_KEY, Link, USB_NS_KEY_SIGNATURE) + +typedef struct { + UINTN Signature; + EFI_DEVICE_PATH_PROTOCOL *DevicePath; + EFI_EVENT DelayedRecoveryEvent; + EFI_SIMPLE_TEXT_INPUT_PROTOCOL SimpleInput; + EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL SimpleInputEx; + EFI_USB_IO_PROTOCOL *UsbIo; + + EFI_USB_INTERFACE_DESCRIPTOR InterfaceDescriptor; + EFI_USB_ENDPOINT_DESCRIPTOR IntEndpointDescriptor; + + USB_KB_BUFFER KeyboardBuffer; + UINT8 CtrlOn; + UINT8 AltOn; + UINT8 ShiftOn; + UINT8 NumLockOn; + UINT8 CapsOn; + UINT8 ScrollOn; + UINT8 LastKeyCodeArray[8]; + UINT8 CurKeyChar; + + UINT8 RepeatKey; + EFI_EVENT RepeatTimer; + + EFI_UNICODE_STRING_TABLE *ControllerNameTable; + + UINT8 LeftCtrlOn; + UINT8 LeftAltOn; + UINT8 LeftShiftOn; + UINT8 LeftLogoOn; + UINT8 RightCtrlOn; + UINT8 RightAltOn; + UINT8 RightShiftOn; + UINT8 RightLogoOn; + UINT8 MenuKeyOn; + UINT8 SysReqOn; + UINT8 AltGrOn; + + EFI_KEY_STATE KeyState; + // + // Notification function list + // + LIST_ENTRY NotifyList; + + // + // Non-spacing key list + // + LIST_ENTRY NsKeyList; + USB_NS_KEY *CurrentNsKey; + EFI_KEY_DESCRIPTOR *KeyConvertionTable; + EFI_EVENT KeyboardLayoutEvent; +} USB_KB_DEV; + +// +// Global Variables +// +extern EFI_DRIVER_BINDING_PROTOCOL gUsbKeyboardDriverBinding; +extern EFI_COMPONENT_NAME_PROTOCOL gUsbKeyboardComponentName; +extern EFI_COMPONENT_NAME2_PROTOCOL gUsbKeyboardComponentName2; +extern EFI_GUID gEfiUsbKeyboardDriverGuid; +extern EFI_GUID gSimpleTextInExNotifyGuid; + +/** + Report Status Code in Usb Keyboard Driver. + + @param DevicePath Use this to get Device Path. + @param CodeType Status Code Type. + @param CodeValue Status Code Value. + + @return None. + +**/ +VOID +EFIAPI +KbdReportStatusCode ( + IN EFI_DEVICE_PATH_PROTOCOL *DevicePath, + IN EFI_STATUS_CODE_TYPE CodeType, + IN EFI_STATUS_CODE_VALUE Value + ); + +#define USB_KB_DEV_FROM_THIS(a) \ + CR(a, USB_KB_DEV, SimpleInput, USB_KB_DEV_SIGNATURE) +#define TEXT_INPUT_EX_USB_KB_DEV_FROM_THIS(a) \ + CR(a, USB_KB_DEV, SimpleInputEx, USB_KB_DEV_SIGNATURE) + + +#define MOD_CONTROL_L 0x01 +#define MOD_CONTROL_R 0x10 +#define MOD_SHIFT_L 0x02 +#define MOD_SHIFT_R 0x20 +#define MOD_ALT_L 0x04 +#define MOD_ALT_R 0x40 +#define MOD_WIN_L 0x08 +#define MOD_WIN_R 0x80 + +typedef struct { + UINT8 Mask; + UINT8 Key; +} KB_MODIFIER; + +#define USB_KEYCODE_MAX_MAKE 0x62 + +#define USBKBD_VALID_KEYCODE(key) ((UINT8) (key) > 3) + +typedef struct { + UINT8 NumLock : 1; + UINT8 CapsLock : 1; + UINT8 ScrollLock : 1; + UINT8 Resrvd : 5; +} LED_MAP; + +// +// Simple Text Input Ex protocol functions +// +/** + The extension routine to reset the input device. + + @param This Protocol instance pointer. + @param ExtendedVerification Driver may perform diagnostics on reset. + + @retval EFI_SUCCESS The device was reset. + @retval EFI_DEVICE_ERROR The device is not functioning properly and could + not be reset. + +**/ +EFI_STATUS +EFIAPI +USBKeyboardResetEx ( + IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *This, + IN BOOLEAN ExtendedVerification + ) +; + +/** + Reads the next keystroke from the input device. The WaitForKey Event can + be used to test for existance of a keystroke via WaitForEvent () call. + + @param This Protocol instance pointer. + @param KeyData A pointer to a buffer that is filled in with the keystroke + state data for the key that was pressed. + + @return EFI_SUCCESS The keystroke information was returned successfully. + @retval EFI_INVALID_PARAMETER KeyData is NULL. + @retval Other Read key stroke information failed. + +**/ +EFI_STATUS +EFIAPI +USBKeyboardReadKeyStrokeEx ( + IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *This, + OUT EFI_KEY_DATA *KeyData + ) +; + +/** + Set certain state for the input device. + + @param This Protocol instance pointer. + @param KeyToggleState A pointer to the EFI_KEY_TOGGLE_STATE to set the + state for the input device. + + @retval EFI_SUCCESS The device state was set successfully. + @retval EFI_UNSUPPORTED The device does not have the ability to set its state. + @retval EFI_INVALID_PARAMETER KeyToggleState is NULL. + +**/ +EFI_STATUS +EFIAPI +USBKeyboardSetState ( + IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *This, + IN EFI_KEY_TOGGLE_STATE *KeyToggleState + ) +; + +/** + Register a notification function for a particular keystroke for the input device. + + @param This Protocol instance pointer. + @param KeyData A pointer to a buffer that is filled in with the keystroke + information data for the key that was pressed. + @param KeyNotificationFunction Points to the function to be called when the key + sequence is typed specified by KeyData. + @param NotifyHandle Points to the unique handle assigned to the registered notification. + + @retval EFI_SUCCESS The notification function was registered successfully. + @retval EFI_OUT_OF_RESOURCES Unable to allocate resources for necesssary data structures. + @retval EFI_INVALID_PARAMETER KeyData or NotifyHandle is NULL. + +**/ +EFI_STATUS +EFIAPI +USBKeyboardRegisterKeyNotify ( + IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *This, + IN EFI_KEY_DATA *KeyData, + IN EFI_KEY_NOTIFY_FUNCTION KeyNotificationFunction, + OUT EFI_HANDLE *NotifyHandle + ) +; + +/** + Remove a registered notification function from a particular keystroke. + + @param This Protocol instance pointer. + @param NotificationHandle The handle of the notification function being unregistered. + + @retval EFI_SUCCESS The notification function was unregistered successfully. + @retval EFI_INVALID_PARAMETER The NotificationHandle is invalid or opening gSimpleTextInExNotifyGuid + on NotificationHandle fails. + @retval EFI_NOT_FOUND Can not find the matching entry in database. + +**/ +EFI_STATUS +EFIAPI +USBKeyboardUnregisterKeyNotify ( + IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *This, + IN EFI_HANDLE NotificationHandle + ) +; + +#endif + diff --git a/MdeModulePkg/Bus/Usb/UsbKbDxe/KeyBoard.c b/MdeModulePkg/Bus/Usb/UsbKbDxe/KeyBoard.c new file mode 100644 index 000000000..489ec5c61 --- /dev/null +++ b/MdeModulePkg/Bus/Usb/UsbKbDxe/KeyBoard.c @@ -0,0 +1,1924 @@ +/** @file + + Helper functions for USB Keyboard Driver. + +Copyright (c) 2004 - 2008, Intel Corporation +All rights reserved. This program and the accompanying materials +are licensed and made available under the terms and conditions of the BSD License +which accompanies this distribution. The full text of the license may be found at +http://opensource.org/licenses/bsd-license.php + +THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, +WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. + +**/ + +#include "KeyBoard.h" +#include + +// +// Static English keyboard layout +// Format:, , , , +// +STATIC +UINT8 KeyboardLayoutTable[USB_KEYCODE_MAX_MAKE + 8][5] = { + {EfiKeyC1, 'a', 'A', EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT | EFI_AFFECTED_BY_CAPS_LOCK}, // 0x04 + {EfiKeyB5, 'b', 'B', EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT | EFI_AFFECTED_BY_CAPS_LOCK}, // 0x05 + {EfiKeyB3, 'c', 'C', EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT | EFI_AFFECTED_BY_CAPS_LOCK}, // 0x06 + {EfiKeyC3, 'd', 'D', EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT | EFI_AFFECTED_BY_CAPS_LOCK}, // 0x07 + {EfiKeyD3, 'e', 'E', EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT | EFI_AFFECTED_BY_CAPS_LOCK}, // 0x08 + {EfiKeyC4, 'f', 'F', EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT | EFI_AFFECTED_BY_CAPS_LOCK}, // 0x09 + {EfiKeyC5, 'g', 'G', EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT | EFI_AFFECTED_BY_CAPS_LOCK}, // 0x0A + {EfiKeyC6, 'h', 'H', EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT | EFI_AFFECTED_BY_CAPS_LOCK}, // 0x0B + {EfiKeyD8, 'i', 'I', EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT | EFI_AFFECTED_BY_CAPS_LOCK}, // 0x0C + {EfiKeyC7, 'j', 'J', EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT | EFI_AFFECTED_BY_CAPS_LOCK}, // 0x0D + {EfiKeyC8, 'k', 'K', EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT | EFI_AFFECTED_BY_CAPS_LOCK}, // 0x0E + {EfiKeyC9, 'l', 'L', EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT | EFI_AFFECTED_BY_CAPS_LOCK}, // 0x0F + {EfiKeyB7, 'm', 'M', EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT | EFI_AFFECTED_BY_CAPS_LOCK}, // 0x10 + {EfiKeyB6, 'n', 'N', EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT | EFI_AFFECTED_BY_CAPS_LOCK}, // 0x11 + {EfiKeyD9, 'o', 'O', EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT | EFI_AFFECTED_BY_CAPS_LOCK}, // 0x12 + {EfiKeyD10, 'p', 'P', EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT | EFI_AFFECTED_BY_CAPS_LOCK}, // 0x13 + {EfiKeyD1, 'q', 'Q', EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT | EFI_AFFECTED_BY_CAPS_LOCK}, // 0x14 + {EfiKeyD4, 'r', 'R', EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT | EFI_AFFECTED_BY_CAPS_LOCK}, // 0x15 + {EfiKeyC2, 's', 'S', EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT | EFI_AFFECTED_BY_CAPS_LOCK}, // 0x16 + {EfiKeyD5, 't', 'T', EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT | EFI_AFFECTED_BY_CAPS_LOCK}, // 0x17 + {EfiKeyD7, 'u', 'U', EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT | EFI_AFFECTED_BY_CAPS_LOCK}, // 0x18 + {EfiKeyB4, 'v', 'V', EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT | EFI_AFFECTED_BY_CAPS_LOCK}, // 0x19 + {EfiKeyD2, 'w', 'W', EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT | EFI_AFFECTED_BY_CAPS_LOCK}, // 0x1A + {EfiKeyB2, 'x', 'X', EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT | EFI_AFFECTED_BY_CAPS_LOCK}, // 0x1B + {EfiKeyD6, 'y', 'Y', EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT | EFI_AFFECTED_BY_CAPS_LOCK}, // 0x1C + {EfiKeyB1, 'z', 'Z', EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT | EFI_AFFECTED_BY_CAPS_LOCK}, // 0x1D + {EfiKeyE1, '1', '!', EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT}, // 0x1E + {EfiKeyE2, '2', '@', EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT}, // 0x1F + {EfiKeyE3, '3', '#', EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT}, // 0x20 + {EfiKeyE4, '4', '$', EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT}, // 0x21 + {EfiKeyE5, '5', '%', EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT}, // 0x22 + {EfiKeyE6, '6', '^', EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT}, // 0x23 + {EfiKeyE7, '7', '&', EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT}, // 0x24 + {EfiKeyE8, '8', '*', EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT}, // 0x25 + {EfiKeyE9, '9', '(', EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT}, // 0x26 + {EfiKeyE10, '0', ')', EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT}, // 0x27 + {EfiKeyEnter, 0x0d, 0x0d, EFI_NULL_MODIFIER, 0}, // 0x28 Enter + {EfiKeyEsc, 0x1b, 0x1b, EFI_NULL_MODIFIER, 0}, // 0x29 Esc + {EfiKeyBackSpace, 0x08, 0x08, EFI_NULL_MODIFIER, 0}, // 0x2A Backspace + {EfiKeyTab, 0x09, 0x09, EFI_NULL_MODIFIER, 0}, // 0x2B Tab + {EfiKeySpaceBar, ' ', ' ', EFI_NULL_MODIFIER, 0}, // 0x2C Spacebar + {EfiKeyE11, '-', '_', EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT}, // 0x2D + {EfiKeyE12, '=', '+', EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT}, // 0x2E + {EfiKeyD11, '[', '{', EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT}, // 0x2F + {EfiKeyD12, ']', '}', EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT}, // 0x30 + {EfiKeyD13, '\\', '|', EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT}, // 0x31 + {EfiKeyC12, '\\', '|', EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT}, // 0x32 Keyboard Non-US # and ~ + {EfiKeyC10, ';', ':', EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT}, // 0x33 + {EfiKeyC11, '\'', '"', EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT}, // 0x34 + {EfiKeyE0, '`', '~', EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT}, // 0x35 Keyboard Grave Accent and Tlide + {EfiKeyB8, ',', '<', EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT}, // 0x36 + {EfiKeyB9, '.', '>', EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT}, // 0x37 + {EfiKeyB10, '/', '?', EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT}, // 0x38 + {EfiKeyCapsLock, 0x00, 0x00, EFI_CAPS_LOCK_MODIFIER, 0}, // 0x39 CapsLock + {EfiKeyF1, 0x00, 0x00, EFI_FUNCTION_KEY_ONE_MODIFIER, 0}, // 0x3A + {EfiKeyF2, 0x00, 0x00, EFI_FUNCTION_KEY_TWO_MODIFIER, 0}, // 0x3B + {EfiKeyF3, 0x00, 0x00, EFI_FUNCTION_KEY_THREE_MODIFIER, 0}, // 0x3C + {EfiKeyF4, 0x00, 0x00, EFI_FUNCTION_KEY_FOUR_MODIFIER, 0}, // 0x3D + {EfiKeyF5, 0x00, 0x00, EFI_FUNCTION_KEY_FIVE_MODIFIER, 0}, // 0x3E + {EfiKeyF6, 0x00, 0x00, EFI_FUNCTION_KEY_SIX_MODIFIER, 0}, // 0x3F + {EfiKeyF7, 0x00, 0x00, EFI_FUNCTION_KEY_SEVEN_MODIFIER, 0}, // 0x40 + {EfiKeyF8, 0x00, 0x00, EFI_FUNCTION_KEY_EIGHT_MODIFIER, 0}, // 0x41 + {EfiKeyF9, 0x00, 0x00, EFI_FUNCTION_KEY_NINE_MODIFIER, 0}, // 0x42 + {EfiKeyF10, 0x00, 0x00, EFI_FUNCTION_KEY_TEN_MODIFIER, 0}, // 0x43 + {EfiKeyF11, 0x00, 0x00, EFI_FUNCTION_KEY_ELEVEN_MODIFIER, 0}, // 0x44 F11 + {EfiKeyF12, 0x00, 0x00, EFI_FUNCTION_KEY_TWELVE_MODIFIER, 0}, // 0x45 F12 + {EfiKeyPrint, 0x00, 0x00, EFI_PRINT_MODIFIER, 0}, // 0x46 PrintScreen + {EfiKeySLck, 0x00, 0x00, EFI_SCROLL_LOCK_MODIFIER, 0}, // 0x47 Scroll Lock + {EfiKeyPause, 0x00, 0x00, EFI_PAUSE_MODIFIER, 0}, // 0x48 Pause + {EfiKeyIns, 0x00, 0x00, EFI_INSERT_MODIFIER, 0}, // 0x49 + {EfiKeyHome, 0x00, 0x00, EFI_HOME_MODIFIER, 0}, // 0x4A + {EfiKeyPgUp, 0x00, 0x00, EFI_PAGE_UP_MODIFIER, 0}, // 0x4B + {EfiKeyDel, 0x00, 0x00, EFI_DELETE_MODIFIER, 0}, // 0x4C + {EfiKeyEnd, 0x00, 0x00, EFI_END_MODIFIER, 0}, // 0x4D + {EfiKeyPgDn, 0x00, 0x00, EFI_PAGE_DOWN_MODIFIER, 0}, // 0x4E + {EfiKeyRightArrow, 0x00, 0x00, EFI_RIGHT_ARROW_MODIFIER, 0}, // 0x4F + {EfiKeyLeftArrow, 0x00, 0x00, EFI_LEFT_ARROW_MODIFIER, 0}, // 0x50 + {EfiKeyDownArrow, 0x00, 0x00, EFI_DOWN_ARROW_MODIFIER, 0}, // 0x51 + {EfiKeyUpArrow, 0x00, 0x00, EFI_UP_ARROW_MODIFIER, 0}, // 0x52 + {EfiKeyNLck, 0x00, 0x00, EFI_NUM_LOCK_MODIFIER, 0}, // 0x53 NumLock + {EfiKeySlash, '/', '/', EFI_NULL_MODIFIER, 0}, // 0x54 + {EfiKeyAsterisk, '*', '*', EFI_NULL_MODIFIER, 0}, // 0x55 + {EfiKeyMinus, '-', '-', EFI_NULL_MODIFIER, 0}, // 0x56 + {EfiKeyPlus, '+', '+', EFI_NULL_MODIFIER, 0}, // 0x57 + {EfiKeyEnter, 0x0d, 0x0d, EFI_NULL_MODIFIER, 0}, // 0x58 + {EfiKeyOne, '1', '1', EFI_END_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT | EFI_AFFECTED_BY_NUM_LOCK}, // 0x59 + {EfiKeyTwo, '2', '2', EFI_DOWN_ARROW_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT | EFI_AFFECTED_BY_NUM_LOCK}, // 0x5A + {EfiKeyThree, '3', '3', EFI_PAGE_DOWN_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT | EFI_AFFECTED_BY_NUM_LOCK}, // 0x5B + {EfiKeyFour, '4', '4', EFI_LEFT_ARROW_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT | EFI_AFFECTED_BY_NUM_LOCK}, // 0x5C + {EfiKeyFive, '5', '5', EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT | EFI_AFFECTED_BY_NUM_LOCK}, // 0x5D + {EfiKeySix, '6', '6', EFI_RIGHT_ARROW_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT | EFI_AFFECTED_BY_NUM_LOCK}, // 0x5E + {EfiKeySeven, '7', '7', EFI_HOME_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT | EFI_AFFECTED_BY_NUM_LOCK}, // 0x5F + {EfiKeyEight, '8', '8', EFI_UP_ARROW_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT | EFI_AFFECTED_BY_NUM_LOCK}, // 0x60 + {EfiKeyNine, '9', '9', EFI_PAGE_UP_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT | EFI_AFFECTED_BY_NUM_LOCK}, // 0x61 + {EfiKeyZero, '0', '0', EFI_INSERT_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT | EFI_AFFECTED_BY_NUM_LOCK}, // 0x62 + {EfiKeyPeriod, '.', '.', EFI_DELETE_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT | EFI_AFFECTED_BY_NUM_LOCK}, // 0x63 + {EfiKeyB0, '\\', '|', EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT}, // 0x64 Keyboard Non-US \ and | + {EfiKeyA4, 0x00, 0x00, EFI_MENU_MODIFIER, 0}, // 0x65 Keyboard Application + + {EfiKeyLCtrl, 0, 0, EFI_LEFT_CONTROL_MODIFIER, 0}, // 0xe0 + {EfiKeyLShift, 0, 0, EFI_LEFT_SHIFT_MODIFIER, 0}, // 0xe1 + {EfiKeyLAlt, 0, 0, EFI_LEFT_ALT_MODIFIER, 0}, // 0xe2 + {EfiKeyA0, 0, 0, EFI_LEFT_LOGO_MODIFIER, 0}, // 0xe3 + {EfiKeyRCtrl, 0, 0, EFI_RIGHT_CONTROL_MODIFIER, 0}, // 0xe4 + {EfiKeyRShift, 0, 0, EFI_RIGHT_SHIFT_MODIFIER, 0}, // 0xe5 + {EfiKeyA2, 0, 0, EFI_RIGHT_ALT_MODIFIER, 0}, // 0xe6 + {EfiKeyA3, 0, 0, EFI_RIGHT_LOGO_MODIFIER, 0}, // 0xe7 +}; + +/** + Initialize KeyConvertionTable by using default keyboard layout. + + @param UsbKeyboardDevice The USB_KB_DEV instance. + @retval None. + +**/ +VOID +EFIAPI +LoadDefaultKeyboardLayout ( + IN USB_KB_DEV *UsbKeyboardDevice + ) +{ + UINTN Index; + EFI_KEY_DESCRIPTOR *KeyDescriptor; + + // + // Construct KeyConvertionTable by default keyboard layout + // + KeyDescriptor = &UsbKeyboardDevice->KeyConvertionTable[0]; + + for (Index = 0; Index < (USB_KEYCODE_MAX_MAKE + 8); Index++) { + KeyDescriptor->Key = (EFI_KEY) KeyboardLayoutTable[Index][0]; + KeyDescriptor->Unicode = KeyboardLayoutTable[Index][1]; + KeyDescriptor->ShiftedUnicode = KeyboardLayoutTable[Index][2]; + KeyDescriptor->AltGrUnicode = 0; + KeyDescriptor->ShiftedAltGrUnicode = 0; + KeyDescriptor->Modifier = KeyboardLayoutTable[Index][3]; + KeyDescriptor->AffectedAttribute = KeyboardLayoutTable[Index][4]; + + KeyDescriptor++; + } +} + +// +// EFI_KEY to USB Scan Code convertion table +// +STATIC +UINT8 UsbScanCodeConvertionTable[] = { + 0xe0, // EfiKeyLCtrl + 0xe3, // EfiKeyA0 + 0xe2, // EfiKeyLAlt + 0x2c, // EfiKeySpaceBar + 0xe6, // EfiKeyA2 + 0xe7, // EfiKeyA3 + 0x65, // EfiKeyA4 + 0xe4, // EfiKeyRCtrl + 0x50, // EfiKeyLeftArrow + 0x51, // EfiKeyDownArrow + 0x4F, // EfiKeyRightArrow + 0x62, // EfiKeyZero + 0x63, // EfiKeyPeriod + 0x28, // EfiKeyEnter + 0xe1, // EfiKeyLShift + 0x64, // EfiKeyB0 + 0x1D, // EfiKeyB1 + 0x1B, // EfiKeyB2 + 0x06, // EfiKeyB3 + 0x19, // EfiKeyB4 + 0x05, // EfiKeyB5 + 0x11, // EfiKeyB6 + 0x10, // EfiKeyB7 + 0x36, // EfiKeyB8 + 0x37, // EfiKeyB9 + 0x38, // EfiKeyB10 + 0xe5, // EfiKeyRShift + 0x52, // EfiKeyUpArrow + 0x59, // EfiKeyOne + 0x5A, // EfiKeyTwo + 0x5B, // EfiKeyThree + 0x39, // EfiKeyCapsLock + 0x04, // EfiKeyC1 + 0x16, // EfiKeyC2 + 0x07, // EfiKeyC3 + 0x09, // EfiKeyC4 + 0x0A, // EfiKeyC5 + 0x0B, // EfiKeyC6 + 0x0D, // EfiKeyC7 + 0x0E, // EfiKeyC8 + 0x0F, // EfiKeyC9 + 0x33, // EfiKeyC10 + 0x34, // EfiKeyC11 + 0x32, // EfiKeyC12 + 0x5C, // EfiKeyFour + 0x5D, // EfiKeyFive + 0x5E, // EfiKeySix + 0x57, // EfiKeyPlus + 0x2B, // EfiKeyTab + 0x14, // EfiKeyD1 + 0x1A, // EfiKeyD2 + 0x08, // EfiKeyD3 + 0x15, // EfiKeyD4 + 0x17, // EfiKeyD5 + 0x1C, // EfiKeyD6 + 0x18, // EfiKeyD7 + 0x0C, // EfiKeyD8 + 0x12, // EfiKeyD9 + 0x13, // EfiKeyD10 + 0x2F, // EfiKeyD11 + 0x30, // EfiKeyD12 + 0x31, // EfiKeyD13 + 0x4C, // EfiKeyDel + 0x4D, // EfiKeyEnd + 0x4E, // EfiKeyPgDn + 0x5F, // EfiKeySeven + 0x60, // EfiKeyEight + 0x61, // EfiKeyNine + 0x35, // EfiKeyE0 + 0x1E, // EfiKeyE1 + 0x1F, // EfiKeyE2 + 0x20, // EfiKeyE3 + 0x21, // EfiKeyE4 + 0x22, // EfiKeyE5 + 0x23, // EfiKeyE6 + 0x24, // EfiKeyE7 + 0x25, // EfiKeyE8 + 0x26, // EfiKeyE9 + 0x27, // EfiKeyE10 + 0x2D, // EfiKeyE11 + 0x2E, // EfiKeyE12 + 0x2A, // EfiKeyBackSpace + 0x49, // EfiKeyIns + 0x4A, // EfiKeyHome + 0x4B, // EfiKeyPgUp + 0x53, // EfiKeyNLck + 0x54, // EfiKeySlash + 0x55, // EfiKeyAsterisk + 0x56, // EfiKeyMinus + 0x29, // EfiKeyEsc + 0x3A, // EfiKeyF1 + 0x3B, // EfiKeyF2 + 0x3C, // EfiKeyF3 + 0x3D, // EfiKeyF4 + 0x3E, // EfiKeyF5 + 0x3F, // EfiKeyF6 + 0x40, // EfiKeyF7 + 0x41, // EfiKeyF8 + 0x42, // EfiKeyF9 + 0x43, // EfiKeyF10 + 0x44, // EfiKeyF11 + 0x45, // EfiKeyF12 + 0x46, // EfiKeyPrint + 0x47, // EfiKeySLck + 0x48 // EfiKeyPause +}; + +// +// Keyboard Layout Modifier to EFI Scan Code convertion table +// +STATIC +UINT8 EfiScanCodeConvertionTable[] = { + SCAN_NULL, // EFI_NULL_MODIFIER + SCAN_NULL, // EFI_LEFT_CONTROL_MODIFIER + SCAN_NULL, // EFI_RIGHT_CONTROL_MODIFIER + SCAN_NULL, // EFI_LEFT_ALT_MODIFIER + SCAN_NULL, // EFI_RIGHT_ALT_MODIFIER + SCAN_NULL, // EFI_ALT_GR_MODIFIER + SCAN_INSERT, // EFI_INSERT_MODIFIER + SCAN_DELETE, // EFI_DELETE_MODIFIER + SCAN_PAGE_DOWN, // EFI_PAGE_DOWN_MODIFIER + SCAN_PAGE_UP, // EFI_PAGE_UP_MODIFIER + SCAN_HOME, // EFI_HOME_MODIFIER + SCAN_END, // EFI_END_MODIFIER + SCAN_NULL, // EFI_LEFT_SHIFT_MODIFIER + SCAN_NULL, // EFI_RIGHT_SHIFT_MODIFIER + SCAN_NULL, // EFI_CAPS_LOCK_MODIFIER + SCAN_NULL, // EFI_NUM_LOCK_MODIFIER + SCAN_LEFT, // EFI_LEFT_ARROW_MODIFIER + SCAN_RIGHT, // EFI_RIGHT_ARROW_MODIFIER + SCAN_DOWN, // EFI_DOWN_ARROW_MODIFIER + SCAN_UP, // EFI_UP_ARROW_MODIFIER + SCAN_NULL, // EFI_NS_KEY_MODIFIER + SCAN_NULL, // EFI_NS_KEY_DEPENDENCY_MODIFIER + SCAN_F1, // EFI_FUNCTION_KEY_ONE_MODIFIER + SCAN_F2, // EFI_FUNCTION_KEY_TWO_MODIFIER + SCAN_F3, // EFI_FUNCTION_KEY_THREE_MODIFIER + SCAN_F4, // EFI_FUNCTION_KEY_FOUR_MODIFIER + SCAN_F5, // EFI_FUNCTION_KEY_FIVE_MODIFIER + SCAN_F6, // EFI_FUNCTION_KEY_SIX_MODIFIER + SCAN_F7, // EFI_FUNCTION_KEY_SEVEN_MODIFIER + SCAN_F8, // EFI_FUNCTION_KEY_EIGHT_MODIFIER + SCAN_F9, // EFI_FUNCTION_KEY_NINE_MODIFIER + SCAN_F10, // EFI_FUNCTION_KEY_TEN_MODIFIER + SCAN_F11, // EFI_FUNCTION_KEY_ELEVEN_MODIFIER + SCAN_F12, // EFI_FUNCTION_KEY_TWELVE_MODIFIER +}; + +EFI_GUID mKeyboardLayoutEventGuid = EFI_HII_SET_KEYBOARD_LAYOUT_EVENT_GUID; + + +STATIC KB_MODIFIER KB_Mod[8] = { + { MOD_CONTROL_L, 0xe0 }, // 11100000 + { MOD_CONTROL_R, 0xe4 }, // 11100100 + { MOD_SHIFT_L, 0xe1 }, // 11100001 + { MOD_SHIFT_R, 0xe5 }, // 11100101 + { MOD_ALT_L, 0xe2 }, // 11100010 + { MOD_ALT_R, 0xe6 }, // 11100110 + { MOD_WIN_L, 0xe3 }, // 11100011 + { MOD_WIN_R, 0xe7 }, // 11100111 +}; + + + +/** + Uses USB I/O to check whether the device is a USB Keyboard device. + + @param UsbIo Points to a USB I/O protocol instance. + @retval None + +**/ +BOOLEAN +EFIAPI +IsUSBKeyboard ( + IN EFI_USB_IO_PROTOCOL *UsbIo + ) +{ + EFI_STATUS Status; + EFI_USB_INTERFACE_DESCRIPTOR InterfaceDescriptor; + + // + // Get the Default interface descriptor, currently we + // assume it is interface 1 + // + Status = UsbIo->UsbGetInterfaceDescriptor ( + UsbIo, + &InterfaceDescriptor + ); + + if (EFI_ERROR (Status)) { + return FALSE; + } + + if (InterfaceDescriptor.InterfaceClass == CLASS_HID && + InterfaceDescriptor.InterfaceSubClass == SUBCLASS_BOOT && + InterfaceDescriptor.InterfaceProtocol == PROTOCOL_KEYBOARD + ) { + + return TRUE; + } + + return FALSE; +} + +/** + Get current keyboard layout from HII database. + + @retval Pointer to EFI_HII_KEYBOARD_LAYOUT. + +**/ +EFI_HII_KEYBOARD_LAYOUT * +EFIAPI +GetCurrentKeyboardLayout ( + VOID + ) +{ + EFI_STATUS Status; + EFI_HII_DATABASE_PROTOCOL *HiiDatabase; + EFI_HII_KEYBOARD_LAYOUT *KeyboardLayout; + UINT16 Length; + + // + // Locate Hii database protocol + // + Status = gBS->LocateProtocol ( + &gEfiHiiDatabaseProtocolGuid, + NULL, + (VOID **) &HiiDatabase + ); + if (EFI_ERROR (Status)) { + return NULL; + } + + // + // Get current keyboard layout from HII database + // + Length = 0; + KeyboardLayout = NULL; + Status = HiiDatabase->GetKeyboardLayout ( + HiiDatabase, + NULL, + &Length, + KeyboardLayout + ); + if (Status == EFI_BUFFER_TOO_SMALL) { + KeyboardLayout = AllocatePool (Length); + ASSERT (KeyboardLayout != NULL); + + Status = HiiDatabase->GetKeyboardLayout ( + HiiDatabase, + NULL, + &Length, + KeyboardLayout + ); + if (EFI_ERROR (Status)) { + gBS->FreePool (KeyboardLayout); + KeyboardLayout = NULL; + } + } + + return KeyboardLayout; +} + +/** + Find Key Descriptor in KeyConvertionTable given its scan code. + + @param UsbKeyboardDevice The USB_KB_DEV instance. + @param ScanCode USB scan code. + + @return The Key descriptor in KeyConvertionTable. + +**/ +EFI_KEY_DESCRIPTOR * +EFIAPI +GetKeyDescriptor ( + IN USB_KB_DEV *UsbKeyboardDevice, + IN UINT8 ScanCode + ) +{ + UINT8 Index; + + if (((ScanCode > 0x65) && (ScanCode < 0xe0)) || (ScanCode > 0xe7)) { + return NULL; + } + + if (ScanCode <= 0x65) { + Index = (UINT8) (ScanCode - 4); + } else { + Index = (UINT8) (ScanCode - 0xe0 + USB_KEYCODE_MAX_MAKE); + } + + return &UsbKeyboardDevice->KeyConvertionTable[Index]; +} + +/** + Find Non-Spacing key for given KeyDescriptor. + + @param UsbKeyboardDevice The USB_KB_DEV instance. + @param KeyDescriptor Key descriptor. + + @retval NULL Key list is empty. + @return Other The Non-Spacing key. + +**/ +USB_NS_KEY * +EFIAPI +FindUsbNsKey ( + IN USB_KB_DEV *UsbKeyboardDevice, + IN EFI_KEY_DESCRIPTOR *KeyDescriptor + ) +{ + LIST_ENTRY *Link; + USB_NS_KEY *UsbNsKey; + + Link = GetFirstNode (&UsbKeyboardDevice->NsKeyList); + while (!IsNull (&UsbKeyboardDevice->NsKeyList, Link)) { + UsbNsKey = USB_NS_KEY_FORM_FROM_LINK (Link); + + if (UsbNsKey->NsKey[0].Key == KeyDescriptor->Key) { + return UsbNsKey; + } + + Link = GetNextNode (&UsbKeyboardDevice->NsKeyList, Link); + } + + return NULL; +} + +/** + Find physical key definition for a given Key stroke. + + @param UsbNsKey The Non-Spacing key information. + @param KeyDescriptor The key stroke. + + @return The physical key definition. + +**/ +EFI_KEY_DESCRIPTOR * +EFIAPI +FindPhysicalKey ( + IN USB_NS_KEY *UsbNsKey, + IN EFI_KEY_DESCRIPTOR *KeyDescriptor + ) +{ + UINTN Index; + EFI_KEY_DESCRIPTOR *PhysicalKey; + + PhysicalKey = &UsbNsKey->NsKey[1]; + for (Index = 0; Index < UsbNsKey->KeyCount; Index++) { + if (KeyDescriptor->Key == PhysicalKey->Key) { + return PhysicalKey; + } + + PhysicalKey++; + } + + // + // No children definition matched, return original key + // + return KeyDescriptor; +} + +/** + The notification function for SET_KEYBOARD_LAYOUT_EVENT. + + @param Event The instance of EFI_EVENT. + @param Context passing parameter. + +**/ +VOID +EFIAPI +SetKeyboardLayoutEvent ( + EFI_EVENT Event, + VOID *Context + ) +{ + USB_KB_DEV *UsbKeyboardDevice; + EFI_HII_KEYBOARD_LAYOUT *KeyboardLayout; + EFI_KEY_DESCRIPTOR TempKey; + EFI_KEY_DESCRIPTOR *KeyDescriptor; + EFI_KEY_DESCRIPTOR *TableEntry; + EFI_KEY_DESCRIPTOR *NsKey; + USB_NS_KEY *UsbNsKey; + UINTN Index; + UINTN Index2; + UINTN KeyCount; + UINT8 ScanCode; + + UsbKeyboardDevice = (USB_KB_DEV *) Context; + + // + // Try to get current Keyboard Layout from HII database + // + KeyboardLayout = GetCurrentKeyboardLayout (); + if (KeyboardLayout == NULL) { + return; + } + + // + // Allocate resource for KeyConvertionTable + // + ReleaseKeyboardLayoutResources (UsbKeyboardDevice); + UsbKeyboardDevice->KeyConvertionTable = AllocateZeroPool ((USB_KEYCODE_MAX_MAKE + 8) * sizeof (EFI_KEY_DESCRIPTOR)); + ASSERT (UsbKeyboardDevice->KeyConvertionTable != NULL); + + KeyDescriptor = (EFI_KEY_DESCRIPTOR *) (((UINT8 *) KeyboardLayout) + sizeof (EFI_HII_KEYBOARD_LAYOUT)); + for (Index = 0; Index < KeyboardLayout->DescriptorCount; Index++) { + // + // Copy from HII keyboard layout package binary for alignment + // + CopyMem (&TempKey, KeyDescriptor, sizeof (EFI_KEY_DESCRIPTOR)); + + // + // Fill the key into KeyConvertionTable (which use USB Scan Code as index) + // + ScanCode = UsbScanCodeConvertionTable [(UINT8) (TempKey.Key)]; + TableEntry = GetKeyDescriptor (UsbKeyboardDevice, ScanCode); + CopyMem (TableEntry, KeyDescriptor, sizeof (EFI_KEY_DESCRIPTOR)); + + if (TempKey.Modifier == EFI_NS_KEY_MODIFIER) { + // + // Non-spacing key + // + UsbNsKey = AllocatePool (sizeof (USB_NS_KEY)); + ASSERT (UsbNsKey != NULL); + + // + // Search for sequential children physical key definitions + // + KeyCount = 0; + NsKey = KeyDescriptor + 1; + for (Index2 = Index + 1; Index2 < KeyboardLayout->DescriptorCount; Index2++) { + CopyMem (&TempKey, NsKey, sizeof (EFI_KEY_DESCRIPTOR)); + if (TempKey.Modifier & EFI_NS_KEY_DEPENDENCY_MODIFIER) { + KeyCount++; + } else { + break; + } + NsKey++; + } + + UsbNsKey->Signature = USB_NS_KEY_SIGNATURE; + UsbNsKey->KeyCount = KeyCount; + UsbNsKey->NsKey = AllocateCopyPool ( + (KeyCount + 1) * sizeof (EFI_KEY_DESCRIPTOR), + KeyDescriptor + ); + InsertTailList (&UsbKeyboardDevice->NsKeyList, &UsbNsKey->Link); + + // + // Skip over the child physical keys + // + Index += KeyCount; + KeyDescriptor += KeyCount; + } + + KeyDescriptor++; + } + + // + // There are two EfiKeyEnter, duplicate its Key Descriptor + // + TableEntry = GetKeyDescriptor (UsbKeyboardDevice, 0x58); + KeyDescriptor = GetKeyDescriptor (UsbKeyboardDevice, 0x28); + CopyMem (TableEntry, KeyDescriptor, sizeof (EFI_KEY_DESCRIPTOR)); + + gBS->FreePool (KeyboardLayout); +} + +/** + Destroy resources for Keyboard layout. + + @param UsbKeyboardDevice The USB_KB_DEV instance. + +**/ +VOID +EFIAPI +ReleaseKeyboardLayoutResources ( + IN USB_KB_DEV *UsbKeyboardDevice + ) +{ + USB_NS_KEY *UsbNsKey; + LIST_ENTRY *Link; + + SafeFreePool (UsbKeyboardDevice->KeyConvertionTable); + UsbKeyboardDevice->KeyConvertionTable = NULL; + + while (!IsListEmpty (&UsbKeyboardDevice->NsKeyList)) { + Link = GetFirstNode (&UsbKeyboardDevice->NsKeyList); + UsbNsKey = USB_NS_KEY_FORM_FROM_LINK (Link); + RemoveEntryList (&UsbNsKey->Link); + + gBS->FreePool (UsbNsKey->NsKey); + gBS->FreePool (UsbNsKey); + } +} + +/** + Initialize USB Keyboard layout. + + @param UsbKeyboardDevice The USB_KB_DEV instance. + + @retval EFI_SUCCESS Initialization Success. + @retval Other Keyboard layout initial failed. + +**/ +EFI_STATUS +EFIAPI +InitKeyboardLayout ( + IN USB_KB_DEV *UsbKeyboardDevice + ) +{ + EFI_HII_KEYBOARD_LAYOUT *KeyboardLayout; + EFI_STATUS Status; + + UsbKeyboardDevice->KeyConvertionTable = AllocateZeroPool ((USB_KEYCODE_MAX_MAKE + 8) * sizeof (EFI_KEY_DESCRIPTOR)); + ASSERT (UsbKeyboardDevice->KeyConvertionTable != NULL); + + InitializeListHead (&UsbKeyboardDevice->NsKeyList); + UsbKeyboardDevice->CurrentNsKey = NULL; + UsbKeyboardDevice->KeyboardLayoutEvent = NULL; + + // + // Register SET_KEYBOARD_LAYOUT_EVENT notification + // + Status = gBS->CreateEventEx ( + EFI_EVENT_NOTIFY_SIGNAL, + TPL_NOTIFY, + SetKeyboardLayoutEvent, + UsbKeyboardDevice, + &mKeyboardLayoutEventGuid, + &UsbKeyboardDevice->KeyboardLayoutEvent + ); + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Try to get current keyboard layout from HII database + // + KeyboardLayout = GetCurrentKeyboardLayout (); + if (KeyboardLayout != NULL) { + // + // Force to initialize the keyboard layout + // + gBS->SignalEvent (UsbKeyboardDevice->KeyboardLayoutEvent); + } else { + if (FeaturePcdGet (PcdDisableDefaultKeyboardLayoutInUsbKbDriver)) { + return EFI_NOT_READY; + } else { + + // + // Fail to get keyboard layout from HII database, + // use default keyboard layout + // + LoadDefaultKeyboardLayout (UsbKeyboardDevice); + } + } + + return EFI_SUCCESS; +} + + +/** + Initialize USB Keyboard device and all private data structures. + + @param UsbKeyboardDevice The USB_KB_DEV instance. + + @retval EFI_SUCCESS Initialization is successful. + @retval EFI_DEVICE_ERROR Configure hardware failed. + +**/ +EFI_STATUS +EFIAPI +InitUSBKeyboard ( + IN USB_KB_DEV *UsbKeyboardDevice + ) +{ + UINT8 ConfigValue; + UINT8 Protocol; + UINT8 ReportId; + UINT8 Duration; + EFI_STATUS Status; + UINT32 TransferResult; + + KbdReportStatusCode ( + UsbKeyboardDevice->DevicePath, + EFI_PROGRESS_CODE, + PcdGet32 (PcdStatusCodeValueKeyboardSelfTest) + ); + + InitUSBKeyBuffer (&(UsbKeyboardDevice->KeyboardBuffer)); + + // + // default configurations + // + ConfigValue = 0x01; + + // + // Uses default configuration to configure the USB Keyboard device. + // + Status = UsbSetConfiguration ( + UsbKeyboardDevice->UsbIo, + (UINT16) ConfigValue, + &TransferResult + ); + if (EFI_ERROR (Status)) { + // + // If configuration could not be set here, it means + // the keyboard interface has some errors and could + // not be initialized + // + KbdReportStatusCode ( + UsbKeyboardDevice->DevicePath, + EFI_ERROR_CODE | EFI_ERROR_MINOR, + PcdGet32 (PcdStatusCodeValueKeyboardInterfaceError) + ); + + return EFI_DEVICE_ERROR; + } + + UsbGetProtocolRequest ( + UsbKeyboardDevice->UsbIo, + UsbKeyboardDevice->InterfaceDescriptor.InterfaceNumber, + &Protocol + ); + // + // Sets boot protocol for the USB Keyboard. + // This driver only supports boot protocol. + // !!BugBug: How about the device that does not support boot protocol? + // + if (Protocol != BOOT_PROTOCOL) { + UsbSetProtocolRequest ( + UsbKeyboardDevice->UsbIo, + UsbKeyboardDevice->InterfaceDescriptor.InterfaceNumber, + BOOT_PROTOCOL + ); + } + // + // the duration is indefinite, so the endpoint will inhibit reporting forever, + // and only reporting when a change is detected in the report data. + // + + // + // idle value for all report ID + // + ReportId = 0; + // + // idle forever until there is a key pressed and released. + // + Duration = 0; + UsbSetIdleRequest ( + UsbKeyboardDevice->UsbIo, + UsbKeyboardDevice->InterfaceDescriptor.InterfaceNumber, + ReportId, + Duration + ); + + UsbKeyboardDevice->CtrlOn = 0; + UsbKeyboardDevice->AltOn = 0; + UsbKeyboardDevice->ShiftOn = 0; + UsbKeyboardDevice->NumLockOn = 0; + UsbKeyboardDevice->CapsOn = 0; + UsbKeyboardDevice->ScrollOn = 0; + + UsbKeyboardDevice->LeftCtrlOn = 0; + UsbKeyboardDevice->LeftAltOn = 0; + UsbKeyboardDevice->LeftShiftOn = 0; + UsbKeyboardDevice->LeftLogoOn = 0; + UsbKeyboardDevice->RightCtrlOn = 0; + UsbKeyboardDevice->RightAltOn = 0; + UsbKeyboardDevice->RightShiftOn = 0; + UsbKeyboardDevice->RightLogoOn = 0; + UsbKeyboardDevice->MenuKeyOn = 0; + UsbKeyboardDevice->SysReqOn = 0; + + UsbKeyboardDevice->AltGrOn = 0; + + UsbKeyboardDevice->CurrentNsKey = NULL; + + // + // Sync the initial state of lights + // + SetKeyLED (UsbKeyboardDevice); + + ZeroMem (UsbKeyboardDevice->LastKeyCodeArray, sizeof (UINT8) * 8); + + // + // Set a timer for repeat keys' generation. + // + if (UsbKeyboardDevice->RepeatTimer != NULL) { + gBS->CloseEvent (UsbKeyboardDevice->RepeatTimer); + UsbKeyboardDevice->RepeatTimer = 0; + } + + Status = gBS->CreateEvent ( + EVT_TIMER | EVT_NOTIFY_SIGNAL, + TPL_NOTIFY, + USBKeyboardRepeatHandler, + UsbKeyboardDevice, + &UsbKeyboardDevice->RepeatTimer + ); + + if (UsbKeyboardDevice->DelayedRecoveryEvent != NULL) { + gBS->CloseEvent (UsbKeyboardDevice->DelayedRecoveryEvent); + UsbKeyboardDevice->DelayedRecoveryEvent = 0; + } + + Status = gBS->CreateEvent ( + EVT_TIMER | EVT_NOTIFY_SIGNAL, + TPL_NOTIFY, + USBKeyboardRecoveryHandler, + UsbKeyboardDevice, + &UsbKeyboardDevice->DelayedRecoveryEvent + ); + + return EFI_SUCCESS; +} + + +/** + Handler function for USB Keyboard's asynchronous interrupt transfer. + + @param Data A pointer to a buffer that is filled with key data which is + retrieved via asynchronous interrupt transfer. + @param DataLength Indicates the size of the data buffer. + @param Context Pointing to USB_KB_DEV instance. + @param Result Indicates the result of the asynchronous interrupt transfer. + + @retval EFI_SUCCESS Handler is successful. + @retval EFI_DEVICE_ERROR Hardware Error + +**/ +EFI_STATUS +EFIAPI +KeyboardHandler ( + IN VOID *Data, + IN UINTN DataLength, + IN VOID *Context, + IN UINT32 Result + ) +{ + USB_KB_DEV *UsbKeyboardDevice; + EFI_USB_IO_PROTOCOL *UsbIo; + UINT8 *CurKeyCodeBuffer; + UINT8 *OldKeyCodeBuffer; + UINT8 CurModifierMap; + UINT8 OldModifierMap; + UINT8 Index; + UINT8 Index2; + BOOLEAN Down; + BOOLEAN KeyRelease; + BOOLEAN KeyPress; + UINT8 SavedTail; + USB_KEY UsbKey; + UINT8 NewRepeatKey; + UINT32 UsbStatus; + EFI_KEY_DESCRIPTOR *KeyDescriptor; + + ASSERT (Context); + + NewRepeatKey = 0; + UsbKeyboardDevice = (USB_KB_DEV *) Context; + UsbIo = UsbKeyboardDevice->UsbIo; + + // + // Analyzes the Result and performs corresponding action. + // + if (Result != EFI_USB_NOERROR) { + // + // Some errors happen during the process + // + KbdReportStatusCode ( + UsbKeyboardDevice->DevicePath, + EFI_ERROR_CODE | EFI_ERROR_MINOR, + PcdGet32 (PcdStatusCodeValueKeyboardInputError) + ); + + // + // stop the repeat key generation if any + // + UsbKeyboardDevice->RepeatKey = 0; + + gBS->SetTimer ( + UsbKeyboardDevice->RepeatTimer, + TimerCancel, + USBKBD_REPEAT_RATE + ); + + if ((Result & EFI_USB_ERR_STALL) == EFI_USB_ERR_STALL) { + UsbClearEndpointHalt ( + UsbIo, + UsbKeyboardDevice->IntEndpointDescriptor.EndpointAddress, + &UsbStatus + ); + } + + // + // Delete & Submit this interrupt again + // + + UsbIo->UsbAsyncInterruptTransfer ( + UsbIo, + UsbKeyboardDevice->IntEndpointDescriptor.EndpointAddress, + FALSE, + 0, + 0, + NULL, + NULL + ); + + gBS->SetTimer ( + UsbKeyboardDevice->DelayedRecoveryEvent, + TimerRelative, + EFI_USB_INTERRUPT_DELAY + ); + + return EFI_DEVICE_ERROR; + } + + if (DataLength == 0 || Data == NULL) { + return EFI_SUCCESS; + } + + CurKeyCodeBuffer = (UINT8 *) Data; + OldKeyCodeBuffer = UsbKeyboardDevice->LastKeyCodeArray; + + // + // checks for new key stroke. + // if no new key got, return immediately. + // + for (Index = 0; Index < 8; Index++) { + if (OldKeyCodeBuffer[Index] != CurKeyCodeBuffer[Index]) { + break; + } + } + + if (Index == 8) { + return EFI_SUCCESS; + } + + // + // Parse the modifier key + // + CurModifierMap = CurKeyCodeBuffer[0]; + OldModifierMap = OldKeyCodeBuffer[0]; + + // + // handle modifier key's pressing or releasing situation. + // + for (Index = 0; Index < 8; Index++) { + + if ((CurModifierMap & KB_Mod[Index].Mask) != (OldModifierMap & KB_Mod[Index].Mask)) { + // + // if current modifier key is up, then + // CurModifierMap & KB_Mod[Index].Mask = 0; + // otherwize it is a non-zero value. + // Inserts the pressed modifier key into key buffer. + // + Down = (UINT8) (CurModifierMap & KB_Mod[Index].Mask); + InsertKeyCode (&(UsbKeyboardDevice->KeyboardBuffer), KB_Mod[Index].Key, Down); + } + } + + // + // handle normal key's releasing situation + // + KeyRelease = FALSE; + for (Index = 2; Index < 8; Index++) { + + if (!USBKBD_VALID_KEYCODE (OldKeyCodeBuffer[Index])) { + continue; + } + + KeyRelease = TRUE; + for (Index2 = 2; Index2 < 8; Index2++) { + + if (!USBKBD_VALID_KEYCODE (CurKeyCodeBuffer[Index2])) { + continue; + } + + if (OldKeyCodeBuffer[Index] == CurKeyCodeBuffer[Index2]) { + KeyRelease = FALSE; + break; + } + } + + if (KeyRelease) { + InsertKeyCode ( + &(UsbKeyboardDevice->KeyboardBuffer), + OldKeyCodeBuffer[Index], + 0 + ); + // + // the original reapeat key is released. + // + if (OldKeyCodeBuffer[Index] == UsbKeyboardDevice->RepeatKey) { + UsbKeyboardDevice->RepeatKey = 0; + } + } + } + + // + // original repeat key is released, cancel the repeat timer + // + if (UsbKeyboardDevice->RepeatKey == 0) { + gBS->SetTimer ( + UsbKeyboardDevice->RepeatTimer, + TimerCancel, + USBKBD_REPEAT_RATE + ); + } + + // + // handle normal key's pressing situation + // + KeyPress = FALSE; + for (Index = 2; Index < 8; Index++) { + + if (!USBKBD_VALID_KEYCODE (CurKeyCodeBuffer[Index])) { + continue; + } + + KeyPress = TRUE; + for (Index2 = 2; Index2 < 8; Index2++) { + + if (!USBKBD_VALID_KEYCODE (OldKeyCodeBuffer[Index2])) { + continue; + } + + if (CurKeyCodeBuffer[Index] == OldKeyCodeBuffer[Index2]) { + KeyPress = FALSE; + break; + } + } + + if (KeyPress) { + InsertKeyCode (&(UsbKeyboardDevice->KeyboardBuffer), CurKeyCodeBuffer[Index], 1); + // + // NumLock pressed or CapsLock pressed + // + KeyDescriptor = GetKeyDescriptor (UsbKeyboardDevice, CurKeyCodeBuffer[Index]); + if (KeyDescriptor->Modifier == EFI_NUM_LOCK_MODIFIER || KeyDescriptor->Modifier == EFI_CAPS_LOCK_MODIFIER) { + UsbKeyboardDevice->RepeatKey = 0; + } else { + NewRepeatKey = CurKeyCodeBuffer[Index]; + // + // do not repeat the original repeated key + // + UsbKeyboardDevice->RepeatKey = 0; + } + } + } + + // + // Update LastKeycodeArray[] buffer in the + // Usb Keyboard Device data structure. + // + for (Index = 0; Index < 8; Index++) { + UsbKeyboardDevice->LastKeyCodeArray[Index] = CurKeyCodeBuffer[Index]; + } + + // + // pre-process KeyboardBuffer, pop out the ctrl,alt,del key in sequence + // and judge whether it will invoke reset event. + // + SavedTail = UsbKeyboardDevice->KeyboardBuffer.bTail; + Index = UsbKeyboardDevice->KeyboardBuffer.bHead; + while (Index != SavedTail) { + RemoveKeyCode (&(UsbKeyboardDevice->KeyboardBuffer), &UsbKey); + + KeyDescriptor = GetKeyDescriptor (UsbKeyboardDevice, UsbKey.KeyCode); + + switch (KeyDescriptor->Modifier) { + + case EFI_LEFT_CONTROL_MODIFIER: + case EFI_RIGHT_CONTROL_MODIFIER: + if (UsbKey.Down != 0) { + UsbKeyboardDevice->CtrlOn = 1; + } else { + UsbKeyboardDevice->CtrlOn = 0; + } + break; + + case EFI_LEFT_ALT_MODIFIER: + case EFI_RIGHT_ALT_MODIFIER: + if (UsbKey.Down != 0) { + UsbKeyboardDevice->AltOn = 1; + } else { + UsbKeyboardDevice->AltOn = 0; + } + break; + + case EFI_ALT_GR_MODIFIER: + if (UsbKey.Down != 0) { + UsbKeyboardDevice->AltGrOn = 1; + } else { + UsbKeyboardDevice->AltGrOn = 0; + } + break; + + // + // Del Key Code + // + case EFI_DELETE_MODIFIER: + if (UsbKey.Down != 0) { + if ((UsbKeyboardDevice->CtrlOn != 0) && (UsbKeyboardDevice->AltOn != 0)) { + gRT->ResetSystem (EfiResetWarm, EFI_SUCCESS, 0, NULL); + } + } + break; + + default: + break; + } + + // + // insert the key back to the buffer. + // so the key sequence will not be destroyed. + // + InsertKeyCode ( + &(UsbKeyboardDevice->KeyboardBuffer), + UsbKey.KeyCode, + UsbKey.Down + ); + Index = UsbKeyboardDevice->KeyboardBuffer.bHead; + + } + // + // If have new key pressed, update the RepeatKey value, and set the + // timer to repeate delay timer + // + if (NewRepeatKey != 0) { + // + // sets trigger time to "Repeat Delay Time", + // to trigger the repeat timer when the key is hold long + // enough time. + // + gBS->SetTimer ( + UsbKeyboardDevice->RepeatTimer, + TimerRelative, + USBKBD_REPEAT_DELAY + ); + UsbKeyboardDevice->RepeatKey = NewRepeatKey; + } + + return EFI_SUCCESS; +} + + +/** + Retrieves a key character after parsing the raw data in keyboard buffer. + + @param UsbKeyboardDevice The USB_KB_DEV instance. + @param KeyChar Points to the Key character after key parsing. + + @retval EFI_SUCCESS Parse key is successful. + @retval EFI_NOT_READY Device is not ready. + +**/ +EFI_STATUS +EFIAPI +USBParseKey ( + IN OUT USB_KB_DEV *UsbKeyboardDevice, + OUT UINT8 *KeyChar + ) +{ + USB_KEY UsbKey; + EFI_KEY_DESCRIPTOR *KeyDescriptor; + + *KeyChar = 0; + + while (!IsUSBKeyboardBufferEmpty (&UsbKeyboardDevice->KeyboardBuffer)) { + // + // pops one raw data off. + // + RemoveKeyCode (&(UsbKeyboardDevice->KeyboardBuffer), &UsbKey); + + KeyDescriptor = GetKeyDescriptor (UsbKeyboardDevice, UsbKey.KeyCode); + if (UsbKey.Down == 0) { + switch (KeyDescriptor->Modifier) { + + // + // CTRL release + // + case EFI_LEFT_CONTROL_MODIFIER: + UsbKeyboardDevice->LeftCtrlOn = 0; + UsbKeyboardDevice->CtrlOn = 0; + break; + case EFI_RIGHT_CONTROL_MODIFIER: + UsbKeyboardDevice->RightCtrlOn = 0; + UsbKeyboardDevice->CtrlOn = 0; + break; + + // + // Shift release + // + case EFI_LEFT_SHIFT_MODIFIER: + UsbKeyboardDevice->LeftShiftOn = 0; + UsbKeyboardDevice->ShiftOn = 0; + break; + case EFI_RIGHT_SHIFT_MODIFIER: + UsbKeyboardDevice->RightShiftOn = 0; + UsbKeyboardDevice->ShiftOn = 0; + break; + + // + // Alt release + // + case EFI_LEFT_ALT_MODIFIER: + UsbKeyboardDevice->LeftAltOn = 0; + UsbKeyboardDevice->AltOn = 0; + break; + case EFI_RIGHT_ALT_MODIFIER: + UsbKeyboardDevice->RightAltOn = 0; + UsbKeyboardDevice->AltOn = 0; + break; + + // + // Left Logo release + // + case EFI_LEFT_LOGO_MODIFIER: + UsbKeyboardDevice->LeftLogoOn = 0; + break; + + // + // Right Logo release + // + case EFI_RIGHT_LOGO_MODIFIER: + UsbKeyboardDevice->RightLogoOn = 0; + break; + + // + // Menu key release + // + case EFI_MENU_MODIFIER: + UsbKeyboardDevice->MenuKeyOn = 0; + break; + + // + // SysReq release + // + case EFI_PRINT_MODIFIER: + case EFI_SYS_REQUEST_MODIFIER: + UsbKeyboardDevice->SysReqOn = 0; + break; + + // + // AltGr release + // + case EFI_ALT_GR_MODIFIER: + UsbKeyboardDevice->AltGrOn = 0; + break; + + default: + break; + } + + continue; + } + + // + // Analyzes key pressing situation + // + switch (KeyDescriptor->Modifier) { + + // + // CTRL press + // + case EFI_LEFT_CONTROL_MODIFIER: + UsbKeyboardDevice->LeftCtrlOn = 1; + UsbKeyboardDevice->CtrlOn = 1; + continue; + break; + case EFI_RIGHT_CONTROL_MODIFIER: + UsbKeyboardDevice->RightCtrlOn = 1; + UsbKeyboardDevice->CtrlOn = 1; + continue; + break; + + // + // Shift press + // + case EFI_LEFT_SHIFT_MODIFIER: + UsbKeyboardDevice->LeftShiftOn = 1; + UsbKeyboardDevice->ShiftOn = 1; + continue; + break; + case EFI_RIGHT_SHIFT_MODIFIER: + UsbKeyboardDevice->RightShiftOn = 1; + UsbKeyboardDevice->ShiftOn = 1; + continue; + break; + + // + // Alt press + // + case EFI_LEFT_ALT_MODIFIER: + UsbKeyboardDevice->LeftAltOn = 1; + UsbKeyboardDevice->AltOn = 1; + continue; + break; + case EFI_RIGHT_ALT_MODIFIER: + UsbKeyboardDevice->RightAltOn = 1; + UsbKeyboardDevice->AltOn = 1; + continue; + break; + + // + // Left Logo press + // + case EFI_LEFT_LOGO_MODIFIER: + UsbKeyboardDevice->LeftLogoOn = 1; + break; + + // + // Right Logo press + // + case EFI_RIGHT_LOGO_MODIFIER: + UsbKeyboardDevice->RightLogoOn = 1; + break; + + // + // Menu key press + // + case EFI_MENU_MODIFIER: + UsbKeyboardDevice->MenuKeyOn = 1; + break; + + // + // SysReq press + // + case EFI_PRINT_MODIFIER: + case EFI_SYS_REQUEST_MODIFIER: + UsbKeyboardDevice->SysReqOn = 1; + continue; + break; + + // + // AltGr press + // + case EFI_ALT_GR_MODIFIER: + UsbKeyboardDevice->AltGrOn = 1; + break; + + case EFI_NUM_LOCK_MODIFIER: + UsbKeyboardDevice->NumLockOn ^= 1; + // + // Turn on the NumLock light on KB + // + SetKeyLED (UsbKeyboardDevice); + continue; + break; + + case EFI_CAPS_LOCK_MODIFIER: + UsbKeyboardDevice->CapsOn ^= 1; + // + // Turn on the CapsLock light on KB + // + SetKeyLED (UsbKeyboardDevice); + continue; + break; + + case EFI_SCROLL_LOCK_MODIFIER: + UsbKeyboardDevice->ScrollOn ^= 1; + // + // Turn on the ScrollLock light on KB + // + SetKeyLED (UsbKeyboardDevice); + continue; + break; + + // + // F11,F12,PrintScreen,Pause/Break + // could not be retrieved via SimpleTxtInEx protocol + // + case EFI_FUNCTION_KEY_ELEVEN_MODIFIER: + case EFI_FUNCTION_KEY_TWELVE_MODIFIER: + case EFI_PAUSE_MODIFIER: + case EFI_BREAK_MODIFIER: + // + // fall through + // + continue; + break; + + default: + break; + } + + // + // When encountered Del Key... + // + if (KeyDescriptor->Modifier == EFI_DELETE_MODIFIER) { + if ((UsbKeyboardDevice->CtrlOn != 0) && (UsbKeyboardDevice->AltOn != 0)) { + gRT->ResetSystem (EfiResetWarm, EFI_SUCCESS, 0, NULL); + } + } + + *KeyChar = UsbKey.KeyCode; + return EFI_SUCCESS; + } + + return EFI_NOT_READY; +} + + +/** + Converts USB Keyboard code to EFI Scan Code. + + @param UsbKeyboardDevice The USB_KB_DEV instance. + @param KeyChar Indicates the key code that will be interpreted. + @param Key A pointer to a buffer that is filled in with + the keystroke information for the key that + was pressed. + + @retval EFI_NOT_READY Device is not ready + @retval EFI_SUCCESS Success. + +**/ +EFI_STATUS +EFIAPI +USBKeyCodeToEFIScanCode ( + IN USB_KB_DEV *UsbKeyboardDevice, + IN UINT8 KeyChar, + OUT EFI_INPUT_KEY *Key + ) +{ + UINT8 Index; + EFI_KEY_DESCRIPTOR *KeyDescriptor; + + if (!USBKBD_VALID_KEYCODE (KeyChar)) { + return EFI_NOT_READY; + } + + // + // valid USB Key Code starts from 4 + // + Index = (UINT8) (KeyChar - 4); + + if (Index >= USB_KEYCODE_MAX_MAKE) { + return EFI_NOT_READY; + } + + KeyDescriptor = GetKeyDescriptor (UsbKeyboardDevice, KeyChar); + + // + // Check for Non-spacing key + // + if (KeyDescriptor->Modifier == EFI_NS_KEY_MODIFIER) { + UsbKeyboardDevice->CurrentNsKey = FindUsbNsKey (UsbKeyboardDevice, KeyDescriptor); + return EFI_NOT_READY; + } + + // + // Check whether this keystroke follows a Non-spacing key + // + if (UsbKeyboardDevice->CurrentNsKey != NULL) { + KeyDescriptor = FindPhysicalKey (UsbKeyboardDevice->CurrentNsKey, KeyDescriptor); + UsbKeyboardDevice->CurrentNsKey = NULL; + } + + Key->ScanCode = EfiScanCodeConvertionTable[KeyDescriptor->Modifier]; + Key->UnicodeChar = KeyDescriptor->Unicode; + + if (KeyDescriptor->AffectedAttribute & EFI_AFFECTED_BY_STANDARD_SHIFT) { + if (UsbKeyboardDevice->ShiftOn != 0) { + Key->UnicodeChar = KeyDescriptor->ShiftedUnicode; + + // + // Need not return associated shift state if a class of printable characters that + // are normally adjusted by shift modifiers. e.g. Shift Key + 'f' key = 'F' + // + if (KeyDescriptor->AffectedAttribute & EFI_AFFECTED_BY_CAPS_LOCK) { + UsbKeyboardDevice->LeftShiftOn = 0; + UsbKeyboardDevice->RightShiftOn = 0; + } + + if (UsbKeyboardDevice->AltGrOn != 0) { + Key->UnicodeChar = KeyDescriptor->ShiftedAltGrUnicode; + } + } else { + // + // Shift off + // + Key->UnicodeChar = KeyDescriptor->Unicode; + + if (UsbKeyboardDevice->AltGrOn != 0) { + Key->UnicodeChar = KeyDescriptor->AltGrUnicode; + } + } + } + + if (KeyDescriptor->AffectedAttribute & EFI_AFFECTED_BY_CAPS_LOCK) { + if (UsbKeyboardDevice->CapsOn != 0) { + + if (Key->UnicodeChar == KeyDescriptor->Unicode) { + + Key->UnicodeChar = KeyDescriptor->ShiftedUnicode; + + } else if (Key->UnicodeChar == KeyDescriptor->ShiftedUnicode) { + + Key->UnicodeChar = KeyDescriptor->Unicode; + + } + } + } + + // + // Translate the CTRL-Alpha characters to their corresponding control value (ctrl-a = 0x0001 through ctrl-Z = 0x001A) + // + if (UsbKeyboardDevice->CtrlOn != 0) { + if (Key->UnicodeChar >= 'a' && Key->UnicodeChar <= 'z') { + Key->UnicodeChar = (UINT8) (Key->UnicodeChar - 'a' + 1); + } else if (Key->UnicodeChar >= 'A' && Key->UnicodeChar <= 'Z') { + Key->UnicodeChar = (UINT8) (Key->UnicodeChar - 'A' + 1); + } + } + + if (KeyDescriptor->AffectedAttribute & EFI_AFFECTED_BY_NUM_LOCK) { + + if ((UsbKeyboardDevice->NumLockOn != 0) && (UsbKeyboardDevice->ShiftOn == 0)) { + + Key->ScanCode = SCAN_NULL; + + } else { + Key->UnicodeChar = 0x00; + } + } + + // + // Translate Unicode 0x1B (ESC) to EFI Scan Code + // + if (Key->UnicodeChar == 0x1B && Key->ScanCode == SCAN_NULL) { + Key->ScanCode = SCAN_ESC; + Key->UnicodeChar = 0x00; + } + + if (Key->UnicodeChar == 0 && Key->ScanCode == SCAN_NULL) { + return EFI_NOT_READY; + } + + + // + // Save Shift/Toggle state + // + if (UsbKeyboardDevice->LeftCtrlOn == 1) { + UsbKeyboardDevice->KeyState.KeyShiftState |= EFI_LEFT_CONTROL_PRESSED; + } + if (UsbKeyboardDevice->RightCtrlOn == 1) { + UsbKeyboardDevice->KeyState.KeyShiftState |= EFI_RIGHT_CONTROL_PRESSED; + } + if (UsbKeyboardDevice->LeftAltOn == 1) { + UsbKeyboardDevice->KeyState.KeyShiftState |= EFI_LEFT_ALT_PRESSED; + } + if (UsbKeyboardDevice->RightAltOn == 1) { + UsbKeyboardDevice->KeyState.KeyShiftState |= EFI_RIGHT_ALT_PRESSED; + } + if (UsbKeyboardDevice->LeftShiftOn == 1) { + UsbKeyboardDevice->KeyState.KeyShiftState |= EFI_LEFT_SHIFT_PRESSED; + } + if (UsbKeyboardDevice->RightShiftOn == 1) { + UsbKeyboardDevice->KeyState.KeyShiftState |= EFI_RIGHT_SHIFT_PRESSED; + } + if (UsbKeyboardDevice->LeftLogoOn == 1) { + UsbKeyboardDevice->KeyState.KeyShiftState |= EFI_LEFT_LOGO_PRESSED; + } + if (UsbKeyboardDevice->RightLogoOn == 1) { + UsbKeyboardDevice->KeyState.KeyShiftState |= EFI_RIGHT_LOGO_PRESSED; + } + if (UsbKeyboardDevice->MenuKeyOn == 1) { + UsbKeyboardDevice->KeyState.KeyShiftState |= EFI_MENU_KEY_PRESSED; + } + if (UsbKeyboardDevice->SysReqOn == 1) { + UsbKeyboardDevice->KeyState.KeyShiftState |= EFI_SYS_REQ_PRESSED; + } + + if (UsbKeyboardDevice->ScrollOn == 1) { + UsbKeyboardDevice->KeyState.KeyToggleState |= EFI_SCROLL_LOCK_ACTIVE; + } + if (UsbKeyboardDevice->NumLockOn == 1) { + UsbKeyboardDevice->KeyState.KeyToggleState |= EFI_NUM_LOCK_ACTIVE; + } + if (UsbKeyboardDevice->CapsOn == 1) { + UsbKeyboardDevice->KeyState.KeyToggleState |= EFI_CAPS_LOCK_ACTIVE; + } + + return EFI_SUCCESS; + +} + + +/** + Resets USB Keyboard Buffer. + + @param KeyboardBuffer Points to the USB Keyboard Buffer. + + @retval EFI_SUCCESS Init key buffer successfully. + +**/ +EFI_STATUS +EFIAPI +InitUSBKeyBuffer ( + IN OUT USB_KB_BUFFER *KeyboardBuffer + ) +{ + ZeroMem (KeyboardBuffer, sizeof (USB_KB_BUFFER)); + + KeyboardBuffer->bHead = KeyboardBuffer->bTail; + + return EFI_SUCCESS; +} + + +/** + Check whether USB Keyboard buffer is empty. + + @param KeyboardBuffer USB Keyboard Buffer. + + @retval TRUE Key buffer is empty. + @retval FALSE Key buffer is not empty. + +**/ +BOOLEAN +EFIAPI +IsUSBKeyboardBufferEmpty ( + IN USB_KB_BUFFER *KeyboardBuffer + ) +{ + // + // meet FIFO empty condition + // + return (BOOLEAN) (KeyboardBuffer->bHead == KeyboardBuffer->bTail); +} + + +/** + Check whether USB Keyboard buffer is full. + + @param KeyboardBuffer USB Keyboard Buffer. + + @retval TRUE Key buffer is full. + @retval FALSE Key buffer is not full. + +**/ +BOOLEAN +EFIAPI +IsUSBKeyboardBufferFull ( + IN USB_KB_BUFFER *KeyboardBuffer + ) +{ + return (BOOLEAN)(((KeyboardBuffer->bTail + 1) % (MAX_KEY_ALLOWED + 1)) == + KeyboardBuffer->bHead); +} + + +/** + Inserts a key code into keyboard buffer. + + @param KeyboardBuffer Points to the USB Keyboard Buffer. + @param Key Key code + @param Down Special key + + @retval EFI_SUCCESS Success + +**/ +EFI_STATUS +EFIAPI +InsertKeyCode ( + IN OUT USB_KB_BUFFER *KeyboardBuffer, + IN UINT8 Key, + IN UINT8 Down + ) +{ + USB_KEY UsbKey; + + // + // if keyboard buffer is full, throw the + // first key out of the keyboard buffer. + // + if (IsUSBKeyboardBufferFull (KeyboardBuffer)) { + RemoveKeyCode (KeyboardBuffer, &UsbKey); + } + + KeyboardBuffer->buffer[KeyboardBuffer->bTail].KeyCode = Key; + KeyboardBuffer->buffer[KeyboardBuffer->bTail].Down = Down; + + // + // adjust the tail pointer of the FIFO keyboard buffer. + // + KeyboardBuffer->bTail = (UINT8) ((KeyboardBuffer->bTail + 1) % (MAX_KEY_ALLOWED + 1)); + + return EFI_SUCCESS; +} + + +/** + Pops a key code off from keyboard buffer. + + @param KeyboardBuffer Points to the USB Keyboard Buffer. + @param UsbKey Points to the buffer that contains a usb key code. + + @retval EFI_SUCCESS Success + @retval EFI_DEVICE_ERROR Hardware Error + +**/ +EFI_STATUS +EFIAPI +RemoveKeyCode ( + IN OUT USB_KB_BUFFER *KeyboardBuffer, + OUT USB_KEY *UsbKey + ) +{ + if (IsUSBKeyboardBufferEmpty (KeyboardBuffer)) { + return EFI_DEVICE_ERROR; + } + + UsbKey->KeyCode = KeyboardBuffer->buffer[KeyboardBuffer->bHead].KeyCode; + UsbKey->Down = KeyboardBuffer->buffer[KeyboardBuffer->bHead].Down; + + // + // adjust the head pointer of the FIFO keyboard buffer. + // + KeyboardBuffer->bHead = (UINT8) ((KeyboardBuffer->bHead + 1) % (MAX_KEY_ALLOWED + 1)); + + return EFI_SUCCESS; +} + + +/** + Sets USB Keyboard LED state. + + @param UsbKeyboardDevice The USB_KB_DEV instance. + + @retval EFI_SUCCESS Success + +**/ +EFI_STATUS +EFIAPI +SetKeyLED ( + IN USB_KB_DEV *UsbKeyboardDevice + ) +{ + LED_MAP Led; + UINT8 ReportId; + + // + // Set each field in Led map. + // + Led.NumLock = (UINT8) UsbKeyboardDevice->NumLockOn; + Led.CapsLock = (UINT8) UsbKeyboardDevice->CapsOn; + Led.ScrollLock = (UINT8) UsbKeyboardDevice->ScrollOn; + Led.Resrvd = 0; + + ReportId = 0; + // + // call Set Report Request to lighten the LED. + // + UsbSetReportRequest ( + UsbKeyboardDevice->UsbIo, + UsbKeyboardDevice->InterfaceDescriptor.InterfaceNumber, + ReportId, + HID_OUTPUT_REPORT, + 1, + (UINT8 *) &Led + ); + + return EFI_SUCCESS; +} + + +/** + Timer handler for Repeat Key timer. + + @param Event The Repeat Key event. + @param Context Points to the USB_KB_DEV instance. + + +**/ +VOID +EFIAPI +USBKeyboardRepeatHandler ( + IN EFI_EVENT Event, + IN VOID *Context + ) +{ + USB_KB_DEV *UsbKeyboardDevice; + + UsbKeyboardDevice = (USB_KB_DEV *) Context; + + // + // Do nothing when there is no repeat key. + // + if (UsbKeyboardDevice->RepeatKey != 0) { + // + // Inserts one Repeat key into keyboard buffer, + // + InsertKeyCode ( + &(UsbKeyboardDevice->KeyboardBuffer), + UsbKeyboardDevice->RepeatKey, + 1 + ); + + // + // set repeate rate for repeat key generation. + // + gBS->SetTimer ( + UsbKeyboardDevice->RepeatTimer, + TimerRelative, + USBKBD_REPEAT_RATE + ); + + } +} + + +/** + Timer handler for Delayed Recovery timer. + + @param Event The Delayed Recovery event. + @param Context Points to the USB_KB_DEV instance. + + +**/ +VOID +EFIAPI +USBKeyboardRecoveryHandler ( + IN EFI_EVENT Event, + IN VOID *Context + ) +{ + + USB_KB_DEV *UsbKeyboardDevice; + EFI_USB_IO_PROTOCOL *UsbIo; + UINT8 PacketSize; + + UsbKeyboardDevice = (USB_KB_DEV *) Context; + + UsbIo = UsbKeyboardDevice->UsbIo; + + PacketSize = (UINT8) (UsbKeyboardDevice->IntEndpointDescriptor.MaxPacketSize); + + UsbIo->UsbAsyncInterruptTransfer ( + UsbIo, + UsbKeyboardDevice->IntEndpointDescriptor.EndpointAddress, + TRUE, + UsbKeyboardDevice->IntEndpointDescriptor.Interval, + PacketSize, + KeyboardHandler, + UsbKeyboardDevice + ); +} diff --git a/MdeModulePkg/Bus/Usb/UsbKbDxe/KeyBoard.h b/MdeModulePkg/Bus/Usb/UsbKbDxe/KeyBoard.h new file mode 100644 index 000000000..22aaf2127 --- /dev/null +++ b/MdeModulePkg/Bus/Usb/UsbKbDxe/KeyBoard.h @@ -0,0 +1,260 @@ +/** @file + + Function prototype for USB Keyboard Driver. + +Copyright (c) 2004 - 2008, Intel Corporation +All rights reserved. This program and the accompanying materials +are licensed and made available under the terms and conditions of the BSD License +which accompanies this distribution. The full text of the license may be found at +http://opensource.org/licenses/bsd-license.php + +THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, +WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. + +**/ + +#ifndef _EFI_KEYBOARD_H_ +#define _EFI_KEYBOARD_H_ + + +#include "EfiKey.h" + +/** + Uses USB I/O to check whether the device is a USB Keyboard device. + + @param UsbIo Points to a USB I/O protocol instance. + @retval None + +**/ +BOOLEAN +EFIAPI +IsUSBKeyboard ( + IN EFI_USB_IO_PROTOCOL *UsbIo + ); + +/** + Initialize USB Keyboard device and all private data structures. + + @param UsbKeyboardDevice The USB_KB_DEV instance. + + @retval EFI_SUCCESS Initialization is successful. + @retval EFI_DEVICE_ERROR Configure hardware failed. + +**/ +EFI_STATUS +EFIAPI +InitUSBKeyboard ( + IN USB_KB_DEV *UsbKeyboardDevice + ); + +/** + Initialize USB Keyboard layout. + + @param UsbKeyboardDevice The USB_KB_DEV instance. + + @retval EFI_SUCCESS Initialization Success. + @retval Other Keyboard layout initial failed. + +**/ +EFI_STATUS +EFIAPI +InitKeyboardLayout ( + IN USB_KB_DEV *UsbKeyboardDevice + ); + +/** + Destroy resources for Keyboard layout. + + @param UsbKeyboardDevice The USB_KB_DEV instance. + +**/ +VOID +EFIAPI +ReleaseKeyboardLayoutResources ( + IN USB_KB_DEV *UsbKeyboardDevice + ); + +/** + Handler function for USB Keyboard's asynchronous interrupt transfer. + + @param Data A pointer to a buffer that is filled with key data which is + retrieved via asynchronous interrupt transfer. + @param DataLength Indicates the size of the data buffer. + @param Context Pointing to USB_KB_DEV instance. + @param Result Indicates the result of the asynchronous interrupt transfer. + + @retval EFI_SUCCESS Handler is successful. + @retval EFI_DEVICE_ERROR Hardware Error + +**/ +EFI_STATUS +EFIAPI +KeyboardHandler ( + IN VOID *Data, + IN UINTN DataLength, + IN VOID *Context, + IN UINT32 Result + ); + +/** + Timer handler for Delayed Recovery timer. + + @param Event The Delayed Recovery event. + @param Context Points to the USB_KB_DEV instance. + + +**/ +VOID +EFIAPI +USBKeyboardRecoveryHandler ( + IN EFI_EVENT Event, + IN VOID *Context + ); + +/** + Retrieves a key character after parsing the raw data in keyboard buffer. + + @param UsbKeyboardDevice The USB_KB_DEV instance. + @param KeyChar Points to the Key character after key parsing. + + @retval EFI_SUCCESS Parse key is successful. + @retval EFI_NOT_READY Device is not ready. + +**/ +EFI_STATUS +EFIAPI +USBParseKey ( + IN OUT USB_KB_DEV *UsbKeyboardDevice, + OUT UINT8 *KeyChar + ); + +/** + Converts USB Keyboard code to EFI Scan Code. + + @param UsbKeyboardDevice The USB_KB_DEV instance. + @param KeyChar Indicates the key code that will be interpreted. + @param Key A pointer to a buffer that is filled in with + the keystroke information for the key that + was pressed. + + @retval EFI_NOT_READY Device is not ready + @retval EFI_SUCCESS Success. + +**/ +EFI_STATUS +EFIAPI +USBKeyCodeToEFIScanCode ( + IN USB_KB_DEV *UsbKeyboardDevice, + IN UINT8 KeyChar, + OUT EFI_INPUT_KEY *Key + ); + +/** + Resets USB Keyboard Buffer. + + @param KeyboardBuffer Points to the USB Keyboard Buffer. + + @retval EFI_SUCCESS Init key buffer successfully. + +**/ +EFI_STATUS +EFIAPI +InitUSBKeyBuffer ( + IN OUT USB_KB_BUFFER *KeyboardBuffer + ); + +/** + Check whether USB Keyboard buffer is empty. + + @param KeyboardBuffer USB Keyboard Buffer. + + @retval TRUE Key buffer is empty. + @retval FALSE Key buffer is not empty. + +**/ +BOOLEAN +EFIAPI +IsUSBKeyboardBufferEmpty ( + IN USB_KB_BUFFER *KeyboardBuffer + ); + +/** + Check whether USB Keyboard buffer is full. + + @param KeyboardBuffer USB Keyboard Buffer. + + @retval TRUE Key buffer is full. + @retval FALSE Key buffer is not full. + +**/ +BOOLEAN +EFIAPI +IsUSBKeyboardBufferFull ( + IN USB_KB_BUFFER *KeyboardBuffer + ); + +/** + Inserts a key code into keyboard buffer. + + @param KeyboardBuffer Points to the USB Keyboard Buffer. + @param Key Key code + @param Down Special key + + @retval EFI_SUCCESS Success + +**/ +EFI_STATUS +EFIAPI +InsertKeyCode ( + IN OUT USB_KB_BUFFER *KeyboardBuffer, + IN UINT8 Key, + IN UINT8 Down + ); + +/** + Pops a key code off from keyboard buffer. + + @param KeyboardBuffer Points to the USB Keyboard Buffer. + @param UsbKey Points to the buffer that contains a usb key code. + + @retval EFI_SUCCESS Success + @retval EFI_DEVICE_ERROR Hardware Error + +**/ +EFI_STATUS +EFIAPI +RemoveKeyCode ( + IN OUT USB_KB_BUFFER *KeyboardBuffer, + OUT USB_KEY *UsbKey + ); + +/** + Timer handler for Repeat Key timer. + + @param Event The Repeat Key event. + @param Context Points to the USB_KB_DEV instance. + + +**/ +VOID +EFIAPI +USBKeyboardRepeatHandler ( + IN EFI_EVENT Event, + IN VOID *Context + ); + +/** + Sets USB Keyboard LED state. + + @param UsbKeyboardDevice The USB_KB_DEV instance. + + @retval EFI_SUCCESS Success + +**/ +EFI_STATUS +EFIAPI +SetKeyLED ( + IN USB_KB_DEV *UsbKeyboardDevice + ); + +#endif diff --git a/MdeModulePkg/Bus/Usb/UsbKbDxe/UsbKbDxe.inf b/MdeModulePkg/Bus/Usb/UsbKbDxe/UsbKbDxe.inf index 0a50c277c..5098a8b9c 100644 --- a/MdeModulePkg/Bus/Usb/UsbKbDxe/UsbKbDxe.inf +++ b/MdeModulePkg/Bus/Usb/UsbKbDxe/UsbKbDxe.inf @@ -33,11 +33,11 @@ # [Sources.common] - efikey.c - efikey.h - keyboard.c + EfiKey.c + EfiKey.h + KeyBoard.c ComponentName.c - keyboard.h + KeyBoard.h [Packages] MdePkg/MdePkg.dec diff --git a/MdeModulePkg/Bus/Usb/UsbKbDxe/efikey.c b/MdeModulePkg/Bus/Usb/UsbKbDxe/efikey.c deleted file mode 100644 index 50413bd50..000000000 --- a/MdeModulePkg/Bus/Usb/UsbKbDxe/efikey.c +++ /dev/null @@ -1,1340 +0,0 @@ -/** @file - -Copyright (c) 2004 - 2008, Intel Corporation -All rights reserved. This program and the accompanying materials -are licensed and made available under the terms and conditions of the BSD License -which accompanies this distribution. The full text of the license may be found at -http://opensource.org/licenses/bsd-license.php - -THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, -WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. - -Module Name: - - EfiKey.c - -Abstract: - - USB Keyboard Driver - -Revision History - - -**/ - -#include "efikey.h" -#include "keyboard.h" - -// -// Prototypes -// Driver model protocol interface -// -EFI_STATUS -EFIAPI -USBKeyboardDriverBindingEntryPoint ( - IN EFI_HANDLE ImageHandle, - IN EFI_SYSTEM_TABLE *SystemTable - ); - -EFI_STATUS -EFIAPI -USBKeyboardDriverBindingSupported ( - IN EFI_DRIVER_BINDING_PROTOCOL *This, - IN EFI_HANDLE Controller, - IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath - ); - -EFI_STATUS -EFIAPI -USBKeyboardDriverBindingStart ( - IN EFI_DRIVER_BINDING_PROTOCOL *This, - IN EFI_HANDLE Controller, - IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath - ); - -EFI_STATUS -EFIAPI -USBKeyboardDriverBindingStop ( - IN EFI_DRIVER_BINDING_PROTOCOL *This, - IN EFI_HANDLE Controller, - IN UINTN NumberOfChildren, - IN EFI_HANDLE *ChildHandleBuffer - ); - -// -// Simple Text In Protocol Interface -// -STATIC -EFI_STATUS -EFIAPI -USBKeyboardReset ( - IN EFI_SIMPLE_TEXT_INPUT_PROTOCOL *This, - IN BOOLEAN ExtendedVerification - ); - -STATIC -EFI_STATUS -EFIAPI -USBKeyboardReadKeyStroke ( - IN EFI_SIMPLE_TEXT_INPUT_PROTOCOL *This, - OUT EFI_INPUT_KEY *Key - ); - -STATIC -VOID -EFIAPI -USBKeyboardWaitForKey ( - IN EFI_EVENT Event, - IN VOID *Context - ); - -// -// Helper functions -// -STATIC -EFI_STATUS -USBKeyboardCheckForKey ( - IN USB_KB_DEV *UsbKeyboardDevice - ); - -EFI_GUID gEfiUsbKeyboardDriverGuid = { - 0xa05f5f78, 0xfb3, 0x4d10, {0x90, 0x90, 0xac, 0x4, 0x6e, 0xeb, 0x7c, 0x3c} -}; -STATIC -EFI_STATUS -KbdFreeNotifyList ( - IN OUT LIST_ENTRY *ListHead - ); -STATIC -BOOLEAN -IsKeyRegistered ( - IN EFI_KEY_DATA *RegsiteredData, - IN EFI_KEY_DATA *InputData - ); - - -// -// USB Keyboard Driver Global Variables -// -EFI_DRIVER_BINDING_PROTOCOL gUsbKeyboardDriverBinding = { - USBKeyboardDriverBindingSupported, - USBKeyboardDriverBindingStart, - USBKeyboardDriverBindingStop, - 0xa, - NULL, - NULL -}; - -EFI_STATUS -EFIAPI -USBKeyboardDriverBindingEntryPoint ( - IN EFI_HANDLE ImageHandle, - IN EFI_SYSTEM_TABLE *SystemTable - ) -/*++ - - Routine Description: - Driver Entry Point. - - Arguments: - ImageHandle - EFI_HANDLE - SystemTable - EFI_SYSTEM_TABLE - Returns: - EFI_STATUS - ---*/ -{ - return EfiLibInstallDriverBindingComponentName2 ( - ImageHandle, - SystemTable, - &gUsbKeyboardDriverBinding, - ImageHandle, - &gUsbKeyboardComponentName, - &gUsbKeyboardComponentName2 - ); -} - - - -/** - Supported. - - @param This EFI_DRIVER_BINDING_PROTOCOL - @param Controller Controller handle - @param RemainingDevicePath EFI_DEVICE_PATH_PROTOCOL - EFI_STATUS - -**/ -EFI_STATUS -EFIAPI -USBKeyboardDriverBindingSupported ( - IN EFI_DRIVER_BINDING_PROTOCOL *This, - IN EFI_HANDLE Controller, - IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath - ) -{ - EFI_STATUS OpenStatus; - EFI_USB_IO_PROTOCOL *UsbIo; - EFI_STATUS Status; - - // - // Check if USB_IO protocol is attached on the controller handle. - // - OpenStatus = gBS->OpenProtocol ( - Controller, - &gEfiUsbIoProtocolGuid, - (VOID **) &UsbIo, - This->DriverBindingHandle, - Controller, - EFI_OPEN_PROTOCOL_BY_DRIVER - ); - if (EFI_ERROR (OpenStatus)) { - return OpenStatus; - } - - // - // Use the USB I/O protocol interface to check whether the Controller is - // the Keyboard controller that can be managed by this driver. - // - Status = EFI_SUCCESS; - - if (!IsUSBKeyboard (UsbIo)) { - Status = EFI_UNSUPPORTED; - } - - gBS->CloseProtocol ( - Controller, - &gEfiUsbIoProtocolGuid, - This->DriverBindingHandle, - Controller - ); - - return Status; -} - - -/** - Start. - - @param This EFI_DRIVER_BINDING_PROTOCOL - @param Controller Controller handle - @param RemainingDevicePath EFI_DEVICE_PATH_PROTOCOL - - @retval EFI_SUCCESS Success - @retval EFI_OUT_OF_RESOURCES Can't allocate memory - @retval EFI_UNSUPPORTED The Start routine fail - -**/ -EFI_STATUS -EFIAPI -USBKeyboardDriverBindingStart ( - IN EFI_DRIVER_BINDING_PROTOCOL *This, - IN EFI_HANDLE Controller, - IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath - ) -{ - EFI_STATUS Status; - EFI_USB_IO_PROTOCOL *UsbIo; - USB_KB_DEV *UsbKeyboardDevice; - UINT8 EndpointNumber; - EFI_USB_ENDPOINT_DESCRIPTOR EndpointDescriptor; - UINT8 Index; - UINT8 EndpointAddr; - UINT8 PollingInterval; - UINT8 PacketSize; - BOOLEAN Found; - - UsbKeyboardDevice = NULL; - Found = FALSE; - - // - // Open USB_IO Protocol - // - Status = gBS->OpenProtocol ( - Controller, - &gEfiUsbIoProtocolGuid, - (VOID **) &UsbIo, - This->DriverBindingHandle, - Controller, - EFI_OPEN_PROTOCOL_BY_DRIVER - ); - if (EFI_ERROR (Status)) { - return Status; - } - - UsbKeyboardDevice = AllocateZeroPool (sizeof (USB_KB_DEV)); - if (UsbKeyboardDevice == NULL) { - gBS->CloseProtocol ( - Controller, - &gEfiUsbIoProtocolGuid, - This->DriverBindingHandle, - Controller - ); - return EFI_OUT_OF_RESOURCES; - } - // - // Get the Device Path Protocol on Controller's handle - // - Status = gBS->OpenProtocol ( - Controller, - &gEfiDevicePathProtocolGuid, - (VOID **) &UsbKeyboardDevice->DevicePath, - This->DriverBindingHandle, - Controller, - EFI_OPEN_PROTOCOL_GET_PROTOCOL - ); - - if (EFI_ERROR (Status)) { - gBS->FreePool (UsbKeyboardDevice); - gBS->CloseProtocol ( - Controller, - &gEfiUsbIoProtocolGuid, - This->DriverBindingHandle, - Controller - ); - return Status; - } - // - // Report that the usb keyboard is being enabled - // - KbdReportStatusCode ( - UsbKeyboardDevice->DevicePath, - EFI_PROGRESS_CODE, - PcdGet32 (PcdStatusCodeValueKeyboardEnable) - ); - - // - // This is pretty close to keyboard detection, so log progress - // - KbdReportStatusCode ( - UsbKeyboardDevice->DevicePath, - EFI_PROGRESS_CODE, - PcdGet32 (PcdStatusCodeValueKeyboardPresenceDetect) - ); - - // - // Initialize UsbKeyboardDevice - // - UsbKeyboardDevice->UsbIo = UsbIo; - - // - // Get interface & endpoint descriptor - // - UsbIo->UsbGetInterfaceDescriptor ( - UsbIo, - &UsbKeyboardDevice->InterfaceDescriptor - ); - - EndpointNumber = UsbKeyboardDevice->InterfaceDescriptor.NumEndpoints; - - for (Index = 0; Index < EndpointNumber; Index++) { - - UsbIo->UsbGetEndpointDescriptor ( - UsbIo, - Index, - &EndpointDescriptor - ); - - if ((EndpointDescriptor.Attributes & 0x03) == 0x03) { - // - // We only care interrupt endpoint here - // - CopyMem(&UsbKeyboardDevice->IntEndpointDescriptor, &EndpointDescriptor, sizeof(EndpointDescriptor)); - Found = TRUE; - } - } - - if (!Found) { - // - // No interrupt endpoint found, then return unsupported. - // - gBS->FreePool (UsbKeyboardDevice); - gBS->CloseProtocol ( - Controller, - &gEfiUsbIoProtocolGuid, - This->DriverBindingHandle, - Controller - ); - return EFI_UNSUPPORTED; - } - - UsbKeyboardDevice->Signature = USB_KB_DEV_SIGNATURE; - UsbKeyboardDevice->SimpleInput.Reset = USBKeyboardReset; - UsbKeyboardDevice->SimpleInput.ReadKeyStroke = USBKeyboardReadKeyStroke; - - UsbKeyboardDevice->SimpleInputEx.Reset = USBKeyboardResetEx; - UsbKeyboardDevice->SimpleInputEx.ReadKeyStrokeEx = USBKeyboardReadKeyStrokeEx; - UsbKeyboardDevice->SimpleInputEx.SetState = USBKeyboardSetState; - UsbKeyboardDevice->SimpleInputEx.RegisterKeyNotify = USBKeyboardRegisterKeyNotify; - UsbKeyboardDevice->SimpleInputEx.UnregisterKeyNotify = USBKeyboardUnregisterKeyNotify; - - InitializeListHead (&UsbKeyboardDevice->NotifyList); - - Status = gBS->CreateEvent ( - EVT_NOTIFY_WAIT, - TPL_NOTIFY, - USBKeyboardWaitForKey, - UsbKeyboardDevice, - &(UsbKeyboardDevice->SimpleInputEx.WaitForKeyEx) - ); - - if (EFI_ERROR (Status)) { - goto ErrorExit; - } - - Status = InitKeyboardLayout (UsbKeyboardDevice); - if (EFI_ERROR (Status)) { - goto ErrorExit; - } - - Status = gBS->CreateEvent ( - EVT_NOTIFY_WAIT, - TPL_NOTIFY, - USBKeyboardWaitForKey, - UsbKeyboardDevice, - &(UsbKeyboardDevice->SimpleInput.WaitForKey) - ); - - if (EFI_ERROR (Status)) { - gBS->FreePool (UsbKeyboardDevice); - gBS->CloseProtocol ( - Controller, - &gEfiUsbIoProtocolGuid, - This->DriverBindingHandle, - Controller - ); - return Status; - } - - // - // Install simple txt in protocol interface - // for the usb keyboard device. - // Usb keyboard is a hot plug device, and expected to work immediately - // when plugging into system, so a HotPlugDeviceGuid is installed onto - // the usb keyboard device handle, to distinguish it from other conventional - // console devices. - // - Status = gBS->InstallMultipleProtocolInterfaces ( - &Controller, - &gEfiSimpleTextInProtocolGuid, - &UsbKeyboardDevice->SimpleInput, - &gEfiSimpleTextInputExProtocolGuid, - &UsbKeyboardDevice->SimpleInputEx, - &gEfiHotPlugDeviceGuid, - NULL, - NULL - ); - if (EFI_ERROR (Status)) { - gBS->CloseEvent (UsbKeyboardDevice->SimpleInput.WaitForKey); - gBS->FreePool (UsbKeyboardDevice); - gBS->CloseProtocol ( - Controller, - &gEfiUsbIoProtocolGuid, - This->DriverBindingHandle, - Controller - ); - return Status; - } - - // - // Reset USB Keyboard Device - // - Status = UsbKeyboardDevice->SimpleInput.Reset ( - &UsbKeyboardDevice->SimpleInput, - TRUE - ); - if (EFI_ERROR (Status)) { - gBS->UninstallMultipleProtocolInterfaces ( - Controller, - &gEfiSimpleTextInProtocolGuid, - &UsbKeyboardDevice->SimpleInput, - &gEfiSimpleTextInputExProtocolGuid, - &UsbKeyboardDevice->SimpleInputEx, - &gEfiHotPlugDeviceGuid, - NULL, - NULL - ); - gBS->CloseEvent (UsbKeyboardDevice->SimpleInput.WaitForKey); - gBS->FreePool (UsbKeyboardDevice); - gBS->CloseProtocol ( - Controller, - &gEfiUsbIoProtocolGuid, - This->DriverBindingHandle, - Controller - ); - return Status; - } - // - // submit async interrupt transfer - // - EndpointAddr = UsbKeyboardDevice->IntEndpointDescriptor.EndpointAddress; - PollingInterval = UsbKeyboardDevice->IntEndpointDescriptor.Interval; - PacketSize = (UINT8) (UsbKeyboardDevice->IntEndpointDescriptor.MaxPacketSize); - - Status = UsbIo->UsbAsyncInterruptTransfer ( - UsbIo, - EndpointAddr, - TRUE, - PollingInterval, - PacketSize, - KeyboardHandler, - UsbKeyboardDevice - ); - - if (EFI_ERROR (Status)) { - - gBS->UninstallMultipleProtocolInterfaces ( - Controller, - &gEfiSimpleTextInProtocolGuid, - &UsbKeyboardDevice->SimpleInput, - &gEfiSimpleTextInputExProtocolGuid, - &UsbKeyboardDevice->SimpleInputEx, - &gEfiHotPlugDeviceGuid, - NULL, - NULL - ); - gBS->CloseEvent (UsbKeyboardDevice->SimpleInput.WaitForKey); - gBS->FreePool (UsbKeyboardDevice); - gBS->CloseProtocol ( - Controller, - &gEfiUsbIoProtocolGuid, - This->DriverBindingHandle, - Controller - ); - return Status; - } - - UsbKeyboardDevice->ControllerNameTable = NULL; - AddUnicodeString2 ( - "eng", - gUsbKeyboardComponentName.SupportedLanguages, - &UsbKeyboardDevice->ControllerNameTable, - L"Generic Usb Keyboard", - TRUE - ); - AddUnicodeString2 ( - "en", - gUsbKeyboardComponentName2.SupportedLanguages, - &UsbKeyboardDevice->ControllerNameTable, - L"Generic Usb Keyboard", - FALSE - ); - - - return EFI_SUCCESS; - -ErrorExit: - if (UsbKeyboardDevice != NULL) { - if (UsbKeyboardDevice->SimpleInput.WaitForKey != NULL) { - gBS->CloseEvent (UsbKeyboardDevice->SimpleInput.WaitForKey); - } - if (UsbKeyboardDevice->SimpleInputEx.WaitForKeyEx != NULL) { - gBS->CloseEvent (UsbKeyboardDevice->SimpleInputEx.WaitForKeyEx); - } - KbdFreeNotifyList (&UsbKeyboardDevice->NotifyList); - gBS->FreePool (UsbKeyboardDevice); - UsbKeyboardDevice = NULL; - } - gBS->CloseProtocol ( - Controller, - &gEfiUsbIoProtocolGuid, - This->DriverBindingHandle, - Controller - ); - return Status; - -} - - - -/** - Stop. - - @param This EFI_DRIVER_BINDING_PROTOCOL - @param Controller Controller handle - @param NumberOfChildren Child handle number - @param ChildHandleBuffer Child handle buffer - - @retval EFI_SUCCESS Success - @retval EFI_UNSUPPORTED Can't support - -**/ -EFI_STATUS -EFIAPI -USBKeyboardDriverBindingStop ( - IN EFI_DRIVER_BINDING_PROTOCOL *This, - IN EFI_HANDLE Controller, - IN UINTN NumberOfChildren, - IN EFI_HANDLE *ChildHandleBuffer - ) -{ - EFI_STATUS Status; - EFI_SIMPLE_TEXT_INPUT_PROTOCOL *SimpleInput; - USB_KB_DEV *UsbKeyboardDevice; - - Status = gBS->OpenProtocol ( - Controller, - &gEfiSimpleTextInProtocolGuid, - (VOID **) &SimpleInput, - This->DriverBindingHandle, - Controller, - EFI_OPEN_PROTOCOL_GET_PROTOCOL - ); - if (EFI_ERROR (Status)) { - return EFI_UNSUPPORTED; - } - Status = gBS->OpenProtocol ( - Controller, - &gEfiSimpleTextInputExProtocolGuid, - NULL, - This->DriverBindingHandle, - Controller, - EFI_OPEN_PROTOCOL_TEST_PROTOCOL - ); - if (EFI_ERROR (Status)) { - return EFI_UNSUPPORTED; - } - // - // Get USB_KB_DEV instance. - // - UsbKeyboardDevice = USB_KB_DEV_FROM_THIS (SimpleInput); - - gBS->CloseProtocol ( - Controller, - &gEfiSimpleTextInProtocolGuid, - This->DriverBindingHandle, - Controller - ); - - // - // Uninstall the Asyn Interrupt Transfer from this device - // will disable the key data input from this device - // - KbdReportStatusCode ( - UsbKeyboardDevice->DevicePath, - EFI_PROGRESS_CODE, - PcdGet32 (PcdStatusCodeValueKeyboardDisable) - ); - - // - // Destroy asynchronous interrupt transfer - // - UsbKeyboardDevice->UsbIo->UsbAsyncInterruptTransfer ( - UsbKeyboardDevice->UsbIo, - UsbKeyboardDevice->IntEndpointDescriptor.EndpointAddress, - FALSE, - UsbKeyboardDevice->IntEndpointDescriptor.Interval, - 0, - NULL, - NULL - ); - - gBS->CloseProtocol ( - Controller, - &gEfiUsbIoProtocolGuid, - This->DriverBindingHandle, - Controller - ); - - Status = gBS->UninstallMultipleProtocolInterfaces ( - Controller, - &gEfiSimpleTextInProtocolGuid, - &UsbKeyboardDevice->SimpleInput, - &gEfiSimpleTextInputExProtocolGuid, - &UsbKeyboardDevice->SimpleInputEx, - &gEfiHotPlugDeviceGuid, - NULL, - NULL - ); - // - // free all the resources. - // - gBS->CloseEvent (UsbKeyboardDevice->RepeatTimer); - gBS->CloseEvent (UsbKeyboardDevice->DelayedRecoveryEvent); - gBS->CloseEvent ((UsbKeyboardDevice->SimpleInput).WaitForKey); - gBS->CloseEvent (UsbKeyboardDevice->SimpleInputEx.WaitForKeyEx); - KbdFreeNotifyList (&UsbKeyboardDevice->NotifyList); - - ReleaseKeyboardLayoutResources (UsbKeyboardDevice); - gBS->CloseEvent (UsbKeyboardDevice->KeyboardLayoutEvent); - - if (UsbKeyboardDevice->ControllerNameTable != NULL) { - FreeUnicodeStringTable (UsbKeyboardDevice->ControllerNameTable); - } - - gBS->FreePool (UsbKeyboardDevice); - - return Status; - -} - -STATIC -EFI_STATUS -USBKeyboardReadKeyStrokeWorker ( - IN USB_KB_DEV *UsbKeyboardDevice, - OUT EFI_KEY_DATA *KeyData - ) -/*++ - - Routine Description: - Reads the next keystroke from the input device. The WaitForKey Event can - be used to test for existance of a keystroke via WaitForEvent () call. - - Arguments: - UsbKeyboardDevice - Usb keyboard private structure. - KeyData - A pointer to a buffer that is filled in with the keystroke - state data for the key that was pressed. - - Returns: - EFI_SUCCESS - The keystroke information was returned. - EFI_NOT_READY - There was no keystroke data availiable. - EFI_DEVICE_ERROR - The keystroke information was not returned due to - hardware errors. - EFI_INVALID_PARAMETER - KeyData is NULL. - ---*/ -{ - - EFI_STATUS Status; - UINT8 KeyChar; - LIST_ENTRY *Link; - KEYBOARD_CONSOLE_IN_EX_NOTIFY *CurrentNotify; - EFI_KEY_DATA OriginalKeyData; - - if (KeyData == NULL) { - return EFI_INVALID_PARAMETER; - } - - // - // if there is no saved ASCII byte, fetch it - // by calling USBKeyboardCheckForKey(). - // - if (UsbKeyboardDevice->CurKeyChar == 0) { - Status = USBKeyboardCheckForKey (UsbKeyboardDevice); - if (EFI_ERROR (Status)) { - return Status; - } - } - - KeyData->Key.UnicodeChar = 0; - KeyData->Key.ScanCode = SCAN_NULL; - - KeyChar = UsbKeyboardDevice->CurKeyChar; - - UsbKeyboardDevice->CurKeyChar = 0; - - // - // Translate saved ASCII byte into EFI_INPUT_KEY - // - Status = USBKeyCodeToEFIScanCode (UsbKeyboardDevice, KeyChar, &KeyData->Key); - if (EFI_ERROR (Status)) { - return Status; - } - - CopyMem (&KeyData->KeyState, &UsbKeyboardDevice->KeyState, sizeof (KeyData->KeyState)); - - UsbKeyboardDevice->KeyState.KeyShiftState = EFI_SHIFT_STATE_VALID; - UsbKeyboardDevice->KeyState.KeyToggleState = EFI_TOGGLE_STATE_VALID; - - // - //Switch the control value to their original characters. In USBKeyCodeToEFIScanCode() the CTRL-Alpha characters have been switched to - // their corresponding control value (ctrl-a = 0x0001 through ctrl-Z = 0x001A), here switch them back for notification function. - // - CopyMem (&OriginalKeyData, KeyData, sizeof (EFI_KEY_DATA)); - if (UsbKeyboardDevice->CtrlOn) { - if (OriginalKeyData.Key.UnicodeChar >= 0x01 && OriginalKeyData.Key.UnicodeChar <= 0x1A) { - if (UsbKeyboardDevice->CapsOn) { - OriginalKeyData.Key.UnicodeChar = (CHAR16)(OriginalKeyData.Key.UnicodeChar + 'A' - 1); - } else { - OriginalKeyData.Key.UnicodeChar = (CHAR16)(OriginalKeyData.Key.UnicodeChar + 'a' - 1); - } - } - } - - // - // Invoke notification functions if exist - // - for (Link = UsbKeyboardDevice->NotifyList.ForwardLink; Link != &UsbKeyboardDevice->NotifyList; Link = Link->ForwardLink) { - CurrentNotify = CR ( - Link, - KEYBOARD_CONSOLE_IN_EX_NOTIFY, - NotifyEntry, - USB_KB_CONSOLE_IN_EX_NOTIFY_SIGNATURE - ); - if (IsKeyRegistered (&CurrentNotify->KeyData, &OriginalKeyData)) { - CurrentNotify->KeyNotificationFn (&OriginalKeyData); - } - } - - return EFI_SUCCESS; - -} -EFI_STATUS -EFIAPI -USBKeyboardReset ( - IN EFI_SIMPLE_TEXT_INPUT_PROTOCOL *This, - IN BOOLEAN ExtendedVerification - ) -{ - EFI_STATUS Status; - USB_KB_DEV *UsbKeyboardDevice; - - UsbKeyboardDevice = USB_KB_DEV_FROM_THIS (This); - - KbdReportStatusCode ( - UsbKeyboardDevice->DevicePath, - EFI_PROGRESS_CODE, - PcdGet32 (PcdStatusCodeValueKeyboardReset) - ); - - // - // Non Exhaustive reset: - // only reset private data structures. - // - if (!ExtendedVerification) { - // - // Clear the key buffer of this Usb keyboard - // - KbdReportStatusCode ( - UsbKeyboardDevice->DevicePath, - EFI_PROGRESS_CODE, - PcdGet32 (PcdStatusCodeValueKeyboardClearBuffer) - ); - - InitUSBKeyBuffer (&(UsbKeyboardDevice->KeyboardBuffer)); - UsbKeyboardDevice->CurKeyChar = 0; - return EFI_SUCCESS; - } - - // - // Exhaustive reset - // - Status = InitUSBKeyboard (UsbKeyboardDevice); - UsbKeyboardDevice->CurKeyChar = 0; - if (EFI_ERROR (Status)) { - return EFI_DEVICE_ERROR; - } - - return EFI_SUCCESS; -} - - -/** - Implements EFI_SIMPLE_TEXT_INPUT_PROTOCOL.ReadKeyStroke() function. - - This The EFI_SIMPLE_TEXT_INPUT_PROTOCOL instance. - Key A pointer to a buffer that is filled in with the keystroke - information for the key that was pressed. - - @retval EFI_SUCCESS Success - -**/ -STATIC -EFI_STATUS -EFIAPI -USBKeyboardReadKeyStroke ( - IN EFI_SIMPLE_TEXT_INPUT_PROTOCOL *This, - OUT EFI_INPUT_KEY *Key - ) -{ - USB_KB_DEV *UsbKeyboardDevice; - EFI_STATUS Status; - EFI_KEY_DATA KeyData; - - UsbKeyboardDevice = USB_KB_DEV_FROM_THIS (This); - - Status = USBKeyboardReadKeyStrokeWorker (UsbKeyboardDevice, &KeyData); - if (EFI_ERROR (Status)) { - return Status; - } - - CopyMem (Key, &KeyData.Key, sizeof (EFI_INPUT_KEY)); - - return EFI_SUCCESS; - -} - - -/** - Handler function for WaitForKey event. - - Event Event to be signaled when a key is pressed. - Context Points to USB_KB_DEV instance. - - @return VOID - -**/ -STATIC -VOID -EFIAPI -USBKeyboardWaitForKey ( - IN EFI_EVENT Event, - IN VOID *Context - ) -{ - USB_KB_DEV *UsbKeyboardDevice; - - UsbKeyboardDevice = (USB_KB_DEV *) Context; - - if (UsbKeyboardDevice->CurKeyChar == 0) { - - if (EFI_ERROR (USBKeyboardCheckForKey (UsbKeyboardDevice))) { - return ; - } - } - // - // If has key pending, signal the event. - // - gBS->SignalEvent (Event); -} - - - -/** - Check whether there is key pending. - - UsbKeyboardDevice The USB_KB_DEV instance. - - @retval EFI_SUCCESS Success - -**/ -STATIC -EFI_STATUS -USBKeyboardCheckForKey ( - IN USB_KB_DEV *UsbKeyboardDevice - ) -{ - EFI_STATUS Status; - UINT8 KeyChar; - - // - // Fetch raw data from the USB keyboard input, - // and translate it into ASCII data. - // - Status = USBParseKey (UsbKeyboardDevice, &KeyChar); - if (EFI_ERROR (Status)) { - return Status; - } - - UsbKeyboardDevice->CurKeyChar = KeyChar; - return EFI_SUCCESS; -} - - -/** - Report Status Code in Usb Bot Driver - - @param DevicePath Use this to get Device Path - @param CodeType Status Code Type - @param CodeValue Status Code Value - - @return None - -**/ -VOID -KbdReportStatusCode ( - IN EFI_DEVICE_PATH_PROTOCOL *DevicePath, - IN EFI_STATUS_CODE_TYPE CodeType, - IN EFI_STATUS_CODE_VALUE Value - ) -{ - - REPORT_STATUS_CODE_WITH_DEVICE_PATH ( - CodeType, - Value, - DevicePath - ); -} -STATIC -EFI_STATUS -KbdFreeNotifyList ( - IN OUT LIST_ENTRY *ListHead - ) -/*++ - -Routine Description: - -Arguments: - - ListHead - The list head - -Returns: - - EFI_SUCCESS - Free the notify list successfully - EFI_INVALID_PARAMETER - ListHead is invalid. - ---*/ -{ - KEYBOARD_CONSOLE_IN_EX_NOTIFY *NotifyNode; - - if (ListHead == NULL) { - return EFI_INVALID_PARAMETER; - } - while (!IsListEmpty (ListHead)) { - NotifyNode = CR ( - ListHead->ForwardLink, - KEYBOARD_CONSOLE_IN_EX_NOTIFY, - NotifyEntry, - USB_KB_CONSOLE_IN_EX_NOTIFY_SIGNATURE - ); - RemoveEntryList (ListHead->ForwardLink); - gBS->FreePool (NotifyNode); - } - - return EFI_SUCCESS; -} - -STATIC -BOOLEAN -IsKeyRegistered ( - IN EFI_KEY_DATA *RegsiteredData, - IN EFI_KEY_DATA *InputData - ) -/*++ - -Routine Description: - -Arguments: - - RegsiteredData - A pointer to a buffer that is filled in with the keystroke - state data for the key that was registered. - InputData - A pointer to a buffer that is filled in with the keystroke - state data for the key that was pressed. - -Returns: - TRUE - Key be pressed matches a registered key. - FLASE - Match failed. - ---*/ -{ - ASSERT (RegsiteredData != NULL && InputData != NULL); - - if ((RegsiteredData->Key.ScanCode != InputData->Key.ScanCode) || - (RegsiteredData->Key.UnicodeChar != InputData->Key.UnicodeChar)) { - return FALSE; - } - - // - // Assume KeyShiftState/KeyToggleState = 0 in Registered key data means these state could be ignored. - // - if (RegsiteredData->KeyState.KeyShiftState != 0 && - RegsiteredData->KeyState.KeyShiftState != InputData->KeyState.KeyShiftState) { - return FALSE; - } - if (RegsiteredData->KeyState.KeyToggleState != 0 && - RegsiteredData->KeyState.KeyToggleState != InputData->KeyState.KeyToggleState) { - return FALSE; - } - - return TRUE; - -} - -// -// Simple Text Input Ex protocol functions -// -EFI_STATUS -EFIAPI -USBKeyboardResetEx ( - IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *This, - IN BOOLEAN ExtendedVerification - ) -/*++ - - Routine Description: - Reset the input device and optionaly run diagnostics - - Arguments: - This - Protocol instance pointer. - ExtendedVerification - Driver may perform diagnostics on reset. - - Returns: - EFI_SUCCESS - The device was reset. - EFI_DEVICE_ERROR - The device is not functioning properly and could - not be reset. - ---*/ -{ - EFI_STATUS Status; - USB_KB_DEV *UsbKeyboardDevice; - EFI_TPL OldTpl; - - - UsbKeyboardDevice = TEXT_INPUT_EX_USB_KB_DEV_FROM_THIS (This); - - Status = UsbKeyboardDevice->SimpleInput.Reset (&UsbKeyboardDevice->SimpleInput, ExtendedVerification); - if (EFI_ERROR (Status)) { - return EFI_DEVICE_ERROR; - } - - OldTpl = gBS->RaiseTPL (TPL_NOTIFY); - UsbKeyboardDevice->KeyState.KeyShiftState = EFI_SHIFT_STATE_VALID; - UsbKeyboardDevice->KeyState.KeyToggleState = EFI_TOGGLE_STATE_VALID; - gBS->RestoreTPL (OldTpl); - - return EFI_SUCCESS; - -} - -EFI_STATUS -EFIAPI -USBKeyboardReadKeyStrokeEx ( - IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *This, - OUT EFI_KEY_DATA *KeyData - ) -/*++ - - Routine Description: - Reads the next keystroke from the input device. The WaitForKey Event can - be used to test for existance of a keystroke via WaitForEvent () call. - - Arguments: - This - Protocol instance pointer. - KeyData - A pointer to a buffer that is filled in with the keystroke - state data for the key that was pressed. - - Returns: - EFI_SUCCESS - The keystroke information was returned. - EFI_NOT_READY - There was no keystroke data availiable. - EFI_DEVICE_ERROR - The keystroke information was not returned due to - hardware errors. - EFI_INVALID_PARAMETER - KeyData is NULL. - ---*/ -{ - USB_KB_DEV *UsbKeyboardDevice; - - if (KeyData == NULL) { - return EFI_INVALID_PARAMETER; - } - - UsbKeyboardDevice = TEXT_INPUT_EX_USB_KB_DEV_FROM_THIS (This); - - return USBKeyboardReadKeyStrokeWorker (UsbKeyboardDevice, KeyData); - -} - -EFI_STATUS -EFIAPI -USBKeyboardSetState ( - IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *This, - IN EFI_KEY_TOGGLE_STATE *KeyToggleState - ) -/*++ - - Routine Description: - Set certain state for the input device. - - Arguments: - This - Protocol instance pointer. - KeyToggleState - A pointer to the EFI_KEY_TOGGLE_STATE to set the - state for the input device. - - Returns: - EFI_SUCCESS - The device state was set successfully. - EFI_DEVICE_ERROR - The device is not functioning correctly and could - not have the setting adjusted. - EFI_UNSUPPORTED - The device does not have the ability to set its state. - EFI_INVALID_PARAMETER - KeyToggleState is NULL. - ---*/ -{ - USB_KB_DEV *UsbKeyboardDevice; - - if (KeyToggleState == NULL) { - return EFI_INVALID_PARAMETER; - } - - UsbKeyboardDevice = TEXT_INPUT_EX_USB_KB_DEV_FROM_THIS (This); - - if (((UsbKeyboardDevice->KeyState.KeyToggleState & EFI_TOGGLE_STATE_VALID) != EFI_TOGGLE_STATE_VALID) || - ((*KeyToggleState & EFI_TOGGLE_STATE_VALID) != EFI_TOGGLE_STATE_VALID)) { - return EFI_UNSUPPORTED; - } - - // - // Update the status light - // - - UsbKeyboardDevice->ScrollOn = 0; - UsbKeyboardDevice->NumLockOn = 0; - UsbKeyboardDevice->CapsOn = 0; - - if ((*KeyToggleState & EFI_SCROLL_LOCK_ACTIVE) == EFI_SCROLL_LOCK_ACTIVE) { - UsbKeyboardDevice->ScrollOn = 1; - } - if ((*KeyToggleState & EFI_NUM_LOCK_ACTIVE) == EFI_NUM_LOCK_ACTIVE) { - UsbKeyboardDevice->NumLockOn = 1; - } - if ((*KeyToggleState & EFI_CAPS_LOCK_ACTIVE) == EFI_CAPS_LOCK_ACTIVE) { - UsbKeyboardDevice->CapsOn = 1; - } - - SetKeyLED (UsbKeyboardDevice); - - UsbKeyboardDevice->KeyState.KeyToggleState = *KeyToggleState; - - return EFI_SUCCESS; - -} - -EFI_STATUS -EFIAPI -USBKeyboardRegisterKeyNotify ( - IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *This, - IN EFI_KEY_DATA *KeyData, - IN EFI_KEY_NOTIFY_FUNCTION KeyNotificationFunction, - OUT EFI_HANDLE *NotifyHandle - ) -/*++ - - Routine Description: - Register a notification function for a particular keystroke for the input device. - - Arguments: - This - Protocol instance pointer. - KeyData - A pointer to a buffer that is filled in with the keystroke - information data for the key that was pressed. - KeyNotificationFunction - Points to the function to be called when the key - sequence is typed specified by KeyData. - NotifyHandle - Points to the unique handle assigned to the registered notification. - - Returns: - EFI_SUCCESS - The notification function was registered successfully. - EFI_OUT_OF_RESOURCES - Unable to allocate resources for necesssary data structures. - EFI_INVALID_PARAMETER - KeyData or NotifyHandle is NULL. - ---*/ -{ - USB_KB_DEV *UsbKeyboardDevice; - EFI_STATUS Status; - KEYBOARD_CONSOLE_IN_EX_NOTIFY *NewNotify; - LIST_ENTRY *Link; - KEYBOARD_CONSOLE_IN_EX_NOTIFY *CurrentNotify; - - if (KeyData == NULL || NotifyHandle == NULL || KeyNotificationFunction == NULL) { - return EFI_INVALID_PARAMETER; - } - - UsbKeyboardDevice = TEXT_INPUT_EX_USB_KB_DEV_FROM_THIS (This); - - // - // Return EFI_SUCCESS if the (KeyData, NotificationFunction) is already registered. - // - for (Link = UsbKeyboardDevice->NotifyList.ForwardLink; Link != &UsbKeyboardDevice->NotifyList; Link = Link->ForwardLink) { - CurrentNotify = CR ( - Link, - KEYBOARD_CONSOLE_IN_EX_NOTIFY, - NotifyEntry, - USB_KB_CONSOLE_IN_EX_NOTIFY_SIGNATURE - ); - if (IsKeyRegistered (&CurrentNotify->KeyData, KeyData)) { - if (CurrentNotify->KeyNotificationFn == KeyNotificationFunction) { - *NotifyHandle = CurrentNotify->NotifyHandle; - return EFI_SUCCESS; - } - } - } - - // - // Allocate resource to save the notification function - // - NewNotify = (KEYBOARD_CONSOLE_IN_EX_NOTIFY *) AllocateZeroPool (sizeof (KEYBOARD_CONSOLE_IN_EX_NOTIFY)); - if (NewNotify == NULL) { - return EFI_OUT_OF_RESOURCES; - } - - NewNotify->Signature = USB_KB_CONSOLE_IN_EX_NOTIFY_SIGNATURE; - NewNotify->KeyNotificationFn = KeyNotificationFunction; - CopyMem (&NewNotify->KeyData, KeyData, sizeof (EFI_KEY_DATA)); - InsertTailList (&UsbKeyboardDevice->NotifyList, &NewNotify->NotifyEntry); - - // - // Use gSimpleTextInExNotifyGuid to get a valid EFI_HANDLE - // - Status = gBS->InstallMultipleProtocolInterfaces ( - &NewNotify->NotifyHandle, - &gSimpleTextInExNotifyGuid, - NULL, - NULL - ); - ASSERT_EFI_ERROR (Status); - - *NotifyHandle = NewNotify->NotifyHandle; - - return EFI_SUCCESS; - -} - -EFI_STATUS -EFIAPI -USBKeyboardUnregisterKeyNotify ( - IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *This, - IN EFI_HANDLE NotificationHandle - ) -/*++ - - Routine Description: - Remove a registered notification function from a particular keystroke. - - Arguments: - This - Protocol instance pointer. - NotificationHandle - The handle of the notification function being unregistered. - - Returns: - EFI_SUCCESS - The notification function was unregistered successfully. - EFI_INVALID_PARAMETER - The NotificationHandle is invalid. - EFI_NOT_FOUND - Can not find the matching entry in database. - ---*/ -{ - USB_KB_DEV *UsbKeyboardDevice; - EFI_STATUS Status; - KEYBOARD_CONSOLE_IN_EX_NOTIFY *CurrentNotify; - LIST_ENTRY *Link; - - if (NotificationHandle == NULL) { - return EFI_INVALID_PARAMETER; - } - - UsbKeyboardDevice = TEXT_INPUT_EX_USB_KB_DEV_FROM_THIS (This); - - Status = gBS->OpenProtocol ( - NotificationHandle, - &gSimpleTextInExNotifyGuid, - NULL, - NULL, - NULL, - EFI_OPEN_PROTOCOL_TEST_PROTOCOL - ); - if (EFI_ERROR (Status)) { - return EFI_INVALID_PARAMETER; - } - - for (Link = UsbKeyboardDevice->NotifyList.ForwardLink; Link != &UsbKeyboardDevice->NotifyList; Link = Link->ForwardLink) { - CurrentNotify = CR ( - Link, - KEYBOARD_CONSOLE_IN_EX_NOTIFY, - NotifyEntry, - USB_KB_CONSOLE_IN_EX_NOTIFY_SIGNATURE - ); - if (CurrentNotify->NotifyHandle == NotificationHandle) { - // - // Remove the notification function from NotifyList and free resources - // - RemoveEntryList (&CurrentNotify->NotifyEntry); - Status = gBS->UninstallMultipleProtocolInterfaces ( - CurrentNotify->NotifyHandle, - &gSimpleTextInExNotifyGuid, - NULL, - NULL - ); - ASSERT_EFI_ERROR (Status); - gBS->FreePool (CurrentNotify); - return EFI_SUCCESS; - } - } - - return EFI_NOT_FOUND; -} - diff --git a/MdeModulePkg/Bus/Usb/UsbKbDxe/efikey.h b/MdeModulePkg/Bus/Usb/UsbKbDxe/efikey.h deleted file mode 100644 index 19443949e..000000000 --- a/MdeModulePkg/Bus/Usb/UsbKbDxe/efikey.h +++ /dev/null @@ -1,337 +0,0 @@ -/** @file -Copyright (c) 2004 - 2008, Intel Corporation -All rights reserved. This program and the accompanying materials -are licensed and made available under the terms and conditions of the BSD License -which accompanies this distribution. The full text of the license may be found at -http://opensource.org/licenses/bsd-license.php - -THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, -WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. - -Module Name: - - EfiKey.h - -Abstract: - - Header file for USB Keyboard Driver's Data Structures - -Revision History - -**/ -#ifndef _USB_KB_H -#define _USB_KB_H - - -#include - -#include -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include - -#define MAX_KEY_ALLOWED 32 - -#define HZ 1000 * 1000 * 10 -#define USBKBD_REPEAT_DELAY ((HZ) / 2) -#define USBKBD_REPEAT_RATE ((HZ) / 50) - -#define CLASS_HID 3 -#define SUBCLASS_BOOT 1 -#define PROTOCOL_KEYBOARD 1 - -#define BOOT_PROTOCOL 0 -#define REPORT_PROTOCOL 1 - -typedef struct { - UINT8 Down; - UINT8 KeyCode; -} USB_KEY; - -typedef struct { - USB_KEY buffer[MAX_KEY_ALLOWED + 1]; - UINT8 bHead; - UINT8 bTail; -} USB_KB_BUFFER; - -#define USB_KB_DEV_SIGNATURE EFI_SIGNATURE_32 ('u', 'k', 'b', 'd') -#define USB_KB_CONSOLE_IN_EX_NOTIFY_SIGNATURE EFI_SIGNATURE_32 ('u', 'k', 'b', 'x') - -typedef struct _KEYBOARD_CONSOLE_IN_EX_NOTIFY { - UINTN Signature; - EFI_HANDLE NotifyHandle; - EFI_KEY_DATA KeyData; - EFI_KEY_NOTIFY_FUNCTION KeyNotificationFn; - LIST_ENTRY NotifyEntry; -} KEYBOARD_CONSOLE_IN_EX_NOTIFY; - -#define USB_NS_KEY_SIGNATURE EFI_SIGNATURE_32 ('u', 'n', 's', 'k') - -typedef struct { - UINTN Signature; - LIST_ENTRY Link; - - // - // The number of EFI_NS_KEY_MODIFIER children definitions - // - UINTN KeyCount; - - // - // NsKey[0] : Non-spacing key - // NsKey[1] ~ NsKey[KeyCount] : Physical keys - // - EFI_KEY_DESCRIPTOR *NsKey; -} USB_NS_KEY; - -#define USB_NS_KEY_FORM_FROM_LINK(a) CR (a, USB_NS_KEY, Link, USB_NS_KEY_SIGNATURE) - -typedef struct { - UINTN Signature; - EFI_DEVICE_PATH_PROTOCOL *DevicePath; - EFI_EVENT DelayedRecoveryEvent; - EFI_SIMPLE_TEXT_INPUT_PROTOCOL SimpleInput; - EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL SimpleInputEx; - EFI_USB_IO_PROTOCOL *UsbIo; - - EFI_USB_INTERFACE_DESCRIPTOR InterfaceDescriptor; - EFI_USB_ENDPOINT_DESCRIPTOR IntEndpointDescriptor; - - USB_KB_BUFFER KeyboardBuffer; - UINT8 CtrlOn; - UINT8 AltOn; - UINT8 ShiftOn; - UINT8 NumLockOn; - UINT8 CapsOn; - UINT8 ScrollOn; - UINT8 LastKeyCodeArray[8]; - UINT8 CurKeyChar; - - UINT8 RepeatKey; - EFI_EVENT RepeatTimer; - - EFI_UNICODE_STRING_TABLE *ControllerNameTable; - - UINT8 LeftCtrlOn; - UINT8 LeftAltOn; - UINT8 LeftShiftOn; - UINT8 LeftLogoOn; - UINT8 RightCtrlOn; - UINT8 RightAltOn; - UINT8 RightShiftOn; - UINT8 RightLogoOn; - UINT8 MenuKeyOn; - UINT8 SysReqOn; - UINT8 AltGrOn; - - EFI_KEY_STATE KeyState; - // - // Notification function list - // - LIST_ENTRY NotifyList; - - // - // Non-spacing key list - // - LIST_ENTRY NsKeyList; - USB_NS_KEY *CurrentNsKey; - EFI_KEY_DESCRIPTOR *KeyConvertionTable; - EFI_EVENT KeyboardLayoutEvent; -} USB_KB_DEV; - -// -// Global Variables -// -extern EFI_DRIVER_BINDING_PROTOCOL gUsbKeyboardDriverBinding; -extern EFI_COMPONENT_NAME_PROTOCOL gUsbKeyboardComponentName; -extern EFI_COMPONENT_NAME2_PROTOCOL gUsbKeyboardComponentName2; -extern EFI_GUID gEfiUsbKeyboardDriverGuid; -extern EFI_GUID gSimpleTextInExNotifyGuid; - -VOID -KbdReportStatusCode ( - IN EFI_DEVICE_PATH_PROTOCOL *DevicePath, - IN EFI_STATUS_CODE_TYPE CodeType, - IN EFI_STATUS_CODE_VALUE Value - ); - -#define USB_KB_DEV_FROM_THIS(a) \ - CR(a, USB_KB_DEV, SimpleInput, USB_KB_DEV_SIGNATURE) -#define TEXT_INPUT_EX_USB_KB_DEV_FROM_THIS(a) \ - CR(a, USB_KB_DEV, SimpleInputEx, USB_KB_DEV_SIGNATURE) - - -#define MOD_CONTROL_L 0x01 -#define MOD_CONTROL_R 0x10 -#define MOD_SHIFT_L 0x02 -#define MOD_SHIFT_R 0x20 -#define MOD_ALT_L 0x04 -#define MOD_ALT_R 0x40 -#define MOD_WIN_L 0x08 -#define MOD_WIN_R 0x80 - -typedef struct { - UINT8 Mask; - UINT8 Key; -} KB_MODIFIER; - -#define USB_KEYCODE_MAX_MAKE 0x62 - -#define USBKBD_VALID_KEYCODE(key) ((UINT8) (key) > 3) - -typedef struct { - UINT8 NumLock : 1; - UINT8 CapsLock : 1; - UINT8 ScrollLock : 1; - UINT8 Resrvd : 5; -} LED_MAP; - -// -// Simple Text Input Ex protocol functions -// -EFI_STATUS -EFIAPI -USBKeyboardResetEx ( - IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *This, - IN BOOLEAN ExtendedVerification - ) -/*++ - - Routine Description: - Reset the input device and optionaly run diagnostics - - Arguments: - This - Protocol instance pointer. - ExtendedVerification - Driver may perform diagnostics on reset. - - Returns: - EFI_SUCCESS - The device was reset. - EFI_DEVICE_ERROR - The device is not functioning properly and could - not be reset. - ---*/ -; - -EFI_STATUS -EFIAPI -USBKeyboardReadKeyStrokeEx ( - IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *This, - OUT EFI_KEY_DATA *KeyData - ) -/*++ - - Routine Description: - Reads the next keystroke from the input device. The WaitForKey Event can - be used to test for existance of a keystroke via WaitForEvent () call. - - Arguments: - This - Protocol instance pointer. - KeyData - A pointer to a buffer that is filled in with the keystroke - state data for the key that was pressed. - - Returns: - EFI_SUCCESS - The keystroke information was returned. - EFI_NOT_READY - There was no keystroke data availiable. - EFI_DEVICE_ERROR - The keystroke information was not returned due to - hardware errors. - EFI_INVALID_PARAMETER - KeyData is NULL. - ---*/ -; - -EFI_STATUS -EFIAPI -USBKeyboardSetState ( - IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *This, - IN EFI_KEY_TOGGLE_STATE *KeyToggleState - ) -/*++ - - Routine Description: - Set certain state for the input device. - - Arguments: - This - Protocol instance pointer. - KeyToggleState - A pointer to the EFI_KEY_TOGGLE_STATE to set the - state for the input device. - - Returns: - EFI_SUCCESS - The device state was set successfully. - EFI_DEVICE_ERROR - The device is not functioning correctly and could - not have the setting adjusted. - EFI_UNSUPPORTED - The device does not have the ability to set its state. - EFI_INVALID_PARAMETER - KeyToggleState is NULL. - ---*/ -; - -EFI_STATUS -EFIAPI -USBKeyboardRegisterKeyNotify ( - IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *This, - IN EFI_KEY_DATA *KeyData, - IN EFI_KEY_NOTIFY_FUNCTION KeyNotificationFunction, - OUT EFI_HANDLE *NotifyHandle - ) -/*++ - - Routine Description: - Register a notification function for a particular keystroke for the input device. - - Arguments: - This - Protocol instance pointer. - KeyData - A pointer to a buffer that is filled in with the keystroke - information data for the key that was pressed. - KeyNotificationFunction - Points to the function to be called when the key - sequence is typed specified by KeyData. - NotifyHandle - Points to the unique handle assigned to the registered notification. - - Returns: - EFI_SUCCESS - The notification function was registered successfully. - EFI_OUT_OF_RESOURCES - Unable to allocate resources for necesssary data structures. - EFI_INVALID_PARAMETER - KeyData or NotifyHandle is NULL. - ---*/ -; - -EFI_STATUS -EFIAPI -USBKeyboardUnregisterKeyNotify ( - IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *This, - IN EFI_HANDLE NotificationHandle - ) -/*++ - - Routine Description: - Remove a registered notification function from a particular keystroke. - - Arguments: - This - Protocol instance pointer. - NotificationHandle - The handle of the notification function being unregistered. - - Returns: - EFI_SUCCESS - The notification function was unregistered successfully. - EFI_INVALID_PARAMETER - The NotificationHandle is invalid. - EFI_NOT_FOUND - Can not find the matching entry in database. - ---*/ -; - -#endif - diff --git a/MdeModulePkg/Bus/Usb/UsbKbDxe/keyboard.c b/MdeModulePkg/Bus/Usb/UsbKbDxe/keyboard.c deleted file mode 100644 index b7cd8c119..000000000 --- a/MdeModulePkg/Bus/Usb/UsbKbDxe/keyboard.c +++ /dev/null @@ -1,1951 +0,0 @@ -/** @file - -Copyright (c) 2004 - 2008, Intel Corporation -All rights reserved. This program and the accompanying materials -are licensed and made available under the terms and conditions of the BSD License -which accompanies this distribution. The full text of the license may be found at -http://opensource.org/licenses/bsd-license.php - -THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, -WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. - -Module Name: - - Keyboard.c - -Abstract: - - Helper functions for USB Keyboard Driver - -Revision History - - -**/ - -#include "keyboard.h" -#include - -// -// Static English keyboard layout -// Format:, , , , -// -STATIC -UINT8 KeyboardLayoutTable[USB_KEYCODE_MAX_MAKE + 8][5] = { - {EfiKeyC1, 'a', 'A', EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT | EFI_AFFECTED_BY_CAPS_LOCK}, // 0x04 - {EfiKeyB5, 'b', 'B', EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT | EFI_AFFECTED_BY_CAPS_LOCK}, // 0x05 - {EfiKeyB3, 'c', 'C', EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT | EFI_AFFECTED_BY_CAPS_LOCK}, // 0x06 - {EfiKeyC3, 'd', 'D', EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT | EFI_AFFECTED_BY_CAPS_LOCK}, // 0x07 - {EfiKeyD3, 'e', 'E', EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT | EFI_AFFECTED_BY_CAPS_LOCK}, // 0x08 - {EfiKeyC4, 'f', 'F', EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT | EFI_AFFECTED_BY_CAPS_LOCK}, // 0x09 - {EfiKeyC5, 'g', 'G', EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT | EFI_AFFECTED_BY_CAPS_LOCK}, // 0x0A - {EfiKeyC6, 'h', 'H', EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT | EFI_AFFECTED_BY_CAPS_LOCK}, // 0x0B - {EfiKeyD8, 'i', 'I', EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT | EFI_AFFECTED_BY_CAPS_LOCK}, // 0x0C - {EfiKeyC7, 'j', 'J', EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT | EFI_AFFECTED_BY_CAPS_LOCK}, // 0x0D - {EfiKeyC8, 'k', 'K', EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT | EFI_AFFECTED_BY_CAPS_LOCK}, // 0x0E - {EfiKeyC9, 'l', 'L', EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT | EFI_AFFECTED_BY_CAPS_LOCK}, // 0x0F - {EfiKeyB7, 'm', 'M', EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT | EFI_AFFECTED_BY_CAPS_LOCK}, // 0x10 - {EfiKeyB6, 'n', 'N', EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT | EFI_AFFECTED_BY_CAPS_LOCK}, // 0x11 - {EfiKeyD9, 'o', 'O', EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT | EFI_AFFECTED_BY_CAPS_LOCK}, // 0x12 - {EfiKeyD10, 'p', 'P', EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT | EFI_AFFECTED_BY_CAPS_LOCK}, // 0x13 - {EfiKeyD1, 'q', 'Q', EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT | EFI_AFFECTED_BY_CAPS_LOCK}, // 0x14 - {EfiKeyD4, 'r', 'R', EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT | EFI_AFFECTED_BY_CAPS_LOCK}, // 0x15 - {EfiKeyC2, 's', 'S', EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT | EFI_AFFECTED_BY_CAPS_LOCK}, // 0x16 - {EfiKeyD5, 't', 'T', EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT | EFI_AFFECTED_BY_CAPS_LOCK}, // 0x17 - {EfiKeyD7, 'u', 'U', EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT | EFI_AFFECTED_BY_CAPS_LOCK}, // 0x18 - {EfiKeyB4, 'v', 'V', EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT | EFI_AFFECTED_BY_CAPS_LOCK}, // 0x19 - {EfiKeyD2, 'w', 'W', EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT | EFI_AFFECTED_BY_CAPS_LOCK}, // 0x1A - {EfiKeyB2, 'x', 'X', EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT | EFI_AFFECTED_BY_CAPS_LOCK}, // 0x1B - {EfiKeyD6, 'y', 'Y', EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT | EFI_AFFECTED_BY_CAPS_LOCK}, // 0x1C - {EfiKeyB1, 'z', 'Z', EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT | EFI_AFFECTED_BY_CAPS_LOCK}, // 0x1D - {EfiKeyE1, '1', '!', EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT}, // 0x1E - {EfiKeyE2, '2', '@', EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT}, // 0x1F - {EfiKeyE3, '3', '#', EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT}, // 0x20 - {EfiKeyE4, '4', '$', EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT}, // 0x21 - {EfiKeyE5, '5', '%', EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT}, // 0x22 - {EfiKeyE6, '6', '^', EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT}, // 0x23 - {EfiKeyE7, '7', '&', EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT}, // 0x24 - {EfiKeyE8, '8', '*', EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT}, // 0x25 - {EfiKeyE9, '9', '(', EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT}, // 0x26 - {EfiKeyE10, '0', ')', EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT}, // 0x27 - {EfiKeyEnter, 0x0d, 0x0d, EFI_NULL_MODIFIER, 0}, // 0x28 Enter - {EfiKeyEsc, 0x1b, 0x1b, EFI_NULL_MODIFIER, 0}, // 0x29 Esc - {EfiKeyBackSpace, 0x08, 0x08, EFI_NULL_MODIFIER, 0}, // 0x2A Backspace - {EfiKeyTab, 0x09, 0x09, EFI_NULL_MODIFIER, 0}, // 0x2B Tab - {EfiKeySpaceBar, ' ', ' ', EFI_NULL_MODIFIER, 0}, // 0x2C Spacebar - {EfiKeyE11, '-', '_', EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT}, // 0x2D - {EfiKeyE12, '=', '+', EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT}, // 0x2E - {EfiKeyD11, '[', '{', EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT}, // 0x2F - {EfiKeyD12, ']', '}', EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT}, // 0x30 - {EfiKeyD13, '\\', '|', EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT}, // 0x31 - {EfiKeyC12, '\\', '|', EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT}, // 0x32 Keyboard Non-US # and ~ - {EfiKeyC10, ';', ':', EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT}, // 0x33 - {EfiKeyC11, '\'', '"', EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT}, // 0x34 - {EfiKeyE0, '`', '~', EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT}, // 0x35 Keyboard Grave Accent and Tlide - {EfiKeyB8, ',', '<', EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT}, // 0x36 - {EfiKeyB9, '.', '>', EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT}, // 0x37 - {EfiKeyB10, '/', '?', EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT}, // 0x38 - {EfiKeyCapsLock, 0x00, 0x00, EFI_CAPS_LOCK_MODIFIER, 0}, // 0x39 CapsLock - {EfiKeyF1, 0x00, 0x00, EFI_FUNCTION_KEY_ONE_MODIFIER, 0}, // 0x3A - {EfiKeyF2, 0x00, 0x00, EFI_FUNCTION_KEY_TWO_MODIFIER, 0}, // 0x3B - {EfiKeyF3, 0x00, 0x00, EFI_FUNCTION_KEY_THREE_MODIFIER, 0}, // 0x3C - {EfiKeyF4, 0x00, 0x00, EFI_FUNCTION_KEY_FOUR_MODIFIER, 0}, // 0x3D - {EfiKeyF5, 0x00, 0x00, EFI_FUNCTION_KEY_FIVE_MODIFIER, 0}, // 0x3E - {EfiKeyF6, 0x00, 0x00, EFI_FUNCTION_KEY_SIX_MODIFIER, 0}, // 0x3F - {EfiKeyF7, 0x00, 0x00, EFI_FUNCTION_KEY_SEVEN_MODIFIER, 0}, // 0x40 - {EfiKeyF8, 0x00, 0x00, EFI_FUNCTION_KEY_EIGHT_MODIFIER, 0}, // 0x41 - {EfiKeyF9, 0x00, 0x00, EFI_FUNCTION_KEY_NINE_MODIFIER, 0}, // 0x42 - {EfiKeyF10, 0x00, 0x00, EFI_FUNCTION_KEY_TEN_MODIFIER, 0}, // 0x43 - {EfiKeyF11, 0x00, 0x00, EFI_FUNCTION_KEY_ELEVEN_MODIFIER, 0}, // 0x44 F11 - {EfiKeyF12, 0x00, 0x00, EFI_FUNCTION_KEY_TWELVE_MODIFIER, 0}, // 0x45 F12 - {EfiKeyPrint, 0x00, 0x00, EFI_PRINT_MODIFIER, 0}, // 0x46 PrintScreen - {EfiKeySLck, 0x00, 0x00, EFI_SCROLL_LOCK_MODIFIER, 0}, // 0x47 Scroll Lock - {EfiKeyPause, 0x00, 0x00, EFI_PAUSE_MODIFIER, 0}, // 0x48 Pause - {EfiKeyIns, 0x00, 0x00, EFI_INSERT_MODIFIER, 0}, // 0x49 - {EfiKeyHome, 0x00, 0x00, EFI_HOME_MODIFIER, 0}, // 0x4A - {EfiKeyPgUp, 0x00, 0x00, EFI_PAGE_UP_MODIFIER, 0}, // 0x4B - {EfiKeyDel, 0x00, 0x00, EFI_DELETE_MODIFIER, 0}, // 0x4C - {EfiKeyEnd, 0x00, 0x00, EFI_END_MODIFIER, 0}, // 0x4D - {EfiKeyPgDn, 0x00, 0x00, EFI_PAGE_DOWN_MODIFIER, 0}, // 0x4E - {EfiKeyRightArrow, 0x00, 0x00, EFI_RIGHT_ARROW_MODIFIER, 0}, // 0x4F - {EfiKeyLeftArrow, 0x00, 0x00, EFI_LEFT_ARROW_MODIFIER, 0}, // 0x50 - {EfiKeyDownArrow, 0x00, 0x00, EFI_DOWN_ARROW_MODIFIER, 0}, // 0x51 - {EfiKeyUpArrow, 0x00, 0x00, EFI_UP_ARROW_MODIFIER, 0}, // 0x52 - {EfiKeyNLck, 0x00, 0x00, EFI_NUM_LOCK_MODIFIER, 0}, // 0x53 NumLock - {EfiKeySlash, '/', '/', EFI_NULL_MODIFIER, 0}, // 0x54 - {EfiKeyAsterisk, '*', '*', EFI_NULL_MODIFIER, 0}, // 0x55 - {EfiKeyMinus, '-', '-', EFI_NULL_MODIFIER, 0}, // 0x56 - {EfiKeyPlus, '+', '+', EFI_NULL_MODIFIER, 0}, // 0x57 - {EfiKeyEnter, 0x0d, 0x0d, EFI_NULL_MODIFIER, 0}, // 0x58 - {EfiKeyOne, '1', '1', EFI_END_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT | EFI_AFFECTED_BY_NUM_LOCK}, // 0x59 - {EfiKeyTwo, '2', '2', EFI_DOWN_ARROW_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT | EFI_AFFECTED_BY_NUM_LOCK}, // 0x5A - {EfiKeyThree, '3', '3', EFI_PAGE_DOWN_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT | EFI_AFFECTED_BY_NUM_LOCK}, // 0x5B - {EfiKeyFour, '4', '4', EFI_LEFT_ARROW_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT | EFI_AFFECTED_BY_NUM_LOCK}, // 0x5C - {EfiKeyFive, '5', '5', EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT | EFI_AFFECTED_BY_NUM_LOCK}, // 0x5D - {EfiKeySix, '6', '6', EFI_RIGHT_ARROW_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT | EFI_AFFECTED_BY_NUM_LOCK}, // 0x5E - {EfiKeySeven, '7', '7', EFI_HOME_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT | EFI_AFFECTED_BY_NUM_LOCK}, // 0x5F - {EfiKeyEight, '8', '8', EFI_UP_ARROW_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT | EFI_AFFECTED_BY_NUM_LOCK}, // 0x60 - {EfiKeyNine, '9', '9', EFI_PAGE_UP_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT | EFI_AFFECTED_BY_NUM_LOCK}, // 0x61 - {EfiKeyZero, '0', '0', EFI_INSERT_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT | EFI_AFFECTED_BY_NUM_LOCK}, // 0x62 - {EfiKeyPeriod, '.', '.', EFI_DELETE_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT | EFI_AFFECTED_BY_NUM_LOCK}, // 0x63 - {EfiKeyB0, '\\', '|', EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT}, // 0x64 Keyboard Non-US \ and | - {EfiKeyA4, 0x00, 0x00, EFI_MENU_MODIFIER, 0}, // 0x65 Keyboard Application - - {EfiKeyLCtrl, 0, 0, EFI_LEFT_CONTROL_MODIFIER, 0}, // 0xe0 - {EfiKeyLShift, 0, 0, EFI_LEFT_SHIFT_MODIFIER, 0}, // 0xe1 - {EfiKeyLAlt, 0, 0, EFI_LEFT_ALT_MODIFIER, 0}, // 0xe2 - {EfiKeyA0, 0, 0, EFI_LEFT_LOGO_MODIFIER, 0}, // 0xe3 - {EfiKeyRCtrl, 0, 0, EFI_RIGHT_CONTROL_MODIFIER, 0}, // 0xe4 - {EfiKeyRShift, 0, 0, EFI_RIGHT_SHIFT_MODIFIER, 0}, // 0xe5 - {EfiKeyA2, 0, 0, EFI_RIGHT_ALT_MODIFIER, 0}, // 0xe6 - {EfiKeyA3, 0, 0, EFI_RIGHT_LOGO_MODIFIER, 0}, // 0xe7 -}; - -VOID -LoadDefaultKeyboardLayout ( - IN USB_KB_DEV *UsbKeyboardDevice - ) -/*++ - - Routine Description: - Initialize KeyConvertionTable by using default keyboard layout. - - Arguments: - UsbKeyboardDevice The USB_KB_DEV instance. - - Returns: - None. - ---*/ -{ - UINTN Index; - EFI_KEY_DESCRIPTOR *KeyDescriptor; - - // - // Construct KeyConvertionTable by default keyboard layout - // - KeyDescriptor = &UsbKeyboardDevice->KeyConvertionTable[0]; - - for (Index = 0; Index < (USB_KEYCODE_MAX_MAKE + 8); Index++) { - KeyDescriptor->Key = (EFI_KEY) KeyboardLayoutTable[Index][0]; - KeyDescriptor->Unicode = KeyboardLayoutTable[Index][1]; - KeyDescriptor->ShiftedUnicode = KeyboardLayoutTable[Index][2]; - KeyDescriptor->AltGrUnicode = 0; - KeyDescriptor->ShiftedAltGrUnicode = 0; - KeyDescriptor->Modifier = KeyboardLayoutTable[Index][3]; - KeyDescriptor->AffectedAttribute = KeyboardLayoutTable[Index][4]; - - KeyDescriptor++; - } -} - -// -// EFI_KEY to USB Scan Code convertion table -// -STATIC -UINT8 UsbScanCodeConvertionTable[] = { - 0xe0, // EfiKeyLCtrl - 0xe3, // EfiKeyA0 - 0xe2, // EfiKeyLAlt - 0x2c, // EfiKeySpaceBar - 0xe6, // EfiKeyA2 - 0xe7, // EfiKeyA3 - 0x65, // EfiKeyA4 - 0xe4, // EfiKeyRCtrl - 0x50, // EfiKeyLeftArrow - 0x51, // EfiKeyDownArrow - 0x4F, // EfiKeyRightArrow - 0x62, // EfiKeyZero - 0x63, // EfiKeyPeriod - 0x28, // EfiKeyEnter - 0xe1, // EfiKeyLShift - 0x64, // EfiKeyB0 - 0x1D, // EfiKeyB1 - 0x1B, // EfiKeyB2 - 0x06, // EfiKeyB3 - 0x19, // EfiKeyB4 - 0x05, // EfiKeyB5 - 0x11, // EfiKeyB6 - 0x10, // EfiKeyB7 - 0x36, // EfiKeyB8 - 0x37, // EfiKeyB9 - 0x38, // EfiKeyB10 - 0xe5, // EfiKeyRShift - 0x52, // EfiKeyUpArrow - 0x59, // EfiKeyOne - 0x5A, // EfiKeyTwo - 0x5B, // EfiKeyThree - 0x39, // EfiKeyCapsLock - 0x04, // EfiKeyC1 - 0x16, // EfiKeyC2 - 0x07, // EfiKeyC3 - 0x09, // EfiKeyC4 - 0x0A, // EfiKeyC5 - 0x0B, // EfiKeyC6 - 0x0D, // EfiKeyC7 - 0x0E, // EfiKeyC8 - 0x0F, // EfiKeyC9 - 0x33, // EfiKeyC10 - 0x34, // EfiKeyC11 - 0x32, // EfiKeyC12 - 0x5C, // EfiKeyFour - 0x5D, // EfiKeyFive - 0x5E, // EfiKeySix - 0x57, // EfiKeyPlus - 0x2B, // EfiKeyTab - 0x14, // EfiKeyD1 - 0x1A, // EfiKeyD2 - 0x08, // EfiKeyD3 - 0x15, // EfiKeyD4 - 0x17, // EfiKeyD5 - 0x1C, // EfiKeyD6 - 0x18, // EfiKeyD7 - 0x0C, // EfiKeyD8 - 0x12, // EfiKeyD9 - 0x13, // EfiKeyD10 - 0x2F, // EfiKeyD11 - 0x30, // EfiKeyD12 - 0x31, // EfiKeyD13 - 0x4C, // EfiKeyDel - 0x4D, // EfiKeyEnd - 0x4E, // EfiKeyPgDn - 0x5F, // EfiKeySeven - 0x60, // EfiKeyEight - 0x61, // EfiKeyNine - 0x35, // EfiKeyE0 - 0x1E, // EfiKeyE1 - 0x1F, // EfiKeyE2 - 0x20, // EfiKeyE3 - 0x21, // EfiKeyE4 - 0x22, // EfiKeyE5 - 0x23, // EfiKeyE6 - 0x24, // EfiKeyE7 - 0x25, // EfiKeyE8 - 0x26, // EfiKeyE9 - 0x27, // EfiKeyE10 - 0x2D, // EfiKeyE11 - 0x2E, // EfiKeyE12 - 0x2A, // EfiKeyBackSpace - 0x49, // EfiKeyIns - 0x4A, // EfiKeyHome - 0x4B, // EfiKeyPgUp - 0x53, // EfiKeyNLck - 0x54, // EfiKeySlash - 0x55, // EfiKeyAsterisk - 0x56, // EfiKeyMinus - 0x29, // EfiKeyEsc - 0x3A, // EfiKeyF1 - 0x3B, // EfiKeyF2 - 0x3C, // EfiKeyF3 - 0x3D, // EfiKeyF4 - 0x3E, // EfiKeyF5 - 0x3F, // EfiKeyF6 - 0x40, // EfiKeyF7 - 0x41, // EfiKeyF8 - 0x42, // EfiKeyF9 - 0x43, // EfiKeyF10 - 0x44, // EfiKeyF11 - 0x45, // EfiKeyF12 - 0x46, // EfiKeyPrint - 0x47, // EfiKeySLck - 0x48 // EfiKeyPause -}; - -// -// Keyboard Layout Modifier to EFI Scan Code convertion table -// -STATIC -UINT8 EfiScanCodeConvertionTable[] = { - SCAN_NULL, // EFI_NULL_MODIFIER - SCAN_NULL, // EFI_LEFT_CONTROL_MODIFIER - SCAN_NULL, // EFI_RIGHT_CONTROL_MODIFIER - SCAN_NULL, // EFI_LEFT_ALT_MODIFIER - SCAN_NULL, // EFI_RIGHT_ALT_MODIFIER - SCAN_NULL, // EFI_ALT_GR_MODIFIER - SCAN_INSERT, // EFI_INSERT_MODIFIER - SCAN_DELETE, // EFI_DELETE_MODIFIER - SCAN_PAGE_DOWN, // EFI_PAGE_DOWN_MODIFIER - SCAN_PAGE_UP, // EFI_PAGE_UP_MODIFIER - SCAN_HOME, // EFI_HOME_MODIFIER - SCAN_END, // EFI_END_MODIFIER - SCAN_NULL, // EFI_LEFT_SHIFT_MODIFIER - SCAN_NULL, // EFI_RIGHT_SHIFT_MODIFIER - SCAN_NULL, // EFI_CAPS_LOCK_MODIFIER - SCAN_NULL, // EFI_NUM_LOCK_MODIFIER - SCAN_LEFT, // EFI_LEFT_ARROW_MODIFIER - SCAN_RIGHT, // EFI_RIGHT_ARROW_MODIFIER - SCAN_DOWN, // EFI_DOWN_ARROW_MODIFIER - SCAN_UP, // EFI_UP_ARROW_MODIFIER - SCAN_NULL, // EFI_NS_KEY_MODIFIER - SCAN_NULL, // EFI_NS_KEY_DEPENDENCY_MODIFIER - SCAN_F1, // EFI_FUNCTION_KEY_ONE_MODIFIER - SCAN_F2, // EFI_FUNCTION_KEY_TWO_MODIFIER - SCAN_F3, // EFI_FUNCTION_KEY_THREE_MODIFIER - SCAN_F4, // EFI_FUNCTION_KEY_FOUR_MODIFIER - SCAN_F5, // EFI_FUNCTION_KEY_FIVE_MODIFIER - SCAN_F6, // EFI_FUNCTION_KEY_SIX_MODIFIER - SCAN_F7, // EFI_FUNCTION_KEY_SEVEN_MODIFIER - SCAN_F8, // EFI_FUNCTION_KEY_EIGHT_MODIFIER - SCAN_F9, // EFI_FUNCTION_KEY_NINE_MODIFIER - SCAN_F10, // EFI_FUNCTION_KEY_TEN_MODIFIER - SCAN_F11, // EFI_FUNCTION_KEY_ELEVEN_MODIFIER - SCAN_F12, // EFI_FUNCTION_KEY_TWELVE_MODIFIER -}; - -EFI_GUID mKeyboardLayoutEventGuid = EFI_HII_SET_KEYBOARD_LAYOUT_EVENT_GUID; - - -STATIC KB_MODIFIER KB_Mod[8] = { - { MOD_CONTROL_L, 0xe0 }, // 11100000 - { MOD_CONTROL_R, 0xe4 }, // 11100100 - { MOD_SHIFT_L, 0xe1 }, // 11100001 - { MOD_SHIFT_R, 0xe5 }, // 11100101 - { MOD_ALT_L, 0xe2 }, // 11100010 - { MOD_ALT_R, 0xe6 }, // 11100110 - { MOD_WIN_L, 0xe3 }, // 11100011 - { MOD_WIN_R, 0xe7 }, // 11100111 -}; - - - -/** - Uses USB I/O to check whether the device is a USB Keyboard device. - - UsbIo: Points to a USB I/O protocol instance. - - -**/ -BOOLEAN -IsUSBKeyboard ( - IN EFI_USB_IO_PROTOCOL *UsbIo - ) -{ - EFI_STATUS Status; - EFI_USB_INTERFACE_DESCRIPTOR InterfaceDescriptor; - - // - // Get the Default interface descriptor, currently we - // assume it is interface 1 - // - Status = UsbIo->UsbGetInterfaceDescriptor ( - UsbIo, - &InterfaceDescriptor - ); - - if (EFI_ERROR (Status)) { - return FALSE; - } - - if (InterfaceDescriptor.InterfaceClass == CLASS_HID && - InterfaceDescriptor.InterfaceSubClass == SUBCLASS_BOOT && - InterfaceDescriptor.InterfaceProtocol == PROTOCOL_KEYBOARD - ) { - - return TRUE; - } - - return FALSE; -} - - -EFI_HII_KEYBOARD_LAYOUT * -GetCurrentKeyboardLayout ( - VOID - ) -/*++ - - Routine Description: - Get current keyboard layout from HII database. - - Arguments: - None. - - Returns: - Pointer to EFI_HII_KEYBOARD_LAYOUT. - ---*/ -{ - EFI_STATUS Status; - EFI_HII_DATABASE_PROTOCOL *HiiDatabase; - EFI_HII_KEYBOARD_LAYOUT *KeyboardLayout; - UINT16 Length; - - // - // Locate Hii database protocol - // - Status = gBS->LocateProtocol ( - &gEfiHiiDatabaseProtocolGuid, - NULL, - (VOID **) &HiiDatabase - ); - if (EFI_ERROR (Status)) { - return NULL; - } - - // - // Get current keyboard layout from HII database - // - Length = 0; - KeyboardLayout = NULL; - Status = HiiDatabase->GetKeyboardLayout ( - HiiDatabase, - NULL, - &Length, - KeyboardLayout - ); - if (Status == EFI_BUFFER_TOO_SMALL) { - KeyboardLayout = AllocatePool (Length); - ASSERT (KeyboardLayout != NULL); - - Status = HiiDatabase->GetKeyboardLayout ( - HiiDatabase, - NULL, - &Length, - KeyboardLayout - ); - if (EFI_ERROR (Status)) { - gBS->FreePool (KeyboardLayout); - KeyboardLayout = NULL; - } - } - - return KeyboardLayout; -} - -EFI_KEY_DESCRIPTOR * -GetKeyDescriptor ( - IN USB_KB_DEV *UsbKeyboardDevice, - IN UINT8 ScanCode - ) -/*++ - - Routine Description: - Find Key Descriptor in KeyConvertionTable given its scan code. - - Arguments: - UsbKeyboardDevice - The USB_KB_DEV instance. - ScanCode - USB scan code. - - Returns: - The Key descriptor in KeyConvertionTable. - ---*/ -{ - UINT8 Index; - - if (((ScanCode > 0x65) && (ScanCode < 0xe0)) || (ScanCode > 0xe7)) { - return NULL; - } - - if (ScanCode <= 0x65) { - Index = (UINT8) (ScanCode - 4); - } else { - Index = (UINT8) (ScanCode - 0xe0 + USB_KEYCODE_MAX_MAKE); - } - - return &UsbKeyboardDevice->KeyConvertionTable[Index]; -} - -USB_NS_KEY * -FindUsbNsKey ( - IN USB_KB_DEV *UsbKeyboardDevice, - IN EFI_KEY_DESCRIPTOR *KeyDescriptor - ) -/*++ - - Routine Description: - Find Non-Spacing key for given KeyDescriptor. - - Arguments: - UsbKeyboardDevice - The USB_KB_DEV instance. - KeyDescriptor - Key descriptor. - - Returns: - The Non-Spacing key. - ---*/ -{ - LIST_ENTRY *Link; - USB_NS_KEY *UsbNsKey; - - Link = GetFirstNode (&UsbKeyboardDevice->NsKeyList); - while (!IsNull (&UsbKeyboardDevice->NsKeyList, Link)) { - UsbNsKey = USB_NS_KEY_FORM_FROM_LINK (Link); - - if (UsbNsKey->NsKey[0].Key == KeyDescriptor->Key) { - return UsbNsKey; - } - - Link = GetNextNode (&UsbKeyboardDevice->NsKeyList, Link); - } - - return NULL; -} - -EFI_KEY_DESCRIPTOR * -FindPhysicalKey ( - IN USB_NS_KEY *UsbNsKey, - IN EFI_KEY_DESCRIPTOR *KeyDescriptor - ) -/*++ - - Routine Description: - Find physical key definition for a given Key stroke. - - Arguments: - UsbNsKey - The Non-Spacing key information. - KeyDescriptor - The key stroke. - - Returns: - The physical key definition. - ---*/ -{ - UINTN Index; - EFI_KEY_DESCRIPTOR *PhysicalKey; - - PhysicalKey = &UsbNsKey->NsKey[1]; - for (Index = 0; Index < UsbNsKey->KeyCount; Index++) { - if (KeyDescriptor->Key == PhysicalKey->Key) { - return PhysicalKey; - } - - PhysicalKey++; - } - - // - // No children definition matched, return original key - // - return KeyDescriptor; -} - -VOID -EFIAPI -SetKeyboardLayoutEvent ( - EFI_EVENT Event, - VOID *Context - ) -/*++ - - Routine Description: - The notification function for SET_KEYBOARD_LAYOUT_EVENT. - - Arguments: - - Returns: - ---*/ -{ - USB_KB_DEV *UsbKeyboardDevice; - EFI_HII_KEYBOARD_LAYOUT *KeyboardLayout; - EFI_KEY_DESCRIPTOR TempKey; - EFI_KEY_DESCRIPTOR *KeyDescriptor; - EFI_KEY_DESCRIPTOR *TableEntry; - EFI_KEY_DESCRIPTOR *NsKey; - USB_NS_KEY *UsbNsKey; - UINTN Index; - UINTN Index2; - UINTN KeyCount; - UINT8 ScanCode; - - UsbKeyboardDevice = (USB_KB_DEV *) Context; - - // - // Try to get current Keyboard Layout from HII database - // - KeyboardLayout = GetCurrentKeyboardLayout (); - if (KeyboardLayout == NULL) { - return; - } - - // - // Allocate resource for KeyConvertionTable - // - ReleaseKeyboardLayoutResources (UsbKeyboardDevice); - UsbKeyboardDevice->KeyConvertionTable = AllocateZeroPool ((USB_KEYCODE_MAX_MAKE + 8) * sizeof (EFI_KEY_DESCRIPTOR)); - ASSERT (UsbKeyboardDevice->KeyConvertionTable != NULL); - - KeyDescriptor = (EFI_KEY_DESCRIPTOR *) (((UINT8 *) KeyboardLayout) + sizeof (EFI_HII_KEYBOARD_LAYOUT)); - for (Index = 0; Index < KeyboardLayout->DescriptorCount; Index++) { - // - // Copy from HII keyboard layout package binary for alignment - // - CopyMem (&TempKey, KeyDescriptor, sizeof (EFI_KEY_DESCRIPTOR)); - - // - // Fill the key into KeyConvertionTable (which use USB Scan Code as index) - // - ScanCode = UsbScanCodeConvertionTable [(UINT8) (TempKey.Key)]; - TableEntry = GetKeyDescriptor (UsbKeyboardDevice, ScanCode); - CopyMem (TableEntry, KeyDescriptor, sizeof (EFI_KEY_DESCRIPTOR)); - - if (TempKey.Modifier == EFI_NS_KEY_MODIFIER) { - // - // Non-spacing key - // - UsbNsKey = AllocatePool (sizeof (USB_NS_KEY)); - ASSERT (UsbNsKey != NULL); - - // - // Search for sequential children physical key definitions - // - KeyCount = 0; - NsKey = KeyDescriptor + 1; - for (Index2 = Index + 1; Index2 < KeyboardLayout->DescriptorCount; Index2++) { - CopyMem (&TempKey, NsKey, sizeof (EFI_KEY_DESCRIPTOR)); - if (TempKey.Modifier & EFI_NS_KEY_DEPENDENCY_MODIFIER) { - KeyCount++; - } else { - break; - } - NsKey++; - } - - UsbNsKey->Signature = USB_NS_KEY_SIGNATURE; - UsbNsKey->KeyCount = KeyCount; - UsbNsKey->NsKey = AllocateCopyPool ( - (KeyCount + 1) * sizeof (EFI_KEY_DESCRIPTOR), - KeyDescriptor - ); - InsertTailList (&UsbKeyboardDevice->NsKeyList, &UsbNsKey->Link); - - // - // Skip over the child physical keys - // - Index += KeyCount; - KeyDescriptor += KeyCount; - } - - KeyDescriptor++; - } - - // - // There are two EfiKeyEnter, duplicate its Key Descriptor - // - TableEntry = GetKeyDescriptor (UsbKeyboardDevice, 0x58); - KeyDescriptor = GetKeyDescriptor (UsbKeyboardDevice, 0x28); - CopyMem (TableEntry, KeyDescriptor, sizeof (EFI_KEY_DESCRIPTOR)); - - gBS->FreePool (KeyboardLayout); -} - -VOID -ReleaseKeyboardLayoutResources ( - IN USB_KB_DEV *UsbKeyboardDevice - ) -/*++ - - Routine Description: - Destroy resources for Keyboard layout. - - Arguments: - UsbKeyboardDevice - The USB_KB_DEV instance. - - Returns: - None. - ---*/ -{ - USB_NS_KEY *UsbNsKey; - LIST_ENTRY *Link; - - SafeFreePool (UsbKeyboardDevice->KeyConvertionTable); - UsbKeyboardDevice->KeyConvertionTable = NULL; - - while (!IsListEmpty (&UsbKeyboardDevice->NsKeyList)) { - Link = GetFirstNode (&UsbKeyboardDevice->NsKeyList); - UsbNsKey = USB_NS_KEY_FORM_FROM_LINK (Link); - RemoveEntryList (&UsbNsKey->Link); - - gBS->FreePool (UsbNsKey->NsKey); - gBS->FreePool (UsbNsKey); - } -} - -EFI_STATUS -InitKeyboardLayout ( - IN USB_KB_DEV *UsbKeyboardDevice - ) -/*++ - - Routine Description: - Initialize USB Keyboard layout. - - Arguments: - UsbKeyboardDevice The USB_KB_DEV instance. - - Returns: - EFI_SUCCESS - Success - Other - Keyboard layout initial failed. ---*/ -{ - EFI_HII_KEYBOARD_LAYOUT *KeyboardLayout; - EFI_STATUS Status; - - UsbKeyboardDevice->KeyConvertionTable = AllocateZeroPool ((USB_KEYCODE_MAX_MAKE + 8) * sizeof (EFI_KEY_DESCRIPTOR)); - ASSERT (UsbKeyboardDevice->KeyConvertionTable != NULL); - - InitializeListHead (&UsbKeyboardDevice->NsKeyList); - UsbKeyboardDevice->CurrentNsKey = NULL; - UsbKeyboardDevice->KeyboardLayoutEvent = NULL; - - // - // Register SET_KEYBOARD_LAYOUT_EVENT notification - // - Status = gBS->CreateEventEx ( - EFI_EVENT_NOTIFY_SIGNAL, - TPL_NOTIFY, - SetKeyboardLayoutEvent, - UsbKeyboardDevice, - &mKeyboardLayoutEventGuid, - &UsbKeyboardDevice->KeyboardLayoutEvent - ); - if (EFI_ERROR (Status)) { - return Status; - } - - // - // Try to get current keyboard layout from HII database - // - KeyboardLayout = GetCurrentKeyboardLayout (); - if (KeyboardLayout != NULL) { - // - // Force to initialize the keyboard layout - // - gBS->SignalEvent (UsbKeyboardDevice->KeyboardLayoutEvent); - } else { - if (FeaturePcdGet (PcdDisableDefaultKeyboardLayoutInUsbKbDriver)) { - return EFI_NOT_READY; - } else { - - // - // Fail to get keyboard layout from HII database, - // use default keyboard layout - // - LoadDefaultKeyboardLayout (UsbKeyboardDevice); - } - } - - return EFI_SUCCESS; -} - - -/** - Initialize USB Keyboard device and all private data structures. - - UsbKeyboardDevice The USB_KB_DEV instance. - - @retval EFI_SUCCESS Success - @retval EFI_DEVICE_ERROR Hardware Error - -**/ -EFI_STATUS -InitUSBKeyboard ( - IN USB_KB_DEV *UsbKeyboardDevice - ) -{ - UINT8 ConfigValue; - UINT8 Protocol; - UINT8 ReportId; - UINT8 Duration; - EFI_STATUS Status; - UINT32 TransferResult; - - KbdReportStatusCode ( - UsbKeyboardDevice->DevicePath, - EFI_PROGRESS_CODE, - PcdGet32 (PcdStatusCodeValueKeyboardSelfTest) - ); - - InitUSBKeyBuffer (&(UsbKeyboardDevice->KeyboardBuffer)); - - // - // default configurations - // - ConfigValue = 0x01; - - // - // Uses default configuration to configure the USB Keyboard device. - // - Status = UsbSetConfiguration ( - UsbKeyboardDevice->UsbIo, - (UINT16) ConfigValue, - &TransferResult - ); - if (EFI_ERROR (Status)) { - // - // If configuration could not be set here, it means - // the keyboard interface has some errors and could - // not be initialized - // - KbdReportStatusCode ( - UsbKeyboardDevice->DevicePath, - EFI_ERROR_CODE | EFI_ERROR_MINOR, - PcdGet32 (PcdStatusCodeValueKeyboardInterfaceError) - ); - - return EFI_DEVICE_ERROR; - } - - UsbGetProtocolRequest ( - UsbKeyboardDevice->UsbIo, - UsbKeyboardDevice->InterfaceDescriptor.InterfaceNumber, - &Protocol - ); - // - // Sets boot protocol for the USB Keyboard. - // This driver only supports boot protocol. - // !!BugBug: How about the device that does not support boot protocol? - // - if (Protocol != BOOT_PROTOCOL) { - UsbSetProtocolRequest ( - UsbKeyboardDevice->UsbIo, - UsbKeyboardDevice->InterfaceDescriptor.InterfaceNumber, - BOOT_PROTOCOL - ); - } - // - // the duration is indefinite, so the endpoint will inhibit reporting forever, - // and only reporting when a change is detected in the report data. - // - - // - // idle value for all report ID - // - ReportId = 0; - // - // idle forever until there is a key pressed and released. - // - Duration = 0; - UsbSetIdleRequest ( - UsbKeyboardDevice->UsbIo, - UsbKeyboardDevice->InterfaceDescriptor.InterfaceNumber, - ReportId, - Duration - ); - - UsbKeyboardDevice->CtrlOn = 0; - UsbKeyboardDevice->AltOn = 0; - UsbKeyboardDevice->ShiftOn = 0; - UsbKeyboardDevice->NumLockOn = 0; - UsbKeyboardDevice->CapsOn = 0; - UsbKeyboardDevice->ScrollOn = 0; - - UsbKeyboardDevice->LeftCtrlOn = 0; - UsbKeyboardDevice->LeftAltOn = 0; - UsbKeyboardDevice->LeftShiftOn = 0; - UsbKeyboardDevice->LeftLogoOn = 0; - UsbKeyboardDevice->RightCtrlOn = 0; - UsbKeyboardDevice->RightAltOn = 0; - UsbKeyboardDevice->RightShiftOn = 0; - UsbKeyboardDevice->RightLogoOn = 0; - UsbKeyboardDevice->MenuKeyOn = 0; - UsbKeyboardDevice->SysReqOn = 0; - - UsbKeyboardDevice->AltGrOn = 0; - - UsbKeyboardDevice->CurrentNsKey = NULL; - - // - // Sync the initial state of lights - // - SetKeyLED (UsbKeyboardDevice); - - ZeroMem (UsbKeyboardDevice->LastKeyCodeArray, sizeof (UINT8) * 8); - - // - // Set a timer for repeat keys' generation. - // - if (UsbKeyboardDevice->RepeatTimer) { - gBS->CloseEvent (UsbKeyboardDevice->RepeatTimer); - UsbKeyboardDevice->RepeatTimer = 0; - } - - Status = gBS->CreateEvent ( - EVT_TIMER | EVT_NOTIFY_SIGNAL, - TPL_NOTIFY, - USBKeyboardRepeatHandler, - UsbKeyboardDevice, - &UsbKeyboardDevice->RepeatTimer - ); - - if (UsbKeyboardDevice->DelayedRecoveryEvent) { - gBS->CloseEvent (UsbKeyboardDevice->DelayedRecoveryEvent); - UsbKeyboardDevice->DelayedRecoveryEvent = 0; - } - - Status = gBS->CreateEvent ( - EVT_TIMER | EVT_NOTIFY_SIGNAL, - TPL_NOTIFY, - USBKeyboardRecoveryHandler, - UsbKeyboardDevice, - &UsbKeyboardDevice->DelayedRecoveryEvent - ); - - return EFI_SUCCESS; -} - - -/** - Handler function for USB Keyboard's asynchronous interrupt transfer. - - Data A pointer to a buffer that is filled with key data which is - retrieved via asynchronous interrupt transfer. - DataLength Indicates the size of the data buffer. - Context Pointing to USB_KB_DEV instance. - Result Indicates the result of the asynchronous interrupt transfer. - - @retval EFI_SUCCESS Success - @retval EFI_DEVICE_ERROR Hardware Error - -**/ -EFI_STATUS -EFIAPI -KeyboardHandler ( - IN VOID *Data, - IN UINTN DataLength, - IN VOID *Context, - IN UINT32 Result - ) -{ - USB_KB_DEV *UsbKeyboardDevice; - EFI_USB_IO_PROTOCOL *UsbIo; - UINT8 *CurKeyCodeBuffer; - UINT8 *OldKeyCodeBuffer; - UINT8 CurModifierMap; - UINT8 OldModifierMap; - UINT8 Index; - UINT8 Index2; - BOOLEAN Down; - BOOLEAN KeyRelease; - BOOLEAN KeyPress; - UINT8 SavedTail; - USB_KEY UsbKey; - UINT8 NewRepeatKey; - UINT32 UsbStatus; - EFI_KEY_DESCRIPTOR *KeyDescriptor; - - ASSERT (Context); - - NewRepeatKey = 0; - UsbKeyboardDevice = (USB_KB_DEV *) Context; - UsbIo = UsbKeyboardDevice->UsbIo; - - // - // Analyzes the Result and performs corresponding action. - // - if (Result != EFI_USB_NOERROR) { - // - // Some errors happen during the process - // - KbdReportStatusCode ( - UsbKeyboardDevice->DevicePath, - EFI_ERROR_CODE | EFI_ERROR_MINOR, - PcdGet32 (PcdStatusCodeValueKeyboardInputError) - ); - - // - // stop the repeat key generation if any - // - UsbKeyboardDevice->RepeatKey = 0; - - gBS->SetTimer ( - UsbKeyboardDevice->RepeatTimer, - TimerCancel, - USBKBD_REPEAT_RATE - ); - - if ((Result & EFI_USB_ERR_STALL) == EFI_USB_ERR_STALL) { - UsbClearEndpointHalt ( - UsbIo, - UsbKeyboardDevice->IntEndpointDescriptor.EndpointAddress, - &UsbStatus - ); - } - - // - // Delete & Submit this interrupt again - // - - UsbIo->UsbAsyncInterruptTransfer ( - UsbIo, - UsbKeyboardDevice->IntEndpointDescriptor.EndpointAddress, - FALSE, - 0, - 0, - NULL, - NULL - ); - - gBS->SetTimer ( - UsbKeyboardDevice->DelayedRecoveryEvent, - TimerRelative, - EFI_USB_INTERRUPT_DELAY - ); - - return EFI_DEVICE_ERROR; - } - - if (DataLength == 0 || Data == NULL) { - return EFI_SUCCESS; - } - - CurKeyCodeBuffer = (UINT8 *) Data; - OldKeyCodeBuffer = UsbKeyboardDevice->LastKeyCodeArray; - - // - // checks for new key stroke. - // if no new key got, return immediately. - // - for (Index = 0; Index < 8; Index++) { - if (OldKeyCodeBuffer[Index] != CurKeyCodeBuffer[Index]) { - break; - } - } - - if (Index == 8) { - return EFI_SUCCESS; - } - - // - // Parse the modifier key - // - CurModifierMap = CurKeyCodeBuffer[0]; - OldModifierMap = OldKeyCodeBuffer[0]; - - // - // handle modifier key's pressing or releasing situation. - // - for (Index = 0; Index < 8; Index++) { - - if ((CurModifierMap & KB_Mod[Index].Mask) != (OldModifierMap & KB_Mod[Index].Mask)) { - // - // if current modifier key is up, then - // CurModifierMap & KB_Mod[Index].Mask = 0; - // otherwize it is a non-zero value. - // Inserts the pressed modifier key into key buffer. - // - Down = (UINT8) (CurModifierMap & KB_Mod[Index].Mask); - InsertKeyCode (&(UsbKeyboardDevice->KeyboardBuffer), KB_Mod[Index].Key, Down); - } - } - - // - // handle normal key's releasing situation - // - KeyRelease = FALSE; - for (Index = 2; Index < 8; Index++) { - - if (!USBKBD_VALID_KEYCODE (OldKeyCodeBuffer[Index])) { - continue; - } - - KeyRelease = TRUE; - for (Index2 = 2; Index2 < 8; Index2++) { - - if (!USBKBD_VALID_KEYCODE (CurKeyCodeBuffer[Index2])) { - continue; - } - - if (OldKeyCodeBuffer[Index] == CurKeyCodeBuffer[Index2]) { - KeyRelease = FALSE; - break; - } - } - - if (KeyRelease) { - InsertKeyCode ( - &(UsbKeyboardDevice->KeyboardBuffer), - OldKeyCodeBuffer[Index], - 0 - ); - // - // the original reapeat key is released. - // - if (OldKeyCodeBuffer[Index] == UsbKeyboardDevice->RepeatKey) { - UsbKeyboardDevice->RepeatKey = 0; - } - } - } - - // - // original repeat key is released, cancel the repeat timer - // - if (UsbKeyboardDevice->RepeatKey == 0) { - gBS->SetTimer ( - UsbKeyboardDevice->RepeatTimer, - TimerCancel, - USBKBD_REPEAT_RATE - ); - } - - // - // handle normal key's pressing situation - // - KeyPress = FALSE; - for (Index = 2; Index < 8; Index++) { - - if (!USBKBD_VALID_KEYCODE (CurKeyCodeBuffer[Index])) { - continue; - } - - KeyPress = TRUE; - for (Index2 = 2; Index2 < 8; Index2++) { - - if (!USBKBD_VALID_KEYCODE (OldKeyCodeBuffer[Index2])) { - continue; - } - - if (CurKeyCodeBuffer[Index] == OldKeyCodeBuffer[Index2]) { - KeyPress = FALSE; - break; - } - } - - if (KeyPress) { - InsertKeyCode (&(UsbKeyboardDevice->KeyboardBuffer), CurKeyCodeBuffer[Index], 1); - // - // NumLock pressed or CapsLock pressed - // - KeyDescriptor = GetKeyDescriptor (UsbKeyboardDevice, CurKeyCodeBuffer[Index]); - if (KeyDescriptor->Modifier == EFI_NUM_LOCK_MODIFIER || KeyDescriptor->Modifier == EFI_CAPS_LOCK_MODIFIER) { - UsbKeyboardDevice->RepeatKey = 0; - } else { - NewRepeatKey = CurKeyCodeBuffer[Index]; - // - // do not repeat the original repeated key - // - UsbKeyboardDevice->RepeatKey = 0; - } - } - } - - // - // Update LastKeycodeArray[] buffer in the - // Usb Keyboard Device data structure. - // - for (Index = 0; Index < 8; Index++) { - UsbKeyboardDevice->LastKeyCodeArray[Index] = CurKeyCodeBuffer[Index]; - } - - // - // pre-process KeyboardBuffer, pop out the ctrl,alt,del key in sequence - // and judge whether it will invoke reset event. - // - SavedTail = UsbKeyboardDevice->KeyboardBuffer.bTail; - Index = UsbKeyboardDevice->KeyboardBuffer.bHead; - while (Index != SavedTail) { - RemoveKeyCode (&(UsbKeyboardDevice->KeyboardBuffer), &UsbKey); - - KeyDescriptor = GetKeyDescriptor (UsbKeyboardDevice, UsbKey.KeyCode); - - switch (KeyDescriptor->Modifier) { - - case EFI_LEFT_CONTROL_MODIFIER: - case EFI_RIGHT_CONTROL_MODIFIER: - if (UsbKey.Down) { - UsbKeyboardDevice->CtrlOn = 1; - } else { - UsbKeyboardDevice->CtrlOn = 0; - } - break; - - case EFI_LEFT_ALT_MODIFIER: - case EFI_RIGHT_ALT_MODIFIER: - if (UsbKey.Down) { - UsbKeyboardDevice->AltOn = 1; - } else { - UsbKeyboardDevice->AltOn = 0; - } - break; - - case EFI_ALT_GR_MODIFIER: - if (UsbKey.Down) { - UsbKeyboardDevice->AltGrOn = 1; - } else { - UsbKeyboardDevice->AltGrOn = 0; - } - break; - - // - // Del Key Code - // - case EFI_DELETE_MODIFIER: - if (UsbKey.Down) { - if (UsbKeyboardDevice->CtrlOn && UsbKeyboardDevice->AltOn) { - gRT->ResetSystem (EfiResetWarm, EFI_SUCCESS, 0, NULL); - } - } - break; - - default: - break; - } - - // - // insert the key back to the buffer. - // so the key sequence will not be destroyed. - // - InsertKeyCode ( - &(UsbKeyboardDevice->KeyboardBuffer), - UsbKey.KeyCode, - UsbKey.Down - ); - Index = UsbKeyboardDevice->KeyboardBuffer.bHead; - - } - // - // If have new key pressed, update the RepeatKey value, and set the - // timer to repeate delay timer - // - if (NewRepeatKey != 0) { - // - // sets trigger time to "Repeat Delay Time", - // to trigger the repeat timer when the key is hold long - // enough time. - // - gBS->SetTimer ( - UsbKeyboardDevice->RepeatTimer, - TimerRelative, - USBKBD_REPEAT_DELAY - ); - UsbKeyboardDevice->RepeatKey = NewRepeatKey; - } - - return EFI_SUCCESS; -} - - -/** - Retrieves a key character after parsing the raw data in keyboard buffer. - - UsbKeyboardDevice The USB_KB_DEV instance. - KeyChar Points to the Key character after key parsing. - - @retval EFI_SUCCESS Success - @retval EFI_NOT_READY Device is not ready - -**/ -EFI_STATUS -USBParseKey ( - IN OUT USB_KB_DEV *UsbKeyboardDevice, - OUT UINT8 *KeyChar - ) -{ - USB_KEY UsbKey; - EFI_KEY_DESCRIPTOR *KeyDescriptor; - - *KeyChar = 0; - - while (!IsUSBKeyboardBufferEmpty (&UsbKeyboardDevice->KeyboardBuffer)) { - // - // pops one raw data off. - // - RemoveKeyCode (&(UsbKeyboardDevice->KeyboardBuffer), &UsbKey); - - KeyDescriptor = GetKeyDescriptor (UsbKeyboardDevice, UsbKey.KeyCode); - if (!UsbKey.Down) { - switch (KeyDescriptor->Modifier) { - - // - // CTRL release - // - case EFI_LEFT_CONTROL_MODIFIER: - UsbKeyboardDevice->LeftCtrlOn = 0; - UsbKeyboardDevice->CtrlOn = 0; - break; - case EFI_RIGHT_CONTROL_MODIFIER: - UsbKeyboardDevice->RightCtrlOn = 0; - UsbKeyboardDevice->CtrlOn = 0; - break; - - // - // Shift release - // - case EFI_LEFT_SHIFT_MODIFIER: - UsbKeyboardDevice->LeftShiftOn = 0; - UsbKeyboardDevice->ShiftOn = 0; - break; - case EFI_RIGHT_SHIFT_MODIFIER: - UsbKeyboardDevice->RightShiftOn = 0; - UsbKeyboardDevice->ShiftOn = 0; - break; - - // - // Alt release - // - case EFI_LEFT_ALT_MODIFIER: - UsbKeyboardDevice->LeftAltOn = 0; - UsbKeyboardDevice->AltOn = 0; - break; - case EFI_RIGHT_ALT_MODIFIER: - UsbKeyboardDevice->RightAltOn = 0; - UsbKeyboardDevice->AltOn = 0; - break; - - // - // Left Logo release - // - case EFI_LEFT_LOGO_MODIFIER: - UsbKeyboardDevice->LeftLogoOn = 0; - break; - - // - // Right Logo release - // - case EFI_RIGHT_LOGO_MODIFIER: - UsbKeyboardDevice->RightLogoOn = 0; - break; - - // - // Menu key release - // - case EFI_MENU_MODIFIER: - UsbKeyboardDevice->MenuKeyOn = 0; - break; - - // - // SysReq release - // - case EFI_PRINT_MODIFIER: - case EFI_SYS_REQUEST_MODIFIER: - UsbKeyboardDevice->SysReqOn = 0; - break; - - // - // AltGr release - // - case EFI_ALT_GR_MODIFIER: - UsbKeyboardDevice->AltGrOn = 0; - break; - - default: - break; - } - - continue; - } - - // - // Analyzes key pressing situation - // - switch (KeyDescriptor->Modifier) { - - // - // CTRL press - // - case EFI_LEFT_CONTROL_MODIFIER: - UsbKeyboardDevice->LeftCtrlOn = 1; - UsbKeyboardDevice->CtrlOn = 1; - continue; - break; - case EFI_RIGHT_CONTROL_MODIFIER: - UsbKeyboardDevice->RightCtrlOn = 1; - UsbKeyboardDevice->CtrlOn = 1; - continue; - break; - - // - // Shift press - // - case EFI_LEFT_SHIFT_MODIFIER: - UsbKeyboardDevice->LeftShiftOn = 1; - UsbKeyboardDevice->ShiftOn = 1; - continue; - break; - case EFI_RIGHT_SHIFT_MODIFIER: - UsbKeyboardDevice->RightShiftOn = 1; - UsbKeyboardDevice->ShiftOn = 1; - continue; - break; - - // - // Alt press - // - case EFI_LEFT_ALT_MODIFIER: - UsbKeyboardDevice->LeftAltOn = 1; - UsbKeyboardDevice->AltOn = 1; - continue; - break; - case EFI_RIGHT_ALT_MODIFIER: - UsbKeyboardDevice->RightAltOn = 1; - UsbKeyboardDevice->AltOn = 1; - continue; - break; - - // - // Left Logo press - // - case EFI_LEFT_LOGO_MODIFIER: - UsbKeyboardDevice->LeftLogoOn = 1; - break; - - // - // Right Logo press - // - case EFI_RIGHT_LOGO_MODIFIER: - UsbKeyboardDevice->RightLogoOn = 1; - break; - - // - // Menu key press - // - case EFI_MENU_MODIFIER: - UsbKeyboardDevice->MenuKeyOn = 1; - break; - - // - // SysReq press - // - case EFI_PRINT_MODIFIER: - case EFI_SYS_REQUEST_MODIFIER: - UsbKeyboardDevice->SysReqOn = 1; - continue; - break; - - // - // AltGr press - // - case EFI_ALT_GR_MODIFIER: - UsbKeyboardDevice->AltGrOn = 1; - break; - - case EFI_NUM_LOCK_MODIFIER: - UsbKeyboardDevice->NumLockOn ^= 1; - // - // Turn on the NumLock light on KB - // - SetKeyLED (UsbKeyboardDevice); - continue; - break; - - case EFI_CAPS_LOCK_MODIFIER: - UsbKeyboardDevice->CapsOn ^= 1; - // - // Turn on the CapsLock light on KB - // - SetKeyLED (UsbKeyboardDevice); - continue; - break; - - case EFI_SCROLL_LOCK_MODIFIER: - UsbKeyboardDevice->ScrollOn ^= 1; - // - // Turn on the ScrollLock light on KB - // - SetKeyLED (UsbKeyboardDevice); - continue; - break; - - // - // F11,F12,PrintScreen,Pause/Break - // could not be retrieved via SimpleTxtInEx protocol - // - case EFI_FUNCTION_KEY_ELEVEN_MODIFIER: - case EFI_FUNCTION_KEY_TWELVE_MODIFIER: - case EFI_PAUSE_MODIFIER: - case EFI_BREAK_MODIFIER: - // - // fall through - // - continue; - break; - - default: - break; - } - - // - // When encountered Del Key... - // - if (KeyDescriptor->Modifier == EFI_DELETE_MODIFIER) { - if (UsbKeyboardDevice->CtrlOn && UsbKeyboardDevice->AltOn) { - gRT->ResetSystem (EfiResetWarm, EFI_SUCCESS, 0, NULL); - } - } - - *KeyChar = UsbKey.KeyCode; - return EFI_SUCCESS; - } - - return EFI_NOT_READY; -} - - - -/** - Converts USB Keyboard code to EFI Scan Code. - - UsbKeyboardDevice The USB_KB_DEV instance. - KeyChar Indicates the key code that will be interpreted. - Key A pointer to a buffer that is filled in with - the keystroke information for the key that - was pressed. - - @retval EFI_NOT_READY Device is not ready - @retval EFI_SUCCESS Success - -**/ -EFI_STATUS -USBKeyCodeToEFIScanCode ( - IN USB_KB_DEV *UsbKeyboardDevice, - IN UINT8 KeyChar, - OUT EFI_INPUT_KEY *Key - ) -{ - UINT8 Index; - EFI_KEY_DESCRIPTOR *KeyDescriptor; - - if (!USBKBD_VALID_KEYCODE (KeyChar)) { - return EFI_NOT_READY; - } - - // - // valid USB Key Code starts from 4 - // - Index = (UINT8) (KeyChar - 4); - - if (Index >= USB_KEYCODE_MAX_MAKE) { - return EFI_NOT_READY; - } - - KeyDescriptor = GetKeyDescriptor (UsbKeyboardDevice, KeyChar); - - // - // Check for Non-spacing key - // - if (KeyDescriptor->Modifier == EFI_NS_KEY_MODIFIER) { - UsbKeyboardDevice->CurrentNsKey = FindUsbNsKey (UsbKeyboardDevice, KeyDescriptor); - return EFI_NOT_READY; - } - - // - // Check whether this keystroke follows a Non-spacing key - // - if (UsbKeyboardDevice->CurrentNsKey != NULL) { - KeyDescriptor = FindPhysicalKey (UsbKeyboardDevice->CurrentNsKey, KeyDescriptor); - UsbKeyboardDevice->CurrentNsKey = NULL; - } - - Key->ScanCode = EfiScanCodeConvertionTable[KeyDescriptor->Modifier]; - Key->UnicodeChar = KeyDescriptor->Unicode; - - if (KeyDescriptor->AffectedAttribute & EFI_AFFECTED_BY_STANDARD_SHIFT) { - if (UsbKeyboardDevice->ShiftOn) { - Key->UnicodeChar = KeyDescriptor->ShiftedUnicode; - - // - // Need not return associated shift state if a class of printable characters that - // are normally adjusted by shift modifiers. e.g. Shift Key + 'f' key = 'F' - // - if (KeyDescriptor->AffectedAttribute & EFI_AFFECTED_BY_CAPS_LOCK) { - UsbKeyboardDevice->LeftShiftOn = 0; - UsbKeyboardDevice->RightShiftOn = 0; - } - - if (UsbKeyboardDevice->AltGrOn) { - Key->UnicodeChar = KeyDescriptor->ShiftedAltGrUnicode; - } - } else { - // - // Shift off - // - Key->UnicodeChar = KeyDescriptor->Unicode; - - if (UsbKeyboardDevice->AltGrOn) { - Key->UnicodeChar = KeyDescriptor->AltGrUnicode; - } - } - } - - if (KeyDescriptor->AffectedAttribute & EFI_AFFECTED_BY_CAPS_LOCK) { - if (UsbKeyboardDevice->CapsOn) { - - if (Key->UnicodeChar == KeyDescriptor->Unicode) { - - Key->UnicodeChar = KeyDescriptor->ShiftedUnicode; - - } else if (Key->UnicodeChar == KeyDescriptor->ShiftedUnicode) { - - Key->UnicodeChar = KeyDescriptor->Unicode; - - } - } - } - - // - // Translate the CTRL-Alpha characters to their corresponding control value (ctrl-a = 0x0001 through ctrl-Z = 0x001A) - // - if (UsbKeyboardDevice->CtrlOn) { - if (Key->UnicodeChar >= 'a' && Key->UnicodeChar <= 'z') { - Key->UnicodeChar = (UINT8) (Key->UnicodeChar - 'a' + 1); - } else if (Key->UnicodeChar >= 'A' && Key->UnicodeChar <= 'Z') { - Key->UnicodeChar = (UINT8) (Key->UnicodeChar - 'A' + 1); - } - } - - if (KeyDescriptor->AffectedAttribute & EFI_AFFECTED_BY_NUM_LOCK) { - - if (UsbKeyboardDevice->NumLockOn && !UsbKeyboardDevice->ShiftOn) { - - Key->ScanCode = SCAN_NULL; - - } else { - Key->UnicodeChar = 0x00; - } - } - - // - // Translate Unicode 0x1B (ESC) to EFI Scan Code - // - if (Key->UnicodeChar == 0x1B && Key->ScanCode == SCAN_NULL) { - Key->ScanCode = SCAN_ESC; - Key->UnicodeChar = 0x00; - } - - if (Key->UnicodeChar == 0 && Key->ScanCode == SCAN_NULL) { - return EFI_NOT_READY; - } - - - // - // Save Shift/Toggle state - // - if (UsbKeyboardDevice->LeftCtrlOn == 1) { - UsbKeyboardDevice->KeyState.KeyShiftState |= EFI_LEFT_CONTROL_PRESSED; - } - if (UsbKeyboardDevice->RightCtrlOn == 1) { - UsbKeyboardDevice->KeyState.KeyShiftState |= EFI_RIGHT_CONTROL_PRESSED; - } - if (UsbKeyboardDevice->LeftAltOn == 1) { - UsbKeyboardDevice->KeyState.KeyShiftState |= EFI_LEFT_ALT_PRESSED; - } - if (UsbKeyboardDevice->RightAltOn == 1) { - UsbKeyboardDevice->KeyState.KeyShiftState |= EFI_RIGHT_ALT_PRESSED; - } - if (UsbKeyboardDevice->LeftShiftOn == 1) { - UsbKeyboardDevice->KeyState.KeyShiftState |= EFI_LEFT_SHIFT_PRESSED; - } - if (UsbKeyboardDevice->RightShiftOn == 1) { - UsbKeyboardDevice->KeyState.KeyShiftState |= EFI_RIGHT_SHIFT_PRESSED; - } - if (UsbKeyboardDevice->LeftLogoOn == 1) { - UsbKeyboardDevice->KeyState.KeyShiftState |= EFI_LEFT_LOGO_PRESSED; - } - if (UsbKeyboardDevice->RightLogoOn == 1) { - UsbKeyboardDevice->KeyState.KeyShiftState |= EFI_RIGHT_LOGO_PRESSED; - } - if (UsbKeyboardDevice->MenuKeyOn == 1) { - UsbKeyboardDevice->KeyState.KeyShiftState |= EFI_MENU_KEY_PRESSED; - } - if (UsbKeyboardDevice->SysReqOn == 1) { - UsbKeyboardDevice->KeyState.KeyShiftState |= EFI_SYS_REQ_PRESSED; - } - - if (UsbKeyboardDevice->ScrollOn == 1) { - UsbKeyboardDevice->KeyState.KeyToggleState |= EFI_SCROLL_LOCK_ACTIVE; - } - if (UsbKeyboardDevice->NumLockOn == 1) { - UsbKeyboardDevice->KeyState.KeyToggleState |= EFI_NUM_LOCK_ACTIVE; - } - if (UsbKeyboardDevice->CapsOn == 1) { - UsbKeyboardDevice->KeyState.KeyToggleState |= EFI_CAPS_LOCK_ACTIVE; - } - - return EFI_SUCCESS; - -} - - - -/** - Resets USB Keyboard Buffer. - - @param KeyboardBuffer Points to the USB Keyboard Buffer. - - @retval EFI_SUCCESS Success - -**/ -EFI_STATUS -InitUSBKeyBuffer ( - IN OUT USB_KB_BUFFER *KeyboardBuffer - ) -{ - ZeroMem (KeyboardBuffer, sizeof (USB_KB_BUFFER)); - - KeyboardBuffer->bHead = KeyboardBuffer->bTail; - - return EFI_SUCCESS; -} - - -/** - Check whether USB Keyboard buffer is empty. - - @param KeyboardBuffer USB Keyboard Buffer. - - -**/ -BOOLEAN -IsUSBKeyboardBufferEmpty ( - IN USB_KB_BUFFER *KeyboardBuffer - ) -{ - // - // meet FIFO empty condition - // - return (BOOLEAN) (KeyboardBuffer->bHead == KeyboardBuffer->bTail); -} - - - -/** - Check whether USB Keyboard buffer is full. - - @param KeyboardBuffer USB Keyboard Buffer. - - -**/ -BOOLEAN -IsUSBKeyboardBufferFull ( - IN USB_KB_BUFFER *KeyboardBuffer - ) -{ - return (BOOLEAN)(((KeyboardBuffer->bTail + 1) % (MAX_KEY_ALLOWED + 1)) == - KeyboardBuffer->bHead); -} - - - -/** - Inserts a key code into keyboard buffer. - - @param KeyboardBuffer Points to the USB Keyboard Buffer. - @param Key Key code - @param Down Special key - - @retval EFI_SUCCESS Success - -**/ -EFI_STATUS -InsertKeyCode ( - IN OUT USB_KB_BUFFER *KeyboardBuffer, - IN UINT8 Key, - IN UINT8 Down - ) -{ - USB_KEY UsbKey; - - // - // if keyboard buffer is full, throw the - // first key out of the keyboard buffer. - // - if (IsUSBKeyboardBufferFull (KeyboardBuffer)) { - RemoveKeyCode (KeyboardBuffer, &UsbKey); - } - - KeyboardBuffer->buffer[KeyboardBuffer->bTail].KeyCode = Key; - KeyboardBuffer->buffer[KeyboardBuffer->bTail].Down = Down; - - // - // adjust the tail pointer of the FIFO keyboard buffer. - // - KeyboardBuffer->bTail = (UINT8) ((KeyboardBuffer->bTail + 1) % (MAX_KEY_ALLOWED + 1)); - - return EFI_SUCCESS; -} - - -/** - Pops a key code off from keyboard buffer. - - @param KeyboardBuffer Points to the USB Keyboard Buffer. - @param UsbKey Points to the buffer that contains a usb key code. - - @retval EFI_SUCCESS Success - @retval EFI_DEVICE_ERROR Hardware Error - -**/ -EFI_STATUS -RemoveKeyCode ( - IN OUT USB_KB_BUFFER *KeyboardBuffer, - OUT USB_KEY *UsbKey - ) -{ - if (IsUSBKeyboardBufferEmpty (KeyboardBuffer)) { - return EFI_DEVICE_ERROR; - } - - UsbKey->KeyCode = KeyboardBuffer->buffer[KeyboardBuffer->bHead].KeyCode; - UsbKey->Down = KeyboardBuffer->buffer[KeyboardBuffer->bHead].Down; - - // - // adjust the head pointer of the FIFO keyboard buffer. - // - KeyboardBuffer->bHead = (UINT8) ((KeyboardBuffer->bHead + 1) % (MAX_KEY_ALLOWED + 1)); - - return EFI_SUCCESS; -} - - -/** - Sets USB Keyboard LED state. - - @param UsbKeyboardDevice The USB_KB_DEV instance. - - @retval EFI_SUCCESS Success - -**/ -EFI_STATUS -SetKeyLED ( - IN USB_KB_DEV *UsbKeyboardDevice - ) -{ - LED_MAP Led; - UINT8 ReportId; - - // - // Set each field in Led map. - // - Led.NumLock = (UINT8) UsbKeyboardDevice->NumLockOn; - Led.CapsLock = (UINT8) UsbKeyboardDevice->CapsOn; - Led.ScrollLock = (UINT8) UsbKeyboardDevice->ScrollOn; - Led.Resrvd = 0; - - ReportId = 0; - // - // call Set Report Request to lighten the LED. - // - UsbSetReportRequest ( - UsbKeyboardDevice->UsbIo, - UsbKeyboardDevice->InterfaceDescriptor.InterfaceNumber, - ReportId, - HID_OUTPUT_REPORT, - 1, - (UINT8 *) &Led - ); - - return EFI_SUCCESS; -} - - -/** - Timer handler for Repeat Key timer. - - @param Event The Repeat Key event. - @param Context Points to the USB_KB_DEV instance. - - -**/ -VOID -EFIAPI -USBKeyboardRepeatHandler ( - IN EFI_EVENT Event, - IN VOID *Context - ) -{ - USB_KB_DEV *UsbKeyboardDevice; - - UsbKeyboardDevice = (USB_KB_DEV *) Context; - - // - // Do nothing when there is no repeat key. - // - if (UsbKeyboardDevice->RepeatKey != 0) { - // - // Inserts one Repeat key into keyboard buffer, - // - InsertKeyCode ( - &(UsbKeyboardDevice->KeyboardBuffer), - UsbKeyboardDevice->RepeatKey, - 1 - ); - - // - // set repeate rate for repeat key generation. - // - gBS->SetTimer ( - UsbKeyboardDevice->RepeatTimer, - TimerRelative, - USBKBD_REPEAT_RATE - ); - - } -} - - -/** - Timer handler for Delayed Recovery timer. - - @param Event The Delayed Recovery event. - @param Context Points to the USB_KB_DEV instance. - - -**/ -VOID -EFIAPI -USBKeyboardRecoveryHandler ( - IN EFI_EVENT Event, - IN VOID *Context - ) -{ - - USB_KB_DEV *UsbKeyboardDevice; - EFI_USB_IO_PROTOCOL *UsbIo; - UINT8 PacketSize; - - UsbKeyboardDevice = (USB_KB_DEV *) Context; - - UsbIo = UsbKeyboardDevice->UsbIo; - - PacketSize = (UINT8) (UsbKeyboardDevice->IntEndpointDescriptor.MaxPacketSize); - - UsbIo->UsbAsyncInterruptTransfer ( - UsbIo, - UsbKeyboardDevice->IntEndpointDescriptor.EndpointAddress, - TRUE, - UsbKeyboardDevice->IntEndpointDescriptor.Interval, - PacketSize, - KeyboardHandler, - UsbKeyboardDevice - ); -} diff --git a/MdeModulePkg/Bus/Usb/UsbKbDxe/keyboard.h b/MdeModulePkg/Bus/Usb/UsbKbDxe/keyboard.h deleted file mode 100644 index 2e7ccf788..000000000 --- a/MdeModulePkg/Bus/Usb/UsbKbDxe/keyboard.h +++ /dev/null @@ -1,118 +0,0 @@ -/** @file -Copyright (c) 2004 - 2005, Intel Corporation -All rights reserved. This program and the accompanying materials -are licensed and made available under the terms and conditions of the BSD License -which accompanies this distribution. The full text of the license may be found at -http://opensource.org/licenses/bsd-license.php - -THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, -WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. - -Module Name: - - Keyboard.h - -Abstract: - - Function prototype for USB Keyboard Driver - -Revision History - -**/ - -#ifndef _KEYBOARD_H -#define _KEYBOARD_H - - -#include "efikey.h" - -BOOLEAN -IsUSBKeyboard ( - IN EFI_USB_IO_PROTOCOL *UsbIo - ); - -EFI_STATUS -InitUSBKeyboard ( - IN USB_KB_DEV *UsbKeyboardDevice - ); - -EFI_STATUS -InitKeyboardLayout ( - IN USB_KB_DEV *UsbKeyboardDevice - ); - -VOID -ReleaseKeyboardLayoutResources ( - USB_KB_DEV *UsbKeyboardDevice - ); - -EFI_STATUS -EFIAPI -KeyboardHandler ( - IN VOID *Data, - IN UINTN DataLength, - IN VOID *Context, - IN UINT32 Result - ); - -VOID -EFIAPI -USBKeyboardRecoveryHandler ( - IN EFI_EVENT Event, - IN VOID *Context - ); - -EFI_STATUS -USBParseKey ( - IN OUT USB_KB_DEV *UsbKeyboardDevice, - OUT UINT8 *KeyChar - ); - -EFI_STATUS -USBKeyCodeToEFIScanCode ( - IN USB_KB_DEV *UsbKeyboardDevice, - IN UINT8 KeyChar, - OUT EFI_INPUT_KEY *Key - ); - -EFI_STATUS -InitUSBKeyBuffer ( - IN OUT USB_KB_BUFFER *KeyboardBuffer - ); - -BOOLEAN -IsUSBKeyboardBufferEmpty ( - IN USB_KB_BUFFER *KeyboardBuffer - ); - -BOOLEAN -IsUSBKeyboardBufferFull ( - IN USB_KB_BUFFER *KeyboardBuffer - ); - -EFI_STATUS -InsertKeyCode ( - IN OUT USB_KB_BUFFER *KeyboardBuffer, - IN UINT8 Key, - IN UINT8 Down - ); - -EFI_STATUS -RemoveKeyCode ( - IN OUT USB_KB_BUFFER *KeyboardBuffer, - OUT USB_KEY *UsbKey - ); - -VOID -EFIAPI -USBKeyboardRepeatHandler ( - IN EFI_EVENT Event, - IN VOID *Context - ); - -EFI_STATUS -SetKeyLED ( - IN USB_KB_DEV *UsbKeyboardDevice - ); - -#endif -- cgit v1.2.3