3 PS/2 Keyboard driver. Routines that interacts with callers,
4 conforming to EFI driver model
6 Copyright (c) 2006 - 2012, Intel Corporation. All rights reserved.<BR>
7 This program and the accompanying materials
8 are licensed and made available under the terms and conditions of the BSD License
9 which accompanies this distribution. The full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
17 #include "Ps2Keyboard.h"
20 // Function prototypes
23 Test controller is a keyboard Controller.
25 @param This Pointer of EFI_DRIVER_BINDING_PROTOCOL
26 @param Controller driver's controller
27 @param RemainingDevicePath children device path
29 @retval EFI_UNSUPPORTED controller is not floppy disk
30 @retval EFI_SUCCESS controller is floppy disk
34 KbdControllerDriverSupported (
35 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
36 IN EFI_HANDLE Controller
,
37 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
41 Create KEYBOARD_CONSOLE_IN_DEV instance on controller.
43 @param This Pointer of EFI_DRIVER_BINDING_PROTOCOL
44 @param Controller driver controller handle
45 @param RemainingDevicePath Children's device path
47 @retval whether success to create floppy control instance.
51 KbdControllerDriverStart (
52 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
53 IN EFI_HANDLE Controller
,
54 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
58 Stop this driver on ControllerHandle. Support stoping any child handles
59 created by this driver.
61 @param This Protocol instance pointer.
62 @param ControllerHandle Handle of device to stop driver on
63 @param NumberOfChildren Number of Handles in ChildHandleBuffer. If number of
64 children is zero stop the entire bus driver.
65 @param ChildHandleBuffer List of Child Handles to Stop.
67 @retval EFI_SUCCESS This driver is removed ControllerHandle
68 @retval other This driver was not removed from this device
73 KbdControllerDriverStop (
74 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
75 IN EFI_HANDLE Controller
,
76 IN UINTN NumberOfChildren
,
77 IN EFI_HANDLE
*ChildHandleBuffer
81 Free the waiting key notify list.
83 @param ListHead Pointer to list head
85 @retval EFI_INVALID_PARAMETER ListHead is NULL
86 @retval EFI_SUCCESS Sucess to free NotifyList
90 IN OUT LIST_ENTRY
*ListHead
94 // DriverBinding Protocol Instance
96 EFI_DRIVER_BINDING_PROTOCOL gKeyboardControllerDriver
= {
97 KbdControllerDriverSupported
,
98 KbdControllerDriverStart
,
99 KbdControllerDriverStop
,
106 Test controller is a keyboard Controller.
108 @param This Pointer of EFI_DRIVER_BINDING_PROTOCOL
109 @param Controller driver's controller
110 @param RemainingDevicePath children device path
112 @retval EFI_UNSUPPORTED controller is not floppy disk
113 @retval EFI_SUCCESS controller is floppy disk
117 KbdControllerDriverSupported (
118 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
119 IN EFI_HANDLE Controller
,
120 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
124 EFI_ISA_IO_PROTOCOL
*IsaIo
;
127 // Open the IO Abstraction(s) needed to perform the supported test
129 Status
= gBS
->OpenProtocol (
131 &gEfiIsaIoProtocolGuid
,
133 This
->DriverBindingHandle
,
135 EFI_OPEN_PROTOCOL_BY_DRIVER
137 if (EFI_ERROR (Status
)) {
141 // Use the ISA I/O Protocol to see if Controller is the Keyboard controller
143 if (IsaIo
->ResourceList
->Device
.HID
!= EISA_PNP_ID (0x303) || IsaIo
->ResourceList
->Device
.UID
!= 0) {
144 Status
= EFI_UNSUPPORTED
;
147 // Close the I/O Abstraction(s) used to perform the supported test
151 &gEfiIsaIoProtocolGuid
,
152 This
->DriverBindingHandle
,
160 Create KEYBOARD_CONSOLE_IN_DEV instance on controller.
162 @param This Pointer of EFI_DRIVER_BINDING_PROTOCOL
163 @param Controller driver controller handle
164 @param RemainingDevicePath Children's device path
166 @retval whether success to create floppy control instance.
170 KbdControllerDriverStart (
171 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
172 IN EFI_HANDLE Controller
,
173 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
178 EFI_ISA_IO_PROTOCOL
*IsaIo
;
179 KEYBOARD_CONSOLE_IN_DEV
*ConsoleIn
;
181 EFI_STATUS_CODE_VALUE StatusCode
;
182 EFI_DEVICE_PATH_PROTOCOL
*ParentDevicePath
;
186 Status
= gBS
->OpenProtocol (
188 &gEfiDevicePathProtocolGuid
,
189 (VOID
**) &ParentDevicePath
,
190 This
->DriverBindingHandle
,
192 EFI_OPEN_PROTOCOL_BY_DRIVER
194 if (EFI_ERROR (Status
)) {
198 // Report that the keyboard is being enabled
200 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
202 EFI_PERIPHERAL_KEYBOARD
| EFI_P_PC_ENABLE
,
207 // Get the ISA I/O Protocol on Controller's handle
209 Status
= gBS
->OpenProtocol (
211 &gEfiIsaIoProtocolGuid
,
213 This
->DriverBindingHandle
,
215 EFI_OPEN_PROTOCOL_BY_DRIVER
217 if (EFI_ERROR (Status
)) {
220 &gEfiDevicePathProtocolGuid
,
221 This
->DriverBindingHandle
,
224 return EFI_INVALID_PARAMETER
;
227 // Allocate private data
229 ConsoleIn
= AllocateZeroPool (sizeof (KEYBOARD_CONSOLE_IN_DEV
));
230 if (ConsoleIn
== NULL
) {
231 Status
= EFI_OUT_OF_RESOURCES
;
232 StatusCode
= EFI_PERIPHERAL_KEYBOARD
| EFI_P_EC_CONTROLLER_ERROR
;
236 // Setup the device instance
238 ConsoleIn
->Signature
= KEYBOARD_CONSOLE_IN_DEV_SIGNATURE
;
239 ConsoleIn
->Handle
= Controller
;
240 (ConsoleIn
->ConIn
).Reset
= KeyboardEfiReset
;
241 (ConsoleIn
->ConIn
).ReadKeyStroke
= KeyboardReadKeyStroke
;
242 ConsoleIn
->DataRegisterAddress
= KEYBOARD_8042_DATA_REGISTER
;
243 ConsoleIn
->StatusRegisterAddress
= KEYBOARD_8042_STATUS_REGISTER
;
244 ConsoleIn
->CommandRegisterAddress
= KEYBOARD_8042_COMMAND_REGISTER
;
245 ConsoleIn
->IsaIo
= IsaIo
;
246 ConsoleIn
->DevicePath
= ParentDevicePath
;
248 ConsoleIn
->ConInEx
.Reset
= KeyboardEfiResetEx
;
249 ConsoleIn
->ConInEx
.ReadKeyStrokeEx
= KeyboardReadKeyStrokeEx
;
250 ConsoleIn
->ConInEx
.SetState
= KeyboardSetState
;
251 ConsoleIn
->ConInEx
.RegisterKeyNotify
= KeyboardRegisterKeyNotify
;
252 ConsoleIn
->ConInEx
.UnregisterKeyNotify
= KeyboardUnregisterKeyNotify
;
254 InitializeListHead (&ConsoleIn
->NotifyList
);
257 // Fix for random hangs in System waiting for the Key if no KBC is present in BIOS.
258 // When KBC decode (IO port 0x60/0x64 decode) is not enabled,
259 // KeyboardRead will read back as 0xFF and return status is EFI_SUCCESS.
260 // So instead we read status register to detect after read if KBC decode is enabled.
264 // Return code is ignored on purpose.
266 if (!PcdGetBool (PcdFastPS2Detection
)) {
267 KeyboardRead (ConsoleIn
, &Data
);
268 if ((KeyReadStatusRegister (ConsoleIn
) & (KBC_PARE
| KBC_TIM
)) == (KBC_PARE
| KBC_TIM
)) {
270 // If nobody decodes KBC I/O port, it will read back as 0xFF.
271 // Check the Time-Out and Parity bit to see if it has an active KBC in system
273 Status
= EFI_DEVICE_ERROR
;
274 StatusCode
= EFI_PERIPHERAL_KEYBOARD
| EFI_P_EC_NOT_DETECTED
;
280 // Setup the WaitForKey event
282 Status
= gBS
->CreateEvent (
287 &((ConsoleIn
->ConIn
).WaitForKey
)
289 if (EFI_ERROR (Status
)) {
290 Status
= EFI_OUT_OF_RESOURCES
;
291 StatusCode
= EFI_PERIPHERAL_KEYBOARD
| EFI_P_EC_CONTROLLER_ERROR
;
295 // Setup the WaitForKeyEx event
297 Status
= gBS
->CreateEvent (
300 KeyboardWaitForKeyEx
,
302 &(ConsoleIn
->ConInEx
.WaitForKeyEx
)
304 if (EFI_ERROR (Status
)) {
305 Status
= EFI_OUT_OF_RESOURCES
;
306 StatusCode
= EFI_PERIPHERAL_KEYBOARD
| EFI_P_EC_CONTROLLER_ERROR
;
309 // Setup a periodic timer, used for reading keystrokes at a fixed interval
311 Status
= gBS
->CreateEvent (
312 EVT_TIMER
| EVT_NOTIFY_SIGNAL
,
314 KeyboardTimerHandler
,
316 &ConsoleIn
->TimerEvent
318 if (EFI_ERROR (Status
)) {
319 Status
= EFI_OUT_OF_RESOURCES
;
320 StatusCode
= EFI_PERIPHERAL_KEYBOARD
| EFI_P_EC_CONTROLLER_ERROR
;
324 Status
= gBS
->SetTimer (
325 ConsoleIn
->TimerEvent
,
327 KEYBOARD_TIMER_INTERVAL
329 if (EFI_ERROR (Status
)) {
330 Status
= EFI_OUT_OF_RESOURCES
;
331 StatusCode
= EFI_PERIPHERAL_KEYBOARD
| EFI_P_EC_CONTROLLER_ERROR
;
335 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
337 EFI_PERIPHERAL_KEYBOARD
| EFI_P_PC_PRESENCE_DETECT
,
342 // Reset the keyboard device
344 Status
= ConsoleIn
->ConInEx
.Reset (&ConsoleIn
->ConInEx
, FeaturePcdGet (PcdPs2KbdExtendedVerification
));
345 if (EFI_ERROR (Status
)) {
346 Status
= EFI_DEVICE_ERROR
;
347 StatusCode
= EFI_PERIPHERAL_KEYBOARD
| EFI_P_EC_NOT_DETECTED
;
351 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
353 EFI_PERIPHERAL_KEYBOARD
| EFI_P_PC_DETECTED
,
357 ConsoleIn
->ControllerNameTable
= NULL
;
360 gPs2KeyboardComponentName
.SupportedLanguages
,
361 &ConsoleIn
->ControllerNameTable
,
362 L
"PS/2 Keyboard Device",
367 gPs2KeyboardComponentName2
.SupportedLanguages
,
368 &ConsoleIn
->ControllerNameTable
,
369 L
"PS/2 Keyboard Device",
375 // Install protocol interfaces for the keyboard device.
377 Status
= gBS
->InstallMultipleProtocolInterfaces (
379 &gEfiSimpleTextInProtocolGuid
,
381 &gEfiSimpleTextInputExProtocolGuid
,
385 if (EFI_ERROR (Status
)) {
386 StatusCode
= EFI_PERIPHERAL_KEYBOARD
| EFI_P_EC_CONTROLLER_ERROR
;
396 if (StatusCode
!= 0) {
397 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
398 EFI_ERROR_CODE
| EFI_ERROR_MINOR
,
404 if ((ConsoleIn
!= NULL
) && (ConsoleIn
->ConIn
.WaitForKey
!= NULL
)) {
405 gBS
->CloseEvent (ConsoleIn
->ConIn
.WaitForKey
);
408 if ((ConsoleIn
!= NULL
) && (ConsoleIn
->TimerEvent
!= NULL
)) {
409 gBS
->CloseEvent (ConsoleIn
->TimerEvent
);
411 if ((ConsoleIn
!= NULL
) && (ConsoleIn
->ConInEx
.WaitForKeyEx
!= NULL
)) {
412 gBS
->CloseEvent (ConsoleIn
->ConInEx
.WaitForKeyEx
);
414 KbdFreeNotifyList (&ConsoleIn
->NotifyList
);
415 if ((ConsoleIn
!= NULL
) && (ConsoleIn
->ControllerNameTable
!= NULL
)) {
416 FreeUnicodeStringTable (ConsoleIn
->ControllerNameTable
);
419 // Since there will be no timer handler for keyboard input any more,
420 // exhaust input data just in case there is still keyboard data left
422 if (ConsoleIn
!= NULL
) {
423 Status1
= EFI_SUCCESS
;
424 while (!EFI_ERROR (Status1
) && (Status
!= EFI_DEVICE_ERROR
)) {
425 Status1
= KeyboardRead (ConsoleIn
, &Data
);;
429 if (ConsoleIn
!= NULL
) {
430 gBS
->FreePool (ConsoleIn
);
435 &gEfiDevicePathProtocolGuid
,
436 This
->DriverBindingHandle
,
442 &gEfiIsaIoProtocolGuid
,
443 This
->DriverBindingHandle
,
451 Stop this driver on ControllerHandle. Support stoping any child handles
452 created by this driver.
454 @param This Protocol instance pointer.
455 @param ControllerHandle Handle of device to stop driver on
456 @param NumberOfChildren Number of Handles in ChildHandleBuffer. If number of
457 children is zero stop the entire bus driver.
458 @param ChildHandleBuffer List of Child Handles to Stop.
460 @retval EFI_SUCCESS This driver is removed ControllerHandle
461 @retval other This driver was not removed from this device
466 KbdControllerDriverStop (
467 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
468 IN EFI_HANDLE Controller
,
469 IN UINTN NumberOfChildren
,
470 IN EFI_HANDLE
*ChildHandleBuffer
474 EFI_SIMPLE_TEXT_INPUT_PROTOCOL
*ConIn
;
475 KEYBOARD_CONSOLE_IN_DEV
*ConsoleIn
;
481 Status
= gBS
->OpenProtocol (
483 &gEfiSimpleTextInProtocolGuid
,
485 This
->DriverBindingHandle
,
487 EFI_OPEN_PROTOCOL_GET_PROTOCOL
489 if (EFI_ERROR (Status
)) {
492 Status
= gBS
->OpenProtocol (
494 &gEfiSimpleTextInputExProtocolGuid
,
496 This
->DriverBindingHandle
,
498 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
500 if (EFI_ERROR (Status
)) {
504 ConsoleIn
= KEYBOARD_CONSOLE_IN_DEV_FROM_THIS (ConIn
);
507 // Report that the keyboard is being disabled
509 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
511 EFI_PERIPHERAL_KEYBOARD
| EFI_P_PC_DISABLE
,
512 ConsoleIn
->DevicePath
515 if (ConsoleIn
->TimerEvent
!= NULL
) {
516 gBS
->CloseEvent (ConsoleIn
->TimerEvent
);
517 ConsoleIn
->TimerEvent
= NULL
;
521 // Since there will be no timer handler for keyboard input any more,
522 // exhaust input data just in case there is still keyboard data left
524 Status
= EFI_SUCCESS
;
525 while (!EFI_ERROR (Status
)) {
526 Status
= KeyboardRead (ConsoleIn
, &Data
);;
529 // Uninstall the SimpleTextIn and SimpleTextInEx protocols
531 Status
= gBS
->UninstallMultipleProtocolInterfaces (
533 &gEfiSimpleTextInProtocolGuid
,
535 &gEfiSimpleTextInputExProtocolGuid
,
539 if (EFI_ERROR (Status
)) {
545 &gEfiDevicePathProtocolGuid
,
546 This
->DriverBindingHandle
,
552 &gEfiIsaIoProtocolGuid
,
553 This
->DriverBindingHandle
,
558 // Free other resources
560 if ((ConsoleIn
->ConIn
).WaitForKey
!= NULL
) {
561 gBS
->CloseEvent ((ConsoleIn
->ConIn
).WaitForKey
);
562 (ConsoleIn
->ConIn
).WaitForKey
= NULL
;
564 if (ConsoleIn
->ConInEx
.WaitForKeyEx
!= NULL
) {
565 gBS
->CloseEvent (ConsoleIn
->ConInEx
.WaitForKeyEx
);
566 ConsoleIn
->ConInEx
.WaitForKeyEx
= NULL
;
568 KbdFreeNotifyList (&ConsoleIn
->NotifyList
);
569 FreeUnicodeStringTable (ConsoleIn
->ControllerNameTable
);
570 gBS
->FreePool (ConsoleIn
);
576 Free the waiting key notify list.
578 @param ListHead Pointer to list head
580 @retval EFI_INVALID_PARAMETER ListHead is NULL
581 @retval EFI_SUCCESS Sucess to free NotifyList
585 IN OUT LIST_ENTRY
*ListHead
588 KEYBOARD_CONSOLE_IN_EX_NOTIFY
*NotifyNode
;
590 if (ListHead
== NULL
) {
591 return EFI_INVALID_PARAMETER
;
593 while (!IsListEmpty (ListHead
)) {
595 ListHead
->ForwardLink
,
596 KEYBOARD_CONSOLE_IN_EX_NOTIFY
,
598 KEYBOARD_CONSOLE_IN_EX_NOTIFY_SIGNATURE
600 RemoveEntryList (ListHead
->ForwardLink
);
601 gBS
->FreePool (NotifyNode
);
608 The module Entry Point for module Ps2Keyboard.
610 @param[in] ImageHandle The firmware allocated handle for the EFI image.
611 @param[in] SystemTable A pointer to the EFI System Table.
613 @retval EFI_SUCCESS The entry point is executed successfully.
614 @retval other Some error occurs when executing this entry point.
619 InitializePs2Keyboard(
620 IN EFI_HANDLE ImageHandle
,
621 IN EFI_SYSTEM_TABLE
*SystemTable
627 // Install driver model protocol(s).
629 Status
= EfiLibInstallDriverBindingComponentName2 (
632 &gKeyboardControllerDriver
,
634 &gPs2KeyboardComponentName
,
635 &gPs2KeyboardComponentName2
637 ASSERT_EFI_ERROR (Status
);