/** @file
Routines that access 8042 keyboard controller
Copyright (c) 2006 - 2011, Intel Corporation. All rights reserved.<BR>
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
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 "Ps2Keyboard.h"
struct {
}
{
0x01, // Escape
0x0000,
0x0000
},
{
0x02,
L'1',
L'!'
},
{
0x03,
L'2',
L'@'
},
{
0x04,
L'3',
L'#'
},
{
0x05,
L'4',
L'$'
},
{
0x06,
L'5',
L'%'
},
{
0x07,
L'6',
L'^'
},
{
0x08,
L'7',
L'&'
},
{
0x09,
L'8',
L'*'
},
{
0x0A,
L'9',
L'('
},
{
0x0B,
L'0',
L')'
},
{
0x0C,
L'-',
L'_'
},
{
0x0D,
L'=',
L'+'
},
{
0x0E, // BackSpace
0x0008,
0x0008
},
{
0x0F, // Tab
0x0009,
0x0009
},
{
0x10,
L'q',
L'Q'
},
{
0x11,
L'w',
L'W'
},
{
0x12,
L'e',
L'E'
},
{
0x13,
L'r',
L'R'
},
{
0x14,
L't',
L'T'
},
{
0x15,
L'y',
L'Y'
},
{
0x16,
L'u',
L'U'
},
{
0x17,
L'i',
L'I'
},
{
0x18,
L'o',
L'O'
},
{
0x19,
L'p',
L'P'
},
{
0x1a,
L'[',
L'{'
},
{
0x1b,
L']',
L'}'
},
{
0x1c, // Enter
0x000d,
0x000d
},
{
0x1d,
0x0000,
0x0000
},
{
0x1e,
L'a',
L'A'
},
{
0x1f,
L's',
L'S'
},
{
0x20,
L'd',
L'D'
},
{
0x21,
L'f',
L'F'
},
{
0x22,
L'g',
L'G'
},
{
0x23,
L'h',
L'H'
},
{
0x24,
L'j',
L'J'
},
{
0x25,
L'k',
L'K'
},
{
0x26,
L'l',
L'L'
},
{
0x27,
L';',
L':'
},
{
0x28,
L'\'',
L'"'
},
{
0x29,
L'`',
L'~'
},
{
0x2a, // Left Shift
0x0000,
0x0000
},
{
0x2b,
L'\\',
L'|'
},
{
0x2c,
L'z',
L'Z'
},
{
0x2d,
L'x',
L'X'
},
{
0x2e,
L'c',
L'C'
},
{
0x2f,
L'v',
L'V'
},
{
0x30,
L'b',
L'B'
},
{
0x31,
L'n',
L'N'
},
{
0x32,
L'm',
L'M'
},
{
0x33,
L',',
L'<'
},
{
0x34,
L'.',
L'>'
},
{
0x35,
L'/',
L'?'
},
{
0x36, //Right Shift
0x0000,
0x0000
},
{
0x37, // Numeric Keypad *
L'*',
L'*'
},
{
0x0000,
0x0000
},
{
0x39,
L' ',
L' '
},
{
0x3A, //CapsLock
0x0000,
0x0000
},
{
0x3B,
0x0000,
0x0000
},
{
0x3C,
0x0000,
0x0000
},
{
0x3D,
0x0000,
0x0000
},
{
0x3E,
0x0000,
0x0000
},
{
0x3F,
0x0000,
0x0000
},
{
0x40,
0x0000,
0x0000
},
{
0x41,
0x0000,
0x0000
},
{
0x42,
0x0000,
0x0000
},
{
0x43,
0x0000,
0x0000
},
{
0x44,
0x0000,
0x0000
},
{
0x45, // NumLock
0x0000,
0x0000
},
{
0x46, // ScrollLock
0x0000,
0x0000
},
{
0x47,
L'7',
L'7'
},
{
0x48,
L'8',
L'8'
},
{
0x49,
L'9',
L'9'
},
{
0x4a,
L'-',
L'-'
},
{
0x4b,
L'4',
L'4'
},
{
0x4c, // Numeric Keypad 5
L'5',
L'5'
},
{
0x4d,
L'6',
L'6'
},
{
0x4e,
L'+',
L'+'
},
{
0x4f,
L'1',
L'1'
},
{
0x50,
L'2',
L'2'
},
{
0x51,
L'3',
L'3'
},
{
0x52,
L'0',
L'0'
},
{
0x53,
L'.',
L'.'
},
{
0x57,
0x0000,
0x0000
},
{
0x58,
0x0000,
0x0000
},
{
0x5B, //Left LOGO
0x0000,
0x0000
},
{
0x5C, //Right LOGO
0x0000,
0x0000
},
{
0x5D, //Menu key
0x0000,
0x0000
},
{
},
};
//
// The WaitForValue time out
//
/**
Return the count of scancode in the queue.
@param Queue Pointer to instance of SCAN_CODE_QUEUE.
@return Count of the scancode.
**/
)
{
} else {
}
}
/**
Read several bytes from the scancode buffer without removing them.
This function is called to see if there are enough bytes of scancode
representing a single key.
@param Queue Pointer to instance of SCAN_CODE_QUEUE.
@param Count Number of bytes to be read
@param Buf Store the results
@retval EFI_SUCCESS success to scan the keyboard code
@retval EFI_NOT_READY invalid parameter
**/
)
{
//
// check the valid range of parameter 'Count'
//
return EFI_NOT_READY;
}
//
// retrieve the values
//
for (Index = 0, Pos = Queue->Head; Index < Count; Index++, Pos = (Pos + 1) % KEYBOARD_SCAN_CODE_MAX_COUNT) {
}
return EFI_SUCCESS;
}
/**
Read & remove several bytes from the scancode buffer.
This function is usually called after GetScancodeBufHead()
@param Queue Pointer to instance of SCAN_CODE_QUEUE.
@param Count Number of bytes to be read
@param Buf Store the results
@retval EFI_SUCCESS success to scan the keyboard code
@retval EFI_NOT_READY invalid parameter
**/
)
{
//
// Check the valid range of parameter 'Count'
//
return EFI_NOT_READY;
}
//
// Retrieve and remove the values
//
for (Index = 0; Index < Count; Index++, Queue->Head = (Queue->Head + 1) % KEYBOARD_SCAN_CODE_MAX_COUNT) {
}
}
return EFI_SUCCESS;
}
/**
Push one byte to the scancode buffer.
@param Queue Pointer to instance of SCAN_CODE_QUEUE.
@param Scancode The byte to push.
**/
)
{
}
}
/**
Read data register .
@param ConsoleIn Pointer to instance of KEYBOARD_CONSOLE_IN_DEV
@return return the value
**/
)
{
//
// Use IsaIo protocol to perform IO operations
//
1,
&Data
);
return Data;
}
/**
Write data register.
@param ConsoleIn Pointer to instance of KEYBOARD_CONSOLE_IN_DEV
@param Data value wanted to be written
**/
)
{
1,
&Data
);
}
/**
Read status register.
@param ConsoleIn Pointer to instance of KEYBOARD_CONSOLE_IN_DEV
@return value in status register
**/
)
{
1,
&Data
);
return Data;
}
/**
Write command register .
@param ConsoleIn Pointer to instance of KEYBOARD_CONSOLE_IN_DEV
@param Data The value wanted to be written
**/
)
{
1,
&Data
);
}
/**
Display error message.
@param ConsoleIn Pointer to instance of KEYBOARD_CONSOLE_IN_DEV
@param ErrMsg Unicode string of error message
**/
)
{
}
/**
Timer event handler: read a series of scancodes from 8042
and put them into memory scancode buffer.
it read as much scancodes to either fill
the memory buffer or empty the keyboard buffer.
It is registered as running under TPL_NOTIFY
@param Event The timer event
@param Context A KEYBOARD_CONSOLE_IN_DEV pointer
**/
)
{
//
// Enter critical section
//
//
// Leave critical section and return
//
return ;
}
//
// To let KB driver support Hot plug, here should skip the 'resend' command for the case that
// KB is not connected to system. If KB is not connected to system, driver will find there's something
// error in the following code and wait for the input buffer empty, this waiting time shoulb be short enough since
// this is a NOTIFY TPL period function, or the system performance will degrade hardly when KB is not connected.
// Just skip the 'resend' process simply.
//
while ((KeyReadStatusRegister (ConsoleIn) & (KEYBOARD_STATUS_REGISTER_TRANSMIT_TIMEOUT|KEYBOARD_STATUS_REGISTER_HAS_OUTPUT_DATA)) ==
) {
//
// Read one byte of the scan code and store it into the memory buffer
//
}
//
// Leave critical section and return
//
}
/**
Read key value .
@param ConsoleIn - Pointer to instance of KEYBOARD_CONSOLE_IN_DEV
@param Data - Pointer to outof buffer for keeping key value
@retval EFI_TIMEOUT Status resigter time out
@retval EFI_SUCCESS Success to read keyboard
**/
)
{
TimeOut = 0;
RegFilled = 0;
//
// wait till output buffer full then perform the read
//
RegFilled = 1;
break;
}
MicroSecondDelay (30);
}
if (RegFilled == 0) {
return EFI_TIMEOUT;
}
return EFI_SUCCESS;
}
/**
write key to keyboard
@param ConsoleIn Pointer to instance of KEYBOARD_CONSOLE_IN_DEV
@param Data value wanted to be written
@retval EFI_TIMEOUT The input buffer register is full for putting new value util timeout
@retval EFI_SUCCESS The new value is sucess put into input buffer register.
**/
)
{
TimeOut = 0;
RegEmptied = 0;
//
// wait for input buffer empty
//
RegEmptied = 1;
break;
}
MicroSecondDelay (30);
}
if (RegEmptied == 0) {
return EFI_TIMEOUT;
}
//
// Write it
//
return EFI_SUCCESS;
}
/**
Issue keyboard command.
@param ConsoleIn Pointer to instance of KEYBOARD_CONSOLE_IN_DEV
@param Data The buff holding the command
@retval EFI_TIMEOUT Keyboard is not ready to issuing
@retval EFI_SUCCESS Success to issue keyboard command
**/
)
{
TimeOut = 0;
RegEmptied = 0;
//
// Wait For Input Buffer Empty
//
RegEmptied = 1;
break;
}
MicroSecondDelay (30);
}
if (RegEmptied == 0) {
return EFI_TIMEOUT;
}
//
// issue the command
//
//
// Wait For Input Buffer Empty again
//
RegEmptied = 0;
RegEmptied = 1;
break;
}
MicroSecondDelay (30);
}
if (RegEmptied == 0) {
return EFI_TIMEOUT;
}
return EFI_SUCCESS;
}
/**
wait for a specific value to be presented on
8042 Data register by keyboard and then read it,
used in keyboard commands ack
@param ConsoleIn Pointer to instance of KEYBOARD_CONSOLE_IN_DEV
@param Value the value wanted to be waited.
@retval EFI_TIMEOUT Fail to get specific value in given time
@retval EFI_SUCCESS Success to get specific value in given time.
**/
)
{
GotIt = 0;
TimeOut = 0;
SumTimeOut = 0;
//
// Make sure the initial value of 'Data' is different from 'Value'
//
Data = 0;
Data = 1;
}
//
// Read from 8042 (multiple times if needed)
// until the expected value appears
// use SumTimeOut to control the iteration
//
while (1) {
//
// Perform a read
//
break;
}
MicroSecondDelay (30);
}
SumTimeOut += TimeOut;
GotIt = 1;
break;
}
if (SumTimeOut >= mWaitForValueTimeOut) {
break;
}
}
//
// Check results
//
if (GotIt == 1) {
return EFI_SUCCESS;
} else {
return EFI_TIMEOUT;
}
}
/**
Show keyboard status lights according to
indicators in ConsoleIn.
@param ConsoleIn Pointer to instance of KEYBOARD_CONSOLE_IN_DEV
@return status of updating keyboard register
**/
)
{
//
// Send keyboard command
//
return Status;
}
//
// Light configuration
//
Command = 0;
Command |= 4;
}
Command |= 2;
}
if (ConsoleIn->ScrollLock) {
Command |= 1;
}
return Status;
}
return Status;
}
/**
Get scancode from scancode buffer and translate into EFI-scancode and unicode defined by EFI spec.
The function is always called in TPL_NOTIFY.
@param ConsoleIn KEYBOARD_CONSOLE_IN_DEV instance pointer
**/
)
{
//
// 3 bytes most
//
//
// Check if there are enough bytes of scancode representing a single key
// available in the buffer
//
while (TRUE) {
ScancodeArrPos = 0;
return ;
}
//
// E0 to look ahead 2 bytes
//
ScancodeArrPos = 1;
return ;
}
//
// E1 to look ahead 3 bytes
//
ScancodeArrPos = 2;
return ;
}
}
//
// if we reach this position, scancodes for a key is in buffer now,pop them
//
//
// store the last available byte, this byte of scancode will be checked
//
if (!Extend1) {
//
// Check for special keys and update the driver state.
//
switch (ScanCode) {
case SCANCODE_CTRL_MAKE:
if (Extend0) {
} else {
}
break;
case SCANCODE_CTRL_BREAK:
if (Extend0) {
} else {
}
break;
case SCANCODE_ALT_MAKE:
if (Extend0) {
} else {
}
break;
case SCANCODE_ALT_BREAK:
if (Extend0) {
} else {
}
break;
case SCANCODE_LEFT_SHIFT_MAKE:
//
// To avoid recognize PRNT_SCRN key as a L_SHIFT key
// because PRNT_SCRN key generates E0 followed by L_SHIFT scan code.
// If it the second byte of the PRNT_ScRN skip it.
//
if (!Extend0) {
break;
}
continue;
if (!Extend0) {
}
break;
break;
break;
case SCANCODE_LEFT_LOGO_MAKE:
break;
case SCANCODE_LEFT_LOGO_BREAK:
break;
case SCANCODE_RIGHT_LOGO_MAKE:
break;
break;
case SCANCODE_MENU_MAKE:
break;
case SCANCODE_MENU_BREAK:
break;
case SCANCODE_SYS_REQ_MAKE:
if (Extend0) {
}
break;
case SCANCODE_SYS_REQ_BREAK:
if (Extend0) {
}
break;
break;
break;
case SCANCODE_CAPS_LOCK_MAKE:
break;
case SCANCODE_NUM_LOCK_MAKE:
break;
if (!Extend0) {
}
break;
}
}
//
// If this is above the valid range, ignore it
//
if (ScanCode >= SCANCODE_MAX_MAKE) {
continue;
} else {
break;
}
}
//
// Handle Ctrl+Alt+Del hotkey
//
) {
}
//
//
);
);
//
// Key Pad "/" shares the same scancode as that of "/" except Key Pad "/" has E0 prefix
//
//
// PAUSE shares the same scancode as that of NUM except PAUSE has E1 prefix
//
//
// PAUSE shares the same scancode as that of SCROLL except PAUSE (CTRL pressed) has E0 prefix
//
//
// PRNT_SCRN shares the same scancode as that of Key Pad "*" except PRNT_SCRN has E0 prefix
//
//
// Except the above special case, all others can be handled by convert table
//
} else {
(ConvertKeyboardScanCodeToEfiKey[Index].UnicodeChar != ConvertKeyboardScanCodeToEfiKey[Index].ShiftUnicodeChar)) {
//
// 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'
//
}
//
// alphabetic key is affected by CapsLock State
//
}
}
break;
}
}
}
//
// distinguish numeric key pad keys' 'up symbol' and 'down symbol'
//
}
}
//
// If the key can not be converted then just return.
//
if (!ConsoleIn->IsSupportPartialKey) {
return ;
}
}
//
// Invoke notification functions if exist
//
for (Link = GetFirstNode (&ConsoleIn->NotifyList); !IsNull (&ConsoleIn->NotifyList, Link); Link = GetNextNode (&ConsoleIn->NotifyList, Link)) {
CurrentNotify = CR (
Link,
);
}
}
}
/**
Perform 8042 controller and keyboard Initialization.
If ExtendedVerification is TRUE, do additional test for
the keyboard interface
@param ConsoleIn - KEYBOARD_CONSOLE_IN_DEV instance pointer
@param ExtendedVerification - indicates a thorough initialization
@retval EFI_DEVICE_ERROR Fail to init keyboard
@retval EFI_SUCCESS Success to init keyboard
**/
)
{
TryTime = 0;
//
// Get Ps2 policy to set this
//
NULL,
);
);
//
// Perform a read to cleanup the Status Register's
// output buffer full bits within MAX TRY times
//
TryTime ++;
}
//
// Exceed the max try times. The device may be error.
//
if (TryTime == KEYBOARD_MAX_TRY) {
goto Done;
}
//
// We should disable mouse interface during the initialization process
// since mouse device output could block keyboard device output in the
// 60H port of 8042 controller.
//
// So if we are not initializing 8042 controller for the
// first time, we have to remember the previous mouse interface
// enabling state
//
// Test the system flag in to determine whether this is the first
// time initialization
//
//
// 8042 controller is already setup (by myself or by mouse driver):
// See whether mouse interface is already enabled
// which determines whether we should enable it later
//
//
// Read the command byte of 8042 controller
//
goto Done;
}
goto Done;
}
//
// Test the mouse enabling bit
//
if ((CommandByte & 0x20) != 0) {
} else {
}
} else {
//
// 8042 controller is not setup yet:
// 8042 controller selftest;
// Don't enable mouse interface later.
//
//
// Disable keyboard and mouse interfaces
//
goto Done;
}
goto Done;
}
);
//
// 8042 Controller Self Test
//
goto Done;
}
goto Done;
}
//
// Don't enable mouse interface later
//
}
}
//
// Write 8042 Command Byte, set System Flag
// While at the same time:
// 1. disable mouse interface,
// 2. enable kbd interface,
// 4. enable mouse and kbd interrupts
//
// ( Command Byte bits:
// 7: Reserved
// 5: Disable Auxiliary device interface
// 4: Disable keyboard interface
// 3: Reserved
// 2: System Flag
// 1: Enable Auxiliary device interrupt
// 0: Enable Keyboard interrupt )
//
goto Done;
}
goto Done;
}
//
// Clear Memory Scancode Buffer
//
//
// Reset the status indicators
//
//
// For reseting keyboard is not mandatory before booting OS and sometimes keyboard responses very slow,
// and to support KB hot plug, we need to let the InitKB succeed no matter whether there is a KB device connected
// to system. So we only do the real reseting for keyboard when user asks and there is a real KB connected t system,
// and normally during booting an OS, it's skipped.
//
//
// Additional verifications for keyboard interface
//
//
// Keyboard Interface Test
//
goto Done;
}
L"Some specific value not aquired from 8042 controller!\n\r"
);
goto Done;
}
//
// Keyboard reset with a BAT(Basic Assurance Test)
//
goto Done;
}
goto Done;
}
//
// wait for BAT completion code
//
goto Done;
}
//
// Set Keyboard to use Scan Code Set 2
//
goto Done;
}
goto Done;
}
goto Done;
}
goto Done;
}
//
// Clear Keyboard Scancode Buffer
//
goto Done;
}
goto Done;
}
//
}
}
}
}
//
// Update Keyboard Lights
//
goto Done;
}
}
//
// At last, we can now enable the mouse interface if appropriate
//
Done:
if (mEnableMouseInterface) {
//
// Enable mouse interface
//
return EFI_DEVICE_ERROR;
}
}
return EFI_SUCCESS;
} else {
return EFI_DEVICE_ERROR;
}
}
/**
Disable the keyboard interface of the 8042 controller.
@param ConsoleIn The device instance
@return status of issuing disable command
**/
)
{
//
// Disable keyboard interface
//
return EFI_DEVICE_ERROR;
}
return Status;
}
/**
Check whether there is Ps/2 Keyboard device in system by 0xF4 Keyboard Command
If Keyboard receives 0xF4, it will respond with 'ACK'. If it doesn't respond, the device
should not be in system.
@param[in] ConsoleIn Keyboard Private Data Structure
@retval TRUE Keyboard in System.
@retval FALSE Keyboard not in System.
**/
)
{
//
// enable keyboard itself and wait for its ack
// If can't receive ack, Keyboard should not be connected.
//
Status = KeyboardWrite (
);
return FALSE;
}
//
// wait for 1s
//
);
return FALSE;
}
return TRUE;
}