Skip to content
This repository was archived by the owner on Mar 28, 2025. It is now read-only.

Commit 0984a02

Browse files
Agyeman, Princemakubacki
Agyeman, Prince
authored andcommitted
BoardModulePkg: Add Ps2 Keyboard Library
Added a generic Ps2 keyboard library that adds ps2 device path to ConIn and ConInDev Uefi variables. Cc: Michael Kubacki <[email protected]> Cc: Chasel Chiu <[email protected]> Cc: Nate DeSimone <[email protected]> Signed-off-by: Prince Agyeman <[email protected]> Reviewed-by: Chasel Chiu <[email protected]> Reviewed-by: Nate DeSimone <[email protected]> Reviewed-by: Michael Kubacki <[email protected]>
1 parent eb4e08a commit 0984a02

File tree

4 files changed

+306
-0
lines changed

4 files changed

+306
-0
lines changed

Diff for: Platform/Intel/BoardModulePkg/BoardModulePkg.dsc

+1
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,4 @@
8888
BoardModulePkg/Library/BiosIdLib/PeiBiosIdLib.inf
8989

9090
BoardModulePkg/Library/PeiFirmwareBootMediaInfoLib/PeiFirmwareBootMediaInfoLib.inf
91+
BoardModulePkg/Library/BdsPs2KbcLib/BdsPs2KbcLib.inf
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/** @file
2+
Header file for the 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+
#ifndef _PS2_KBC_LIB_H
10+
#define _PS2_KBC_LIB_H
11+
12+
#include <Uefi.h>
13+
#include <Library/UefiLib.h>
14+
#include <Library/DevicePathLib.h>
15+
#include <Library/DebugLib.h>
16+
#include <Library/IoLib.h>
17+
#include <Library/TimerLib.h>
18+
#include <Library/UefiBootManagerLib.h>
19+
20+
//
21+
// Below is the platform console device path
22+
//
23+
typedef struct {
24+
ACPI_HID_DEVICE_PATH PciRootBridge;
25+
PCI_DEVICE_PATH IsaBridge;
26+
ACPI_HID_DEVICE_PATH Keyboard;
27+
EFI_DEVICE_PATH_PROTOCOL End;
28+
} PLATFORM_KEYBOARD_DEVICE_PATH;
29+
30+
typedef struct {
31+
UINT8 Segment;
32+
UINT8 Bus;
33+
UINT8 Device;
34+
UINT8 Funtion;
35+
} SIO_PCI_ISA_BRIDGE_DEVICE_INFO;
36+
37+
#define gPciRootBridge \
38+
{ \
39+
{ \
40+
ACPI_DEVICE_PATH, \
41+
ACPI_DP, \
42+
{ \
43+
(UINT8) (sizeof (ACPI_HID_DEVICE_PATH)), \
44+
(UINT8) ((sizeof (ACPI_HID_DEVICE_PATH)) >> 8) \
45+
}, \
46+
}, \
47+
EISA_PNP_ID (0x0A03), \
48+
0 \
49+
}
50+
51+
#define gEndEntire \
52+
{ \
53+
END_DEVICE_PATH_TYPE, END_ENTIRE_DEVICE_PATH_SUBTYPE, { END_DEVICE_PATH_LENGTH, 0 } \
54+
}
55+
56+
#define KBC_INPBUF_VIA60_KBECHO 0xEE
57+
#define KEYBOARD_8042_DATA_REGISTER 0x60
58+
#define KEYBOARD_8042_STATUS_REGISTER 0x64
59+
60+
#define PS2_KEYBOARD_TIMEOUT 65536 // 0.07s
61+
#define PS2_KEYBOARD_WAITFORVALUE_TIMEOUT 1000000 // 1s
62+
#define PS2_KEYBOARD_KBEN 0xF4
63+
#define PS2_KEYBOARD_CMDECHO_ACK 0xFA
64+
65+
#endif
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
## @file
2+
# Component information file for Ps2 keyboard controller library
3+
#
4+
# Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>
5+
#
6+
# SPDX-License-Identifier: BSD-2-Clause-Patent
7+
#
8+
##
9+
[Defines]
10+
INF_VERSION = 0x00010006
11+
BASE_NAME = BdsPs2KbcLib
12+
FILE_GUID = E94EA52E-E84C-42E7-B863-EA1327EFA265
13+
MODULE_TYPE = UEFI_DRIVER
14+
VERSION_STRING = 1.2
15+
LIBRARY_CLASS = NULL|UEFI_DRIVER
16+
CONSTRUCTOR = BdsPs2KbcLibConstructor
17+
18+
[Packages]
19+
MdePkg/MdePkg.dec
20+
MdeModulePkg/MdeModulePkg.dec
21+
BoardModulePkg/BoardModulePkg.dec
22+
23+
[Sources]
24+
BdsPs2KbcLib.c
25+
BdsPs2KbcLib.h
26+
27+
[LibraryClasses]
28+
DevicePathLib
29+
DebugLib
30+
IoLib
31+
UefiDriverEntryPoint
32+
UefiBootManagerLib
33+
UefiLib
34+
TimerLib
35+
36+
[Pcd]
37+
gBoardModulePkgTokenSpaceGuid.PcdPs2KbMsEnable
38+
gBoardModulePkgTokenSpaceGuid.PcdSuperIoPciIsaBridgeDevice

0 commit comments

Comments
 (0)