]>
git.proxmox.com Git - mirror_edk2.git/blob - IntelFrameworkModulePkg/Bus/Isa/Ps2MouseDxe/CommPs2.c
5e18d4949f35e87a9cfc8226b9bf695f5faba639
3 Copyright (c) 2006 - 2007, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
14 PS2 Mouse Communication Interface
20 // Include common header file for this module.
22 #include "CommonHeader.h"
27 UINT8 SampleRateTbl
[ MAX_SR
] = { 0xa , 0x14 , 0x28 , 0x3c , 0x50 , 0x64 , 0xc8 };
29 UINT8 ResolutionTbl
[ MAX_CMR
] = { 0 , 1 , 2 , 3 };
33 IN EFI_ISA_IO_PROTOCOL
* IsaIo
39 GC_TODO: Add function description
43 IsaIo - GC_TODO: add argument description
47 EFI_DEVICE_ERROR - GC_TODO: Add description for return value
48 EFI_SUCCESS - GC_TODO: Add description for return value
56 // Keyboard controller self test
58 Status
= Out8042Command ( IsaIo
, SELF_TEST
);
59 if ( EFI_ERROR ( Status
)) {
65 Status
= In8042Data ( IsaIo
, & Data
);
66 if ( EFI_ERROR ( Status
)) {
71 return EFI_DEVICE_ERROR
;
76 Status
= Out8042Command ( IsaIo
, READ_CMD_BYTE
);
77 if ( EFI_ERROR ( Status
)) {
81 Status
= In8042Data ( IsaIo
, & Data
);
82 if ( EFI_ERROR ( Status
)) {
86 Status
= Out8042Command ( IsaIo
, WRITE_CMD_BYTE
);
87 if ( EFI_ERROR ( Status
)) {
92 Status
= Out8042Data ( IsaIo
, Data
);
93 if ( EFI_ERROR ( Status
)) {
102 IN EFI_ISA_IO_PROTOCOL
* IsaIo
108 GC_TODO: Add function description
112 IsaIo - GC_TODO: add argument description
116 GC_TODO: add return values
121 // Send 8042 enable mouse command
123 return Out8042Command ( IsaIo
, ENABLE_AUX
);
128 IN EFI_ISA_IO_PROTOCOL
* IsaIo
134 GC_TODO: Add function description
138 IsaIo - GC_TODO: add argument description
142 GC_TODO: add return values
147 // Send 8042 disable mouse command
149 return Out8042Command ( IsaIo
, DISABLE_AUX
);
154 IN EFI_ISA_IO_PROTOCOL
* IsaIo
160 GC_TODO: Add function description
164 IsaIo - GC_TODO: add argument description
168 GC_TODO: add return values
173 // Send 8042 enable keyboard command
175 return Out8042Command ( IsaIo
, ENABLE_KB
);
180 IN EFI_ISA_IO_PROTOCOL
* IsaIo
186 GC_TODO: Add function description
190 IsaIo - GC_TODO: add argument description
194 GC_TODO: add return values
199 // Send 8042 disable keyboard command
201 return Out8042Command ( IsaIo
, DISABLE_KB
);
206 IN EFI_ISA_IO_PROTOCOL
* IsaIo
,
207 OUT BOOLEAN
* KeyboardEnable
213 GC_TODO: Add function description
217 IsaIo - GC_TODO: add argument description
218 KeyboardEnable - GC_TODO: add argument description
222 EFI_SUCCESS - GC_TODO: Add description for return value
230 // Send command to read KBC command byte
232 Status
= Out8042Command ( IsaIo
, READ_CMD_BYTE
);
233 if ( EFI_ERROR ( Status
)) {
237 Status
= In8042Data ( IsaIo
, & Data
);
238 if ( EFI_ERROR ( Status
)) {
242 // Check keyboard enable or not
244 if (( Data
& CMD_KB_STS
) == CMD_KB_DIS
) {
245 * KeyboardEnable
= FALSE
;
247 * KeyboardEnable
= TRUE
;
255 IN EFI_ISA_IO_PROTOCOL
* IsaIo
261 GC_TODO: Add function description
265 IsaIo - GC_TODO: add argument description
269 EFI_DEVICE_ERROR - GC_TODO: Add description for return value
270 EFI_DEVICE_ERROR - GC_TODO: Add description for return value
271 EFI_SUCCESS - GC_TODO: Add description for return value
278 Status
= Out8042AuxCommand ( IsaIo
, RESET_CMD
, FALSE
);
279 if ( EFI_ERROR ( Status
)) {
283 Status
= In8042AuxData ( IsaIo
, & Data
);
284 if ( EFI_ERROR ( Status
)) {
288 // Check BAT Complete Code
290 if ( Data
!= PS2MOUSE_BAT1
) {
291 return EFI_DEVICE_ERROR
;
294 Status
= In8042AuxData ( IsaIo
, & Data
);
295 if ( EFI_ERROR ( Status
)) {
299 // Check BAT Complete Code
301 if ( Data
!= PS2MOUSE_BAT2
) {
302 return EFI_DEVICE_ERROR
;
309 PS2MouseSetSampleRate (
310 IN EFI_ISA_IO_PROTOCOL
* IsaIo
,
311 IN MOUSE_SR SampleRate
317 GC_TODO: Add function description
321 IsaIo - GC_TODO: add argument description
322 SampleRate - GC_TODO: add argument description
326 GC_TODO: add return values
333 // Send auxiliary command to set mouse sample rate
335 Status
= Out8042AuxCommand ( IsaIo
, SETSR_CMD
, FALSE
);
336 if ( EFI_ERROR ( Status
)) {
340 Status
= Out8042AuxData ( IsaIo
, SampleRateTbl
[ SampleRate
]);
346 PS2MouseSetResolution (
347 IN EFI_ISA_IO_PROTOCOL
* IsaIo
,
348 IN MOUSE_RE Resolution
354 GC_TODO: Add function description
358 IsaIo - GC_TODO: add argument description
359 Resolution - GC_TODO: add argument description
363 GC_TODO: add return values
370 // Send auxiliary command to set mouse resolution
372 Status
= Out8042AuxCommand ( IsaIo
, SETRE_CMD
, FALSE
);
373 if ( EFI_ERROR ( Status
)) {
377 Status
= Out8042AuxData ( IsaIo
, ResolutionTbl
[ Resolution
]);
384 IN EFI_ISA_IO_PROTOCOL
* IsaIo
,
391 GC_TODO: Add function description
395 IsaIo - GC_TODO: add argument description
396 Scaling - GC_TODO: add argument description
400 GC_TODO: add return values
406 Command
= ( UINT8
) ( Scaling
== SF1
? SETSF1_CMD
: SETSF2_CMD
);
409 // Send auxiliary command to set mouse scaling data
411 return Out8042AuxCommand ( IsaIo
, Command
, FALSE
);
416 IN EFI_ISA_IO_PROTOCOL
* IsaIo
422 GC_TODO: Add function description
426 IsaIo - GC_TODO: add argument description
430 GC_TODO: add return values
435 // Send auxiliary command to enable mouse
437 return Out8042AuxCommand ( IsaIo
, ENABLE_CMD
, FALSE
);
442 PS2_MOUSE_DEV
* MouseDev
448 Get mouse packet . Only care first 3 bytes
452 MouseDev - Pointer of PS2 Mouse Private Data Structure
456 EFI_NOT_READY - Mouse Device not ready to input data packet, or some error happened during getting the packet
457 EFI_SUCCESS - The data packet is gotten successfully.
462 BOOLEAN KeyboardEnable
;
463 UINT8 Packet
[ PS2_PACKET_LENGTH
];
467 INT16 RelativeMovementX
;
468 INT16 RelativeMovementY
;
472 KeyboardEnable
= FALSE
;
474 State
= PS2_READ_BYTE_ONE
;
477 // State machine to get mouse packet
482 case PS2_READ_BYTE_ONE
:
484 // Read mouse first byte data, if failed, immediately return
486 KbcDisableAux ( MouseDev
-> IsaIo
);
487 Status
= PS2MouseRead ( MouseDev
-> IsaIo
, & Data
, & Count
, State
);
488 if ( EFI_ERROR ( Status
)) {
489 KbcEnableAux ( MouseDev
-> IsaIo
);
490 return EFI_NOT_READY
;
494 KbcEnableAux ( MouseDev
-> IsaIo
);
495 return EFI_NOT_READY
;
498 if ( IS_PS2_SYNC_BYTE ( Data
)) {
500 State
= PS2_READ_DATA_BYTE
;
502 CheckKbStatus ( MouseDev
-> IsaIo
, & KeyboardEnable
);
503 KbcDisableKb ( MouseDev
-> IsaIo
);
504 KbcEnableAux ( MouseDev
-> IsaIo
);
508 case PS2_READ_DATA_BYTE
:
510 Status
= PS2MouseRead ( MouseDev
-> IsaIo
, ( Packet
+ 1 ), & Count
, State
);
511 if ( EFI_ERROR ( Status
)) {
512 if ( KeyboardEnable
) {
513 KbcEnableKb ( MouseDev
-> IsaIo
);
516 return EFI_NOT_READY
;
520 if ( KeyboardEnable
) {
521 KbcEnableKb ( MouseDev
-> IsaIo
);
524 return EFI_NOT_READY
;
527 State
= PS2_PROCESS_PACKET
;
530 case PS2_PROCESS_PACKET
:
531 if ( KeyboardEnable
) {
532 KbcEnableKb ( MouseDev
-> IsaIo
);
537 RelativeMovementX
= Packet
[ 1 ];
538 RelativeMovementY
= Packet
[ 2 ];
540 // Bit 7 | Bit 6 | Bit 5 | Bit 4 | Bit 3 | Bit 2 | Bit 1 | Bit 0
541 // Byte 0 | Y overflow | X overflow | Y sign bit | X sign bit | Always 1 | Middle Btn | Right Btn | Left Btn
542 // Byte 1 | 8 bit X Movement
543 // Byte 2 | 8 bit Y Movement
545 // 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.
546 // Y sign bit + 8 bit Y Movement : Same as X sign bit + 8 bit X Movement.
549 // First, Clear X and Y high 8 bits
551 RelativeMovementX
= ( INT16
) ( RelativeMovementX
& 0xFF );
552 RelativeMovementY
= ( INT16
) ( RelativeMovementY
& 0xFF );
554 // Second, if the 9-bit signed twos complement integer is negative, set the high 8 bit 0xff
556 if (( Packet
[ 0 ] & 0x10 ) != 0 ) {
557 RelativeMovementX
= ( INT16
) ( RelativeMovementX
| 0xFF00 );
559 if (( Packet
[ 0 ] & 0x20 ) != 0 ) {
560 RelativeMovementY
= ( INT16
) ( RelativeMovementY
| 0xFF00 );
564 RButton
= ( UINT8
) ( Packet
[ 0 ] & 0x2 );
565 LButton
= ( UINT8
) ( Packet
[ 0 ] & 0x1 );
568 // Update mouse state
570 MouseDev
-> State
. RelativeMovementX
+= RelativeMovementX
;
571 MouseDev
-> State
. RelativeMovementY
-= RelativeMovementY
;
572 MouseDev
-> State
. RightButton
= ( UINT8
) ( RButton
? TRUE
: FALSE
);
573 MouseDev
-> State
. LeftButton
= ( UINT8
) ( LButton
? TRUE
: FALSE
);
574 MouseDev
-> StateChanged
= TRUE
;
583 IN EFI_ISA_IO_PROTOCOL
* IsaIo
,
585 IN OUT UINTN
* BufSize
,
592 GC_TODO: Add function description
596 IsaIo - GC_TODO: add argument description
597 Buffer - GC_TODO: add argument description
598 BufSize - GC_TODO: add argument description
599 State - GC_TODO: add argument description
603 GC_TODO: add return values
610 Status
= EFI_SUCCESS
;
613 if ( State
== PS2_READ_BYTE_ONE
) {
615 // Check input for mouse
617 Status
= CheckForInput ( IsaIo
);
619 if ( EFI_ERROR ( Status
)) {
624 while ( BytesRead
< * BufSize
) {
626 Status
= WaitOutputFull ( IsaIo
, TIMEOUT
);
627 if ( EFI_ERROR ( Status
)) {
631 IsaIo
-> Io
. Read ( IsaIo
, EfiIsaIoWidthUint8
, KBC_DATA_PORT
, 1 , Buffer
);
634 Buffer
= ( UINT8
*) Buffer
+ 1 ;
637 // Verify the correct number of bytes read
639 if ( BytesRead
== 0 || BytesRead
!= * BufSize
) {
640 Status
= EFI_NOT_FOUND
;
643 * BufSize
= BytesRead
;
651 IN EFI_ISA_IO_PROTOCOL
* IsaIo
,
658 GC_TODO: Add function description
662 IsaIo - GC_TODO: add argument description
663 Command - GC_TODO: add argument description
667 EFI_SUCCESS - GC_TODO: Add description for return value
675 // Wait keyboard controller input buffer empty
677 Status
= WaitInputEmpty ( IsaIo
, TIMEOUT
);
678 if ( EFI_ERROR ( Status
)) {
685 IsaIo
-> Io
. Write ( IsaIo
, EfiIsaIoWidthUint8
, KBC_CMD_STS_PORT
, 1 , & Data
);
687 Status
= WaitInputEmpty ( IsaIo
, TIMEOUT
);
688 if ( EFI_ERROR ( Status
)) {
697 IN EFI_ISA_IO_PROTOCOL
* IsaIo
,
704 GC_TODO: Add function description
708 IsaIo - GC_TODO: add argument description
709 Data - GC_TODO: add argument description
713 EFI_SUCCESS - GC_TODO: Add description for return value
720 // Wait keyboard controller input buffer empty
722 Status
= WaitInputEmpty ( IsaIo
, TIMEOUT
);
723 if ( EFI_ERROR ( Status
)) {
728 IsaIo
-> Io
. Write ( IsaIo
, EfiIsaIoWidthUint8
, KBC_DATA_PORT
, 1 , & temp
);
730 Status
= WaitInputEmpty ( IsaIo
, TIMEOUT
);
731 if ( EFI_ERROR ( Status
)) {
740 IN EFI_ISA_IO_PROTOCOL
* IsaIo
,
747 GC_TODO: Add function description
751 IsaIo - GC_TODO: add argument description
752 Data - GC_TODO: add argument description
756 EFI_TIMEOUT - GC_TODO: Add description for return value
757 EFI_SUCCESS - GC_TODO: Add description for return value
764 Delay
= TIMEOUT
/ 50 ;
767 IsaIo
-> Io
. Read ( IsaIo
, EfiIsaIoWidthUint8
, KBC_CMD_STS_PORT
, 1 , & temp
);
770 // Check keyboard controller status bit 0(output buffer status)
772 if (( temp
& KBC_OUTB
) == KBC_OUTB
) {
784 IsaIo
-> Io
. Read ( IsaIo
, EfiIsaIoWidthUint8
, KBC_DATA_PORT
, 1 , Data
);
791 IN EFI_ISA_IO_PROTOCOL
* IsaIo
,
799 GC_TODO: Add function description
803 IsaIo - GC_TODO: add argument description
804 Command - GC_TODO: add argument description
805 Resend - GC_TODO: add argument description
809 EFI_SUCCESS - GC_TODO: Add description for return value
810 EFI_DEVICE_ERROR - GC_TODO: Add description for return value
811 EFI_DEVICE_ERROR - GC_TODO: Add description for return value
812 EFI_SUCCESS - GC_TODO: Add description for return value
820 // Wait keyboard controller input buffer empty
822 Status
= WaitInputEmpty ( IsaIo
, TIMEOUT
);
823 if ( EFI_ERROR ( Status
)) {
827 // Send write to auxiliary device command
829 Data
= WRITE_AUX_DEV
;
830 IsaIo
-> Io
. Write ( IsaIo
, EfiIsaIoWidthUint8
, KBC_CMD_STS_PORT
, 1 , & Data
);
832 Status
= WaitInputEmpty ( IsaIo
, TIMEOUT
);
833 if ( EFI_ERROR ( Status
)) {
837 // Send auxiliary device command
839 IsaIo
-> Io
. Write ( IsaIo
, EfiIsaIoWidthUint8
, KBC_DATA_PORT
, 1 , & Command
);
844 Status
= In8042AuxData ( IsaIo
, & Data
);
845 if ( EFI_ERROR ( Status
)) {
849 if ( Data
== PS2_ACK
) {
851 // Receive mouse acknowledge, command send success
859 return EFI_DEVICE_ERROR
;
861 } else if ( Data
== PS2_RESEND
) {
865 Status
= Out8042AuxCommand ( IsaIo
, Command
, TRUE
);
866 if ( EFI_ERROR ( Status
)) {
872 // Invalid return code
874 return EFI_DEVICE_ERROR
;
883 IN EFI_ISA_IO_PROTOCOL
* IsaIo
,
890 GC_TODO: Add function description
894 IsaIo - GC_TODO: add argument description
895 Data - GC_TODO: add argument description
899 EFI_SUCCESS - GC_TODO: Add description for return value
906 // Wait keyboard controller input buffer empty
908 Status
= WaitInputEmpty ( IsaIo
, TIMEOUT
);
909 if ( EFI_ERROR ( Status
)) {
913 // Send write to auxiliary device command
915 Temp
= WRITE_AUX_DEV
;
916 IsaIo
-> Io
. Write ( IsaIo
, EfiIsaIoWidthUint8
, KBC_CMD_STS_PORT
, 1 , & Temp
);
918 Status
= WaitInputEmpty ( IsaIo
, TIMEOUT
);
919 if ( EFI_ERROR ( Status
)) {
924 IsaIo
-> Io
. Write ( IsaIo
, EfiIsaIoWidthUint8
, KBC_DATA_PORT
, 1 , & Temp
);
926 Status
= WaitInputEmpty ( IsaIo
, TIMEOUT
);
927 if ( EFI_ERROR ( Status
)) {
936 IN EFI_ISA_IO_PROTOCOL
* IsaIo
,
943 GC_TODO: Add function description
947 IsaIo - GC_TODO: add argument description
948 Data - GC_TODO: add argument description
952 EFI_SUCCESS - GC_TODO: Add description for return value
959 // wait for output data
961 Status
= WaitOutputFull ( IsaIo
, BAT_TIMEOUT
);
962 if ( EFI_ERROR ( Status
)) {
966 IsaIo
-> Io
. Read ( IsaIo
, EfiIsaIoWidthUint8
, KBC_DATA_PORT
, 1 , Data
);
973 IN EFI_ISA_IO_PROTOCOL
* IsaIo
979 GC_TODO: Add function description
983 IsaIo - GC_TODO: add argument description
987 EFI_NOT_READY - GC_TODO: Add description for return value
988 EFI_SUCCESS - GC_TODO: Add description for return value
994 IsaIo
-> Io
. Read ( IsaIo
, EfiIsaIoWidthUint8
, KBC_CMD_STS_PORT
, 1 , & Data
);
997 // Check keyboard controller status, if it is output buffer full and for auxiliary device
999 if (( Data
& ( KBC_OUTB
| KBC_AUXB
)) != ( KBC_OUTB
| KBC_AUXB
)) {
1000 return EFI_NOT_READY
;
1008 IN EFI_ISA_IO_PROTOCOL
* IsaIo
,
1013 Routine Description:
1015 GC_TODO: Add function description
1019 IsaIo - GC_TODO: add argument description
1020 Timeout - GC_TODO: add argument description
1024 EFI_TIMEOUT - GC_TODO: Add description for return value
1025 EFI_SUCCESS - GC_TODO: Add description for return value
1032 Delay
= Timeout
/ 50 ;
1035 IsaIo
-> Io
. Read ( IsaIo
, EfiIsaIoWidthUint8
, KBC_CMD_STS_PORT
, 1 , & Data
);
1038 // Check keyboard controller status bit 1(input buffer status)
1040 if (( Data
& KBC_INPB
) == 0 ) {
1057 IN EFI_ISA_IO_PROTOCOL
* IsaIo
,
1062 Routine Description:
1064 GC_TODO: Add function description
1068 IsaIo - GC_TODO: add argument description
1069 Timeout - GC_TODO: add argument description
1073 EFI_TIMEOUT - GC_TODO: Add description for return value
1074 EFI_SUCCESS - GC_TODO: Add description for return value
1081 Delay
= Timeout
/ 50 ;
1084 IsaIo
-> Io
. Read ( IsaIo
, EfiIsaIoWidthUint8
, KBC_CMD_STS_PORT
, 1 , & Data
);
1087 // Check keyboard controller status bit 0(output buffer status)
1088 // & bit5(output buffer for auxiliary device)
1090 if (( Data
& ( KBC_OUTB
| KBC_AUXB
)) == ( KBC_OUTB
| KBC_AUXB
)) {