terminalconin.c

来自「Next BIOS Source code : Extensible Firmw」· C语言 代码 · 共 1,089 行 · 第 1/2 页

C
1,089
字号
/*++

Copyright (c)  1999 - 2002 Intel Corporation. All rights reserved
This software and associated documentation (if any) is furnished
under a license and may only be used or copied in accordance
with the terms of the license. Except as permitted by such
license, no part of this software or documentation may be
reproduced, stored in a retrieval system, or transmitted in any
form or by any means without the express written consent of
Intel Corporation.


Module Name:

    TerminalConIn.c
    
Abstract: 
    

Revision History
--*/

#include "Terminal.h"


EFI_STATUS 
EFIAPI
TerminalConInReset (
  IN  EFI_SIMPLE_TEXT_IN_PROTOCOL  *This,
  IN  BOOLEAN                      ExtendedVerification
  )
/*++
  Routine Description:
  
    Implements EFI_SIMPLE_TEXT_IN_PROTOCOL.Reset().
    This driver only perform dependent serial device reset regardless of 
    the value of ExtendeVerification
  
  Arguments:
  
    EFI_SIMPLE_TEXT_OUT_PROTOCOL  IN    *This
        Indicates the calling context.
    
    BOOLEAN             IN    ExtendedVerification
        Skip by this driver.
        
  Returns:
  
    EFI_SUCCESS
       The reset operation succeeds.   
    
    EFI_DEVICE_ERROR
      The dependent serial port reset fails.
                
--*/    
{
  EFI_STATUS    Status;
  TERMINAL_DEV  *TerminalDevice;

  TerminalDevice = TERMINAL_CON_IN_DEV_FROM_THIS(This);

  Status = TerminalDevice->SerialIo->Reset(TerminalDevice->SerialIo);

  //
  // clear all the internal buffer for keys
  //
  InitializeRawFiFo (TerminalDevice);
  InitializeUnicodeFiFo (TerminalDevice);
  InitializeEfiKeyFiFo (TerminalDevice);

  return Status;
}


EFI_STATUS 
EFIAPI 
TerminalConInReadKeyStroke (
  IN  EFI_SIMPLE_TEXT_IN_PROTOCOL  *This, 
  OUT EFI_INPUT_KEY                *Key
  )
/*++
  Routine Description:
  
    Implements EFI_SIMPLE_TEXT_IN_PROTOCOL.ReadKeyStroke().
      
  Arguments:
  
    EFI_SIMPLE_TEXT_OUT_PROTOCOL  IN    *This
        Indicates the calling context.
    
    EFI_INPUT_KEY         OUT   *Key
        A pointer to a buffer that is filled in with the keystroke
        information for the key that was sent from terminal.        
        
  Returns:
  
    EFI_SUCCESS
      The keystroke information is returned successfully.
       
    EFI_NOT_READY
      There is no keystroke data available.
 
    EFI_DEVICE_ERROR
      The dependent serial device encounters error.
                
--*/    
{
  TERMINAL_DEV            *TerminalDevice;
  EFI_STATUS              Status;  
  
  //
  // Initialize *Key to nonsense value.
  //
  Key->ScanCode = SCAN_NULL;
  Key->UnicodeChar = 0;
  //
  //  get TERMINAL_DEV from "This" parameter.
  //
  TerminalDevice = TERMINAL_CON_IN_DEV_FROM_THIS(This);     

  Status = TerminalConInCheckForKey (This);
  if (EFI_ERROR(Status)) {
    return EFI_NOT_READY;
  }
  
  EfiKeyFiFoRemoveOneKey (TerminalDevice,Key);
  
  return EFI_SUCCESS;

}


VOID
TranslateRawDataToEfiKey (
  IN  TERMINAL_DEV      *TerminalDevice
  )
/*++
    Step1: Turn raw data into Unicode (according to different encode).
    Step2: Translate Unicode into key information. 
    (according to different terminal standard).
--*/  
{
  switch (TerminalDevice->TerminalType) {
  
    case PcAnsiType:
    case VT100Type:
    case VT100PlusType:
      AnsiRawDataToUnicode (TerminalDevice);
      UnicodeToEfiKey (TerminalDevice);
      break;
      
    case VTUTF8Type:
      //
      // Process all the raw data in the RawFIFO,
      // put the processed key into UnicodeFIFO.
      //
      VTUTF8RawDataToUnicode (TerminalDevice);
  
      //
      // Translate all the Unicode data in the UnicodeFIFO to Efi key,
      // then put into EfiKeyFIFO.
      //
      UnicodeToEfiKey (TerminalDevice);
      
      break;
  }   
}   


VOID 
EFIAPI
TerminalConInWaitForKey (
  IN  EFI_EVENT       Event,
  IN  VOID            *Context
  )
