2 PS/2 Mouse driver. Routines that interacts with callers,
3 conforming to EFI driver model.
5 Copyright (c) 2006 - 2011, Intel Corporation. All rights reserved.<BR>
6 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.
20 /// DriverBinding Protocol Instance
22 EFI_DRIVER_BINDING_PROTOCOL gPS2MouseDriver
= {
23 PS2MouseDriverSupported
,
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 PS2MouseDriverSupported (
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 Keyboard 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 PS2MouseDriverStart (
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_DEV
*MouseDev
;
140 EFI_STATUS_CODE_VALUE StatusCode
;
141 EFI_DEVICE_PATH_PROTOCOL
*ParentDevicePath
;
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 MouseDev
= AllocateZeroPool (sizeof (PS2_MOUSE_DEV
));
199 if (MouseDev
== NULL
) {
200 Status
= EFI_OUT_OF_RESOURCES
;
204 // Setup the device instance
206 MouseDev
->Signature
= PS2_MOUSE_DEV_SIGNATURE
;
207 MouseDev
->Handle
= Controller
;
208 MouseDev
->SampleRate
= SampleRate20
;
209 MouseDev
->Resolution
= MouseResolution4
;
210 MouseDev
->Scaling
= Scaling1
;
211 MouseDev
->DataPackageSize
= 3;
212 MouseDev
->IsaIo
= IsaIo
;
213 MouseDev
->DevicePath
= ParentDevicePath
;
216 // Resolution = 4 counts/mm
218 MouseDev
->Mode
.ResolutionX
= 4;
219 MouseDev
->Mode
.ResolutionY
= 4;
220 MouseDev
->Mode
.LeftButton
= TRUE
;
221 MouseDev
->Mode
.RightButton
= TRUE
;
223 MouseDev
->SimplePointerProtocol
.Reset
= MouseReset
;
224 MouseDev
->SimplePointerProtocol
.GetState
= MouseGetState
;
225 MouseDev
->SimplePointerProtocol
.Mode
= &(MouseDev
->Mode
);
228 // Initialize keyboard controller if necessary
230 IsaIo
->Io
.Read (IsaIo
, EfiIsaIoWidthUint8
, KBC_CMD_STS_PORT
, 1, &Data
);
232 // Fix for random hangs in System waiting for the Key if no KBC is present in BIOS.
234 if ((Data
& (KBC_PARE
| KBC_TIM
)) == (KBC_PARE
| KBC_TIM
)) {
236 // If nobody decodes KBC I/O port, it will read back as 0xFF.
237 // Check the Time-Out and Parity bit to see if it has an active KBC in system
239 Status
= EFI_DEVICE_ERROR
;
240 StatusCode
= EFI_PERIPHERAL_MOUSE
| EFI_P_EC_NOT_DETECTED
;
244 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
246 EFI_PERIPHERAL_MOUSE
| EFI_P_MOUSE_PC_SELF_TEST
,
250 if ((Data
& KBC_SYSF
) != KBC_SYSF
) {
251 Status
= KbcSelfTest (IsaIo
);
252 if (EFI_ERROR (Status
)) {
253 StatusCode
= EFI_PERIPHERAL_MOUSE
| EFI_P_EC_CONTROLLER_ERROR
;
258 KbcEnableAux (IsaIo
);
260 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
262 EFI_PERIPHERAL_MOUSE
| EFI_P_PC_PRESENCE_DETECT
,
269 Status
= MouseDev
->SimplePointerProtocol
.Reset (
270 &MouseDev
->SimplePointerProtocol
,
271 FeaturePcdGet (PcdPs2MouseExtendedVerification
)
273 if (EFI_ERROR (Status
)) {
275 // mouse not connected
277 Status
= EFI_SUCCESS
;
278 StatusCode
= EFI_PERIPHERAL_MOUSE
| EFI_P_EC_NOT_DETECTED
;
282 // Setup the WaitForKey event
284 Status
= gBS
->CreateEvent (
289 &((MouseDev
->SimplePointerProtocol
).WaitForInput
)
291 if (EFI_ERROR (Status
)) {
292 Status
= EFI_OUT_OF_RESOURCES
;
296 // Setup a periodic timer, used to poll mouse state
298 Status
= gBS
->CreateEvent (
299 EVT_TIMER
| EVT_NOTIFY_SIGNAL
,
303 &MouseDev
->TimerEvent
305 if (EFI_ERROR (Status
)) {
306 Status
= EFI_OUT_OF_RESOURCES
;
310 // Start timer to poll mouse (100 samples per second)
312 Status
= gBS
->SetTimer (MouseDev
->TimerEvent
, TimerPeriodic
, 100000);
313 if (EFI_ERROR (Status
)) {
314 Status
= EFI_OUT_OF_RESOURCES
;
318 MouseDev
->ControllerNameTable
= NULL
;
321 gPs2MouseComponentName
.SupportedLanguages
,
322 &MouseDev
->ControllerNameTable
,
323 L
"PS/2 Mouse Device",
328 gPs2MouseComponentName2
.SupportedLanguages
,
329 &MouseDev
->ControllerNameTable
,
330 L
"PS/2 Mouse Device",
336 // Install protocol interfaces for the mouse device.
338 Status
= gBS
->InstallMultipleProtocolInterfaces (
340 &gEfiSimplePointerProtocolGuid
,
341 &MouseDev
->SimplePointerProtocol
,
344 if (EFI_ERROR (Status
)) {
348 gBS
->RestoreTPL (OldTpl
);
354 if (Status
!= EFI_DEVICE_ERROR
) {
355 KbcDisableAux (IsaIo
);
358 if (StatusCode
!= 0) {
359 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
360 EFI_ERROR_CODE
| EFI_ERROR_MINOR
,
366 if ((MouseDev
!= NULL
) && (MouseDev
->SimplePointerProtocol
.WaitForInput
!= NULL
)) {
367 gBS
->CloseEvent (MouseDev
->SimplePointerProtocol
.WaitForInput
);
370 if ((MouseDev
!= NULL
) && (MouseDev
->TimerEvent
!= NULL
)) {
371 gBS
->CloseEvent (MouseDev
->TimerEvent
);
374 if ((MouseDev
!= NULL
) && (MouseDev
->ControllerNameTable
!= NULL
)) {
375 FreeUnicodeStringTable (MouseDev
->ControllerNameTable
);
378 if (Status
!= EFI_DEVICE_ERROR
) {
380 // Since there will be no timer handler for mouse input any more,
381 // exhaust input data just in case there is still mouse data left
383 EmptyStatus
= EFI_SUCCESS
;
384 while (!EFI_ERROR (EmptyStatus
)) {
385 EmptyStatus
= In8042Data (IsaIo
, &Data
);
389 if (MouseDev
!= NULL
) {
395 &gEfiDevicePathProtocolGuid
,
396 This
->DriverBindingHandle
,
402 &gEfiIsaIoProtocolGuid
,
403 This
->DriverBindingHandle
,
407 gBS
->RestoreTPL (OldTpl
);
413 Stop this driver on ControllerHandle. Support stoping any child handles
414 created by this driver.
416 @param This Protocol instance pointer.
417 @param ControllerHandle Handle of device to stop driver on
418 @param NumberOfChildren Number of Handles in ChildHandleBuffer. If number of
419 children is zero stop the entire bus driver.
420 @param ChildHandleBuffer List of Child Handles to Stop.
422 @retval EFI_SUCCESS This driver is removed ControllerHandle
423 @retval other This driver was not removed from this device
429 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
430 IN EFI_HANDLE Controller
,
431 IN UINTN NumberOfChildren
,
432 IN EFI_HANDLE
*ChildHandleBuffer
436 EFI_SIMPLE_POINTER_PROTOCOL
*SimplePointerProtocol
;
437 PS2_MOUSE_DEV
*MouseDev
;
440 Status
= gBS
->OpenProtocol (
442 &gEfiSimplePointerProtocolGuid
,
443 (VOID
**) &SimplePointerProtocol
,
444 This
->DriverBindingHandle
,
446 EFI_OPEN_PROTOCOL_GET_PROTOCOL
448 if (EFI_ERROR (Status
)) {
452 MouseDev
= PS2_MOUSE_DEV_FROM_THIS (SimplePointerProtocol
);
455 // Report that the keyboard is being disabled
457 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
459 EFI_PERIPHERAL_MOUSE
| EFI_P_PC_DISABLE
,
463 Status
= gBS
->UninstallProtocolInterface (
465 &gEfiSimplePointerProtocolGuid
,
466 &MouseDev
->SimplePointerProtocol
468 if (EFI_ERROR (Status
)) {
473 // Cancel mouse data polling timer, close timer event
475 gBS
->SetTimer (MouseDev
->TimerEvent
, TimerCancel
, 0);
476 gBS
->CloseEvent (MouseDev
->TimerEvent
);
479 // Since there will be no timer handler for mouse input any more,
480 // exhaust input data just in case there is still mouse data left
482 Status
= EFI_SUCCESS
;
483 while (!EFI_ERROR (Status
)) {
484 Status
= In8042Data (MouseDev
->IsaIo
, &Data
);
487 gBS
->CloseEvent (MouseDev
->SimplePointerProtocol
.WaitForInput
);
488 FreeUnicodeStringTable (MouseDev
->ControllerNameTable
);
493 &gEfiDevicePathProtocolGuid
,
494 This
->DriverBindingHandle
,
500 &gEfiIsaIoProtocolGuid
,
501 This
->DriverBindingHandle
,
509 Reset the Mouse and do BAT test for it, if ExtendedVerification isTRUE and there is a mouse device connectted to system
511 @param This - Pointer of simple pointer Protocol.
512 @param ExtendedVerification - Whether configure mouse parameters. True: do; FALSE: skip.
515 @retval EFI_SUCCESS - The command byte is written successfully.
516 @retval EFI_DEVICE_ERROR - Errors occurred during reseting keyboard.
522 IN EFI_SIMPLE_POINTER_PROTOCOL
*This
,
523 IN BOOLEAN ExtendedVerification
527 PS2_MOUSE_DEV
*MouseDev
;
529 BOOLEAN KeyboardEnable
;
532 MouseDev
= PS2_MOUSE_DEV_FROM_THIS (This
);
535 // Report reset progress code
537 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
539 EFI_PERIPHERAL_MOUSE
| EFI_P_PC_RESET
,
543 KeyboardEnable
= FALSE
;
546 // Raise TPL to avoid keyboard operation impact
548 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
550 ZeroMem (&MouseDev
->State
, sizeof (EFI_SIMPLE_POINTER_STATE
));
551 MouseDev
->StateChanged
= FALSE
;
554 // Exhaust input data
556 Status
= EFI_SUCCESS
;
557 while (!EFI_ERROR (Status
)) {
558 Status
= In8042Data (MouseDev
->IsaIo
, &Data
);
561 CheckKbStatus (MouseDev
->IsaIo
, &KeyboardEnable
);
563 KbcDisableKb (MouseDev
->IsaIo
);
565 MouseDev
->IsaIo
->Io
.Read (MouseDev
->IsaIo
, EfiIsaIoWidthUint8
, KBC_CMD_STS_PORT
, 1, &Data
);
568 // if there's data block on KBC data port, read it out
570 if ((Data
& KBC_OUTB
) == KBC_OUTB
) {
571 MouseDev
->IsaIo
->Io
.Read (MouseDev
->IsaIo
, EfiIsaIoWidthUint8
, KBC_DATA_PORT
, 1, &Data
);
574 Status
= EFI_SUCCESS
;
576 // The PS2 mouse driver reset behavior is always successfully return no matter wheater or not there is mouse connected to system.
577 // This behavior is needed by performance speed. The following mouse command only succeessfully finish when mouse device is
578 // connected to system, so if PS2 mouse device not connect to system or user not ask for, we skip the mouse configuration and enabling
580 if (ExtendedVerification
&& CheckMouseConnect (MouseDev
)) {
582 // Send mouse reset command and set mouse default configure
584 Status
= PS2MouseReset (MouseDev
->IsaIo
);
585 if (EFI_ERROR (Status
)) {
586 Status
= EFI_DEVICE_ERROR
;
590 Status
= PS2MouseSetSampleRate (MouseDev
->IsaIo
, MouseDev
->SampleRate
);
591 if (EFI_ERROR (Status
)) {
592 Status
= EFI_DEVICE_ERROR
;
596 Status
= PS2MouseSetResolution (MouseDev
->IsaIo
, MouseDev
->Resolution
);
597 if (EFI_ERROR (Status
)) {
598 Status
= EFI_DEVICE_ERROR
;
602 Status
= PS2MouseSetScaling (MouseDev
->IsaIo
, MouseDev
->Scaling
);
603 if (EFI_ERROR (Status
)) {
604 Status
= EFI_DEVICE_ERROR
;
608 Status
= PS2MouseEnable (MouseDev
->IsaIo
);
609 if (EFI_ERROR (Status
)) {
610 Status
= EFI_DEVICE_ERROR
;
615 gBS
->RestoreTPL (OldTpl
);
617 if (KeyboardEnable
) {
618 KbcEnableKb (MouseDev
->IsaIo
);
625 Check whether there is Ps/2 mouse device in system
627 @param MouseDev - Mouse Private Data Structure
629 @retval TRUE - Keyboard in System.
630 @retval FALSE - Keyboard not in System.
635 IN PS2_MOUSE_DEV
*MouseDev
641 Status
= PS2MouseEnable (MouseDev
->IsaIo
);
642 if (!EFI_ERROR (Status
)) {
650 Get and Clear mouse status.
652 @param This - Pointer of simple pointer Protocol.
653 @param State - Output buffer holding status.
655 @retval EFI_INVALID_PARAMETER Output buffer is invalid.
656 @retval EFI_NOT_READY Mouse is not changed status yet.
657 @retval EFI_SUCCESS Mouse status is changed and get successful.
662 IN EFI_SIMPLE_POINTER_PROTOCOL
*This
,
663 IN OUT EFI_SIMPLE_POINTER_STATE
*State
666 PS2_MOUSE_DEV
*MouseDev
;
669 MouseDev
= PS2_MOUSE_DEV_FROM_THIS (This
);
672 return EFI_INVALID_PARAMETER
;
675 if (!MouseDev
->StateChanged
) {
676 return EFI_NOT_READY
;
679 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
680 CopyMem (State
, &(MouseDev
->State
), sizeof (EFI_SIMPLE_POINTER_STATE
));
685 MouseDev
->State
.RelativeMovementX
= 0;
686 MouseDev
->State
.RelativeMovementY
= 0;
687 MouseDev
->State
.RelativeMovementZ
= 0;
688 MouseDev
->StateChanged
= FALSE
;
689 gBS
->RestoreTPL (OldTpl
);
696 Event notification function for SIMPLE_POINTER.WaitForInput event.
697 Signal the event if there is input from mouse.
699 @param Event event object
700 @param Context event context
710 PS2_MOUSE_DEV
*MouseDev
;
712 MouseDev
= (PS2_MOUSE_DEV
*) Context
;
715 // Someone is waiting on the mouse event, if there's
716 // input from mouse, signal the event
718 if (MouseDev
->StateChanged
) {
719 gBS
->SignalEvent (Event
);
725 Event notification function for TimerEvent event.
726 If mouse device is connected to system, try to get the mouse packet data.
728 @param Event - TimerEvent in PS2_MOUSE_DEV
729 @param Context - Pointer to PS2_MOUSE_DEV structure
740 PS2_MOUSE_DEV
*MouseDev
;
742 MouseDev
= (PS2_MOUSE_DEV
*) Context
;
745 // Polling mouse packet data
747 PS2MouseGetPacket (MouseDev
);
751 The user Entry Point for module Ps2Mouse. The user code starts with this function.
753 @param[in] ImageHandle The firmware allocated handle for the EFI image.
754 @param[in] SystemTable A pointer to the EFI System Table.
756 @retval EFI_SUCCESS The entry point is executed successfully.
757 @retval other Some error occurs when executing this entry point.
763 IN EFI_HANDLE ImageHandle
,
764 IN EFI_SYSTEM_TABLE
*SystemTable
770 // Install driver model protocol(s).
772 Status
= EfiLibInstallDriverBindingComponentName2 (
777 &gPs2MouseComponentName
,
778 &gPs2MouseComponentName2
780 ASSERT_EFI_ERROR (Status
);