X-Git-Url: https://git.proxmox.com/?p=mirror_edk2.git;a=blobdiff_plain;f=MdeModulePkg%2FBus%2FUsb%2FUsbKbDxe%2FKeyBoard.c;h=b3b5fb9ff4c49136dcd679f28357592054ee1c94;hp=32ea3cfb8a4af858f5c7eea3678b91c5393c1279;hb=dd190645eb43424706eb1709d0032c69a1935d9f;hpb=cb7d01c0c9fd199742d0fed6aa69dab0c79c3338 diff --git a/MdeModulePkg/Bus/Usb/UsbKbDxe/KeyBoard.c b/MdeModulePkg/Bus/Usb/UsbKbDxe/KeyBoard.c index 32ea3cfb8a..b3b5fb9ff4 100644 --- a/MdeModulePkg/Bus/Usb/UsbKbDxe/KeyBoard.c +++ b/MdeModulePkg/Bus/Usb/UsbKbDxe/KeyBoard.c @@ -1,8 +1,8 @@ /** @file Helper functions for USB Keyboard Driver. -Copyright (c) 2004 - 2008, Intel Corporation -All rights reserved. This program and the accompanying materials +Copyright (c) 2004 - 2018, 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 @@ -14,9 +14,6 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. #include "KeyBoard.h" -EFI_GUID mUsbKeyboardLayoutPackageGuid = USB_KEYBOARD_LAYOUT_PACKAGE_GUID; -EFI_GUID mUsbKeyboardLayoutKeyGuid = USB_KEYBOARD_LAYOUT_KEY_GUID; - USB_KEYBOARD_LAYOUT_PACK_BIN mUsbKeyboardLayoutBin = { sizeof (USB_KEYBOARD_LAYOUT_PACK_BIN), // Binary size @@ -82,6 +79,7 @@ USB_KEYBOARD_LAYOUT_PACK_BIN mUsbKeyboardLayoutBin = { {EfiKeyD11, '[', '{', 0, 0, EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT}, {EfiKeyD12, ']', '}', 0, 0, EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT}, {EfiKeyD13, '\\', '|', 0, 0, EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT}, + {EfiKeyC12, '\\', '|', 0, 0, EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT}, {EfiKeyC10, ';', ':', 0, 0, EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT}, {EfiKeyC11, '\'', '"', 0, 0, EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT}, {EfiKeyE0, '`', '~', 0, 0, EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT}, @@ -299,6 +297,17 @@ UINT8 ModifierValueToEfiScanCodeConvertionTable[] = { SCAN_F10, // EFI_FUNCTION_KEY_TEN_MODIFIER SCAN_F11, // EFI_FUNCTION_KEY_ELEVEN_MODIFIER SCAN_F12, // EFI_FUNCTION_KEY_TWELVE_MODIFIER + // + // For Partial Keystroke support + // + SCAN_NULL, // EFI_PRINT_MODIFIER + SCAN_NULL, // EFI_SYS_REQUEST_MODIFIER + SCAN_NULL, // EFI_SCROLL_LOCK_MODIFIER + SCAN_PAUSE, // EFI_PAUSE_MODIFIER + SCAN_NULL, // EFI_BREAK_MODIFIER + SCAN_NULL, // EFI_LEFT_LOGO_MODIFIER + SCAN_NULL, // EFI_RIGHT_LOGO_MODIFER + SCAN_NULL, // EFI_MENU_MODIFER }; /** @@ -310,7 +319,6 @@ UINT8 ModifierValueToEfiScanCodeConvertionTable[] = { @retval Others Failure to install default keyboard layout. **/ EFI_STATUS -EFIAPI InstallDefaultKeyboardLayout ( IN OUT USB_KB_DEV *UsbKeyboardDevice ) @@ -335,7 +343,7 @@ InstallDefaultKeyboardLayout ( // Install Keyboard Layout package to HII database // HiiHandle = HiiAddPackages ( - &mUsbKeyboardLayoutPackageGuid, + &gUsbKeyboardLayoutPackageGuid, UsbKeyboardDevice->ControllerHandle, &mUsbKeyboardLayoutBin, NULL @@ -347,7 +355,7 @@ InstallDefaultKeyboardLayout ( // // Set current keyboard layout // - Status = HiiDatabase->SetKeyboardLayout (HiiDatabase, &mUsbKeyboardLayoutKeyGuid); + Status = HiiDatabase->SetKeyboardLayout (HiiDatabase, &gUsbKeyboardLayoutKeyGuid); return Status; } @@ -363,7 +371,6 @@ InstallDefaultKeyboardLayout ( **/ BOOLEAN -EFIAPI IsUSBKeyboard ( IN EFI_USB_IO_PROTOCOL *UsbIo ) @@ -401,7 +408,6 @@ IsUSBKeyboard ( **/ EFI_HII_KEYBOARD_LAYOUT * -EFIAPI GetCurrentKeyboardLayout ( VOID ) @@ -464,7 +470,6 @@ GetCurrentKeyboardLayout ( **/ EFI_KEY_DESCRIPTOR * -EFIAPI GetKeyDescriptor ( IN USB_KB_DEV *UsbKeyboardDevice, IN UINT8 KeyCode @@ -502,7 +507,6 @@ GetKeyDescriptor ( **/ USB_NS_KEY * -EFIAPI FindUsbNsKey ( IN USB_KB_DEV *UsbKeyboardDevice, IN EFI_KEY_DESCRIPTOR *KeyDescriptor @@ -511,7 +515,7 @@ FindUsbNsKey ( LIST_ENTRY *Link; LIST_ENTRY *NsKeyList; USB_NS_KEY *UsbNsKey; - + NsKeyList = &UsbKeyboardDevice->NsKeyList; Link = GetFirstNode (NsKeyList); while (!IsNull (NsKeyList, Link)) { @@ -543,7 +547,6 @@ FindUsbNsKey ( **/ EFI_KEY_DESCRIPTOR * -EFIAPI FindPhysicalKey ( IN USB_NS_KEY *UsbNsKey, IN EFI_KEY_DESCRIPTOR *KeyDescriptor @@ -598,6 +601,9 @@ SetKeyboardLayoutEvent ( UINT8 KeyCode; UsbKeyboardDevice = (USB_KB_DEV *) Context; + if (UsbKeyboardDevice->Signature != USB_KB_DEV_SIGNATURE) { + return; + } // // Try to get current keyboard layout from HII database @@ -629,13 +635,18 @@ SetKeyboardLayoutEvent ( // KeyCode = EfiKeyToUsbKeyCodeConvertionTable [(UINT8) (TempKey.Key)]; TableEntry = GetKeyDescriptor (UsbKeyboardDevice, KeyCode); + if (TableEntry == NULL) { + ReleaseKeyboardLayoutResources (UsbKeyboardDevice); + FreePool (KeyboardLayout); + return; + } CopyMem (TableEntry, KeyDescriptor, sizeof (EFI_KEY_DESCRIPTOR)); // // For non-spacing key, create the list with a non-spacing key followed by physical keys. // if (TempKey.Modifier == EFI_NS_KEY_MODIFIER) { - UsbNsKey = AllocatePool (sizeof (USB_NS_KEY)); + UsbNsKey = AllocateZeroPool (sizeof (USB_NS_KEY)); ASSERT (UsbNsKey != NULL); // @@ -643,7 +654,7 @@ SetKeyboardLayoutEvent ( // KeyCount = 0; NsKey = KeyDescriptor + 1; - for (Index2 = Index + 1; Index2 < KeyboardLayout->DescriptorCount; Index2++) { + for (Index2 = (UINT8) Index + 1; Index2 < KeyboardLayout->DescriptorCount; Index2++) { CopyMem (&TempKey, NsKey, sizeof (EFI_KEY_DESCRIPTOR)); if (TempKey.Modifier == EFI_NS_KEY_DEPENDENCY_MODIFIER) { KeyCount++; @@ -688,7 +699,6 @@ SetKeyboardLayoutEvent ( **/ VOID -EFIAPI ReleaseKeyboardLayoutResources ( IN OUT USB_KB_DEV *UsbKeyboardDevice ) @@ -727,7 +737,6 @@ ReleaseKeyboardLayoutResources ( **/ EFI_STATUS -EFIAPI InitKeyboardLayout ( OUT USB_KB_DEV *UsbKeyboardDevice ) @@ -779,7 +788,7 @@ InitKeyboardLayout ( // InstallDefaultKeyboardLayout (UsbKeyboardDevice); } - + return EFI_SUCCESS; } @@ -794,48 +803,58 @@ InitKeyboardLayout ( **/ EFI_STATUS -EFIAPI InitUSBKeyboard ( IN OUT USB_KB_DEV *UsbKeyboardDevice ) { - UINT8 ConfigValue; + UINT16 ConfigValue; UINT8 Protocol; - UINT8 ReportId; - UINT8 Duration; EFI_STATUS Status; UINT32 TransferResult; REPORT_STATUS_CODE_WITH_DEVICE_PATH ( EFI_PROGRESS_CODE, - PcdGet32 (PcdStatusCodeValueKeyboardSelfTest), + (EFI_PERIPHERAL_KEYBOARD | EFI_P_KEYBOARD_PC_SELF_TEST), UsbKeyboardDevice->DevicePath ); - InitUSBKeyBuffer (&(UsbKeyboardDevice->KeyboardBuffer)); + InitQueue (&UsbKeyboardDevice->UsbKeyQueue, sizeof (USB_KEY)); + InitQueue (&UsbKeyboardDevice->EfiKeyQueue, sizeof (EFI_KEY_DATA)); + InitQueue (&UsbKeyboardDevice->EfiKeyQueueForNotify, sizeof (EFI_KEY_DATA)); // - // Uses default configuration to configure the USB keyboard device. + // Use the config out of the descriptor + // Assumed the first config is the correct one and this is not always the case // - ConfigValue = 0x01; - Status = UsbSetConfiguration ( + Status = UsbGetConfiguration ( UsbKeyboardDevice->UsbIo, - (UINT16) ConfigValue, + &ConfigValue, &TransferResult ); if (EFI_ERROR (Status)) { + ConfigValue = 0x01; // - // If configuration could not be set here, it means - // the keyboard interface has some errors and could - // not be initialized + // Uses default configuration to configure the USB Keyboard device. // - REPORT_STATUS_CODE_WITH_DEVICE_PATH ( - EFI_ERROR_CODE | EFI_ERROR_MINOR, - PcdGet32 (PcdStatusCodeValueKeyboardInterfaceError), - UsbKeyboardDevice->DevicePath - ); + Status = UsbSetConfiguration ( + UsbKeyboardDevice->UsbIo, + 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 + // + REPORT_STATUS_CODE_WITH_DEVICE_PATH ( + EFI_ERROR_CODE | EFI_ERROR_MINOR, + (EFI_PERIPHERAL_KEYBOARD | EFI_P_EC_INTERFACE_ERROR), + UsbKeyboardDevice->DevicePath + ); - return EFI_DEVICE_ERROR; + return EFI_DEVICE_ERROR; + } } UsbGetProtocolRequest ( @@ -855,30 +874,13 @@ InitUSBKeyboard ( ); } - // - // ReportId is zero, which means the idle rate applies to all input reports. - // - ReportId = 0; - // - // Duration is zero, which means the duration is infinite. - // so the endpoint will inhibit reporting forever, - // and only reporting when a change is detected in the report data. - // - Duration = 0; - UsbSetIdleRequest ( - UsbKeyboardDevice->UsbIo, - UsbKeyboardDevice->InterfaceDescriptor.InterfaceNumber, - ReportId, - Duration - ); - UsbKeyboardDevice->CtrlOn = FALSE; UsbKeyboardDevice->AltOn = FALSE; UsbKeyboardDevice->ShiftOn = FALSE; UsbKeyboardDevice->NumLockOn = FALSE; UsbKeyboardDevice->CapsOn = FALSE; UsbKeyboardDevice->ScrollOn = FALSE; - + UsbKeyboardDevice->LeftCtrlOn = FALSE; UsbKeyboardDevice->LeftAltOn = FALSE; UsbKeyboardDevice->LeftShiftOn = FALSE; @@ -911,7 +913,7 @@ InitUSBKeyboard ( gBS->CreateEvent ( EVT_TIMER | EVT_NOTIFY_SIGNAL, - TPL_NOTIFY, + TPL_CALLBACK, USBKeyboardRepeatHandler, UsbKeyboardDevice, &UsbKeyboardDevice->RepeatTimer @@ -971,12 +973,10 @@ KeyboardHandler ( UINT8 CurModifierMap; UINT8 OldModifierMap; UINT8 Mask; - UINT8 Index; + UINTN Index; UINT8 Index2; - BOOLEAN Down; BOOLEAN KeyRelease; BOOLEAN KeyPress; - UINT8 SavedTail; USB_KEY UsbKey; UINT8 NewRepeatKey; UINT32 UsbStatus; @@ -997,7 +997,7 @@ KeyboardHandler ( // REPORT_STATUS_CODE_WITH_DEVICE_PATH ( EFI_ERROR_CODE | EFI_ERROR_MINOR, - PcdGet32 (PcdStatusCodeValueKeyboardInputError), + (EFI_PERIPHERAL_KEYBOARD | EFI_P_EC_INPUT_ERROR), UsbKeyboardDevice->DevicePath ); @@ -1022,7 +1022,7 @@ KeyboardHandler ( // // Delete & Submit this interrupt again - // Handler of DelayedRecoveryEvent triggered by timer will re-submit the interrupt. + // Handler of DelayedRecoveryEvent triggered by timer will re-submit the interrupt. // UsbIo->UsbAsyncInterruptTransfer ( UsbIo, @@ -1104,8 +1104,9 @@ KeyboardHandler ( // otherwise it is a non-zero value. // Insert the changed modifier key into key buffer. // - Down = (BOOLEAN) ((CurModifierMap & Mask) != 0); - InsertKeyCode (&(UsbKeyboardDevice->KeyboardBuffer), (UINT8) (0xe0 + Index), Down); + UsbKey.KeyCode = (UINT8) (0xe0 + Index); + UsbKey.Down = (BOOLEAN) ((CurModifierMap & Mask) != 0); + Enqueue (&UsbKeyboardDevice->UsbKeyQueue, &UsbKey, sizeof (UsbKey)); } } @@ -1137,11 +1138,9 @@ KeyboardHandler ( } if (KeyRelease) { - InsertKeyCode ( - &(UsbKeyboardDevice->KeyboardBuffer), - OldKeyCodeBuffer[Index], - FALSE - ); + UsbKey.KeyCode = OldKeyCodeBuffer[Index]; + UsbKey.Down = FALSE; + Enqueue (&UsbKeyboardDevice->UsbKeyQueue, &UsbKey, sizeof (UsbKey)); // // The original repeat key is released. // @@ -1189,13 +1188,17 @@ KeyboardHandler ( } if (KeyPress) { - InsertKeyCode (&(UsbKeyboardDevice->KeyboardBuffer), CurKeyCodeBuffer[Index], TRUE); + UsbKey.KeyCode = CurKeyCodeBuffer[Index]; + UsbKey.Down = TRUE; + Enqueue (&UsbKeyboardDevice->UsbKeyQueue, &UsbKey, sizeof (UsbKey)); // // Handle repeat key // KeyDescriptor = GetKeyDescriptor (UsbKeyboardDevice, CurKeyCodeBuffer[Index]); - ASSERT (KeyDescriptor != NULL); + if (KeyDescriptor == NULL) { + continue; + } if (KeyDescriptor->Modifier == EFI_NUM_LOCK_MODIFIER || KeyDescriptor->Modifier == EFI_CAPS_LOCK_MODIFIER) { // @@ -1219,72 +1222,6 @@ KeyboardHandler ( UsbKeyboardDevice->LastKeyCodeArray[Index] = CurKeyCodeBuffer[Index]; } - // - // Pre-process KeyboardBuffer to check if Ctrl + Alt + Del is pressed. - // - SavedTail = UsbKeyboardDevice->KeyboardBuffer.BufferTail; - Index = UsbKeyboardDevice->KeyboardBuffer.BufferHead; - while (Index != SavedTail) { - RemoveKeyCode (&(UsbKeyboardDevice->KeyboardBuffer), &UsbKey); - - KeyDescriptor = GetKeyDescriptor (UsbKeyboardDevice, UsbKey.KeyCode); - ASSERT (KeyDescriptor != NULL); - - switch (KeyDescriptor->Modifier) { - - case EFI_LEFT_CONTROL_MODIFIER: - case EFI_RIGHT_CONTROL_MODIFIER: - if (UsbKey.Down) { - UsbKeyboardDevice->CtrlOn = TRUE; - } else { - UsbKeyboardDevice->CtrlOn = FALSE; - } - break; - - case EFI_LEFT_ALT_MODIFIER: - case EFI_RIGHT_ALT_MODIFIER: - if (UsbKey.Down) { - UsbKeyboardDevice->AltOn = TRUE; - } else { - UsbKeyboardDevice->AltOn = FALSE; - } - break; - - case EFI_ALT_GR_MODIFIER: - if (UsbKey.Down) { - UsbKeyboardDevice->AltGrOn = TRUE; - } else { - UsbKeyboardDevice->AltGrOn = FALSE; - } - break; - - // - // For Del Key, check if Ctrl + Alt + Del occurs for reset. - // - 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.BufferHead; - - } // // If there is new key pressed, update the RepeatKey value, and set the // timer to repeate delay timer @@ -1321,7 +1258,6 @@ KeyboardHandler ( **/ EFI_STATUS -EFIAPI USBParseKey ( IN OUT USB_KB_DEV *UsbKeyboardDevice, OUT UINT8 *KeyCode @@ -1332,15 +1268,16 @@ USBParseKey ( *KeyCode = 0; - while (!IsUSBKeyboardBufferEmpty (&UsbKeyboardDevice->KeyboardBuffer)) { + while (!IsQueueEmpty (&UsbKeyboardDevice->UsbKeyQueue)) { // // Pops one raw data off. // - RemoveKeyCode (&(UsbKeyboardDevice->KeyboardBuffer), &UsbKey); + Dequeue (&UsbKeyboardDevice->UsbKeyQueue, &UsbKey, sizeof (UsbKey)); KeyDescriptor = GetKeyDescriptor (UsbKeyboardDevice, UsbKey.KeyCode); - ASSERT (KeyDescriptor != NULL); - + if (KeyDescriptor == NULL) { + continue; + } if (!UsbKey.Down) { // // Key is released. @@ -1437,12 +1374,10 @@ USBParseKey ( case EFI_LEFT_CONTROL_MODIFIER: UsbKeyboardDevice->LeftCtrlOn = TRUE; UsbKeyboardDevice->CtrlOn = TRUE; - continue; break; case EFI_RIGHT_CONTROL_MODIFIER: UsbKeyboardDevice->RightCtrlOn = TRUE; UsbKeyboardDevice->CtrlOn = TRUE; - continue; break; // @@ -1451,12 +1386,10 @@ USBParseKey ( case EFI_LEFT_SHIFT_MODIFIER: UsbKeyboardDevice->LeftShiftOn = TRUE; UsbKeyboardDevice->ShiftOn = TRUE; - continue; break; case EFI_RIGHT_SHIFT_MODIFIER: UsbKeyboardDevice->RightShiftOn = TRUE; UsbKeyboardDevice->ShiftOn = TRUE; - continue; break; // @@ -1465,12 +1398,10 @@ USBParseKey ( case EFI_LEFT_ALT_MODIFIER: UsbKeyboardDevice->LeftAltOn = TRUE; UsbKeyboardDevice->AltOn = TRUE; - continue; break; case EFI_RIGHT_ALT_MODIFIER: UsbKeyboardDevice->RightAltOn = TRUE; UsbKeyboardDevice->AltOn = TRUE; - continue; break; // @@ -1500,7 +1431,6 @@ USBParseKey ( case EFI_PRINT_MODIFIER: case EFI_SYS_REQUEST_MODIFIER: UsbKeyboardDevice->SysReqOn = TRUE; - continue; break; // @@ -1516,7 +1446,6 @@ USBParseKey ( // UsbKeyboardDevice->NumLockOn = (BOOLEAN) (!(UsbKeyboardDevice->NumLockOn)); SetKeyLED (UsbKeyboardDevice); - continue; break; case EFI_CAPS_LOCK_MODIFIER: @@ -1525,7 +1454,6 @@ USBParseKey ( // UsbKeyboardDevice->CapsOn = (BOOLEAN) (!(UsbKeyboardDevice->CapsOn)); SetKeyLED (UsbKeyboardDevice); - continue; break; case EFI_SCROLL_LOCK_MODIFIER: @@ -1534,18 +1462,6 @@ USBParseKey ( // UsbKeyboardDevice->ScrollOn = (BOOLEAN) (!(UsbKeyboardDevice->ScrollOn)); SetKeyLED (UsbKeyboardDevice); - continue; - break; - - // - // PrintScreen, Pause/Break could not be retrieved via SimpleTextInEx protocol - // - case EFI_PAUSE_MODIFIER: - case EFI_BREAK_MODIFIER: - // - // Fall through - // - continue; break; default: @@ -1568,13 +1484,72 @@ USBParseKey ( return EFI_NOT_READY; } +/** + Initialize the key state. + + @param UsbKeyboardDevice The USB_KB_DEV instance. + @param KeyState A pointer to receive the key state information. +**/ +VOID +InitializeKeyState ( + IN USB_KB_DEV *UsbKeyboardDevice, + OUT EFI_KEY_STATE *KeyState + ) +{ + KeyState->KeyShiftState = EFI_SHIFT_STATE_VALID; + KeyState->KeyToggleState = EFI_TOGGLE_STATE_VALID; + + if (UsbKeyboardDevice->LeftCtrlOn) { + KeyState->KeyShiftState |= EFI_LEFT_CONTROL_PRESSED; + } + if (UsbKeyboardDevice->RightCtrlOn) { + KeyState->KeyShiftState |= EFI_RIGHT_CONTROL_PRESSED; + } + if (UsbKeyboardDevice->LeftAltOn) { + KeyState->KeyShiftState |= EFI_LEFT_ALT_PRESSED; + } + if (UsbKeyboardDevice->RightAltOn) { + KeyState->KeyShiftState |= EFI_RIGHT_ALT_PRESSED; + } + if (UsbKeyboardDevice->LeftShiftOn) { + KeyState->KeyShiftState |= EFI_LEFT_SHIFT_PRESSED; + } + if (UsbKeyboardDevice->RightShiftOn) { + KeyState->KeyShiftState |= EFI_RIGHT_SHIFT_PRESSED; + } + if (UsbKeyboardDevice->LeftLogoOn) { + KeyState->KeyShiftState |= EFI_LEFT_LOGO_PRESSED; + } + if (UsbKeyboardDevice->RightLogoOn) { + KeyState->KeyShiftState |= EFI_RIGHT_LOGO_PRESSED; + } + if (UsbKeyboardDevice->MenuKeyOn) { + KeyState->KeyShiftState |= EFI_MENU_KEY_PRESSED; + } + if (UsbKeyboardDevice->SysReqOn) { + KeyState->KeyShiftState |= EFI_SYS_REQ_PRESSED; + } + + if (UsbKeyboardDevice->ScrollOn) { + KeyState->KeyToggleState |= EFI_SCROLL_LOCK_ACTIVE; + } + if (UsbKeyboardDevice->NumLockOn) { + KeyState->KeyToggleState |= EFI_NUM_LOCK_ACTIVE; + } + if (UsbKeyboardDevice->CapsOn) { + KeyState->KeyToggleState |= EFI_CAPS_LOCK_ACTIVE; + } + if (UsbKeyboardDevice->IsSupportPartialKey) { + KeyState->KeyToggleState |= EFI_KEY_STATE_EXPOSED; + } +} /** Converts USB Keycode ranging from 0x4 to 0x65 to EFI_INPUT_KEY. @param UsbKeyboardDevice The USB_KB_DEV instance. @param KeyCode Indicates the key code that will be interpreted. - @param Key A pointer to a buffer that is filled in with + @param KeyData A pointer to a buffer that is filled in with the keystroke information for the key that was pressed. @@ -1586,27 +1561,24 @@ USBParseKey ( **/ EFI_STATUS -EFIAPI UsbKeyCodeToEfiInputKey ( - IN USB_KB_DEV *UsbKeyboardDevice, - IN UINT8 KeyCode, - OUT EFI_INPUT_KEY *Key + IN USB_KB_DEV *UsbKeyboardDevice, + IN UINT8 KeyCode, + OUT EFI_KEY_DATA *KeyData ) { - EFI_KEY_DESCRIPTOR *KeyDescriptor; + EFI_KEY_DESCRIPTOR *KeyDescriptor; + LIST_ENTRY *Link; + LIST_ENTRY *NotifyList; + KEYBOARD_CONSOLE_IN_EX_NOTIFY *CurrentNotify; // - // KeyCode must in the range of 0x4 to 0x65 + // KeyCode must in the range of [0x4, 0x65] or [0xe0, 0xe7]. // - if (!USBKBD_VALID_KEYCODE (KeyCode)) { - return EFI_INVALID_PARAMETER; - } - if ((KeyCode - 4) >= NUMBER_OF_VALID_NON_MODIFIER_USB_KEYCODE) { - return EFI_INVALID_PARAMETER; - } - KeyDescriptor = GetKeyDescriptor (UsbKeyboardDevice, KeyCode); - ASSERT (KeyDescriptor != NULL); + if (KeyDescriptor == NULL) { + return EFI_DEVICE_ERROR; + } if (KeyDescriptor->Modifier == EFI_NS_KEY_MODIFIER) { // @@ -1628,63 +1600,52 @@ UsbKeyCodeToEfiInputKey ( // // Make sure modifier of Key Descriptor is in the valid range according to UEFI spec. // - if (KeyDescriptor->Modifier > EFI_FUNCTION_KEY_TWELVE_MODIFIER) { + if (KeyDescriptor->Modifier >= (sizeof (ModifierValueToEfiScanCodeConvertionTable) / sizeof (UINT8))) { return EFI_DEVICE_ERROR; } - Key->ScanCode = ModifierValueToEfiScanCodeConvertionTable[KeyDescriptor->Modifier]; - Key->UnicodeChar = KeyDescriptor->Unicode; + KeyData->Key.ScanCode = ModifierValueToEfiScanCodeConvertionTable[KeyDescriptor->Modifier]; + KeyData->Key.UnicodeChar = KeyDescriptor->Unicode; if ((KeyDescriptor->AffectedAttribute & EFI_AFFECTED_BY_STANDARD_SHIFT)!= 0) { if (UsbKeyboardDevice->ShiftOn) { - Key->UnicodeChar = KeyDescriptor->ShiftedUnicode; + KeyData->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) != 0) { + if ((KeyDescriptor->Unicode != CHAR_NULL) && (KeyDescriptor->ShiftedUnicode != CHAR_NULL) && + (KeyDescriptor->Unicode != KeyDescriptor->ShiftedUnicode)) { UsbKeyboardDevice->LeftShiftOn = FALSE; UsbKeyboardDevice->RightShiftOn = FALSE; } if (UsbKeyboardDevice->AltGrOn) { - Key->UnicodeChar = KeyDescriptor->ShiftedAltGrUnicode; + KeyData->Key.UnicodeChar = KeyDescriptor->ShiftedAltGrUnicode; } } else { // // Shift off // - Key->UnicodeChar = KeyDescriptor->Unicode; + KeyData->Key.UnicodeChar = KeyDescriptor->Unicode; if (UsbKeyboardDevice->AltGrOn) { - Key->UnicodeChar = KeyDescriptor->AltGrUnicode; + KeyData->Key.UnicodeChar = KeyDescriptor->AltGrUnicode; } } } if ((KeyDescriptor->AffectedAttribute & EFI_AFFECTED_BY_CAPS_LOCK) != 0) { if (UsbKeyboardDevice->CapsOn) { - if (Key->UnicodeChar == KeyDescriptor->Unicode) { - Key->UnicodeChar = KeyDescriptor->ShiftedUnicode; - } else if (Key->UnicodeChar == KeyDescriptor->ShiftedUnicode) { - Key->UnicodeChar = KeyDescriptor->Unicode; + if (KeyData->Key.UnicodeChar == KeyDescriptor->Unicode) { + KeyData->Key.UnicodeChar = KeyDescriptor->ShiftedUnicode; + } else if (KeyData->Key.UnicodeChar == KeyDescriptor->ShiftedUnicode) { + KeyData->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) != 0) { // // For key affected by NumLock, if NumLock is on and Shift is not pressed, then it means @@ -1692,205 +1653,200 @@ UsbKeyCodeToEfiInputKey ( // Otherwise, it means control key, so preserve the EFI Scan Code and clear the unicode keycode. // if ((UsbKeyboardDevice->NumLockOn) && (!(UsbKeyboardDevice->ShiftOn))) { - Key->ScanCode = SCAN_NULL; + KeyData->Key.ScanCode = SCAN_NULL; } else { - Key->UnicodeChar = 0x00; + KeyData->Key.UnicodeChar = CHAR_NULL; } } // // Translate Unicode 0x1B (ESC) to EFI Scan Code // - if (Key->UnicodeChar == 0x1B && Key->ScanCode == SCAN_NULL) { - Key->ScanCode = SCAN_ESC; - Key->UnicodeChar = 0x00; + if (KeyData->Key.UnicodeChar == 0x1B && KeyData->Key.ScanCode == SCAN_NULL) { + KeyData->Key.ScanCode = SCAN_ESC; + KeyData->Key.UnicodeChar = CHAR_NULL; } // // Not valid for key without both unicode key code and EFI Scan Code. // - if (Key->UnicodeChar == 0 && Key->ScanCode == SCAN_NULL) { + if (KeyData->Key.UnicodeChar == 0 && KeyData->Key.ScanCode == SCAN_NULL) { + if (!UsbKeyboardDevice->IsSupportPartialKey) { return EFI_NOT_READY; + } } - // // Save Shift/Toggle state // - if (UsbKeyboardDevice->LeftCtrlOn) { - UsbKeyboardDevice->KeyState.KeyShiftState |= EFI_LEFT_CONTROL_PRESSED; - } - if (UsbKeyboardDevice->RightCtrlOn) { - UsbKeyboardDevice->KeyState.KeyShiftState |= EFI_RIGHT_CONTROL_PRESSED; - } - if (UsbKeyboardDevice->LeftAltOn) { - UsbKeyboardDevice->KeyState.KeyShiftState |= EFI_LEFT_ALT_PRESSED; - } - if (UsbKeyboardDevice->RightAltOn) { - UsbKeyboardDevice->KeyState.KeyShiftState |= EFI_RIGHT_ALT_PRESSED; - } - if (UsbKeyboardDevice->LeftShiftOn) { - UsbKeyboardDevice->KeyState.KeyShiftState |= EFI_LEFT_SHIFT_PRESSED; - } - if (UsbKeyboardDevice->RightShiftOn) { - UsbKeyboardDevice->KeyState.KeyShiftState |= EFI_RIGHT_SHIFT_PRESSED; - } - if (UsbKeyboardDevice->LeftLogoOn) { - UsbKeyboardDevice->KeyState.KeyShiftState |= EFI_LEFT_LOGO_PRESSED; - } - if (UsbKeyboardDevice->RightLogoOn) { - UsbKeyboardDevice->KeyState.KeyShiftState |= EFI_RIGHT_LOGO_PRESSED; - } - if (UsbKeyboardDevice->MenuKeyOn) { - UsbKeyboardDevice->KeyState.KeyShiftState |= EFI_MENU_KEY_PRESSED; - } - if (UsbKeyboardDevice->SysReqOn) { - UsbKeyboardDevice->KeyState.KeyShiftState |= EFI_SYS_REQ_PRESSED; - } + InitializeKeyState (UsbKeyboardDevice, &KeyData->KeyState); - if (UsbKeyboardDevice->ScrollOn) { - UsbKeyboardDevice->KeyState.KeyToggleState |= EFI_SCROLL_LOCK_ACTIVE; - } - if (UsbKeyboardDevice->NumLockOn) { - UsbKeyboardDevice->KeyState.KeyToggleState |= EFI_NUM_LOCK_ACTIVE; - } - if (UsbKeyboardDevice->CapsOn) { - UsbKeyboardDevice->KeyState.KeyToggleState |= EFI_CAPS_LOCK_ACTIVE; + // + // Signal KeyNotify process event if this key pressed matches any key registered. + // + NotifyList = &UsbKeyboardDevice->NotifyList; + for (Link = GetFirstNode (NotifyList); !IsNull (NotifyList, Link); Link = GetNextNode (NotifyList, Link)) { + CurrentNotify = CR (Link, KEYBOARD_CONSOLE_IN_EX_NOTIFY, NotifyEntry, USB_KB_CONSOLE_IN_EX_NOTIFY_SIGNATURE); + if (IsKeyRegistered (&CurrentNotify->KeyData, KeyData)) { + // + // The key notification function needs to run at TPL_CALLBACK + // while current TPL is TPL_NOTIFY. It will be invoked in + // KeyNotifyProcessHandler() which runs at TPL_CALLBACK. + // + Enqueue (&UsbKeyboardDevice->EfiKeyQueueForNotify, KeyData, sizeof (*KeyData)); + gBS->SignalEvent (UsbKeyboardDevice->KeyNotifyProcessEvent); + } } return EFI_SUCCESS; - } - /** - Resets USB keyboard buffer. + Create the queue. - @param KeyboardBuffer Points to the USB keyboard buffer. + @param Queue Points to the queue. + @param ItemSize Size of the single item. **/ VOID -EFIAPI -InitUSBKeyBuffer ( - OUT USB_KB_BUFFER *KeyboardBuffer +InitQueue ( + IN OUT USB_SIMPLE_QUEUE *Queue, + IN UINTN ItemSize ) { - ZeroMem (KeyboardBuffer, sizeof (USB_KB_BUFFER)); + UINTN Index; - KeyboardBuffer->BufferHead = KeyboardBuffer->BufferTail; + Queue->ItemSize = ItemSize; + Queue->Head = 0; + Queue->Tail = 0; + + if (Queue->Buffer[0] != NULL) { + FreePool (Queue->Buffer[0]); + } + + Queue->Buffer[0] = AllocatePool (sizeof (Queue->Buffer) / sizeof (Queue->Buffer[0]) * ItemSize); + ASSERT (Queue->Buffer[0] != NULL); + + for (Index = 1; Index < sizeof (Queue->Buffer) / sizeof (Queue->Buffer[0]); Index++) { + Queue->Buffer[Index] = ((UINT8 *) Queue->Buffer[Index - 1]) + ItemSize; + } +} + +/** + Destroy the queue + + @param Queue Points to the queue. +**/ +VOID +DestroyQueue ( + IN OUT USB_SIMPLE_QUEUE *Queue + ) +{ + FreePool (Queue->Buffer[0]); } /** - Check whether USB keyboard buffer is empty. + Check whether the queue is empty. - @param KeyboardBuffer USB keyboard buffer + @param Queue Points to the queue. - @retval TRUE Keyboard buffer is empty. - @retval FALSE Keyboard buffer is not empty. + @retval TRUE Queue is empty. + @retval FALSE Queue is not empty. **/ BOOLEAN -EFIAPI -IsUSBKeyboardBufferEmpty ( - IN USB_KB_BUFFER *KeyboardBuffer +IsQueueEmpty ( + IN USB_SIMPLE_QUEUE *Queue ) { // // Meet FIFO empty condition // - return (BOOLEAN) (KeyboardBuffer->BufferHead == KeyboardBuffer->BufferTail); + return (BOOLEAN) (Queue->Head == Queue->Tail); } /** - Check whether USB keyboard buffer is full. + Check whether the queue is full. - @param KeyboardBuffer USB keyboard buffer + @param Queue Points to the queue. - @retval TRUE Keyboard buffer is full. - @retval FALSE Keyboard buffer is not full. + @retval TRUE Queue is full. + @retval FALSE Queue is not full. **/ BOOLEAN -EFIAPI -IsUSBKeyboardBufferFull ( - IN USB_KB_BUFFER *KeyboardBuffer +IsQueueFull ( + IN USB_SIMPLE_QUEUE *Queue ) { - return (BOOLEAN)(((KeyboardBuffer->BufferTail + 1) % (MAX_KEY_ALLOWED + 1)) == KeyboardBuffer->BufferHead); + return (BOOLEAN) (((Queue->Tail + 1) % (MAX_KEY_ALLOWED + 1)) == Queue->Head); } /** - Inserts a keycode into keyboard buffer. - - @param KeyboardBuffer Points to the USB keyboard buffer. - @param Key Keycode to insert. - @param Down TRUE means key is pressed. - FALSE means key is released. + Enqueue the item to the queue. + @param Queue Points to the queue. + @param Item Points to the item to be enqueued. + @param ItemSize Size of the item. **/ VOID -EFIAPI -InsertKeyCode ( - IN OUT USB_KB_BUFFER *KeyboardBuffer, - IN UINT8 Key, - IN BOOLEAN Down +Enqueue ( + IN OUT USB_SIMPLE_QUEUE *Queue, + IN VOID *Item, + IN UINTN ItemSize ) { - USB_KEY UsbKey; - + ASSERT (ItemSize == Queue->ItemSize); // // If keyboard buffer is full, throw the // first key out of the keyboard buffer. // - if (IsUSBKeyboardBufferFull (KeyboardBuffer)) { - RemoveKeyCode (KeyboardBuffer, &UsbKey); + if (IsQueueFull (Queue)) { + Queue->Head = (Queue->Head + 1) % (MAX_KEY_ALLOWED + 1); } - ASSERT (KeyboardBuffer->BufferTail <= MAX_KEY_ALLOWED); - - KeyboardBuffer->Buffer[KeyboardBuffer->BufferTail].KeyCode = Key; - KeyboardBuffer->Buffer[KeyboardBuffer->BufferTail].Down = Down; + CopyMem (Queue->Buffer[Queue->Tail], Item, ItemSize); // // Adjust the tail pointer of the FIFO keyboard buffer. // - KeyboardBuffer->BufferTail = (UINT8) ((KeyboardBuffer->BufferTail + 1) % (MAX_KEY_ALLOWED + 1)); + Queue->Tail = (Queue->Tail + 1) % (MAX_KEY_ALLOWED + 1); } /** - Remove a keycode from keyboard buffer and return it. + Dequeue a item from the queue. - @param KeyboardBuffer Points to the USB keyboard buffer. - @param UsbKey Points to the buffer that contains keycode for output. + @param Queue Points to the queue. + @param Item Receives the item. + @param ItemSize Size of the item. - @retval EFI_SUCCESS Keycode successfully removed from keyboard buffer. - @retval EFI_DEVICE_ERROR Keyboard buffer is empty. + @retval EFI_SUCCESS Item was successfully dequeued. + @retval EFI_DEVICE_ERROR The queue is empty. **/ EFI_STATUS -EFIAPI -RemoveKeyCode ( - IN OUT USB_KB_BUFFER *KeyboardBuffer, - OUT USB_KEY *UsbKey +Dequeue ( + IN OUT USB_SIMPLE_QUEUE *Queue, + OUT VOID *Item, + IN UINTN ItemSize ) { - if (IsUSBKeyboardBufferEmpty (KeyboardBuffer)) { + ASSERT (Queue->ItemSize == ItemSize); + + if (IsQueueEmpty (Queue)) { return EFI_DEVICE_ERROR; } - ASSERT (KeyboardBuffer->BufferHead <= MAX_KEY_ALLOWED); - - UsbKey->KeyCode = KeyboardBuffer->Buffer[KeyboardBuffer->BufferHead].KeyCode; - UsbKey->Down = KeyboardBuffer->Buffer[KeyboardBuffer->BufferHead].Down; + CopyMem (Item, Queue->Buffer[Queue->Head], ItemSize); // // Adjust the head pointer of the FIFO keyboard buffer. // - KeyboardBuffer->BufferHead = (UINT8) ((KeyboardBuffer->BufferHead + 1) % (MAX_KEY_ALLOWED + 1)); + Queue->Head = (Queue->Head + 1) % (MAX_KEY_ALLOWED + 1); return EFI_SUCCESS; } @@ -1903,7 +1859,6 @@ RemoveKeyCode ( **/ VOID -EFIAPI SetKeyLED ( IN USB_KB_DEV *UsbKeyboardDevice ) @@ -1955,6 +1910,7 @@ USBKeyboardRepeatHandler ( ) { USB_KB_DEV *UsbKeyboardDevice; + USB_KEY UsbKey; UsbKeyboardDevice = (USB_KB_DEV *) Context; @@ -1965,11 +1921,9 @@ USBKeyboardRepeatHandler ( // // Inserts the repeat key into keyboard buffer, // - InsertKeyCode ( - &(UsbKeyboardDevice->KeyboardBuffer), - UsbKeyboardDevice->RepeatKey, - TRUE - ); + UsbKey.KeyCode = UsbKeyboardDevice->RepeatKey; + UsbKey.Down = TRUE; + Enqueue (&UsbKeyboardDevice->UsbKeyQueue, &UsbKey, sizeof (UsbKey)); // // Set repeat rate for next repeat key generation.