/*++
  Routine Description:
  
    Event notification function for EFI_SIMPLE_TEXT_IN_PROTOCOL.WaitForKey event
    Signal the event if there is key available     
  
  Arguments:
  
    EFI_EVENT  IN    Event
        Indicates the event that invoke this function.
    
    VOID     IN    *Context
        Indicates the calling context.
        
  Returns:
  
    N/A
                
--*/      
{
  
  //
  // Someone is waiting on the keystroke event, if there's
  // a key pending, signal the event
  //
  // Context is the pointer to EFI_SIMPLE_TEXT_IN_PROTOCOL 

  if (!EFI_ERROR(TerminalConInCheckForKey(Context))) {
    
    gBS->SignalEvent(Event);
  }
}


EFI_STATUS 
TerminalConInCheckForKey (
  IN  EFI_SIMPLE_TEXT_IN_PROTOCOL  *This 
  )
/*++
  Routine Description:
  
    Check for a pending key in the Efi Key FIFO or Serial device buffer.
  
  Arguments:
  
    EFI_SIMPLE_TEXT_IN_PROTOCOL  IN    *This
        Indicates the calling context.
        
  Returns:
  
    EFI_SUCCESS
       There is key pending.   
    
    EFI_NOT_READY
      There is no key pending.
                
--*/      
{ 
  EFI_STATUS              Status;
  TERMINAL_DEV            *TerminalDevice;
  UINT32                  Control;
  UINT8                   Input;
  EFI_SERIAL_IO_MODE      *Mode;
  EFI_SERIAL_IO_PROTOCOL  *SerialIo;
  UINTN                   SerialInTimeOut;    

  TerminalDevice = TERMINAL_CON_IN_DEV_FROM_THIS(This);
  
  SerialIo = TerminalDevice->SerialIo;  
  if (SerialIo == NULL) {
    return EFI_DEVICE_ERROR;
  }
  //
  //  if current timeout value for serial device is not identical with
  //  the value saved in TERMINAL_DEV structure, then recalculate the 
  //  timeout value again and set serial attribute according to this value. 
  //
  Mode = SerialIo->Mode;
  if (Mode->Timeout != TerminalDevice->SerialInTimeOut) {

    SerialInTimeOut = 0;
    if (Mode->BaudRate != 0) {
      SerialInTimeOut = (1 + Mode->DataBits + Mode->StopBits) 
                          * 2 * 1000000 / (UINTN) Mode->BaudRate;
    }
      
    Status = SerialIo->SetAttributes(
                            SerialIo,Mode->BaudRate,
                            Mode->ReceiveFifoDepth,
                            (UINT32)SerialInTimeOut,
                            Mode->Parity,
                            (UINT8)Mode->DataBits,
                            Mode->StopBits
                            );
    
    if(EFI_ERROR(Status)) {
      TerminalDevice->SerialInTimeOut = 0 ;
    } else {
      TerminalDevice->SerialInTimeOut = SerialInTimeOut;
    }  
  }
  
  //
  //  check whether serial buffer is empty
  //
  Status = SerialIo->GetControl(SerialIo,&Control);

  if (Control & EFI_SERIAL_INPUT_BUFFER_EMPTY) { 
  
    //
    // Translate all the raw data in RawFIFO into EFI Key,
    // according to different terminal type supported.
    //
    TranslateRawDataToEfiKey (TerminalDevice) ;

    //
    //  if there is pre-fetched Efi Key in EfiKeyFIFO buffer,
    //  return directly.
    //
    if (!IsEfiKeyFiFoEmpty(TerminalDevice)) {
      return EFI_SUCCESS;
    } else {
      return EFI_NOT_READY;
    }
  }

  //
  // Fetch all the keys in the serial buffer,
  // and insert the byte stream into RawFIFO.
  //
  do {
    
    Status = GetOneKeyFromSerial (TerminalDevice->SerialIo,&Input);
    
    if (EFI_ERROR(Status)) {
      break;
    }
    
    RawFiFoInsertOneKey (TerminalDevice,Input);   
  } while (TRUE);

  //
  // Translate all the raw data in RawFIFO into EFI Key,
  // according to different terminal type supported.
  //
  TranslateRawDataToEfiKey (TerminalDevice) ;

  
  if (IsEfiKeyFiFoEmpty (TerminalDevice)) {
    return EFI_NOT_READY;
  }
  
  return EFI_SUCCESS;
}


EFI_STATUS
GetOneKeyFromSerial (
  EFI_SERIAL_IO_PROTOCOL  *SerialIo,
  UINT8                   *Input
  )
/*++
    Get one key out of serial buffer.
    If serial buffer is empty, return EFI_NOT_READY;
    if reading serial buffer encounter error, returns EFI_DEVICE_ERROR;
    if reading serial buffer successfully, put the fetched key to 
    the parameter "Input", and return EFI_SUCCESS.
--*/  
{
  EFI_STATUS  Status;
  UINTN       Size;

  Size    = 1;
  *Input  = 0;
  
  Status = SerialIo->Read (SerialIo,&Size,Input);

  if (EFI_ERROR(Status)) {
    
    if (Status == EFI_TIMEOUT) {
      return EFI_NOT_READY;
    }
      
    return EFI_DEVICE_ERROR;
    
  }

  if (*Input == 0) {
    return EFI_DEVICE_ERROR;
  }
 
  return EFI_SUCCESS;
}

