|
| 1 | +/** @file |
| 2 | + Main file for Ps2 keyboard controller library. |
| 3 | +
|
| 4 | + Copyright (c) 2019, Intel Corporation. All rights reserved.<BR> |
| 5 | + SPDX-License-Identifier: BSD-2-Clause-Patent |
| 6 | +
|
| 7 | +**/ |
| 8 | + |
| 9 | +#include "BdsPs2KbcLib.h" |
| 10 | + |
| 11 | +GLOBAL_REMOVE_IF_UNREFERENCED PLATFORM_KEYBOARD_DEVICE_PATH gKeyboardDevicePath = { |
| 12 | + gPciRootBridge, |
| 13 | + { |
| 14 | + { |
| 15 | + HARDWARE_DEVICE_PATH, |
| 16 | + HW_PCI_DP, |
| 17 | + { |
| 18 | + (UINT8) (sizeof (PCI_DEVICE_PATH)), |
| 19 | + (UINT8) ((sizeof (PCI_DEVICE_PATH)) >> 8) |
| 20 | + } |
| 21 | + }, |
| 22 | + 0, // Function, patched in EnumPs2Keyboard |
| 23 | + 0 // Device, patched in EnumPs2Keyboard |
| 24 | + }, |
| 25 | + { |
| 26 | + { |
| 27 | + ACPI_DEVICE_PATH, |
| 28 | + ACPI_DP, |
| 29 | + { |
| 30 | + (UINT8) (sizeof (ACPI_HID_DEVICE_PATH)), |
| 31 | + (UINT8) ((sizeof (ACPI_HID_DEVICE_PATH)) >> 8) |
| 32 | + } |
| 33 | + }, |
| 34 | + EISA_PNP_ID(0x0303), |
| 35 | + 0 |
| 36 | + }, |
| 37 | + gEndEntire |
| 38 | +}; |
| 39 | + |
| 40 | +/** |
| 41 | + Check if PS2 keyboard is conntected, by sending ECHO command. |
| 42 | + @retval TRUE if connected FALSE otherwise |
| 43 | +**/ |
| 44 | +BOOLEAN |
| 45 | +DetectPs2Keyboard ( |
| 46 | + VOID |
| 47 | + ) |
| 48 | +{ |
| 49 | + UINT32 TimeOut; |
| 50 | + UINT32 RegEmptied; |
| 51 | + UINT8 Data; |
| 52 | + UINT32 SumTimeOut; |
| 53 | + BOOLEAN FoundPs2Kbc; |
| 54 | + |
| 55 | + TimeOut = 0; |
| 56 | + RegEmptied = 0; |
| 57 | + FoundPs2Kbc = FALSE; |
| 58 | + |
| 59 | + // |
| 60 | + // Wait for input buffer empty |
| 61 | + // |
| 62 | + for (TimeOut = 0; TimeOut < PS2_KEYBOARD_TIMEOUT; TimeOut += 30) { |
| 63 | + if ((IoRead8 (KEYBOARD_8042_STATUS_REGISTER) & 0x02) == 0) { |
| 64 | + FoundPs2Kbc = TRUE; |
| 65 | + break; |
| 66 | + } |
| 67 | + MicroSecondDelay (30); |
| 68 | + } |
| 69 | + |
| 70 | + if (FoundPs2Kbc == FALSE) { |
| 71 | + return FALSE; |
| 72 | + } |
| 73 | + |
| 74 | + // |
| 75 | + // Send echo command |
| 76 | + // |
| 77 | + IoWrite8 (KEYBOARD_8042_DATA_REGISTER, KBC_INPBUF_VIA60_KBECHO); |
| 78 | + |
| 79 | + // |
| 80 | + // Init variables |
| 81 | + // |
| 82 | + FoundPs2Kbc = FALSE; |
| 83 | + TimeOut = 0; |
| 84 | + SumTimeOut = 0; |
| 85 | + Data = 0; |
| 86 | + |
| 87 | + // |
| 88 | + // Read from 8042 (multiple times if needed) |
| 89 | + // until the expected value appears |
| 90 | + // use SumTimeOut to control the iteration |
| 91 | + // |
| 92 | + while (1) { |
| 93 | + // |
| 94 | + // Perform a read |
| 95 | + // |
| 96 | + for (TimeOut = 0; TimeOut < PS2_KEYBOARD_TIMEOUT; TimeOut += 30) { |
| 97 | + if (IoRead8 (KEYBOARD_8042_STATUS_REGISTER) & 0x01) { |
| 98 | + Data = IoRead8 (KEYBOARD_8042_DATA_REGISTER); |
| 99 | + break; |
| 100 | + } |
| 101 | + MicroSecondDelay (30); |
| 102 | + } |
| 103 | + |
| 104 | + SumTimeOut += TimeOut; |
| 105 | + |
| 106 | + if (Data == KBC_INPBUF_VIA60_KBECHO) { |
| 107 | + FoundPs2Kbc = TRUE; |
| 108 | + break; |
| 109 | + } |
| 110 | + |
| 111 | + if (SumTimeOut >= PS2_KEYBOARD_WAITFORVALUE_TIMEOUT) { |
| 112 | + break; |
| 113 | + } |
| 114 | + } |
| 115 | + return FoundPs2Kbc; |
| 116 | +} |
| 117 | + |
| 118 | +/** |
| 119 | + Check if PS2 keyboard is conntected. If the result of first time is |
| 120 | + error, it will retry again. |
| 121 | + @retval TRUE if connected FALSE otherwise |
| 122 | +**/ |
| 123 | +BOOLEAN |
| 124 | +IsPs2KeyboardConnected ( |
| 125 | + VOID |
| 126 | + ) |
| 127 | +{ |
| 128 | + BOOLEAN Result; |
| 129 | + Result = DetectPs2Keyboard (); |
| 130 | + |
| 131 | + if (Result == FALSE) { |
| 132 | + // |
| 133 | + // If there is no ps2 keyboard detected for the 1st time, retry again. |
| 134 | + // |
| 135 | + Result = DetectPs2Keyboard (); |
| 136 | + } |
| 137 | + return Result; |
| 138 | +} |
| 139 | + |
| 140 | + |
| 141 | +/** |
| 142 | + Updates the ConIn variable with Ps2 Keyboard device path, |
| 143 | + if it doesn't already exists in ConIn and ConInDev |
| 144 | +**/ |
| 145 | +VOID |
| 146 | +AddPs2Keyboard ( |
| 147 | + VOID |
| 148 | + ) |
| 149 | +{ |
| 150 | + SIO_PCI_ISA_BRIDGE_DEVICE_INFO *SioIsaInfo; |
| 151 | + |
| 152 | + DEBUG ((DEBUG_INFO, "[AddPs2Keyboard]\n")); |
| 153 | + |
| 154 | + SioIsaInfo = (SIO_PCI_ISA_BRIDGE_DEVICE_INFO*) FixedPcdGetPtr (PcdSuperIoPciIsaBridgeDevice); |
| 155 | + |
| 156 | + // |
| 157 | + // patch IsaBridge device and and function |
| 158 | + // |
| 159 | + gKeyboardDevicePath.IsaBridge.Device = SioIsaInfo->Device; |
| 160 | + gKeyboardDevicePath.IsaBridge.Function = SioIsaInfo->Funtion; |
| 161 | + |
| 162 | + // |
| 163 | + // Append Ps2 Keyboard into "ConIn" |
| 164 | + // |
| 165 | + EfiBootManagerUpdateConsoleVariable (ConIn, (EFI_DEVICE_PATH_PROTOCOL *) &gKeyboardDevicePath, NULL); |
| 166 | + |
| 167 | + // |
| 168 | + // Append Ps2 Keyboard into "ConInDev" |
| 169 | + // |
| 170 | + EfiBootManagerUpdateConsoleVariable (ConInDev, (EFI_DEVICE_PATH_PROTOCOL *) &gKeyboardDevicePath, NULL); |
| 171 | +} |
| 172 | + |
| 173 | + |
| 174 | +/** |
| 175 | + Constructor for the Ps2 keyboard controller library. |
| 176 | +
|
| 177 | + @param ImageHandle the image handle of the process |
| 178 | + @param SystemTable the EFI System Table pointer |
| 179 | +
|
| 180 | + @retval EFI_SUCCESS the shell command handlers were installed sucessfully |
| 181 | + @retval EFI_UNSUPPORTED the shell level required was not found. |
| 182 | +**/ |
| 183 | +EFI_STATUS |
| 184 | +EFIAPI |
| 185 | +BdsPs2KbcLibConstructor ( |
| 186 | + IN EFI_HANDLE ImageHandle, |
| 187 | + IN EFI_SYSTEM_TABLE *SystemTable |
| 188 | + ) |
| 189 | +{ |
| 190 | + UINT8 Ps2KbMsEnable; |
| 191 | + |
| 192 | + Ps2KbMsEnable = PcdGet8 (PcdPs2KbMsEnable); |
| 193 | + |
| 194 | + if (Ps2KbMsEnable == 0x1 |
| 195 | + && IsPs2KeyboardConnected()) |
| 196 | + { |
| 197 | + // add ps2 device path to ConIn and ConInDev |
| 198 | + AddPs2Keyboard (); |
| 199 | + } |
| 200 | + |
| 201 | + return EFI_SUCCESS; |
| 202 | +} |
0 commit comments