]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Bus/Isa/Ps2MouseDxe/CommPs2.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / MdeModulePkg / Bus / Isa / Ps2MouseDxe / CommPs2.c
index 04d536c76c494b4f75e4a2cd8020e7c41af05020..d31665c25df8aa80d25815caa039ae5f2a63d970 100644 (file)
@@ -2,22 +2,16 @@
   PS2 Mouse Communication Interface.\r
 \r
 Copyright (c) 2006 - 2016, Intel Corporation. All rights reserved.<BR>\r
-This program and the accompanying materials\r
-are licensed and made available under the terms and conditions of the BSD License\r
-which accompanies this distribution.  The full text of the license may be found at\r
-http://opensource.org/licenses/bsd-license.php\r
-\r
-THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
-WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+SPDX-License-Identifier: BSD-2-Clause-Patent\r
 \r
 **/\r
 \r
 #include "Ps2Mouse.h"\r
 #include "CommPs2.h"\r
 \r
-UINT8 SampleRateTbl[MaxSampleRate]  = { 0xa, 0x14, 0x28, 0x3c, 0x50, 0x64, 0xc8 };\r
+UINT8  SampleRateTbl[MaxSampleRate] = { 0xa, 0x14, 0x28, 0x3c, 0x50, 0x64, 0xc8 };\r
 \r
