]> git.proxmox.com Git - mirror_edk2.git/blob - IntelFrameworkModulePkg/Bus/Isa/Ps2KeyboardDxe/Ps2Keyboard.c
c73cf936131830a75148cc95550ce7ed2f6a6b5d
[mirror_edk2.git] / IntelFrameworkModulePkg / Bus / Isa / Ps2KeyboardDxe / Ps2Keyboard.c
1 /** @file
2
3 PS/2 Keyboard driver. Routines that interacts with callers,
4 conforming to EFI driver model
5
6 Copyright (c) 2006 - 2011, 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
11
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.
14
15 **/
16
17 #include "Ps2Keyboard.h"
18
19 //
20 // Function prototypes
21 //
22 /**
23 Test controller is a keyboard Controller.
24
25 @param This Pointer of EFI_DRIVER_BINDING_PROTOCOL
26 @param Controller driver's controller
27 @param RemainingDevicePath children device path
28
29 @retval EFI_UNSUPPORTED controller is not floppy disk
30 @retval EFI_SUCCESS controller is floppy disk
31 **/
32 EFI_STATUS
33 EFIAPI
34 KbdControllerDriverSupported (
35 IN EFI_DRIVER_BINDING_PROTOCOL *This,
36 IN EFI_HANDLE Controller,
37 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
38 );
39
40 /**
41 Create KEYBOARD_CONSOLE_IN_DEV instance on controller.
42
43 @param This Pointer of EFI_DRIVER_BINDING_PROTOCOL
44 @param Controller driver controller handle
45 @param RemainingDevicePath Children's device path
46
47 @retval whether success to create floppy control instance.
48 **/
49 EFI_STATUS
50 EFIAPI
51 KbdControllerDriverStart (
52 IN EFI_DRIVER_BINDING_PROTOCOL *This,
53 IN EFI_HANDLE Controller,
54 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
55 );
56
57 /**
58 Stop this driver on ControllerHandle. Support stoping any child handles
59 created by this driver.
60
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.
66
67 @retval EFI_SUCCESS This driver is removed ControllerHandle
68 @retval other This driver was not removed from this device
69
70 **/
71 EFI_STATUS
72 EFIAPI
73 KbdControllerDriverStop (
74 IN EFI_DRIVER_BINDING_PROTOCOL *This,
75 IN EFI_HANDLE Controller,
76 IN UINTN NumberOfChildren,
77 IN EFI_HANDLE *ChildHandleBuffer
78 );
79
80 /**
81 Free the waiting key notify list.
82
83 @param ListHead Pointer to list head
84
85 @retval EFI_INVALID_PARAMETER ListHead is NULL
86 @retval EFI_SUCCESS Sucess to free NotifyList
87 **/
88 EFI_STATUS
89 KbdFreeNotifyList (
90 IN OUT LIST_ENTRY *ListHead
91 );
92
93 //
94 // DriverBinding Protocol Instance
95 //
96 EFI_DRIVER_BINDING_PROTOCOL gKeyboardControllerDriver = {
97 KbdControllerDriverSupported,
98 KbdControllerDriverStart,
99 KbdControllerDriverStop,
100 0xa,
101 NULL,
102 NULL
103 };
104
105 /**
106 Test controller is a keyboard Controller.
107
108 @param This Pointer of EFI_DRIVER_BINDING_PROTOCOL
109 @param Controller driver's controller
110 @param RemainingDevicePath children device path
111
112 @retval EFI_UNSUPPORTED controller is not floppy disk
113 @retval EFI_SUCCESS controller is floppy disk
114 **/
115 EFI_STATUS
116 EFIAPI
117 KbdControllerDriverSupported (
118 IN EFI_DRIVER_BINDING_PROTOCOL *This,
119 IN EFI_HANDLE Controller,
120 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
121 )
122 {
123 EFI_STATUS Status;
124 EFI_ISA_IO_PROTOCOL *IsaIo;
125
126 //
127 // Open the IO Abstraction(s) needed to perform the supported test
128 //
129 Status = gBS->OpenProtocol (
130 Controller,
131 &gEfiIsaIoProtocolGuid,
132 (VOID **) &IsaIo,
133 This->DriverBindingHandle,
134 Controller,
135 EFI_OPEN_PROTOCOL_BY_DRIVER
136 );
137 if (EFI_ERROR (Status)) {
138 return Status;
139 }
140 //
141 // Use the ISA I/O Protocol to see if Controller is the Keyboard controller
142 //
143 if (IsaIo->ResourceList->Device.HID != EISA_PNP_ID (0x303) || IsaIo->ResourceList->Device.UID != 0) {
144 Status = EFI_UNSUPPORTED;
145 }
146 //
147 // Close the I/O Abstraction(s) used to perform the supported test
148 //
149 gBS->CloseProtocol (
150 Controller,
151 &gEfiIsaIoProtocolGuid,
152 This->DriverBindingHandle,
153 Controller
154 );
155
156 return Status;
157 }
158
159 /**
160 Create KEYBOARD_CONSOLE_IN_DEV instance on controller.
161
162 @param This Pointer of EFI_DRIVER_BINDING_PROTOCOL
163 @param Controller driver controller handle
164 @param RemainingDevicePath Children's device path
165
166 @retval whether success to create floppy control instance.
167 **/
168 EFI_STATUS
169 EFIAPI
170 KbdControllerDriverStart (
171 IN EFI_DRIVER_BINDING_PROTOCOL *This,
172 IN EFI_HANDLE Controller,
173 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
174 )
175 {
176 EFI_STATUS Status;
177 EFI_STATUS Status1;
178 EFI_ISA_IO_PROTOCOL *IsaIo;
179 KEYBOARD_CONSOLE_IN_DEV *ConsoleIn;
180 UINT8 Data;
181 EFI_STATUS_CODE_VALUE StatusCode;
182 EFI_DEVICE_PATH_PROTOCOL *ParentDevicePath;
183
184 StatusCode = 0;
185
186 Status = gBS->OpenProtocol (
187 Controller,
188 &gEfiDevicePathProtocolGuid,
189 (VOID **) &ParentDevicePath,
190 This->DriverBindingHandle,
191 Controller,
192 EFI_OPEN_PROTOCOL_BY_DRIVER
193 );
194 if (EFI_ERROR (Status)) {
195 return Status;
196 }
197 //
198 // Report that the keyboard is being enabled
199 //
200 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
201 EFI_PROGRESS_CODE,
202 EFI_PERIPHERAL_KEYBOARD | EFI_P_PC_ENABLE,
203 ParentDevicePath
204 );
205
206 //
207 // Get the ISA I/O Protocol on Controller's handle
208 //
209 Status = gBS->OpenProtocol (
210 Controller,
211 &gEfiIsaIoProtocolGuid,
212 (VOID **) &IsaIo,
213 This->DriverBindingHandle,
214 Controller,
215 EFI_OPEN_PROTOCOL_BY_DRIVER
216 );
217 if (EFI_ERROR (Status)) {
218 gBS->CloseProtocol (
219 Controller,
220 &gEfiDevicePathProtocolGuid,
221 This->DriverBindingHandle,
222 Controller
223 );
224 return EFI_INVALID_PARAMETER;
225 }
226 //
227 // Allocate private data
228 //
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;
233 goto ErrorExit;
234 }
235 //
236 // Setup the device instance
237 //
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;
247
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;
253
254 InitializeListHead (&ConsoleIn->NotifyList);
255
256 //
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.
261 //
262
263 //
264 // Return code is ignored on purpose.
265 //
266 KeyboardRead (ConsoleIn, &Data);
267 if ((KeyReadStatusRegister (ConsoleIn) & (KBC_PARE | KBC_TIM)) == (KBC_PARE | KBC_TIM)) {
268 //
269 // If nobody decodes KBC I/O port, it will read back as 0xFF.
270 // Check the Time-Out and Parity bit to see if it has an active KBC in system
271 //
272 Status = EFI_DEVICE_ERROR;
273 StatusCode = EFI_PERIPHERAL_KEYBOARD | EFI_P_EC_NOT_DETECTED;
274 goto ErrorExit;
275 }
276
277 //
278 // Setup the WaitForKey event
279 //
280 Status = gBS->CreateEvent (
281 EVT_NOTIFY_WAIT,
282 TPL_NOTIFY,
283 KeyboardWaitForKey,
284 ConsoleIn,
285 &((ConsoleIn->ConIn).WaitForKey)
286 );
287 if (EFI_ERROR (Status)) {
288 Status = EFI_OUT_OF_RESOURCES;
289 StatusCode = EFI_PERIPHERAL_KEYBOARD | EFI_P_EC_CONTROLLER_ERROR;
290 goto ErrorExit;
291 }
292 //
293 // Setup the WaitForKeyEx event
294 //
295 Status = gBS->CreateEvent (
296 EVT_NOTIFY_WAIT,
297 TPL_NOTIFY,
298 KeyboardWaitForKeyEx,
299 ConsoleIn,
300 &(ConsoleIn->ConInEx.WaitForKeyEx)
301 );
302 if (EFI_ERROR (Status)) {
303 Status = EFI_OUT_OF_RESOURCES;
304 StatusCode = EFI_PERIPHERAL_KEYBOARD | EFI_P_EC_CONTROLLER_ERROR;
305 goto ErrorExit;
306 }
307 // Setup a periodic timer, used for reading keystrokes at a fixed interval
308 //
309 Status = gBS->CreateEvent (
310 EVT_TIMER | EVT_NOTIFY_SIGNAL,
311 TPL_NOTIFY,
312 KeyboardTimerHandler,
313 ConsoleIn,
314 &ConsoleIn->TimerEvent
315 );
316 if (EFI_ERROR (Status)) {
317 Status = EFI_OUT_OF_RESOURCES;
318 StatusCode = EFI_PERIPHERAL_KEYBOARD | EFI_P_EC_CONTROLLER_ERROR;
319 goto ErrorExit;
320 }
321
322 Status = gBS->SetTimer (
323 ConsoleIn->TimerEvent,
324 TimerPeriodic,
325 KEYBOARD_TIMER_INTERVAL
326 );
327 if (EFI_ERROR (Status)) {
328 Status = EFI_OUT_OF_RESOURCES;
329 StatusCode = EFI_PERIPHERAL_KEYBOARD | EFI_P_EC_CONTROLLER_ERROR;
330 goto ErrorExit;
331 }
332
333 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
334 EFI_PROGRESS_CODE,
335 EFI_PERIPHERAL_KEYBOARD | EFI_P_PC_PRESENCE_DETECT,
336 ParentDevicePath
337 );
338
339 //
340 // Reset the keyboard device
341 //
342 Status = ConsoleIn->ConInEx.Reset (&ConsoleIn->ConInEx, FeaturePcdGet (PcdPs2KbdExtendedVerification));
343 if (EFI_ERROR (Status)) {
344 Status = EFI_DEVICE_ERROR;
345 StatusCode = EFI_PERIPHERAL_KEYBOARD | EFI_P_EC_NOT_DETECTED;
346 goto ErrorExit;
347 }
348
349 ConsoleIn->ControllerNameTable = NULL;
350 AddUnicodeString2 (
351 "eng",
352 gPs2KeyboardComponentName.SupportedLanguages,
353 &ConsoleIn->ControllerNameTable,
354 L"PS/2 Keyboard Device",
355 TRUE
356 );
357 AddUnicodeString2 (
358 "en",
359 gPs2KeyboardComponentName2.SupportedLanguages,
360 &ConsoleIn->ControllerNameTable,
361 L"PS/2 Keyboard Device",
362 FALSE
363 );
364
365
366 //
367 // Install protocol interfaces for the keyboard device.
368 //
369 Status = gBS->InstallMultipleProtocolInterfaces (
370 &Controller,
371 &gEfiSimpleTextInProtocolGuid,
372 &ConsoleIn->ConIn,
373 &gEfiSimpleTextInputExProtocolGuid,
374 &ConsoleIn->ConInEx,
375 NULL
376 );
377 if (EFI_ERROR (Status)) {
378 StatusCode = EFI_PERIPHERAL_KEYBOARD | EFI_P_EC_CONTROLLER_ERROR;
379 goto ErrorExit;
380 }
381
382 return Status;
383
384 ErrorExit:
385 //
386 // Report error code
387 //
388 if (StatusCode != 0) {
389 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
390 EFI_ERROR_CODE | EFI_ERROR_MINOR,
391 StatusCode,
392 ParentDevicePath
393 );
394 }
395
396 if ((ConsoleIn != NULL) && (ConsoleIn->ConIn.WaitForKey != NULL)) {
397 gBS->CloseEvent (ConsoleIn->ConIn.WaitForKey);
398 }
399
400 if ((ConsoleIn != NULL) && (ConsoleIn->TimerEvent != NULL)) {
401 gBS->CloseEvent (ConsoleIn->TimerEvent);
402 }
403 if ((ConsoleIn != NULL) && (ConsoleIn->ConInEx.WaitForKeyEx != NULL)) {
404 gBS->CloseEvent (ConsoleIn->ConInEx.WaitForKeyEx);
405 }
406 KbdFreeNotifyList (&ConsoleIn->NotifyList);
407 if ((ConsoleIn != NULL) && (ConsoleIn->ControllerNameTable != NULL)) {
408 FreeUnicodeStringTable (ConsoleIn->ControllerNameTable);
409 }
410 //
411 // Since there will be no timer handler for keyboard input any more,
412 // exhaust input data just in case there is still keyboard data left
413 //
414 if (ConsoleIn != NULL) {
415 Status1 = EFI_SUCCESS;
416 while (!EFI_ERROR (Status1) && (Status != EFI_DEVICE_ERROR)) {
417 Status1 = KeyboardRead (ConsoleIn, &Data);;
418 }
419 }
420
421 if (ConsoleIn != NULL) {
422 gBS->FreePool (ConsoleIn);
423 }
424
425 gBS->CloseProtocol (
426 Controller,
427 &gEfiDevicePathProtocolGuid,
428 This->DriverBindingHandle,
429 Controller
430 );
431
432 gBS->CloseProtocol (
433 Controller,
434 &gEfiIsaIoProtocolGuid,
435 This->DriverBindingHandle,
436 Controller
437 );
438
439 return Status;
440 }
441
442 /**
443 Stop this driver on ControllerHandle. Support stoping any child handles
444 created by this driver.
445
446 @param This Protocol instance pointer.
447 @param ControllerHandle Handle of device to stop driver on
448 @param NumberOfChildren Number of Handles in ChildHandleBuffer. If number of
449 children is zero stop the entire bus driver.
450 @param ChildHandleBuffer List of Child Handles to Stop.
451
452 @retval EFI_SUCCESS This driver is removed ControllerHandle
453 @retval other This driver was not removed from this device
454
455 **/
456 EFI_STATUS
457 EFIAPI
458 KbdControllerDriverStop (
459 IN EFI_DRIVER_BINDING_PROTOCOL *This,
460 IN EFI_HANDLE Controller,
461 IN UINTN NumberOfChildren,
462 IN EFI_HANDLE *ChildHandleBuffer
463 )
464 {
465 EFI_STATUS Status;
466 EFI_SIMPLE_TEXT_INPUT_PROTOCOL *ConIn;
467 KEYBOARD_CONSOLE_IN_DEV *ConsoleIn;
468 UINT8 Data;
469
470 //
471 // Disable Keyboard
472 //
473 Status = gBS->OpenProtocol (
474 Controller,
475 &gEfiSimpleTextInProtocolGuid,
476 (VOID **) &ConIn,
477 This->DriverBindingHandle,
478 Controller,
479 EFI_OPEN_PROTOCOL_GET_PROTOCOL
480 );
481 if (EFI_ERROR (Status)) {
482 return Status;
483 }
484 Status = gBS->OpenProtocol (
485 Controller,
486 &gEfiSimpleTextInputExProtocolGuid,
487 NULL,
488 This->DriverBindingHandle,
489 Controller,
490 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
491 );
492 if (EFI_ERROR (Status)) {
493 return Status;
494 }
495
496 ConsoleIn = KEYBOARD_CONSOLE_IN_DEV_FROM_THIS (ConIn);
497
498 //
499 // Report that the keyboard is being disabled
500 //
501 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
502 EFI_PROGRESS_CODE,
503 EFI_PERIPHERAL_KEYBOARD | EFI_P_PC_DISABLE,
504 ConsoleIn->DevicePath
505 );
506
507 if (ConsoleIn->TimerEvent != NULL) {
508 gBS->CloseEvent (ConsoleIn->TimerEvent);
509 ConsoleIn->TimerEvent = NULL;
510 }
511
512 //
513 // Since there will be no timer handler for keyboard input any more,
514 // exhaust input data just in case there is still keyboard data left
515 //
516 Status = EFI_SUCCESS;
517 while (!EFI_ERROR (Status)) {
518 Status = KeyboardRead (ConsoleIn, &Data);;
519 }
520 //
521 // Uninstall the SimpleTextIn and SimpleTextInEx protocols
522 //
523 Status = gBS->UninstallMultipleProtocolInterfaces (
524 Controller,
525 &gEfiSimpleTextInProtocolGuid,
526 &ConsoleIn->ConIn,
527 &gEfiSimpleTextInputExProtocolGuid,
528 &ConsoleIn->ConInEx,
529 NULL
530 );
531 if (EFI_ERROR (Status)) {
532 return Status;
533 }
534
535 gBS->CloseProtocol (
536 Controller,
537 &gEfiDevicePathProtocolGuid,
538 This->DriverBindingHandle,
539 Controller
540 );
541
542 gBS->CloseProtocol (
543 Controller,
544 &gEfiIsaIoProtocolGuid,
545 This->DriverBindingHandle,
546 Controller
547 );
548
549 //
550 // Free other resources
551 //
552 if ((ConsoleIn->ConIn).WaitForKey != NULL) {
553 gBS->CloseEvent ((ConsoleIn->ConIn).WaitForKey);
554 (ConsoleIn->ConIn).WaitForKey = NULL;
555 }
556 if (ConsoleIn->ConInEx.WaitForKeyEx != NULL) {
557 gBS->CloseEvent (ConsoleIn->ConInEx.WaitForKeyEx);
558 ConsoleIn->ConInEx.WaitForKeyEx = NULL;
559 }
560 KbdFreeNotifyList (&ConsoleIn->NotifyList);
561 FreeUnicodeStringTable (ConsoleIn->ControllerNameTable);
562 gBS->FreePool (ConsoleIn);
563
564 return EFI_SUCCESS;
565 }
566
567 /**
568 Free the waiting key notify list.
569
570 @param ListHead Pointer to list head
571
572 @retval EFI_INVALID_PARAMETER ListHead is NULL
573 @retval EFI_SUCCESS Sucess to free NotifyList
574 **/
575 EFI_STATUS
576 KbdFreeNotifyList (
577 IN OUT LIST_ENTRY *ListHead
578 )
579 {
580 KEYBOARD_CONSOLE_IN_EX_NOTIFY *NotifyNode;
581
582 if (ListHead == NULL) {
583 return EFI_INVALID_PARAMETER;
584 }
585 while (!IsListEmpty (ListHead)) {
586 NotifyNode = CR (
587 ListHead->ForwardLink,
588 KEYBOARD_CONSOLE_IN_EX_NOTIFY,
589 NotifyEntry,
590 KEYBOARD_CONSOLE_IN_EX_NOTIFY_SIGNATURE
591 );
592 RemoveEntryList (ListHead->ForwardLink);
593 gBS->FreePool (NotifyNode);
594 }
595
596 return EFI_SUCCESS;
597 }
598
599 /**
600 The module Entry Point for module Ps2Keyboard.
601
602 @param[in] ImageHandle The firmware allocated handle for the EFI image.
603 @param[in] SystemTable A pointer to the EFI System Table.
604
605 @retval EFI_SUCCESS The entry point is executed successfully.
606 @retval other Some error occurs when executing this entry point.
607
608 **/
609 EFI_STATUS
610 EFIAPI
611 InitializePs2Keyboard(
612 IN EFI_HANDLE ImageHandle,
613 IN EFI_SYSTEM_TABLE *SystemTable
614 )
615 {
616 EFI_STATUS Status;
617
618 //
619 // Install driver model protocol(s).
620 //
621 Status = EfiLibInstallDriverBindingComponentName2 (
622 ImageHandle,
623 SystemTable,
624 &gKeyboardControllerDriver,
625 ImageHandle,
626 &gPs2KeyboardComponentName,
627 &gPs2KeyboardComponentName2
628 );
629 ASSERT_EFI_ERROR (Status);
630
631
632 return Status;
633 }
634