BOOLEAN
RawFiFoInsertOneKey (
  TERMINAL_DEV      *TerminalDevice,
  UINT8             Input
  )
/*++
    Insert one byte raw data into the Raw Data FIFO.
    If FIFO is FULL before data insertion,
    return FALSE, and the key is lost.
--*/  
{
  UINT8 Tail;

  Tail = TerminalDevice->RawFiFo.Tail;
  
  if (IsRawFiFoFull (TerminalDevice)) {
   
    //
    // Raw FIFO is full
    //
    return FALSE;
  }

  TerminalDevice->RawFiFo.Data[Tail] = Input;

  TerminalDevice->RawFiFo.Tail = (UINT8)((Tail + 1) 
                                  % (RAW_FIFO_MAX_NUMBER + 1));

  return TRUE;
}

BOOLEAN
RawFiFoRemoveOneKey (
  TERMINAL_DEV  *TerminalDevice,
  UINT8         *Output
  )
/*++
    Remove one byte raw data out of the Raw Data FIFO.
    If FIFO buffer is empty before remove operation,
    return FALSE.
--*/  
{
  UINT8 Head;
  
  Head = TerminalDevice->RawFiFo.Head;

  if (IsRawFiFoEmpty(TerminalDevice)) {
  
    //
    //  FIFO is empty
    //
    *Output = 0;
    return FALSE;
  }

  *Output = TerminalDevice->RawFiFo.Data[Head] ;

  TerminalDevice->RawFiFo.Head = (UINT8)((Head + 1) 
                                  % (RAW_FIFO_MAX_NUMBER + 1));

  return TRUE;
}

BOOLEAN
IsRawFiFoEmpty (
  TERMINAL_DEV  *TerminalDevice
  )
/*++
    Clarify whether FIFO buffer is empty.
--*/  
{
  if (TerminalDevice->RawFiFo.Head == TerminalDevice->RawFiFo.Tail) {
    return TRUE;
  }
  else {
    return FALSE;
  }
}

BOOLEAN
IsRawFiFoFull (
  TERMINAL_DEV  *TerminalDevice
  )
/*++
    Clarify whether FIFO buffer is full.
--*/  
{
  UINT8   Tail;
  UINT8   Head;

  Tail = TerminalDevice->RawFiFo.Tail ;
  Head = TerminalDevice->RawFiFo.Head ;

  if (((Tail + 1) % (RAW_FIFO_MAX_NUMBER + 1)) == Head) {
    
    return TRUE;
  }

  return FALSE;
}

BOOLEAN
EfiKeyFiFoInsertOneKey (
  TERMINAL_DEV      *TerminalDevice,
  EFI_INPUT_KEY     Key
  )
/*++
    Insert one pre-fetched key into the FIFO buffer.
    If FIFO buffer is FULL before key insertion,
    return FALSE, and the key is lost.
--*/  
{
  UINT8 Tail;

  Tail = TerminalDevice->EfiKeyFiFo.Tail;
    
  if (IsEfiKeyFiFoFull(TerminalDevice)) {
    //
    // Efi Key FIFO is full
    //
    return FALSE;
  }

  TerminalDevice->EfiKeyFiFo.Data[Tail] = Key;

  TerminalDevice->EfiKeyFiFo.Tail = (UINT8)((Tail + 1) % (FIFO_MAX_NUMBER + 1));

  return TRUE;
}

BOOLEAN
EfiKeyFiFoRemoveOneKey (
  TERMINAL_DEV  *TerminalDevice,
  EFI_INPUT_KEY *Output
  )
/*++
    Remove one pre-fetched key out of the FIFO buffer.
    If FIFO buffer is empty before remove operation,
    return FALSE.
--*/  
{
  UINT8 Head;
  
  Head = TerminalDevice->EfiKeyFiFo.Head;

  if (IsEfiKeyFiFoEmpty(TerminalDevice)) {
  
    //
    //  FIFO is empty
    //
    Output->ScanCode = SCAN_NULL;
    Output->UnicodeChar = 0;
    return FALSE;
  }

  *Output = TerminalDevice->EfiKeyFiFo.Data[Head] ;

  TerminalDevice->EfiKeyFiFo.Head = (UINT8)((Head + 1) % (FIFO_MAX_NUMBER + 1));

  return TRUE;
}

BOOLEAN
IsEfiKeyFiFoEmpty (
  TERMINAL_DEV  *TerminalDevice
  )
/*++
    Clarify whether FIFO buffer is empty.
--*/  
{
  if (TerminalDevice->EfiKeyFiFo.Head == TerminalDevice->EfiKeyFiFo.Tail) {
    return TRUE;
  }
  else {
    return FALSE;
  }

⌨️ 快捷键说明

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