-UINT8 ResolutionTbl[MaxResolution]  = { 0, 1, 2, 3 };\r
+UINT8  ResolutionTbl[MaxResolution] = { 0, 1, 2, 3 };\r
 \r
 /**\r
   Issue self test command via IsaIo interface.\r
@@ -40,6 +34,7 @@ KbcSelfTest (
   if (EFI_ERROR (Status)) {\r
     return Status;\r
   }\r
+\r
   //\r
   // Read return code\r
   //\r
@@ -51,6 +46,7 @@ KbcSelfTest (
   if (Data != 0x55) {\r
     return EFI_DEVICE_ERROR;\r
   }\r
+\r
   //\r
   // Set system flag\r
   //\r
@@ -69,7 +65,7 @@ KbcSelfTest (
     return Status;\r
   }\r
 \r
-  Data |= CMD_SYS_FLAG;\r
+  Data  |= CMD_SYS_FLAG;\r
   Status = Out8042Data (Data);\r
   if (EFI_ERROR (Status)) {\r
     return Status;\r
@@ -155,7 +151,7 @@ KbcDisableKb (
 **/\r
 EFI_STATUS\r
 CheckKbStatus (\r
-  OUT BOOLEAN                             *KeyboardEnable\r
+  OUT BOOLEAN  *KeyboardEnable\r
   )\r
 {\r
   EFI_STATUS  Status;\r
@@ -173,6 +169,7 @@ CheckKbStatus (
   if (EFI_ERROR (Status)) {\r
     return Status;\r
   }\r
+\r
   //\r
   // Check keyboard enable or not\r
   //\r
@@ -207,6 +204,7 @@ PS2MouseReset (
   if (EFI_ERROR (Status)) {\r
     return Status;\r
   }\r
+\r
   //\r
   // Check BAT Complete Code\r
   //\r
@@ -218,6 +216,7 @@ PS2MouseReset (
   if (EFI_ERROR (Status)) {\r
     return Status;\r
   }\r
+\r
   //\r
   // Check BAT Complete Code\r
   //\r
@@ -237,7 +236,7 @@ PS2MouseReset (
 **/\r
 EFI_STATUS\r
 PS2MouseSetSampleRate (\r
-  IN MOUSE_SR                             SampleRate\r
+  IN MOUSE_SR  SampleRate\r
   )\r
 {\r
   EFI_STATUS  Status;\r
@@ -264,7 +263,7 @@ PS2MouseSetSampleRate (
 **/\r
 EFI_STATUS\r
 PS2MouseSetResolution (\r
-  IN MOUSE_RE                             Resolution\r
+  IN MOUSE_RE  Resolution\r
   )\r
 {\r
   EFI_STATUS  Status;\r
@@ -291,7 +290,7 @@ PS2MouseSetResolution (
 **/\r
 EFI_STATUS\r
 PS2MouseSetScaling (\r
-  IN MOUSE_SF                             Scaling\r
+  IN MOUSE_SF  Scaling\r
   )\r
 {\r
   //\r
@@ -327,7 +326,7 @@ PS2MouseEnable (
 **/\r
 EFI_STATUS\r
 PS2MouseGetPacket (\r
-  PS2_MOUSE_DEV     *MouseDev\r
+  PS2_MOUSE_DEV  *MouseDev\r
   )\r
 \r
 {\r
@@ -342,111 +341,112 @@ PS2MouseGetPacket (
   BOOLEAN     LButton;\r
   BOOLEAN     RButton;\r
 \r
-  KeyboardEnable  = FALSE;\r
-  State           = PS2_READ_BYTE_ONE;\r
+  KeyboardEnable = FALSE;\r
+  State          = PS2_READ_BYTE_ONE;\r
 \r
   //\r
   // State machine to get mouse packet\r
   //\r
   while (1) {\r
-\r
     switch (State) {\r
-    case PS2_READ_BYTE_ONE:\r
-      //\r
-      // Read mouse first byte data, if failed, immediately return\r
-      //\r
-      KbcDisableAux ();\r
-      Count  = 1;\r
-      Status = PS2MouseRead (&Data, &Count, State);\r
-      if (EFI_ERROR (Status)) {\r
-        KbcEnableAux ();\r
-        return EFI_NOT_READY;\r
-      }\r
-\r
-      if (Count != 1) {\r
-        KbcEnableAux ();\r
-        return EFI_NOT_READY;\r
-      }\r
-\r
-      if (IS_PS2_SYNC_BYTE (Data)) {\r
-        Packet[0] = Data;\r
-        State     = PS2_READ_DATA_BYTE;\r
-\r
-        CheckKbStatus (&KeyboardEnable);\r
-        KbcDisableKb ();\r
-        KbcEnableAux ();\r
-      }\r
-      break;\r
+      case PS2_READ_BYTE_ONE:\r
+        //\r
+        // Read mouse first byte data, if failed, immediately return\r
+        //\r
+        KbcDisableAux ();\r
+        Count  = 1;\r
+        Status = PS2MouseRead (&Data, &Count, State);\r
+        if (EFI_ERROR (Status)) {\r
+          KbcEnableAux ();\r
+          return EFI_NOT_READY;\r
+        }\r
 \r
-    case PS2_READ_DATA_BYTE:\r
-      Count   = 2;\r
-      Status  = PS2MouseRead ((Packet + 1), &Count, State);\r
-      if (EFI_ERROR (Status)) {\r
-        if (KeyboardEnable) {\r
-          KbcEnableKb ();\r
+        if (Count != 1) {\r
+          KbcEnableAux ();\r
+          return EFI_NOT_READY;\r
+        }\r
+\r
+        if (IS_PS2_SYNC_BYTE (Data)) {\r
+          Packet[0] = Data;\r
+          State     = PS2_READ_DATA_BYTE;\r
+\r
+          CheckKbStatus (&KeyboardEnable);\r
+          KbcDisableKb ();\r
+          KbcEnableAux ();\r
+        }\r
+\r
+        break;\r
+\r
+      case PS2_READ_DATA_BYTE:\r
+        Count  = 2;\r
+        Status = PS2MouseRead ((Packet + 1), &Count, State);\r
+        if (EFI_ERROR (Status)) {\r
+          if (KeyboardEnable) {\r
+            KbcEnableKb ();\r
+          }\r
+\r
+          return EFI_NOT_READY;\r
+        }\r
+\r
+        if (Count != 2) {\r
+          if (KeyboardEnable) {\r
+            KbcEnableKb ();\r
+          }\r
+\r
+          return EFI_NOT_READY;\r
         }\r
 \r
-        return EFI_NOT_READY;\r
-      }\r
+        State = PS2_PROCESS_PACKET;\r
+        break;\r
 \r
-      if (Count != 2) {\r
+      case PS2_PROCESS_PACKET:\r
         if (KeyboardEnable) {\r
           KbcEnableKb ();\r
         }\r
 \r
-        return EFI_NOT_READY;\r
-      }\r
+        //\r
+        // Decode the packet\r
+        //\r
+        RelativeMovementX = Packet[1];\r
+        RelativeMovementY = Packet[2];\r
+        //\r
+        //               Bit 7   |    Bit 6   |    Bit 5   |   Bit 4    |   Bit 3  |   Bit 2    |   Bit 1   |   Bit 0\r
+        //  Byte 0  | Y overflow | X overflow | Y sign bit | X sign bit | Always 1 | Middle Btn | Right Btn | Left Btn\r
+        //  Byte 1  |                                           8 bit X Movement\r
+        //  Byte 2  |                                           8 bit Y Movement\r
+        //\r
+        // X sign bit + 8 bit X Movement : 9-bit signed twos complement integer that presents the relative displacement of the device in the X direction since the last data transmission.\r
+        // Y sign bit + 8 bit Y Movement : Same as X sign bit + 8 bit X Movement.\r
+        //\r
+        //\r
+        // First, Clear X and Y high 8 bits\r
+        //\r
+        RelativeMovementX = (INT16)(RelativeMovementX & 0xFF);\r
+        RelativeMovementY = (INT16)(RelativeMovementY & 0xFF);\r
+        //\r
+        // Second, if the 9-bit signed twos complement integer is negative, set the high 8 bit 0xff\r
+        //\r
+        if ((Packet[0] & 0x10) != 0) {\r
+          RelativeMovementX = (INT16)(RelativeMovementX | 0xFF00);\r
+        }\r
 \r
-      State = PS2_PROCESS_PACKET;\r
-      break;\r
+        if ((Packet[0] & 0x20) != 0) {\r
+          RelativeMovementY = (INT16)(RelativeMovementY | 0xFF00);\r
+        }\r
+\r
+        RButton = (UINT8)(Packet[0] & 0x2);\r
+        LButton = (UINT8)(Packet[0] & 0x1);\r
+\r
+        //\r
+        // Update mouse state\r
+        //\r
+        MouseDev->State.RelativeMovementX += RelativeMovementX;\r
+        MouseDev->State.RelativeMovementY -= RelativeMovementY;\r
+        MouseDev->State.RightButton        = (UINT8)(RButton ? TRUE : FALSE);\r
+        MouseDev->State.LeftButton         = (UINT8)(LButton ? TRUE : FALSE);\r
+        MouseDev->StateChanged             = TRUE;\r
 \r
-    case PS2_PROCESS_PACKET:\r
-      if (KeyboardEnable) {\r
-        KbcEnableKb ();\r
-      }\r
-      //\r
-      // Decode the packet\r
-      //\r
-      RelativeMovementX = Packet[1];\r
-      RelativeMovementY = Packet[2];\r
-      //\r
-      //               Bit 7   |    Bit 6   |    Bit 5   |   Bit 4    |   Bit 3  |   Bit 2    |   Bit 1   |   Bit 0\r
-      //  Byte 0  | Y overflow | X overflow | Y sign bit | X sign bit | Always 1 | Middle Btn | Right Btn | Left Btn\r
-      //  Byte 1  |                                           8 bit X Movement\r
-      //  Byte 2  |                                           8 bit Y Movement\r
-      //\r
-      // X sign bit + 8 bit X Movement : 9-bit signed twos complement integer that presents the relative displacement of the device in the X direction since the last data transmission.\r
-      // Y sign bit + 8 bit Y Movement : Same as X sign bit + 8 bit X Movement.\r
-      //\r
-      //\r
-      // First, Clear X and Y high 8 bits\r
-      //\r
-      RelativeMovementX = (INT16) (RelativeMovementX & 0xFF);\r
-      RelativeMovementY = (INT16) (RelativeMovementY & 0xFF);\r
-      //\r
-      // Second, if the 9-bit signed twos complement integer is negative, set the high 8 bit 0xff\r
-      //\r
-      if ((Packet[0] & 0x10) != 0) {\r
-        RelativeMovementX = (INT16) (RelativeMovementX | 0xFF00);\r
-      }\r
-      if ((Packet[0] & 0x20) != 0) {\r
-        RelativeMovementY = (INT16) (RelativeMovementY | 0xFF00);\r
-      }\r
-\r
-\r
-      RButton           = (UINT8) (Packet[0] & 0x2);\r
-      LButton           = (UINT8) (Packet[0] & 0x1);\r
-\r
-      //\r
-      // Update mouse state\r
-      //\r
-      MouseDev->State.RelativeMovementX += RelativeMovementX;\r
-      MouseDev->State.RelativeMovementY -= RelativeMovementY;\r
-      MouseDev->State.RightButton = (UINT8) (RButton ? TRUE : FALSE);\r
-      MouseDev->State.LeftButton  = (UINT8) (LButton ? TRUE : FALSE);\r
-      MouseDev->StateChanged      = TRUE;\r
-\r
-      return EFI_SUCCESS;\r
+        return EFI_SUCCESS;\r
     }\r
   }\r
 }\r
@@ -462,15 +462,15 @@ PS2MouseGetPacket (
 **/\r
 EFI_STATUS\r
 PS2MouseRead (\r
-  OUT UINT8                               *Buffer,\r
-  IN OUT UINTN                            *BufSize,\r
-  IN  UINTN                               State\r
+  OUT UINT8     *Buffer,\r
+  IN OUT UINTN  *BufSize,\r
+  IN  UINTN     State\r
   )\r
 {\r
   EFI_STATUS  Status;\r
   UINTN       BytesRead;\r
 \r
-  Status    = EFI_SUCCESS;\r
+  Status = EFI_SUCCESS;\r
 \r
   if (State == PS2_READ_BYTE_ONE) {\r
     //\r
@@ -484,17 +484,18 @@ PS2MouseRead (
   }\r
 \r
   for (BytesRead = 0; BytesRead < *BufSize; BytesRead++) {\r
-\r
     Status = WaitOutputFull (TIMEOUT);\r
     if (EFI_ERROR (Status)) {\r
       break;\r
     }\r
+\r
     Buffer[BytesRead] = IoRead8 (KBC_DATA_PORT);\r
   }\r
+\r
   //\r
   // Verify the correct number of bytes read\r
   //\r
-  if (BytesRead == 0 || BytesRead != *BufSize) {\r
+  if ((BytesRead == 0) || (BytesRead != *BufSize)) {\r
     Status = EFI_NOT_FOUND;\r
   }\r
 \r
@@ -505,6 +506,7 @@ PS2MouseRead (
 //\r
 // 8042 I/O function\r
 //\r
+\r
 /**\r
   I/O work flow of outing 8042 command.\r
 \r
@@ -515,7 +517,7 @@ PS2MouseRead (
 **/\r
 EFI_STATUS\r
 Out8042Command (\r
-  IN UINT8                                Command\r
+  IN UINT8  Command\r
   )\r
 {\r
   EFI_STATUS  Status;\r
@@ -527,6 +529,7 @@ Out8042Command (
   if (EFI_ERROR (Status)) {\r
     return Status;\r
   }\r
+\r
   //\r
   // Send command\r
   //\r
@@ -550,10 +553,11 @@ Out8042Command (
 **/\r
 EFI_STATUS\r
 Out8042Data (\r
-  IN UINT8                                Data\r
+  IN UINT8  Data\r
   )\r
 {\r
   EFI_STATUS  Status;\r
+\r
   //\r
   // Wait keyboard controller input buffer empty\r
   //\r
@@ -576,10 +580,10 @@ Out8042Data (
 **/\r
 EFI_STATUS\r
 In8042Data (\r
-  IN OUT UINT8                            *Data\r
+  IN OUT UINT8  *Data\r
   )\r
 {\r
-  UINTN Delay;\r
+  UINTN  Delay;\r
 \r
   Delay = TIMEOUT / 50;\r
 \r
@@ -615,8 +619,8 @@ In8042Data (
 **/\r
 EFI_STATUS\r
 Out8042AuxCommand (\r
-  IN UINT8                                Command,\r
-  IN BOOLEAN                              Resend\r
+  IN UINT8    Command,\r
+  IN BOOLEAN  Resend\r
   )\r
 {\r
   EFI_STATUS  Status;\r
@@ -629,6 +633,7 @@ Out8042AuxCommand (
   if (EFI_ERROR (Status)) {\r
     return Status;\r
   }\r
+\r
   //\r
   // Send write to auxiliary device command\r
   //\r
@@ -638,6 +643,7 @@ Out8042AuxCommand (
   if (EFI_ERROR (Status)) {\r
     return Status;\r
   }\r
+\r
   //\r
   // Send auxiliary device command\r
   //\r
@@ -656,13 +662,11 @@ Out8042AuxCommand (
     // Receive mouse acknowledge, command send success\r
     //\r
     return EFI_SUCCESS;\r
-\r
   } else if (Resend) {\r
     //\r
     // Resend fail\r
     //\r
     return EFI_DEVICE_ERROR;\r
-\r
   } else if (Data == PS2_RESEND) {\r
     //\r
     // Resend command\r
@@ -671,13 +675,11 @@ Out8042AuxCommand (
     if (EFI_ERROR (Status)) {\r
       return Status;\r
     }\r
-\r
   } else {\r
     //\r
     // Invalid return code\r
     //\r
     return EFI_DEVICE_ERROR;\r
-\r
   }\r
 \r
   return EFI_SUCCESS;\r
@@ -693,10 +695,11 @@ Out8042AuxCommand (
 **/\r
 EFI_STATUS\r
 Out8042AuxData (\r
-  IN UINT8                                Data\r
+  IN UINT8  Data\r
   )\r
 {\r
   EFI_STATUS  Status;\r
+\r
   //\r
   // Wait keyboard controller input buffer empty\r
   //\r
@@ -704,6 +707,7 @@ Out8042AuxData (
   if (EFI_ERROR (Status)) {\r
     return Status;\r
   }\r
+\r
   //\r
   // Send write to auxiliary device command\r
   //\r
@@ -734,7 +738,7 @@ Out8042AuxData (
 **/\r
 EFI_STATUS\r
 In8042AuxData (\r
-  IN OUT UINT8                            *Data\r
+  IN OUT UINT8  *Data\r
   )\r
 {\r
   EFI_STATUS  Status;\r
@@ -752,7 +756,6 @@ In8042AuxData (
   return EFI_SUCCESS;\r
 }\r
 \r
-\r
 /**\r
   Check keyboard controller status, if it is output buffer full and for auxiliary device.\r
 \r
@@ -764,7 +767,7 @@ CheckForInput (
   VOID\r
   )\r
 {\r
-  UINT8 Data;\r
+  UINT8  Data;\r
 \r
   Data = IoRead8 (KBC_CMD_STS_PORT);\r
 \r
@@ -781,18 +784,18 @@ CheckForInput (
 /**\r
   I/O work flow to wait input buffer empty in given time.\r
 \r
-  @param Timeout Wating time.\r
+  @param Timeout Waiting time.\r
 \r
   @retval EFI_TIMEOUT if input is still not empty in given time.\r
   @retval EFI_SUCCESS input is empty.\r
 **/\r
 EFI_STATUS\r
 WaitInputEmpty (\r
-  IN UINTN                                Timeout\r
+  IN UINTN  Timeout\r
   )\r
 {\r
-  UINTN Delay;\r
-  UINT8 Data;\r
+  UINTN  Delay;\r
+  UINT8  Data;\r
 \r
   Delay = Timeout / 50;\r
 \r
@@ -827,11 +830,11 @@ WaitInputEmpty (
 **/\r
 EFI_STATUS\r
 WaitOutputFull (\r
-  IN UINTN                                Timeout\r
+  IN UINTN  Timeout\r
   )\r
 {\r
-  UINTN Delay;\r
-  UINT8 Data;\r
+  UINTN  Delay;\r
+  UINT8  Data;\r
 \r
   Delay = Timeout / 50;\r
 \r