public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
From: "Nate DeSimone" <nathaniel.l.desimone@intel.com>
To: "Agyeman, Prince" <prince.agyeman@intel.com>,
	"devel@edk2.groups.io" <devel@edk2.groups.io>
Cc: "Kubacki, Michael A" <michael.a.kubacki@intel.com>,
	"Chiu, Chasel" <chasel.chiu@intel.com>
Subject: Re: [edk2-platforms] [Patch v2 6/9] BoardModulePkg: Add Ps2 Keyboard Library
Date: Wed, 6 Nov 2019 22:39:18 +0000	[thread overview]
Message-ID: <02A34F284D1DA44BB705E61F7180EF0AB5BA472E@ORSMSX113.amr.corp.intel.com> (raw)
In-Reply-To: <20191106012603.4724-7-prince.agyeman@intel.com>

Reviewed-by: Nate DeSimone <nathaniel.l.desimone@intel.com>

-----Original Message-----
From: Agyeman, Prince <prince.agyeman@intel.com> 
Sent: Tuesday, November 5, 2019 5:26 PM
To: devel@edk2.groups.io
Cc: Kubacki, Michael A <michael.a.kubacki@intel.com>; Chiu, Chasel <chasel.chiu@intel.com>; Desimone, Nathaniel L <nathaniel.l.desimone@intel.com>
Subject: [edk2-platforms] [Patch v2 6/9] 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 <michael.a.kubacki@intel.com>
Cc: Chasel Chiu <chasel.chiu@intel.com>
Cc: Nate DeSimone <nathaniel.l.desimone@intel.com>

Signed-off-by: Prince Agyeman <prince.agyeman@intel.com>
---
 .../Intel/BoardModulePkg/BoardModulePkg.dsc   |   1 +
 .../Library/BdsPs2KbcLib/BdsPs2KbcLib.c       | 202 ++++++++++++++++++
 .../Library/BdsPs2KbcLib/BdsPs2KbcLib.h       |  65 ++++++
 .../Library/BdsPs2KbcLib/BdsPs2KbcLib.inf     |  38 ++++
 4 files changed, 306 insertions(+)
 create mode 100644 Platform/Intel/BoardModulePkg/Library/BdsPs2KbcLib/BdsPs2KbcLib.c
 create mode 100644 Platform/Intel/BoardModulePkg/Library/BdsPs2KbcLib/BdsPs2KbcLib.h
 create mode 100644 Platform/Intel/BoardModulePkg/Library/BdsPs2KbcLib/BdsPs2KbcLib.inf

diff --git a/Platform/Intel/BoardModulePkg/BoardModulePkg.dsc b/Platform/Intel/BoardModulePkg/BoardModulePkg.dsc
index 5af26eb97e..bcc2dfbfeb 100644
--- a/Platform/Intel/BoardModulePkg/BoardModulePkg.dsc
+++ b/Platform/Intel/BoardModulePkg/BoardModulePkg.dsc
@@ -88,3 +88,4 @@
   BoardModulePkg/Library/BiosIdLib/PeiBiosIdLib.inf
 
   BoardModulePkg/Library/PeiFirmwareBootMediaInfoLib/PeiFirmwareBootMediaInfoLib.inf
