ps2kbdctrller.c

来自「EFI BIOS是Intel提出的下一代的BIOS标准。这里上传的Edk源代码是」· C语言 代码 · 共 1,781 行 · 第 1/3 页

C
1,781
字号
/*++

Copyright (c) 2006, 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                                            
                                                                                          
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,                     
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.


Module Name:

  Ps2KbdCtrller.c

Abstract:
  PS/2 Keyboard driver
  Routines that access 8042 keyboard controller

Revision History

--*/

#include "Ps2Keyboard.h"

//
// Function declarations
//
STATIC
UINT8
KeyReadDataRegister (
  IN KEYBOARD_CONSOLE_IN_DEV *ConsoleIn
  );

STATIC
VOID
KeyWriteDataRegister (
  IN KEYBOARD_CONSOLE_IN_DEV *ConsoleIn,
  IN UINT8                   Data
  );

STATIC
VOID
KeyWriteCommandRegister (
  IN KEYBOARD_CONSOLE_IN_DEV *ConsoleIn,
  IN UINT8                   Data
  );

STATIC
VOID
KeyboardError (
  IN KEYBOARD_CONSOLE_IN_DEV*ConsoleIn,
  IN CHAR16                 *ErrMsg // should be a unicode string
  );

STATIC
EFI_STATUS
GetScancodeBufHead (
  KEYBOARD_CONSOLE_IN_DEV  *ConsoleIn,
  IN UINT32                Count,
  OUT UINT8                *Buf
  );

STATIC
EFI_STATUS
PopScancodeBufHead (
  KEYBOARD_CONSOLE_IN_DEV  *ConsoleIn,
  IN UINT32                Count,
  OUT UINT8                *Buf
  );

STATIC
EFI_STATUS
KeyboardWrite (
  IN KEYBOARD_CONSOLE_IN_DEV *ConsoleIn,
  IN UINT8                   Data
  );

STATIC
EFI_STATUS
KeyboardCommand (
  IN KEYBOARD_CONSOLE_IN_DEV *ConsoleIn,
  IN UINT8                   Data
  );

STATIC
EFI_STATUS
KeyboardWaitForValue (
  IN KEYBOARD_CONSOLE_IN_DEV *ConsoleIn,
  IN UINT8                   Value
  );

STATIC
EFI_STATUS
UpdateStatusLights (
  IN KEYBOARD_CONSOLE_IN_DEV *ConsoleIn
  );

