2 A faked PS/2 Absolute Pointer 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
,
32 Test to see if this driver supports ControllerHandle. Any ControllerHandle
33 than contains a IsaIo protocol can be supported.
35 @param This Protocol instance pointer.
36 @param ControllerHandle Handle of device to test
37 @param RemainingDevicePath Optional parameter use to pick a specific child
40 @retval EFI_SUCCESS This driver supports this device
41 @retval EFI_ALREADY_STARTED This driver is already running on this device
42 @retval other This driver does not support this device
47 PS2MouseAbsolutePointerDriverSupported (
48 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
49 IN EFI_HANDLE Controller
,
50 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
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
,
112 Start this driver on ControllerHandle by opening a IsaIo protocol, creating
113 PS2_MOUSE_ABSOLUTE_POINTER_DEV device and install gEfiAbsolutePointerProtocolGuid
116 @param This Protocol instance pointer.
117 @param ControllerHandle Handle of device to bind driver to
118 @param RemainingDevicePath Optional parameter use to pick a specific child
121 @retval EFI_SUCCESS This driver is added to ControllerHandle
122 @retval EFI_ALREADY_STARTED This driver is already running on ControllerHandle
123 @retval other This driver does not support this device
128 PS2MouseAbsolutePointerDriverStart (
129 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
130 IN EFI_HANDLE Controller
,
131 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
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
= SampleRate20
;
209 MouseAbsolutePointerDev
->Resolution
= MouseResolution4
;
210 MouseAbsolutePointerDev
->Scaling
= Scaling1
;
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 FreePool (MouseAbsolutePointerDev
);
371 &gEfiDevicePathProtocolGuid
,
372 This
->DriverBindingHandle
,
378 &gEfiIsaIoProtocolGuid
,
379 This
->DriverBindingHandle
,
383 gBS
->RestoreTPL (OldTpl
);
389 Stop this driver on ControllerHandle. Support stoping any child handles
390 created by this driver.
392 @param This Protocol instance pointer.
393 @param ControllerHandle Handle of device to stop driver on
394 @param NumberOfChildren Number of Handles in ChildHandleBuffer. If number of
395 children is zero stop the entire bus driver.
396 @param ChildHandleBuffer List of Child Handles to Stop.
398 @retval EFI_SUCCESS This driver is removed ControllerHandle
399 @retval other This driver was not removed from this device
404 PS2MouseAbsolutePointerDriverStop (
405 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
406 IN EFI_HANDLE Controller
,
407 IN UINTN NumberOfChildren
,
408 IN EFI_HANDLE
*ChildHandleBuffer
412 EFI_ABSOLUTE_POINTER_PROTOCOL
*AbsolutePointerProtocol
;
413 PS2_MOUSE_ABSOLUTE_POINTER_DEV
*MouseAbsolutePointerDev
;
416 Status
= gBS
->OpenProtocol (
418 &gEfiAbsolutePointerProtocolGuid
,
419 (VOID
**) &AbsolutePointerProtocol
,
420 This
->DriverBindingHandle
,
422 EFI_OPEN_PROTOCOL_GET_PROTOCOL
424 if (EFI_ERROR (Status
)) {
428 MouseAbsolutePointerDev
= PS2_MOUSE_ABSOLUTE_POINTER_DEV_FROM_THIS (AbsolutePointerProtocol
);
431 // Report that the keyboard is being disabled
433 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
435 EFI_PERIPHERAL_MOUSE
| EFI_P_PC_DISABLE
,
436 MouseAbsolutePointerDev
->DevicePath
439 Status
= gBS
->UninstallProtocolInterface (
441 &gEfiAbsolutePointerProtocolGuid
,
442 &MouseAbsolutePointerDev
->AbsolutePointerProtocol
444 if (EFI_ERROR (Status
)) {
448 // Disable mouse on keyboard controller
450 KbcDisableAux (MouseAbsolutePointerDev
->IsaIo
);
453 // Cancel mouse data polling timer, close timer event
455 gBS
->SetTimer (MouseAbsolutePointerDev
->TimerEvent
, TimerCancel
, 0);
456 gBS
->CloseEvent (MouseAbsolutePointerDev
->TimerEvent
);
459 // Since there will be no timer handler for mouse input any more,
460 // exhaust input data just in case there is still mouse data left
462 Status
= EFI_SUCCESS
;
463 while (!EFI_ERROR (Status
)) {
464 Status
= In8042Data (MouseAbsolutePointerDev
->IsaIo
, &Data
);
467 gBS
->CloseEvent (MouseAbsolutePointerDev
->AbsolutePointerProtocol
.WaitForInput
);
468 FreeUnicodeStringTable (MouseAbsolutePointerDev
->ControllerNameTable
);
469 FreePool (MouseAbsolutePointerDev
);
473 &gEfiDevicePathProtocolGuid
,
474 This
->DriverBindingHandle
,
480 &gEfiIsaIoProtocolGuid
,
481 This
->DriverBindingHandle
,
489 Reset the Mouse and do BAT test for it, if ExtendedVerification isTRUE and there is a mouse device connectted to system.
491 @param This - Pointer of simple pointer Protocol.
492 @param ExtendedVerification - Whether configure mouse parameters. True: do; FALSE: skip.
495 @retval EFI_SUCCESS - The command byte is written successfully.
496 @retval EFI_DEVICE_ERROR - Errors occurred during reseting keyboard.
501 MouseAbsolutePointerReset (
502 IN EFI_ABSOLUTE_POINTER_PROTOCOL
*This
,
503 IN BOOLEAN ExtendedVerification
507 PS2_MOUSE_ABSOLUTE_POINTER_DEV
*MouseAbsolutePointerDev
;
509 BOOLEAN KeyboardEnable
;
512 MouseAbsolutePointerDev
= PS2_MOUSE_ABSOLUTE_POINTER_DEV_FROM_THIS (This
);
515 // Report reset progress code
517 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
519 EFI_PERIPHERAL_MOUSE
| EFI_P_PC_RESET
,
520 MouseAbsolutePointerDev
->DevicePath
523 KeyboardEnable
= FALSE
;
526 // Raise TPL to avoid keyboard operation impact
528 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
530 ZeroMem (&MouseAbsolutePointerDev
->State
, sizeof (EFI_ABSOLUTE_POINTER_STATE
));
531 MouseAbsolutePointerDev
->StateChanged
= FALSE
;
534 // Exhaust input data
536 Status
= EFI_SUCCESS
;
537 while (!EFI_ERROR (Status
)) {
538 Status
= In8042Data (MouseAbsolutePointerDev
->IsaIo
, &Data
);
541 CheckKbStatus (MouseAbsolutePointerDev
->IsaIo
, &KeyboardEnable
);
543 KbcDisableKb (MouseAbsolutePointerDev
->IsaIo
);
545 MouseAbsolutePointerDev
->IsaIo
->Io
.Read (MouseAbsolutePointerDev
->IsaIo
, EfiIsaIoWidthUint8
, KBC_CMD_STS_PORT
, 1, &Data
);
548 // if there's data block on KBC data port, read it out
550 if ((Data
& KBC_OUTB
) == KBC_OUTB
) {
551 MouseAbsolutePointerDev
->IsaIo
->Io
.Read (MouseAbsolutePointerDev
->IsaIo
, EfiIsaIoWidthUint8
, KBC_DATA_PORT
, 1, &Data
);
554 Status
= EFI_SUCCESS
;
556 // The PS2 mouse driver reset behavior is always successfully return no matter wheater or not there is mouse connected to system.
557 // This behavior is needed by performance speed. The following mouse command only succeessfully finish when mouse device is
558 // connected to system, so if PS2 mouse device not connect to system or user not ask for, we skip the mouse configuration and enabling
560 if (ExtendedVerification
&& CheckMouseAbsolutePointerConnect (MouseAbsolutePointerDev
)) {
562 // Send mouse reset command and set mouse default configure
564 Status
= PS2MouseReset (MouseAbsolutePointerDev
->IsaIo
);
565 if (EFI_ERROR (Status
)) {
566 Status
= EFI_DEVICE_ERROR
;
570 Status
= PS2MouseSetSampleRate (MouseAbsolutePointerDev
->IsaIo
, MouseAbsolutePointerDev
->SampleRate
);
571 if (EFI_ERROR (Status
)) {
572 Status
= EFI_DEVICE_ERROR
;
576 Status
= PS2MouseSetResolution (MouseAbsolutePointerDev
->IsaIo
, MouseAbsolutePointerDev
->Resolution
);
577 if (EFI_ERROR (Status
)) {
578 Status
= EFI_DEVICE_ERROR
;
582 Status
= PS2MouseSetScaling (MouseAbsolutePointerDev
->IsaIo
, MouseAbsolutePointerDev
->Scaling
);
583 if (EFI_ERROR (Status
)) {
584 Status
= EFI_DEVICE_ERROR
;
588 Status
= PS2MouseEnable (MouseAbsolutePointerDev
->IsaIo
);
589 if (EFI_ERROR (Status
)) {
590 Status
= EFI_DEVICE_ERROR
;
595 gBS
->RestoreTPL (OldTpl
);
597 if (KeyboardEnable
) {
598 KbcEnableKb (MouseAbsolutePointerDev
->IsaIo
);
605 Check whether there is Ps/2 mouse device in system
607 @param MouseAbsolutePointerDev - Absolute Pointer Device Private Data Structure
609 @retval TRUE - Keyboard in System.
610 @retval FALSE - Keyboard not in System.
614 CheckMouseAbsolutePointerConnect (
615 IN PS2_MOUSE_ABSOLUTE_POINTER_DEV
*MouseAbsolutePointerDev
621 Status
= PS2MouseEnable (MouseAbsolutePointerDev
->IsaIo
);
622 if (!EFI_ERROR (Status
)) {
630 Get and Clear mouse status.
632 @param This - Pointer of simple pointer Protocol.
633 @param State - Output buffer holding status.
635 @retval EFI_INVALID_PARAMETER Output buffer is invalid.
636 @retval EFI_NOT_READY Mouse is not changed status yet.
637 @retval EFI_SUCCESS Mouse status is changed and get successful.
641 MouseAbsolutePointerGetState (
642 IN EFI_ABSOLUTE_POINTER_PROTOCOL
*This
,
643 IN OUT EFI_ABSOLUTE_POINTER_STATE
*State
646 PS2_MOUSE_ABSOLUTE_POINTER_DEV
*MouseAbsolutePointerDev
;
649 MouseAbsolutePointerDev
= PS2_MOUSE_ABSOLUTE_POINTER_DEV_FROM_THIS (This
);
652 return EFI_INVALID_PARAMETER
;
655 if (!MouseAbsolutePointerDev
->StateChanged
) {
656 return EFI_NOT_READY
;
659 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
660 CopyMem (State
, &(MouseAbsolutePointerDev
->State
), sizeof (EFI_ABSOLUTE_POINTER_STATE
));
665 MouseAbsolutePointerDev
->State
.CurrentX
= 0;
666 MouseAbsolutePointerDev
->State
.CurrentY
= 0;
667 MouseAbsolutePointerDev
->State
.CurrentZ
= 0;
668 MouseAbsolutePointerDev
->State
.ActiveButtons
= 0x0;
669 MouseAbsolutePointerDev
->StateChanged
= FALSE
;
670 gBS
->RestoreTPL (OldTpl
);
677 Event notification function for SIMPLE_POINTER.WaitForInput event.
678 Signal the event if there is input from mouse.
680 @param Event event object
681 @param Context event context
686 MouseAbsolutePointerWaitForInput (
691 PS2_MOUSE_ABSOLUTE_POINTER_DEV
*MouseAbsolutePointerDev
;
693 MouseAbsolutePointerDev
= (PS2_MOUSE_ABSOLUTE_POINTER_DEV
*) Context
;
696 // Someone is waiting on the mouse event, if there's
697 // input from mouse, signal the event
699 if (MouseAbsolutePointerDev
->StateChanged
) {
700 gBS
->SignalEvent (Event
);
706 Event notification function for TimerEvent event.
707 If mouse device is connected to system, try to get the mouse packet data.
709 @param Event - TimerEvent in PS2_MOUSE_DEV
710 @param Context - Pointer to PS2_MOUSE_DEV structure
715 PollMouseAbsolutePointer(
721 PS2_MOUSE_ABSOLUTE_POINTER_DEV
*MouseAbsolutePointerDev
;
723 MouseAbsolutePointerDev
= (PS2_MOUSE_ABSOLUTE_POINTER_DEV
*) Context
;
726 // Polling mouse packet data
728 PS2MouseGetPacket (MouseAbsolutePointerDev
);
732 The user Entry Point for module Ps2MouseAbsolutePointer. The user code starts with this function.
734 @param[in] ImageHandle The firmware allocated handle for the EFI image.
735 @param[in] SystemTable A pointer to the EFI System Table.
737 @retval EFI_SUCCESS The entry point is executed successfully.
738 @retval other Some error occurs when executing this entry point.
743 InitializePs2MouseAbsolutePointer(
744 IN EFI_HANDLE ImageHandle
,
745 IN EFI_SYSTEM_TABLE
*SystemTable
751 // Install driver model protocol(s).
753 Status
= EfiLibInstallDriverBindingComponentName2 (
756 &gPS2MouseAbsolutePointerDriver
,
758 &gPs2MouseAbsolutePointerComponentName
,
759 &gPs2MouseAbsolutePointerComponentName2
761 ASSERT_EFI_ERROR (Status
);