2 A faked PS/2 Touchpad driver. Routines that interacts with callers,
3 conforming to EFI driver model
5 Copyright (c) 2006 - 2007, Intel Corporation
6 All rights reserved. This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16 #include "Ps2MouseAbsolutePointer.h"
20 // DriverBinding Protocol Instance
22 EFI_DRIVER_BINDING_PROTOCOL gPS2MouseAbsolutePointerDriver
= {
23 PS2MouseAbsolutePointerDriverSupported
,
24 PS2MouseAbsolutePointerDriverStart
,
25 PS2MouseAbsolutePointerDriverStop
,
33 PS2MouseAbsolutePointerDriverSupported (
34 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
35 IN EFI_HANDLE Controller
,
36 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
42 ControllerDriver Protocol Method
49 // GC_TODO: This - add argument and description to function comment
50 // GC_TODO: Controller - add argument and description to function comment
51 // GC_TODO: RemainingDevicePath - add argument and description to function comment
54 EFI_ISA_IO_PROTOCOL
*IsaIo
;
59 // Open the IO Abstraction(s) needed to perform the supported test
61 Status
= gBS
->OpenProtocol (
63 &gEfiIsaIoProtocolGuid
,
65 This
->DriverBindingHandle
,
67 EFI_OPEN_PROTOCOL_BY_DRIVER
69 if (EFI_ERROR (Status
)) {
73 // Use the ISA I/O Protocol to see if Controller is the Mouse controller
75 switch (IsaIo
->ResourceList
->Device
.HID
) {
76 case EISA_PNP_ID (0xF03):
78 // Microsoft PS/2 style mouse
80 case EISA_PNP_ID (0xF13):
82 // PS/2 Port for PS/2-style Mice
86 case EISA_PNP_ID (0x303):
88 // IBM Enhanced (101/102-key, PS/2 mouse support)
90 if (IsaIo
->ResourceList
->Device
.UID
== 1) {
95 Status
= EFI_UNSUPPORTED
;
99 // Close the I/O Abstraction(s) used to perform the supported test
103 &gEfiIsaIoProtocolGuid
,
104 This
->DriverBindingHandle
,
113 PS2MouseAbsolutePointerDriverStart (
114 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
115 IN EFI_HANDLE Controller
,
116 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
121 Start protocol interfaces for the mouse device handles.
124 This - Protocol instance pointer.
125 Controller - Handle of device to bind driver to.
126 RemainingDevicePath - Not used.
129 EFI_SUCCESS - This driver is added to DeviceHandle.
130 other - Errors occurred.
135 EFI_STATUS EmptyStatus
;
136 EFI_ISA_IO_PROTOCOL
*IsaIo
;
137 PS2_MOUSE_ABSOLUTE_POINTER_DEV
*MouseAbsolutePointerDev
;
140 EFI_STATUS_CODE_VALUE StatusCode
;
141 EFI_DEVICE_PATH_PROTOCOL
*ParentDevicePath
;
144 MouseAbsolutePointerDev
= NULL
;
148 // Open the device path protocol
150 Status
= gBS
->OpenProtocol (
152 &gEfiDevicePathProtocolGuid
,
153 (VOID
**) &ParentDevicePath
,
154 This
->DriverBindingHandle
,
156 EFI_OPEN_PROTOCOL_BY_DRIVER
158 if (EFI_ERROR (Status
)) {
162 // Report that the keyboard is being enabled
164 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
166 EFI_PERIPHERAL_MOUSE
| EFI_P_PC_ENABLE
,
171 // Get the ISA I/O Protocol on Controller's handle
173 Status
= gBS
->OpenProtocol (
175 &gEfiIsaIoProtocolGuid
,
177 This
->DriverBindingHandle
,
179 EFI_OPEN_PROTOCOL_BY_DRIVER
181 if (EFI_ERROR (Status
)) {
184 &gEfiDevicePathProtocolGuid
,
185 This
->DriverBindingHandle
,
188 return EFI_INVALID_PARAMETER
;
191 // Raise TPL to avoid keyboard operation impact
193 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
196 // Allocate private data
198 MouseAbsolutePointerDev
= AllocateZeroPool (sizeof (PS2_MOUSE_ABSOLUTE_POINTER_DEV
));
199 if (MouseAbsolutePointerDev
== NULL
) {
200 Status
= EFI_OUT_OF_RESOURCES
;
204 // Setup the device instance
206 MouseAbsolutePointerDev
->Signature
= PS2_MOUSE_ABSOLUTE_POINTER_DEV_SIGNATURE
;
207 MouseAbsolutePointerDev
->Handle
= Controller
;
208 MouseAbsolutePointerDev
->SampleRate
= SSR_20
;
209 MouseAbsolutePointerDev
->Resolution
= CMR4
;
210 MouseAbsolutePointerDev
->Scaling
= SF1
;
211 MouseAbsolutePointerDev
->DataPackageSize
= 3;
212 MouseAbsolutePointerDev
->IsaIo
= IsaIo
;
213 MouseAbsolutePointerDev
->DevicePath
= ParentDevicePath
;
216 // Resolution = 4 counts/mm
218 MouseAbsolutePointerDev
->Mode
.AbsoluteMaxX
= 1024;
219 MouseAbsolutePointerDev
->Mode
.AbsoluteMinX
= 0;
220 MouseAbsolutePointerDev
->Mode
.AbsoluteMaxY
= 798;
221 MouseAbsolutePointerDev
->Mode
.AbsoluteMinY
= 0;
222 MouseAbsolutePointerDev
->Mode
.AbsoluteMaxZ
= 0;
223 MouseAbsolutePointerDev
->Mode
.AbsoluteMinZ
= 0;
224 MouseAbsolutePointerDev
->Mode
.Attributes
= 0x03;
226 MouseAbsolutePointerDev
->AbsolutePointerProtocol
.Reset
= MouseAbsolutePointerReset
;
227 MouseAbsolutePointerDev
->AbsolutePointerProtocol
.GetState
= MouseAbsolutePointerGetState
;
228 MouseAbsolutePointerDev
->AbsolutePointerProtocol
.Mode
= &(MouseAbsolutePointerDev
->Mode
);
231 // Initialize keyboard controller if necessary
233 IsaIo
->Io
.Read (IsaIo
, EfiIsaIoWidthUint8
, KBC_CMD_STS_PORT
, 1, &Data
);
234 if ((Data
& KBC_SYSF
) != KBC_SYSF
) {
235 Status
= KbcSelfTest (IsaIo
);
236 if (EFI_ERROR (Status
)) {
237 StatusCode
= EFI_PERIPHERAL_MOUSE
| EFI_P_EC_CONTROLLER_ERROR
;
242 KbcEnableAux (IsaIo
);
244 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
246 EFI_PERIPHERAL_MOUSE
| EFI_P_PC_PRESENCE_DETECT
,
253 Status
= MouseAbsolutePointerDev
->AbsolutePointerProtocol
.Reset (&MouseAbsolutePointerDev
->AbsolutePointerProtocol
, TRUE
);
254 if (EFI_ERROR (Status
)) {
256 // mouse not connected
258 Status
= EFI_SUCCESS
;
259 StatusCode
= EFI_PERIPHERAL_MOUSE
| EFI_P_EC_NOT_DETECTED
;
263 // Setup the WaitForKey event
265 Status
= gBS
->CreateEvent (
268 MouseAbsolutePointerWaitForInput
,
269 MouseAbsolutePointerDev
,
270 &((MouseAbsolutePointerDev
->AbsolutePointerProtocol
).WaitForInput
)
272 if (EFI_ERROR (Status
)) {
273 Status
= EFI_OUT_OF_RESOURCES
;
277 // Setup a periodic timer, used to poll mouse state
279 Status
= gBS
->CreateEvent (
280 EVT_TIMER
| EVT_NOTIFY_SIGNAL
,
282 PollMouseAbsolutePointer
,
283 MouseAbsolutePointerDev
,
284 &MouseAbsolutePointerDev
->TimerEvent
286 if (EFI_ERROR (Status
)) {
287 Status
= EFI_OUT_OF_RESOURCES
;
291 // Start timer to poll mouse (100 samples per second)
293 Status
= gBS
->SetTimer (MouseAbsolutePointerDev
->TimerEvent
, TimerPeriodic
, 100000);
294 if (EFI_ERROR (Status
)) {
295 Status
= EFI_OUT_OF_RESOURCES
;
299 MouseAbsolutePointerDev
->ControllerNameTable
= NULL
;
302 gPs2MouseAbsolutePointerComponentName
.SupportedLanguages
,
303 &MouseAbsolutePointerDev
->ControllerNameTable
,
304 L
"Faked PS/2 Touchpad Device",
309 gPs2MouseAbsolutePointerComponentName2
.SupportedLanguages
,
310 &MouseAbsolutePointerDev
->ControllerNameTable
,
311 L
"Faked PS/2 Touchpad Device",
317 // Install protocol interfaces for the mouse device.
319 Status
= gBS
->InstallMultipleProtocolInterfaces (
321 &gEfiAbsolutePointerProtocolGuid
,
322 &MouseAbsolutePointerDev
->AbsolutePointerProtocol
,
325 if (EFI_ERROR (Status
)) {
329 gBS
->RestoreTPL (OldTpl
);
335 KbcDisableAux (IsaIo
);
337 if (StatusCode
!= 0) {
338 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
339 EFI_ERROR_CODE
| EFI_ERROR_MINOR
,
345 if ((MouseAbsolutePointerDev
!= NULL
) && (MouseAbsolutePointerDev
->AbsolutePointerProtocol
.WaitForInput
!= NULL
)) {
346 gBS
->CloseEvent (MouseAbsolutePointerDev
->AbsolutePointerProtocol
.WaitForInput
);
349 if ((MouseAbsolutePointerDev
!= NULL
) && (MouseAbsolutePointerDev
->TimerEvent
!= NULL
)) {
350 gBS
->CloseEvent (MouseAbsolutePointerDev
->TimerEvent
);
353 if ((MouseAbsolutePointerDev
!= NULL
) && (MouseAbsolutePointerDev
->ControllerNameTable
!= NULL
)) {
354 FreeUnicodeStringTable (MouseAbsolutePointerDev
->ControllerNameTable
);
357 // Since there will be no timer handler for mouse input any more,
358 // exhaust input data just in case there is still mouse data left
360 EmptyStatus
= EFI_SUCCESS
;
361 while (!EFI_ERROR (EmptyStatus
)) {
362 EmptyStatus
= In8042Data (IsaIo
, &Data
);
365 if (MouseAbsolutePointerDev
!= NULL
) {
366 gBS
->FreePool (MouseAbsolutePointerDev
);
371 &gEfiDevicePathProtocolGuid
,
372 This
->DriverBindingHandle
,
378 &gEfiIsaIoProtocolGuid
,
379 This
->DriverBindingHandle
,
383 gBS
->RestoreTPL (OldTpl
);
390 PS2MouseAbsolutePointerDriverStop (
391 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
392 IN EFI_HANDLE Controller
,
393 IN UINTN NumberOfChildren
,
394 IN EFI_HANDLE
*ChildHandleBuffer
405 // GC_TODO: This - add argument and description to function comment
406 // GC_TODO: Controller - add argument and description to function comment
407 // GC_TODO: NumberOfChildren - add argument and description to function comment
408 // GC_TODO: ChildHandleBuffer - add argument and description to function comment
409 // GC_TODO: EFI_SUCCESS - add return value to function comment
410 // GC_TODO: EFI_SUCCESS - add return value to function comment
413 EFI_ABSOLUTE_POINTER_PROTOCOL
*AbsolutePointerProtocol
;
414 PS2_MOUSE_ABSOLUTE_POINTER_DEV
*MouseAbsolutePointerDev
;
417 Status
= gBS
->OpenProtocol (
419 &gEfiAbsolutePointerProtocolGuid
,
420 (VOID
**) &AbsolutePointerProtocol
,
421 This
->DriverBindingHandle
,
423 EFI_OPEN_PROTOCOL_GET_PROTOCOL
425 if (EFI_ERROR (Status
)) {
429 MouseAbsolutePointerDev
= PS2_MOUSE_ABSOLUTE_POINTER_DEV_FROM_THIS (AbsolutePointerProtocol
);
432 // Report that the keyboard is being disabled
434 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
436 EFI_PERIPHERAL_MOUSE
| EFI_P_PC_DISABLE
,
437 MouseAbsolutePointerDev
->DevicePath
440 Status
= gBS
->UninstallProtocolInterface (
442 &gEfiAbsolutePointerProtocolGuid
,
443 &MouseAbsolutePointerDev
->AbsolutePointerProtocol
445 if (EFI_ERROR (Status
)) {
449 // Disable mouse on keyboard controller
451 KbcDisableAux (MouseAbsolutePointerDev
->IsaIo
);
454 // Cancel mouse data polling timer, close timer event
456 gBS
->SetTimer (MouseAbsolutePointerDev
->TimerEvent
, TimerCancel
, 0);
457 gBS
->CloseEvent (MouseAbsolutePointerDev
->TimerEvent
);
460 // Since there will be no timer handler for mouse input any more,
461 // exhaust input data just in case there is still mouse data left
463 Status
= EFI_SUCCESS
;
464 while (!EFI_ERROR (Status
)) {
465 Status
= In8042Data (MouseAbsolutePointerDev
->IsaIo
, &Data
);
468 gBS
->CloseEvent (MouseAbsolutePointerDev
->AbsolutePointerProtocol
.WaitForInput
);
469 FreeUnicodeStringTable (MouseAbsolutePointerDev
->ControllerNameTable
);
470 gBS
->FreePool (MouseAbsolutePointerDev
);
474 &gEfiDevicePathProtocolGuid
,
475 This
->DriverBindingHandle
,
481 &gEfiIsaIoProtocolGuid
,
482 This
->DriverBindingHandle
,
491 MouseAbsolutePointerReset (
492 IN EFI_ABSOLUTE_POINTER_PROTOCOL
*This
,
493 IN BOOLEAN ExtendedVerification
499 Reset the Mouse and do BAT test for it, if ExtendedVerification isTRUE and there is a mouse device connectted to system
503 This - Pointer of simple pointer Protocol.
504 ExtendedVerification - Whether configure mouse parameters. True: do; FALSE: skip.
508 EFI_SUCCESS - The command byte is written successfully.
509 EFI_DEVICE_ERROR - Errors occurred during reseting keyboard.
514 PS2_MOUSE_ABSOLUTE_POINTER_DEV
*MouseAbsolutePointerDev
;
516 BOOLEAN KeyboardEnable
;
519 MouseAbsolutePointerDev
= PS2_MOUSE_ABSOLUTE_POINTER_DEV_FROM_THIS (This
);
522 // Report reset progress code
524 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
526 EFI_PERIPHERAL_MOUSE
| EFI_P_PC_RESET
,
527 MouseAbsolutePointerDev
->DevicePath
530 KeyboardEnable
= FALSE
;
533 // Raise TPL to avoid keyboard operation impact
535 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
537 ZeroMem (&MouseAbsolutePointerDev
->State
, sizeof (EFI_ABSOLUTE_POINTER_STATE
));
538 MouseAbsolutePointerDev
->StateChanged
= FALSE
;
541 // Exhaust input data
543 Status
= EFI_SUCCESS
;
544 while (!EFI_ERROR (Status
)) {
545 Status
= In8042Data (MouseAbsolutePointerDev
->IsaIo
, &Data
);
548 CheckKbStatus (MouseAbsolutePointerDev
->IsaIo
, &KeyboardEnable
);
550 KbcDisableKb (MouseAbsolutePointerDev
->IsaIo
);
552 MouseAbsolutePointerDev
->IsaIo
->Io
.Read (MouseAbsolutePointerDev
->IsaIo
, EfiIsaIoWidthUint8
, KBC_CMD_STS_PORT
, 1, &Data
);
555 // if there's data block on KBC data port, read it out
557 if ((Data
& KBC_OUTB
) == KBC_OUTB
) {
558 MouseAbsolutePointerDev
->IsaIo
->Io
.Read (MouseAbsolutePointerDev
->IsaIo
, EfiIsaIoWidthUint8
, KBC_DATA_PORT
, 1, &Data
);
561 Status
= EFI_SUCCESS
;
563 // The PS2 mouse driver reset behavior is always successfully return no matter wheater or not there is mouse connected to system.
564 // This behavior is needed by performance speed. The following mouse command only succeessfully finish when mouse device is
565 // connected to system, so if PS2 mouse device not connect to system or user not ask for, we skip the mouse configuration and enabling
567 if (ExtendedVerification
&& CheckMouseAbsolutePointerConnect (MouseAbsolutePointerDev
)) {
569 // Send mouse reset command and set mouse default configure
571 Status
= PS2MouseReset (MouseAbsolutePointerDev
->IsaIo
);
572 if (EFI_ERROR (Status
)) {
573 Status
= EFI_DEVICE_ERROR
;
577 Status
= PS2MouseSetSampleRate (MouseAbsolutePointerDev
->IsaIo
, MouseAbsolutePointerDev
->SampleRate
);
578 if (EFI_ERROR (Status
)) {
579 Status
= EFI_DEVICE_ERROR
;
583 Status
= PS2MouseSetResolution (MouseAbsolutePointerDev
->IsaIo
, MouseAbsolutePointerDev
->Resolution
);
584 if (EFI_ERROR (Status
)) {
585 Status
= EFI_DEVICE_ERROR
;
589 Status
= PS2MouseSetScaling (MouseAbsolutePointerDev
->IsaIo
, MouseAbsolutePointerDev
->Scaling
);
590 if (EFI_ERROR (Status
)) {
591 Status
= EFI_DEVICE_ERROR
;
595 Status
= PS2MouseEnable (MouseAbsolutePointerDev
->IsaIo
);
596 if (EFI_ERROR (Status
)) {
597 Status
= EFI_DEVICE_ERROR
;
602 gBS
->RestoreTPL (OldTpl
);
604 if (KeyboardEnable
) {
605 KbcEnableKb (MouseAbsolutePointerDev
->IsaIo
);
612 CheckMouseAbsolutePointerConnect (
613 IN PS2_MOUSE_ABSOLUTE_POINTER_DEV
*MouseAbsolutePointerDev
619 Check whether there is Ps/2 mouse device in system
623 PS2_MOUSE_DEV - Mouse Private Data Structure
627 TRUE - Keyboard in System.
628 FALSE - Keyboard not in System.
634 Status
= PS2MouseEnable (MouseAbsolutePointerDev
->IsaIo
);
635 if (!EFI_ERROR (Status
)) {
644 MouseAbsolutePointerGetState (
645 IN EFI_ABSOLUTE_POINTER_PROTOCOL
*This
,
646 IN OUT EFI_ABSOLUTE_POINTER_STATE
*State
652 GC_TODO: Add function description
656 This - GC_TODO: add argument description
657 State - GC_TODO: add argument description
661 EFI_INVALID_PARAMETER - GC_TODO: Add description for return value
662 EFI_NOT_READY - GC_TODO: Add description for return value
663 EFI_SUCCESS - GC_TODO: Add description for return value
667 PS2_MOUSE_ABSOLUTE_POINTER_DEV
*MouseAbsolutePointerDev
;
670 MouseAbsolutePointerDev
= PS2_MOUSE_ABSOLUTE_POINTER_DEV_FROM_THIS (This
);
673 return EFI_INVALID_PARAMETER
;
676 if (!MouseAbsolutePointerDev
->StateChanged
) {
677 return EFI_NOT_READY
;
680 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
681 CopyMem (State
, &(MouseAbsolutePointerDev
->State
), sizeof (EFI_ABSOLUTE_POINTER_STATE
));
686 MouseAbsolutePointerDev
->State
.CurrentX
= 0;
687 MouseAbsolutePointerDev
->State
.CurrentY
= 0;
688 MouseAbsolutePointerDev
->State
.CurrentZ
= 0;
689 MouseAbsolutePointerDev
->State
.ActiveButtons
= 0x0;
690 MouseAbsolutePointerDev
->StateChanged
= FALSE
;
691 gBS
->RestoreTPL (OldTpl
);
698 MouseAbsolutePointerWaitForInput (
706 Event notification function for SIMPLE_POINTER.WaitForInput event
707 Signal the event if there is input from mouse
714 // GC_TODO: Event - add argument and description to function comment
715 // GC_TODO: Context - add argument and description to function comment
717 PS2_MOUSE_ABSOLUTE_POINTER_DEV
*MouseAbsolutePointerDev
;
719 MouseAbsolutePointerDev
= (PS2_MOUSE_ABSOLUTE_POINTER_DEV
*) Context
;
722 // Someone is waiting on the mouse event, if there's
723 // input from mouse, signal the event
725 if (MouseAbsolutePointerDev
->StateChanged
) {
726 gBS
->SignalEvent (Event
);
733 PollMouseAbsolutePointer(
741 Event notification function for TimerEvent event
742 If mouse device is connected to system, try to get the mouse packet data
746 Event - TimerEvent in PS2_MOUSE_DEV
747 Context - Pointer to PS2_MOUSE_DEV structure
755 PS2_MOUSE_ABSOLUTE_POINTER_DEV
*MouseAbsolutePointerDev
;
757 MouseAbsolutePointerDev
= (PS2_MOUSE_ABSOLUTE_POINTER_DEV
*) Context
;
760 // Polling mouse packet data
762 PS2MouseGetPacket (MouseAbsolutePointerDev
);
766 The user Entry Point for module Ps2MouseAbsolutePointer. The user code starts with this function.
768 @param[in] ImageHandle The firmware allocated handle for the EFI image.
769 @param[in] SystemTable A pointer to the EFI System Table.
771 @retval EFI_SUCCESS The entry point is executed successfully.
772 @retval other Some error occurs when executing this entry point.
777 InitializePs2MouseAbsolutePointer(
778 IN EFI_HANDLE ImageHandle
,
779 IN EFI_SYSTEM_TABLE
*SystemTable
785 // Install driver model protocol(s).
787 Status
= EfiLibInstallDriverBindingComponentName2 (
790 &gPS2MouseAbsolutePointerDriver
,
792 &gPs2MouseAbsolutePointerComponentName
,
793 &gPs2MouseAbsolutePointerComponentName2
795 ASSERT_EFI_ERROR (Status
);