//
// Global variables
//
//
// Scancode to Efi key mapping table
// Format:<efi scan code>, <unicode without shift>, <unicode with shift>
//
STATIC UINT8  ConvertKeyboardScanCodeToEfiKey[SCANCODE_MAX_MAKE][3] = {
  SCAN_NULL,
  0x00,
  0x00, // 0x00  Unknown
  SCAN_ESC,
  0x00,
  0x00, // 0x01  Escape
  SCAN_NULL,
  '1',
  '!',  // 0x02
  SCAN_NULL,
  '2',
  '@',  // 0x03
  SCAN_NULL,
  '3',
  '#',  // 0x04
  SCAN_NULL,
  '4',
  '$',  // 0x05
  SCAN_NULL,
  '5',
  '%',  // 0x06
  SCAN_NULL,
  '6',
  '^',  // 0x07
  SCAN_NULL,
  '7',
  '&',  // 0x08
  SCAN_NULL,
  '8',
  '*',  // 0x09
  SCAN_NULL,
  '9',
  '(',  // 0x0A
  SCAN_NULL,
  '0',
  ')',  // 0x0B
  SCAN_NULL,
  '-',
  '_',  // 0x0C
  SCAN_NULL,
  '=',
  '+',  // 0x0D
  SCAN_NULL,
  0x08,
  0x08, // 0x0E  BackSpace
  SCAN_NULL,
  0x09,
  0x09, // 0x0F  Tab
  SCAN_NULL,
  'q',
  'Q',  // 0x10
  SCAN_NULL,
  'w',
  'W',  // 0x11
  SCAN_NULL,
  'e',
  'E',  // 0x12
  SCAN_NULL,
  'r',
  'R',  // 0x13
  SCAN_NULL,
  't',
  'T',  // 0x14
  SCAN_NULL,
  'y',
  'Y',  // 0x15
  SCAN_NULL,
  'u',
  'U',  // 0x16
  SCAN_NULL,
  'i',
  'I',  // 0x17
  SCAN_NULL,
  'o',
  'O',  // 0x18
  SCAN_NULL,
  'p',
  'P',  // 0x19
  SCAN_NULL,
  '[',
  '{',  // 0x1a
  SCAN_NULL,
  ']',
  '}',  // 0x1b
  SCAN_NULL,
  0x0d,
  0x0d, // 0x1c  Enter
  SCAN_NULL,
  0x00,
  0x00, // 0x1d  Unknown
  SCAN_NULL,
  'a',
  'A',  // 0x1e
  SCAN_NULL,
  's',
  'S',  // 0x1f
  SCAN_NULL,
  'd',
  'D',  // 0x20
  SCAN_NULL,
  'f',
  'F',  // 0x21
  SCAN_NULL,
  'g',
  'G',  // 0x22
  SCAN_NULL,
  'h',
  'H',  // 0x23
  SCAN_NULL,
  'j',
  'J',  // 0x24
  SCAN_NULL,
  'k',
  'K',  // 0x25
  SCAN_NULL,
  'l',
  'L',  // 0x26
  SCAN_NULL,
  ';',
  ':',  // 0x27
  SCAN_NULL,
  '\'',
  '"',  // 0x28
  SCAN_NULL,
  '`',
  '~',  // 0x29
  SCAN_NULL,
  0x00,
  0x00, // 0x2A  Left Shift
  SCAN_NULL,
  '\\',
  '|',  // 0x2B
  SCAN_NULL,
  'z',
  'Z',  // 0x2c
  SCAN_NULL,
  'x',
  'X',  // 0x2d
  SCAN_NULL,
  'c',
  'C',  // 0x2e
  SCAN_NULL,
  'v',
  'V',  // 0x2f
  SCAN_NULL,
  'b',
  'B',  // 0x30
  SCAN_NULL,
  'n',
  'N',  // 0x31
  SCAN_NULL,
  'm',
  'M',  // 0x32
  SCAN_NULL,
  ',',
  '<',  // 0x33
  SCAN_NULL,
  '.',
  '>',  // 0x34
  SCAN_NULL,
  '/',
  '?',  // 0x35
  SCAN_NULL,
  0x00,
  0x00, // 0x36  Right Shift
  SCAN_NULL,
  '*',
  '*',  // 0x37  Numeric Keypad *
  SCAN_NULL,
  0x00,
  0x00, // 0x38  Left Alt/Extended Right Alt
  SCAN_NULL,
  ' ',
  ' ',  // 0x39
  SCAN_NULL,
  0x00,
  0x00, // 0x3A  CapsLock
  SCAN_F1,
  0x00,
  0x00, // 0x3B
  SCAN_F2,
  0x00,
  0x00, // 0x3C
  SCAN_F3,
  0x00,
  0x00, // 0x3D
  SCAN_F4,
  0x00,
  0x00, // 0x3E
  SCAN_F5,
  0x00,
  0x00, // 0x3F
  SCAN_F6,
  0x00,
  0x00, // 0x40
  SCAN_F7,
  0x00,
  0x00, // 0x41
  SCAN_F8,
  0x00,
  0x00, // 0x42
  SCAN_F9,
  0x00,
  0x00, // 0x43
  SCAN_F10,
  0x00,
  0x00, // 0x44
  SCAN_NULL,
  0x00,
  0x00, // 0x45  NumLock
  SCAN_NULL,
  0x00,
  0x00, // 0x46  ScrollLock
  SCAN_HOME,
  '7',
  '7',  // 0x47
  SCAN_UP,
  '8',
  '8',  // 0x48
  SCAN_PAGE_UP,
  '9',
  '9',  // 0x49
  SCAN_NULL,
  '-',
  '-',  // 0x4a
  SCAN_LEFT,
  '4',
  '4',  // 0x4b
  SCAN_NULL,
  '5',
  '5',  // 0x4c  Numeric Keypad 5
  SCAN_RIGHT,
  '6',
  '6',  // 0x4d
  SCAN_NULL,
  '+',
  '+',  // 0x4e
  SCAN_END,
  '1',
  '1',  // 0x4f
  SCAN_DOWN,
  '2',
  '2',  // 0x50
  SCAN_PAGE_DOWN,
  '3',
  '3',  // 0x51
  SCAN_INSERT,
  '0',
  '0',  // 0x52
  SCAN_DELETE,
  '.',
  '.'   // 0x53
};

//
// The WaitForValue time out
//
STATIC UINTN  mWaitForValueTimeOut = KEYBOARD_WAITFORVALUE_TIMEOUT;

STATIC
UINT8
KeyReadDataRegister (
  IN KEYBOARD_CONSOLE_IN_DEV *ConsoleIn
  )