+  BoardModulePkg/Library/BdsPs2KbcLib/BdsPs2KbcLib.inf
diff --git a/Platform/Intel/BoardModulePkg/Library/BdsPs2KbcLib/BdsPs2KbcLib.c b/Platform/Intel/BoardModulePkg/Library/BdsPs2KbcLib/BdsPs2KbcLib.c
new file mode 100644
index 0000000000..3a885e0d71
--- /dev/null
+++ b/Platform/Intel/BoardModulePkg/Library/BdsPs2KbcLib/BdsPs2KbcLib.c
@@ -0,0 +1,202 @@
+/** @file
+  Main file for Ps2 keyboard controller library.
+
+  Copyright (c) 2019, 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;
+  UINT32                RegEmptied;
+  UINT8                 Data;
+  UINT32                SumTimeOut;
+  BOOLEAN               FoundPs2Kbc;
+
+  TimeOut     = 0;
+  RegEmptied  = 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 == FALSE) {
+    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 == FALSE) {
+    //
+    // 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;
+}
diff --git a/Platform/Intel/BoardModulePkg/Library/BdsPs2KbcLib/BdsPs2KbcLib.h b/Platform/Intel/BoardModulePkg/Library/BdsPs2KbcLib/BdsPs2KbcLib.h
new file mode 100644
index 0000000000..d9a27e6681
--- /dev/null
+++ b/Platform/Intel/BoardModulePkg/Library/BdsPs2KbcLib/BdsPs2KbcLib.h
@@ -0,0 +1,65 @@
+/** @file
+  Header file for the Ps2 keyboard controller library.
+
+  Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>
+  SPDX-License-Identifier: BSD-2-Clause-Patent
+
+**/
+
+#ifndef _PS2_KBC_LIB_H
+#define _PS2_KBC_LIB_H
+
+#include <Uefi.h>
+#include <Library/UefiLib.h>
+#include <Library/DevicePathLib.h>
+#include <Library/DebugLib.h>
+#include <Library/IoLib.h>
+#include <Library/TimerLib.h>
+#include <Library/UefiBootManagerLib.h>
+
+//
+// Below is the platform console device path // typedef struct {
+  ACPI_HID_DEVICE_PATH      PciRootBridge;
+  PCI_DEVICE_PATH           IsaBridge;
+  ACPI_HID_DEVICE_PATH      Keyboard;
+  EFI_DEVICE_PATH_PROTOCOL  End;
+} PLATFORM_KEYBOARD_DEVICE_PATH;
+
+typedef struct {
+  UINT8 Segment;
+  UINT8 Bus;
+  UINT8 Device;
+  UINT8 Funtion;
+} SIO_PCI_ISA_BRIDGE_DEVICE_INFO;
+
+#define gPciRootBridge \
+  { \
+    { \
+      ACPI_DEVICE_PATH, \
+      ACPI_DP, \
+      { \
+        (UINT8) (sizeof (ACPI_HID_DEVICE_PATH)), \
+        (UINT8) ((sizeof (ACPI_HID_DEVICE_PATH)) >> 8) \
+      }, \
+    }, \
+    EISA_PNP_ID (0x0A03), \
+    0 \
+  }
+
+#define gEndEntire \
+  { \
+    END_DEVICE_PATH_TYPE, END_ENTIRE_DEVICE_PATH_SUBTYPE, { 
+END_DEVICE_PATH_LENGTH, 0 } \
+  }
+
+#define KBC_INPBUF_VIA60_KBECHO             0xEE
+#define KEYBOARD_8042_DATA_REGISTER         0x60
+#define KEYBOARD_8042_STATUS_REGISTER       0x64
+
+#define PS2_KEYBOARD_TIMEOUT                65536   // 0.07s
+#define PS2_KEYBOARD_WAITFORVALUE_TIMEOUT   1000000 // 1s
+#define PS2_KEYBOARD_KBEN                   0xF4
+#define PS2_KEYBOARD_CMDECHO_ACK            0xFA
+
+#endif
diff --git a/Platform/Intel/BoardModulePkg/Library/BdsPs2KbcLib/BdsPs2KbcLib.inf b/Platform/Intel/BoardModulePkg/Library/BdsPs2KbcLib/BdsPs2KbcLib.inf
new file mode 100644
index 0000000000..e00638daa7
--- /dev/null
+++ b/Platform/Intel/BoardModulePkg/Library/BdsPs2KbcLib/BdsPs2KbcLib.in
+++ f
@@ -0,0 +1,38 @@
+## @file
+# Component information file for Ps2 keyboard controller library # # 
+Copyright (c) 2019, Intel Corporation. All rights reserved.<BR> # # 
+SPDX-License-Identifier: BSD-2-Clause-Patent # ## [Defines]
+  INF_VERSION                    = 0x00010006
+  BASE_NAME                      = BdsPs2KbcLib
+  FILE_GUID                      = E94EA52E-E84C-42E7-B863-EA1327EFA265
+  MODULE_TYPE                    = UEFI_DRIVER
+  VERSION_STRING                 = 1.2
+  LIBRARY_CLASS                  = NULL|UEFI_DRIVER
+  CONSTRUCTOR                    = BdsPs2KbcLibConstructor
+
+[Packages]
+  MdePkg/MdePkg.dec
+  MdeModulePkg/MdeModulePkg.dec
+  BoardModulePkg/BoardModulePkg.dec
+
+[Sources]
+  BdsPs2KbcLib.c
+  BdsPs2KbcLib.h
+
+[LibraryClasses]
+  DevicePathLib
+  DebugLib
+  IoLib
+  UefiDriverEntryPoint
+  UefiBootManagerLib
+  UefiLib
+  TimerLib
+
+[Pcd]
+  gBoardModulePkgTokenSpaceGuid.PcdPs2KbMsEnable
+  gBoardModulePkgTokenSpaceGuid.PcdSuperIoPciIsaBridgeDevice
--
2.19.1.windows.1


  parent reply	other threads:[~2019-11-06 22:39 UTC|newest]

