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