/*++

Routine Description:

  GC_TODO: Add function description

Arguments:

  ConsoleIn - GC_TODO: add argument description

Returns:

  GC_TODO: add return values

--*/
{
  EFI_INTERFACE_DEFINITION_FOR_ISA_IO *IsaIo;
  UINT8                               Data;

  //
  // Use IsaIo protocol to perform IO operations
  //
  IsaIo = ConsoleIn->IsaIo;

  IsaIo->Io.Read (
              IsaIo,
              EfiIsaIoWidthUint8,
              ConsoleIn->DataRegisterAddress,
              1,
              &Data
              );

  return Data;
}

STATIC
VOID
KeyWriteDataRegister (
  IN KEYBOARD_CONSOLE_IN_DEV *ConsoleIn,
  IN UINT8                   Data
  )
/*++

Routine Description:

  GC_TODO: Add function description

Arguments:

  ConsoleIn - GC_TODO: add argument description
  Data      - GC_TODO: add argument description

Returns:

  GC_TODO: add return values

--*/
{
  EFI_INTERFACE_DEFINITION_FOR_ISA_IO *IsaIo;

  //
  // Use IsaIo protocol to perform IO operations
  //
  IsaIo = ConsoleIn->IsaIo;

  IsaIo->Io.Write (
              IsaIo,
              EfiIsaIoWidthUint8,
              ConsoleIn->DataRegisterAddress,
              1,
              &Data
              );

  //
  // outp(ConsoleIn->DataRegisterAddress, Data);
  //
}

UINT8
KeyReadStatusRegister (
  IN KEYBOARD_CONSOLE_IN_DEV *ConsoleIn
  )
/*++

Routine Description:

  GC_TODO: Add function description

Arguments:

  ConsoleIn - GC_TODO: add argument description

Returns:

  GC_TODO: add return values

--*/
{
  EFI_INTERFACE_DEFINITION_FOR_ISA_IO *IsaIo;
  UINT8                               Data;

  //
  // Use IsaIo protocol to perform IO operations
  //
  IsaIo = ConsoleIn->IsaIo;

  IsaIo->Io.Read (
              IsaIo,
              EfiIsaIoWidthUint8,
              ConsoleIn->StatusRegisterAddress,
              1,
              &Data
              );

  return Data;

}

STATIC
VOID
KeyWriteCommandRegister (
  IN KEYBOARD_CONSOLE_IN_DEV *ConsoleIn,
  IN UINT8                   Data
  )
/*++

Routine Description:

  GC_TODO: Add function description

Arguments:

  ConsoleIn - GC_TODO: add argument description
  Data      - GC_TODO: add argument description

Returns:

  GC_TODO: add return values

--*/
{
  EFI_INTERFACE_DEFINITION_FOR_ISA_IO *IsaIo;

  //
  // Use IsaIo protocol to perform IO operations
  //
  IsaIo = ConsoleIn->IsaIo;

  IsaIo->Io.Write (
              IsaIo,
              EfiIsaIoWidthUint8,
              ConsoleIn->CommandRegisterAddress,
              1,
              &Data
              );

}

STATIC
VOID
KeyboardError (
  IN KEYBOARD_CONSOLE_IN_DEV *ConsoleIn,
  IN CHAR16                  *ErrMsg
  )
/*++

Routine Description:

  Display error message

Arguments:

Returns:

--*/
// GC_TODO:    ConsoleIn - add argument and description to function comment
// GC_TODO:    ErrMsg - add argument and description to function comment
{
  ConsoleIn->KeyboardErr = TRUE;

  //
  // gST -> ConOut -> OutputString (gST -> ConOut, L"Keyboard Driver: ");
  // gST -> ConOut -> OutputString (gST -> ConOut, ErrMsg);
  //
}

VOID
EFIAPI
KeyboardTimerHandler (
  IN EFI_EVENT    Event,
  IN VOID         *Context
  )
/*++

Routine Description:

  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 EFI_TPL_NOTIFY

Arguments:
  
  Event - The timer event
  Context - A KEYBOARD_CONSOLE_IN_DEV pointer
  
Returns:

--*/
{
  UINT8                   Data;
  EFI_TPL                 OldTpl;
  UINT32                  TimeOut;
  KEYBOARD_CONSOLE_IN_DEV *ConsoleIn;

  ConsoleIn = Context;

  //
  // Enter critical section
  //
  OldTpl = gBS->RaiseTPL (EFI_TPL_NOTIFY);

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?