Thread overview: 37+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-11-06  1:25 [edk2-platforms] [Patch v2 0/9] Enable Ps2 keyboard Agyeman, Prince
2019-11-06  1:25 ` [edk2-platforms] [Patch v2 1/9] BoardModulePkg: Add gBoardModulePkgTokenSpaceGuid Agyeman, Prince
2019-11-06  2:07   ` Chiu, Chasel
2019-11-06 22:38   ` Nate DeSimone
2019-11-07  0:49   ` Kubacki, Michael A
2019-11-06  1:25 ` [edk2-platforms] [Patch v2 2/9] SimicsOpenBoardPkg: Remove Super I/O DXE driver Agyeman, Prince
2019-11-06  2:07   ` Chiu, Chasel
2019-11-06 22:39   ` Nate DeSimone
2019-11-07  0:49   ` Kubacki, Michael A
2019-11-06  1:25 ` [edk2-platforms] [Patch v2 3/9] BoardModulePkg: Add Super I/O driver Agyeman, Prince
2019-11-06  2:07   ` Chiu, Chasel
2019-11-06 22:39   ` Nate DeSimone
2019-11-07  0:49   ` Kubacki, Michael A
2019-11-06  1:25 ` [edk2-platforms] [Patch v2 4/9] BoardModulePkg: Add fix at build Pcds Agyeman, Prince
2019-11-06  2:07   ` Chiu, Chasel
2019-11-06 22:39   ` Nate DeSimone
2019-11-07  0:49   ` Kubacki, Michael A
2019-11-06  1:25 ` [edk2-platforms] [Patch v2 5/9] BoardModulePkg: Added Pcds to Super I/O driver Agyeman, Prince
2019-11-06  2:08   ` Chiu, Chasel
2019-11-06 22:39   ` Nate DeSimone
2019-11-07  0:49   ` Kubacki, Michael A
2019-11-06  1:26 ` [edk2-platforms] [Patch v2 6/9] BoardModulePkg: Add Ps2 Keyboard Library Agyeman, Prince
2019-11-06  2:08   ` Chiu, Chasel
2019-11-06 22:39   ` Nate DeSimone [this message]
2019-11-07  0:49   ` Kubacki, Michael A
2019-11-06  1:26 ` [edk2-platforms] [Patch v2 7/9] KabylakeOpenBoardPkg: Add Ps2 Keyboard Support Agyeman, Prince
2019-11-06  2:08   ` [edk2-devel] " Chiu, Chasel
2019-11-06 22:39   ` Nate DeSimone
2019-11-07  0:50   ` Kubacki, Michael A
2019-11-06  1:26 ` [edk2-platforms] [Patch v2 8/9] SimicsOpenBoardPkg: Add Ps2 keyboard Support Agyeman, Prince
2019-11-06  2:09   ` Chiu, Chasel
2019-11-06 22:39   ` Nate DeSimone
2019-11-07  0:50   ` Kubacki, Michael A
2019-11-06  1:26 ` [edk2-platforms] [Patch v2 9/9] WhiskeylakeOpenBoardPkg: Add Ps2 Keyboard Support Agyeman, Prince
2019-11-06  2:09   ` Chiu, Chasel
2019-11-06 22:39   ` Nate DeSimone
2019-11-07  0:50   ` Kubacki, Michael A

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=02A34F284D1DA44BB705E61F7180EF0AB5BA472E@ORSMSX113.amr.corp.intel.com \
    --to=devel@edk2.groups.io \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox