]> git.proxmox.com Git - mirror_edk2.git/blame - IntelFrameworkModulePkg/Csm/BiosThunk/KeyboardDxe/BiosKeyboard.c
IntelFrameworkModulePkg Ps2KbDxe: Execute key notify func at TPL_CALLBACK
[mirror_edk2.git] / IntelFrameworkModulePkg / Csm / BiosThunk / KeyboardDxe / BiosKeyboard.c
CommitLineData
bcecde14 1/** @file\r
2 ConsoleOut Routines that speak VGA.\r
3\r
0927c81d 4Copyright (c) 2006 - 2014, Intel Corporation. All rights reserved.<BR>\r
bcecde14 5\r
6This program and the accompanying materials\r
7are licensed and made available under the terms and conditions\r
8of the BSD License which accompanies this distribution. The\r
9full text of the license may be found at\r
10http://opensource.org/licenses/bsd-license.php\r
11\r
12THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
13WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
14\r
15**/\r
16\r
17#include "BiosKeyboard.h"\r
18\r
19//\r
20// EFI Driver Binding Protocol Instance\r
21//\r
22EFI_DRIVER_BINDING_PROTOCOL gBiosKeyboardDriverBinding = {\r
23 BiosKeyboardDriverBindingSupported,\r
24 BiosKeyboardDriverBindingStart,\r
25 BiosKeyboardDriverBindingStop,\r
26 0x3,\r
27 NULL,\r
28 NULL\r
29};\r
30\r
31\r
32/**\r
33 Enqueue the key.\r
34\r
35 @param Queue The queue to be enqueued.\r
36 @param KeyData The key data to be enqueued.\r
37\r
38 @retval EFI_NOT_READY The queue is full.\r
39 @retval EFI_SUCCESS Successfully enqueued the key data.\r
40\r
41**/\r
42EFI_STATUS\r
43Enqueue (\r
44 IN SIMPLE_QUEUE *Queue,\r
45 IN EFI_KEY_DATA *KeyData\r
46 )\r
47{\r
48 if ((Queue->Rear + 1) % QUEUE_MAX_COUNT == Queue->Front) {\r
49 return EFI_NOT_READY;\r
50 }\r
51\r
52 CopyMem (&Queue->Buffer[Queue->Rear], KeyData, sizeof (EFI_KEY_DATA));\r
53 Queue->Rear = (Queue->Rear + 1) % QUEUE_MAX_COUNT;\r
54\r
55 return EFI_SUCCESS;\r
56}\r
57\r
58\r
59/**\r
60 Dequeue the key.\r
61 \r
62 @param Queue The queue to be dequeued.\r
63 @param KeyData The key data to be dequeued.\r
64\r
65 @retval EFI_NOT_READY The queue is empty.\r
66 @retval EFI_SUCCESS Successfully dequeued the key data.\r
67\r
68**/\r
69EFI_STATUS\r
70Dequeue (\r
71 IN SIMPLE_QUEUE *Queue,\r
72 IN EFI_KEY_DATA *KeyData\r
73 )\r
74{\r
75 if (Queue->Front == Queue->Rear) {\r
76 return EFI_NOT_READY;\r
77 }\r
78\r
79 CopyMem (KeyData, &Queue->Buffer[Queue->Front], sizeof (EFI_KEY_DATA));\r
80 Queue->Front = (Queue->Front + 1) % QUEUE_MAX_COUNT;\r
81\r
82 return EFI_SUCCESS;\r
83}\r
84\r
85\r
86/**\r
87 Check whether the queue is empty.\r
88 \r
89 @param Queue The queue to be checked.\r
90\r
91 @retval EFI_NOT_READY The queue is empty.\r
92 @retval EFI_SUCCESS The queue is not empty.\r
93\r
94**/\r
95EFI_STATUS\r
96CheckQueue (\r
97 IN SIMPLE_QUEUE *Queue\r
98 )\r
99{\r
100 if (Queue->Front == Queue->Rear) {\r
101 return EFI_NOT_READY;\r
102 }\r
103\r
104 return EFI_SUCCESS;\r
105}\r
106\r
107//\r
108// EFI Driver Binding Protocol Functions\r
109//\r
110\r
111/**\r
112 Check whether the driver supports this device.\r
113\r
114 @param This The Udriver binding protocol.\r
115 @param Controller The controller handle to check.\r
116 @param RemainingDevicePath The remaining device path.\r
117\r
118 @retval EFI_SUCCESS The driver supports this controller.\r
119 @retval other This device isn't supported.\r
120\r
121**/\r
122EFI_STATUS\r
123EFIAPI\r
124BiosKeyboardDriverBindingSupported (\r
125 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
126 IN EFI_HANDLE Controller,\r
127 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath\r
128 )\r
129{\r
130 EFI_STATUS Status;\r
131 EFI_LEGACY_BIOS_PROTOCOL *LegacyBios;\r
132 EFI_ISA_IO_PROTOCOL *IsaIo;\r
133\r
134 //\r
135 // See if the Legacy BIOS Protocol is available\r
136 //\r
137 Status = gBS->LocateProtocol (\r
138 &gEfiLegacyBiosProtocolGuid,\r
139 NULL,\r
140 (VOID **) &LegacyBios\r
141 );\r
142\r
143 if (EFI_ERROR (Status)) {\r
144 return Status;\r
145 }\r
146 //\r
147 // Open the IO Abstraction(s) needed to perform the supported test\r
148 //\r
149 Status = gBS->OpenProtocol (\r
150 Controller,\r
151 &gEfiIsaIoProtocolGuid,\r
152 (VOID **) &IsaIo,\r
153 This->DriverBindingHandle,\r
154 Controller,\r
155 EFI_OPEN_PROTOCOL_BY_DRIVER\r
156 );\r
157\r
158 if (EFI_ERROR (Status)) {\r
159 return Status;\r
160 }\r
161 //\r
162 // Use the ISA I/O Protocol to see if Controller is the Keyboard controller\r
163 //\r
164 if (IsaIo->ResourceList->Device.HID != EISA_PNP_ID (0x303) || IsaIo->ResourceList->Device.UID != 0) {\r
165 Status = EFI_UNSUPPORTED;\r
166 }\r
167\r
168 gBS->CloseProtocol (\r
169 Controller,\r
170 &gEfiIsaIoProtocolGuid,\r
171 This->DriverBindingHandle,\r
172 Controller\r
173 );\r
174\r
175 return Status;\r
176}\r
177\r
178/**\r
179 Starts the device with this driver.\r
180\r
181 @param This The driver binding instance.\r
182 @param Controller Handle of device to bind driver to.\r
183 @param RemainingDevicePath Optional parameter use to pick a specific child\r
184 device to start.\r
185\r
186 @retval EFI_SUCCESS The controller is controlled by the driver.\r
187 @retval Other This controller cannot be started.\r
188\r
189**/\r
190EFI_STATUS\r
191EFIAPI\r
192BiosKeyboardDriverBindingStart (\r
193 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
194 IN EFI_HANDLE Controller,\r
195 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath\r
196 )\r
197{\r
198 EFI_STATUS Status;\r
199 EFI_LEGACY_BIOS_PROTOCOL *LegacyBios;\r
200 EFI_ISA_IO_PROTOCOL *IsaIo;\r
201 BIOS_KEYBOARD_DEV *BiosKeyboardPrivate;\r
202 EFI_IA32_REGISTER_SET Regs;\r
203 BOOLEAN CarryFlag;\r
204 EFI_PS2_POLICY_PROTOCOL *Ps2Policy;\r
205 UINT8 Command;\r
206 EFI_STATUS_CODE_VALUE StatusCode;\r
207\r
208 BiosKeyboardPrivate = NULL;\r
209 IsaIo = NULL;\r
210 StatusCode = 0;\r
211\r
212 //\r
213 // Get Ps2 policy to set. Will be use if present.\r
214 //\r
215 gBS->LocateProtocol (\r
216 &gEfiPs2PolicyProtocolGuid,\r
217 NULL,\r
218 (VOID **) &Ps2Policy\r
219 );\r
220\r
221 //\r
222 // See if the Legacy BIOS Protocol is available\r
223 //\r
224 Status = gBS->LocateProtocol (\r
225 &gEfiLegacyBiosProtocolGuid,\r
226 NULL,\r
227 (VOID **) &LegacyBios\r
228 );\r
229\r
230 if (EFI_ERROR (Status)) {\r
231 return Status;\r
232 }\r
233 //\r
234 // Open the IO Abstraction(s) needed\r
235 //\r
236 Status = gBS->OpenProtocol (\r
237 Controller,\r
238 &gEfiIsaIoProtocolGuid,\r
239 (VOID **) &IsaIo,\r
240 This->DriverBindingHandle,\r
241 Controller,\r
242 EFI_OPEN_PROTOCOL_BY_DRIVER\r
243 );\r
244 if (EFI_ERROR (Status)) {\r
245 return Status;\r
246 }\r
247\r
248 //\r
249 // Allocate the private device structure\r
250 //\r
251 BiosKeyboardPrivate = (BIOS_KEYBOARD_DEV *) AllocateZeroPool (sizeof (BIOS_KEYBOARD_DEV));\r
252 if (NULL == BiosKeyboardPrivate) {\r
253 Status = EFI_OUT_OF_RESOURCES;\r
254 goto Done;\r
255 }\r
256\r
257 //\r
258 // Initialize the private device structure\r
259 //\r
260 BiosKeyboardPrivate->Signature = BIOS_KEYBOARD_DEV_SIGNATURE;\r
261 BiosKeyboardPrivate->Handle = Controller;\r
262 BiosKeyboardPrivate->LegacyBios = LegacyBios;\r
263 BiosKeyboardPrivate->IsaIo = IsaIo;\r
264\r
265 BiosKeyboardPrivate->SimpleTextIn.Reset = BiosKeyboardReset;\r
266 BiosKeyboardPrivate->SimpleTextIn.ReadKeyStroke = BiosKeyboardReadKeyStroke;\r
267\r
268 BiosKeyboardPrivate->DataRegisterAddress = KEYBOARD_8042_DATA_REGISTER;\r
269 BiosKeyboardPrivate->StatusRegisterAddress = KEYBOARD_8042_STATUS_REGISTER;\r
270 BiosKeyboardPrivate->CommandRegisterAddress = KEYBOARD_8042_COMMAND_REGISTER;\r
271 BiosKeyboardPrivate->ExtendedKeyboard = TRUE;\r
272 \r
273 BiosKeyboardPrivate->Queue.Front = 0;\r
274 BiosKeyboardPrivate->Queue.Rear = 0;\r
275 BiosKeyboardPrivate->SimpleTextInputEx.Reset = BiosKeyboardResetEx;\r
276 BiosKeyboardPrivate->SimpleTextInputEx.ReadKeyStrokeEx = BiosKeyboardReadKeyStrokeEx;\r
277 BiosKeyboardPrivate->SimpleTextInputEx.SetState = BiosKeyboardSetState;\r
278 BiosKeyboardPrivate->SimpleTextInputEx.RegisterKeyNotify = BiosKeyboardRegisterKeyNotify; \r
279 BiosKeyboardPrivate->SimpleTextInputEx.UnregisterKeyNotify = BiosKeyboardUnregisterKeyNotify; \r
280 InitializeListHead (&BiosKeyboardPrivate->NotifyList);\r
281\r
bcecde14 282 //\r
283 // Report that the keyboard is being enabled\r
284 //\r
55d4edfb 285 REPORT_STATUS_CODE (\r
bcecde14 286 EFI_PROGRESS_CODE,\r
55d4edfb 287 EFI_PERIPHERAL_KEYBOARD | EFI_P_PC_ENABLE\r
bcecde14 288 );\r
289\r
290 //\r
291 // Setup the WaitForKey event\r
292 //\r
293 Status = gBS->CreateEvent (\r
294 EVT_NOTIFY_WAIT,\r
295 TPL_NOTIFY,\r
296 BiosKeyboardWaitForKey,\r
297 &(BiosKeyboardPrivate->SimpleTextIn),\r
298 &((BiosKeyboardPrivate->SimpleTextIn).WaitForKey)\r
299 );\r
300 if (EFI_ERROR (Status)) {\r
301 (BiosKeyboardPrivate->SimpleTextIn).WaitForKey = NULL;\r
302 goto Done;\r
303 }\r
304 Status = gBS->CreateEvent (\r
305 EVT_NOTIFY_WAIT,\r
306 TPL_NOTIFY,\r
307 BiosKeyboardWaitForKeyEx,\r
308 &(BiosKeyboardPrivate->SimpleTextInputEx),\r
309 &(BiosKeyboardPrivate->SimpleTextInputEx.WaitForKeyEx)\r
310 );\r
311 if (EFI_ERROR (Status)) {\r
312 BiosKeyboardPrivate->SimpleTextInputEx.WaitForKeyEx = NULL;\r
313 goto Done;\r
314 } \r
315\r
316 //\r
317 // Setup a periodic timer, used for reading keystrokes at a fixed interval\r
318 //\r
319 Status = gBS->CreateEvent (\r
320 EVT_TIMER | EVT_NOTIFY_SIGNAL,\r
321 TPL_NOTIFY,\r
322 BiosKeyboardTimerHandler,\r
323 BiosKeyboardPrivate,\r
324 &BiosKeyboardPrivate->TimerEvent\r
325 );\r
326 if (EFI_ERROR (Status)) {\r
327 Status = EFI_OUT_OF_RESOURCES;\r
328 StatusCode = EFI_PERIPHERAL_KEYBOARD | EFI_P_EC_CONTROLLER_ERROR;\r
329 goto Done;\r
330 }\r
331\r
332 Status = gBS->SetTimer (\r
333 BiosKeyboardPrivate->TimerEvent,\r
334 TimerPeriodic,\r
335 KEYBOARD_TIMER_INTERVAL\r
336 );\r
337 if (EFI_ERROR (Status)) {\r
338 Status = EFI_OUT_OF_RESOURCES;\r
339 StatusCode = EFI_PERIPHERAL_KEYBOARD | EFI_P_EC_CONTROLLER_ERROR;\r
340 goto Done;\r
341 }\r
342 \r
343 //\r
344 // Report a Progress Code for an attempt to detect the precense of the keyboard device in the system\r
345 //\r
55d4edfb 346 REPORT_STATUS_CODE (\r
bcecde14 347 EFI_PROGRESS_CODE,\r
55d4edfb 348 EFI_PERIPHERAL_KEYBOARD | EFI_P_PC_PRESENCE_DETECT\r
bcecde14 349 );\r
350\r
351 //\r
352 // Reset the keyboard device\r
353 //\r
354 Status = BiosKeyboardPrivate->SimpleTextInputEx.Reset (\r
355 &BiosKeyboardPrivate->SimpleTextInputEx,\r
f6c014fb 356 FeaturePcdGet (PcdPs2KbdExtendedVerification)\r
bcecde14 357 );\r
bcecde14 358 if (EFI_ERROR (Status)) {\r
f6c014fb 359 DEBUG ((EFI_D_ERROR, "[KBD]Reset Failed. Status - %r\n", Status)); \r
bcecde14 360 StatusCode = EFI_PERIPHERAL_KEYBOARD | EFI_P_EC_NOT_DETECTED;\r
361 goto Done;\r
362 }\r
363 //\r
364 // Do platform specific policy like port swapping and keyboard light default\r
365 //\r
366 if (Ps2Policy != NULL) {\r
367\r
368 Ps2Policy->Ps2InitHardware (Controller);\r
369\r
370 Command = 0;\r
371 if ((Ps2Policy->KeyboardLight & EFI_KEYBOARD_CAPSLOCK) == EFI_KEYBOARD_CAPSLOCK) {\r
372 Command |= 4;\r
373 }\r
374\r
375 if ((Ps2Policy->KeyboardLight & EFI_KEYBOARD_NUMLOCK) == EFI_KEYBOARD_NUMLOCK) {\r
376 Command |= 2;\r
377 }\r
378\r
379 if ((Ps2Policy->KeyboardLight & EFI_KEYBOARD_SCROLLLOCK) == EFI_KEYBOARD_SCROLLLOCK) {\r
380 Command |= 1;\r
381 }\r
382\r
383 KeyboardWrite (BiosKeyboardPrivate, 0xed);\r
384 KeyboardWaitForValue (BiosKeyboardPrivate, 0xfa, KEYBOARD_WAITFORVALUE_TIMEOUT);\r
385 KeyboardWrite (BiosKeyboardPrivate, Command);\r
386 //\r
387 // Call Legacy BIOS Protocol to set whatever is necessary\r
388 //\r
389 LegacyBios->UpdateKeyboardLedStatus (LegacyBios, Command);\r
390 }\r
391 //\r
392 // Get Configuration\r
393 //\r
394 Regs.H.AH = 0xc0;\r
395 CarryFlag = BiosKeyboardPrivate->LegacyBios->Int86 (\r
396 BiosKeyboardPrivate->LegacyBios,\r
397 0x15,\r
398 &Regs\r
399 );\r
400\r
401 if (!CarryFlag) {\r
402 //\r
403 // Check bit 6 of Feature Byte 2.\r
404 // If it is set, then Int 16 Func 09 is supported\r
405 //\r
406 if (*(UINT8 *)(UINTN) ((Regs.X.ES << 4) + Regs.X.BX + 0x06) & 0x40) {\r
407 //\r
408 // Get Keyboard Functionality\r
409 //\r
410 Regs.H.AH = 0x09;\r
411 CarryFlag = BiosKeyboardPrivate->LegacyBios->Int86 (\r
412 BiosKeyboardPrivate->LegacyBios,\r
413 0x16,\r
414 &Regs\r
415 );\r
416\r
417 if (!CarryFlag) {\r
418 //\r
419 // Check bit 5 of AH.\r
420 // If it is set, then INT 16 Finc 10-12 are supported.\r
421 //\r
422 if ((Regs.H.AL & 0x40) != 0) {\r
423 //\r
424 // Set the flag to use INT 16 Func 10-12\r
425 //\r
426 BiosKeyboardPrivate->ExtendedKeyboard = TRUE;\r
427 }\r
428 }\r
429 }\r
430 }\r
f6c014fb 431 DEBUG ((EFI_D_INFO, "[KBD]Extended keystrokes supported by CSM16 - %02x\n", (UINTN)BiosKeyboardPrivate->ExtendedKeyboard));\r
bcecde14 432 //\r
433 // Install protocol interfaces for the keyboard device.\r
434 //\r
435 Status = gBS->InstallMultipleProtocolInterfaces (\r
436 &Controller,\r
437 &gEfiSimpleTextInProtocolGuid,\r
438 &BiosKeyboardPrivate->SimpleTextIn,\r
439 &gEfiSimpleTextInputExProtocolGuid,\r
440 &BiosKeyboardPrivate->SimpleTextInputEx,\r
441 NULL\r
442 );\r
443\r
444Done:\r
445 if (StatusCode != 0) {\r
446 //\r
447 // Report an Error Code for failing to start the keyboard device\r
448 //\r
55d4edfb 449 REPORT_STATUS_CODE (\r
bcecde14 450 EFI_ERROR_CODE | EFI_ERROR_MINOR,\r
55d4edfb 451 StatusCode\r
bcecde14 452 );\r
453 }\r
454\r
455 if (EFI_ERROR (Status)) {\r
456\r
457 if (BiosKeyboardPrivate != NULL) { \r
458 if ((BiosKeyboardPrivate->SimpleTextIn).WaitForKey != NULL) {\r
459 gBS->CloseEvent ((BiosKeyboardPrivate->SimpleTextIn).WaitForKey);\r
460 }\r
461\r
462 if ((BiosKeyboardPrivate->SimpleTextInputEx).WaitForKeyEx != NULL) {\r
463 gBS->CloseEvent ((BiosKeyboardPrivate->SimpleTextInputEx).WaitForKeyEx); \r
464 }\r
465 BiosKeyboardFreeNotifyList (&BiosKeyboardPrivate->NotifyList);\r
466\r
467 if (BiosKeyboardPrivate->TimerEvent != NULL) {\r
468 gBS->CloseEvent (BiosKeyboardPrivate->TimerEvent); \r
469 }\r
470\r
471 FreePool (BiosKeyboardPrivate);\r
472 }\r
473\r
474 if (IsaIo != NULL) {\r
475 gBS->CloseProtocol (\r
476 Controller,\r
477 &gEfiIsaIoProtocolGuid,\r
478 This->DriverBindingHandle,\r
479 Controller\r
480 );\r
481 }\r
482 }\r
483\r
484 return Status;\r
485}\r
486\r
487/**\r
488 Stop the device handled by this driver.\r
489\r
490 @param This The driver binding protocol.\r
491 @param Controller The controller to release.\r
492 @param NumberOfChildren The number of handles in ChildHandleBuffer.\r
493 @param ChildHandleBuffer The array of child handle.\r
494\r
495 @retval EFI_SUCCESS The device was stopped.\r
496 @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.\r
497 @retval Others Fail to uninstall protocols attached on the device.\r
498\r
499**/\r
500EFI_STATUS\r
501EFIAPI\r
502BiosKeyboardDriverBindingStop (\r
503 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
504 IN EFI_HANDLE Controller,\r
505 IN UINTN NumberOfChildren,\r
506 IN EFI_HANDLE *ChildHandleBuffer\r
507 )\r
508{\r
509 EFI_STATUS Status;\r
510 EFI_SIMPLE_TEXT_INPUT_PROTOCOL *SimpleTextIn;\r
511 BIOS_KEYBOARD_DEV *BiosKeyboardPrivate;\r
512\r
513 //\r
514 // Disable Keyboard\r
515 //\r
516 Status = gBS->OpenProtocol (\r
517 Controller,\r
518 &gEfiSimpleTextInProtocolGuid,\r
519 (VOID **) &SimpleTextIn,\r
520 This->DriverBindingHandle,\r
521 Controller,\r
522 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
523 );\r
524 if (EFI_ERROR (Status)) {\r
525 return Status;\r
526 }\r
527\r
528 Status = gBS->OpenProtocol (\r
529 Controller,\r
530 &gEfiSimpleTextInputExProtocolGuid,\r
531 NULL,\r
532 This->DriverBindingHandle,\r
533 Controller,\r
534 EFI_OPEN_PROTOCOL_TEST_PROTOCOL\r
535 );\r
536 if (EFI_ERROR (Status)) {\r
537 return Status;\r
538 }\r
539 \r
540 BiosKeyboardPrivate = BIOS_KEYBOARD_DEV_FROM_THIS (SimpleTextIn);\r
541\r
542 Status = gBS->UninstallMultipleProtocolInterfaces (\r
543 Controller,\r
544 &gEfiSimpleTextInProtocolGuid,\r
545 &BiosKeyboardPrivate->SimpleTextIn,\r
546 &gEfiSimpleTextInputExProtocolGuid,\r
547 &BiosKeyboardPrivate->SimpleTextInputEx,\r
548 NULL\r
549 );\r
550 if (EFI_ERROR (Status)) {\r
551 return Status;\r
552 }\r
553 //\r
554 // Release the IsaIo protocol on the controller handle\r
555 //\r
556 gBS->CloseProtocol (\r
557 Controller,\r
558 &gEfiIsaIoProtocolGuid,\r
559 This->DriverBindingHandle,\r
560 Controller\r
561 );\r
562\r
563 //\r
564 // Free other resources\r
565 //\r
566 gBS->CloseEvent ((BiosKeyboardPrivate->SimpleTextIn).WaitForKey);\r
567 gBS->CloseEvent (BiosKeyboardPrivate->TimerEvent);\r
568 gBS->CloseEvent (BiosKeyboardPrivate->SimpleTextInputEx.WaitForKeyEx);\r
569 BiosKeyboardFreeNotifyList (&BiosKeyboardPrivate->NotifyList);\r
570\r
571 FreePool (BiosKeyboardPrivate);\r
572\r
573 return EFI_SUCCESS;\r
574}\r
575\r
576/**\r
577 Read data byte from output buffer of Keyboard Controller without delay and waiting for buffer-empty state.\r
578\r
579 @param BiosKeyboardPrivate Keyboard instance pointer.\r
580\r
581 @return The data byte read from output buffer of Keyboard Controller from data port which often is port 60H.\r
582\r
583**/\r
584UINT8\r
585KeyReadDataRegister (\r
586 IN BIOS_KEYBOARD_DEV *BiosKeyboardPrivate\r
587 )\r
588{\r
589 UINT8 Data;\r
590\r
591 //\r
592 // Use IsaIo protocol to perform IO operations\r
593 //\r
594 BiosKeyboardPrivate->IsaIo->Io.Read (\r
595 BiosKeyboardPrivate->IsaIo,\r
596 EfiIsaIoWidthUint8,\r
597 BiosKeyboardPrivate->DataRegisterAddress,\r
598 1,\r
599 &Data\r
600 );\r
601\r
602 return Data;\r
603}\r
604\r
605/**\r
606 Read status byte from status register of Keyboard Controller without delay and waiting for buffer-empty state.\r
607\r
608 @param BiosKeyboardPrivate Keyboard instance pointer.\r
609\r
610 @return The status byte read from status register of Keyboard Controller from command port which often is port 64H.\r
611\r
612**/\r
613UINT8\r
614KeyReadStatusRegister (\r
615 IN BIOS_KEYBOARD_DEV *BiosKeyboardPrivate\r
616 )\r
617{\r
618 UINT8 Data;\r
619\r
620 //\r
621 // Use IsaIo protocol to perform IO operations\r
622 //\r
623 BiosKeyboardPrivate->IsaIo->Io.Read (\r
624 BiosKeyboardPrivate->IsaIo,\r
625 EfiIsaIoWidthUint8,\r
626 BiosKeyboardPrivate->StatusRegisterAddress,\r
627 1,\r
628 &Data\r
629 );\r
630\r
631 return Data;\r
632}\r
633\r
634/**\r
635 Write command byte to control register of Keyboard Controller without delay and waiting for buffer-empty state.\r
636\r
637 @param BiosKeyboardPrivate Keyboard instance pointer.\r
638 @param Data Data byte to write.\r
639\r
640**/\r
641VOID\r
642KeyWriteCommandRegister (\r
643 IN BIOS_KEYBOARD_DEV *BiosKeyboardPrivate,\r
644 IN UINT8 Data\r
645 )\r
646{\r
647 //\r
648 // Use IsaIo protocol to perform IO operations\r
649 //\r
650 BiosKeyboardPrivate->IsaIo->Io.Write (\r
651 BiosKeyboardPrivate->IsaIo,\r
652 EfiIsaIoWidthUint8,\r
653 BiosKeyboardPrivate->CommandRegisterAddress,\r
654 1,\r
655 &Data\r
656 );\r
657}\r
658\r
659/**\r
660 Write data byte to input buffer or input/output ports of Keyboard Controller without delay and waiting for buffer-empty state.\r
661\r
662 @param BiosKeyboardPrivate Keyboard instance pointer.\r
663 @param Data Data byte to write.\r
664\r
665**/\r
666VOID\r
667KeyWriteDataRegister (\r
668 IN BIOS_KEYBOARD_DEV *BiosKeyboardPrivate,\r
669 IN UINT8 Data\r
670 )\r
671{\r
672 //\r
673 // Use IsaIo protocol to perform IO operations\r
674 //\r
675 BiosKeyboardPrivate->IsaIo->Io.Write (\r
676 BiosKeyboardPrivate->IsaIo,\r
677 EfiIsaIoWidthUint8,\r
678 BiosKeyboardPrivate->DataRegisterAddress,\r
679 1,\r
680 &Data\r
681 );\r
682}\r
683\r
684/**\r
685 Read data byte from output buffer of Keyboard Controller with delay and waiting for buffer-empty state.\r
686\r
687 @param BiosKeyboardPrivate Keyboard instance pointer.\r
688 @param Data The pointer for data that being read out.\r
689\r
690 @retval EFI_SUCCESS The data byte read out successfully.\r
691 @retval EFI_TIMEOUT Timeout occurred during reading out data byte.\r
692\r
693**/\r
694EFI_STATUS\r
695KeyboardRead (\r
696 IN BIOS_KEYBOARD_DEV *BiosKeyboardPrivate,\r
697 OUT UINT8 *Data\r
698 )\r
699{\r
700 UINT32 TimeOut;\r
701 UINT32 RegFilled;\r
702\r
703 TimeOut = 0;\r
704 RegFilled = 0;\r
705\r
706 //\r
707 // wait till output buffer full then perform the read\r
708 //\r
709 for (TimeOut = 0; TimeOut < KEYBOARD_TIMEOUT; TimeOut += 30) {\r
710 if ((KeyReadStatusRegister (BiosKeyboardPrivate) & KBC_STSREG_VIA64_OUTB) != 0) {\r
711 RegFilled = 1;\r
712 *Data = KeyReadDataRegister (BiosKeyboardPrivate);\r
713 break;\r
714 }\r
715\r
716 gBS->Stall (30);\r
717 }\r
718\r
719 if (RegFilled == 0) {\r
720 return EFI_TIMEOUT;\r
721 }\r
722\r
723 return EFI_SUCCESS;\r
724}\r
725\r
726/**\r
727 Write data byte to input buffer or input/output ports of Keyboard Controller with delay and waiting for buffer-empty state.\r
728\r
729 @param BiosKeyboardPrivate Keyboard instance pointer.\r
730 @param Data Data byte to write.\r
731\r
732 @retval EFI_SUCCESS The data byte is written successfully.\r
733 @retval EFI_TIMEOUT Timeout occurred during writing.\r
734\r
735**/\r
736EFI_STATUS\r
737KeyboardWrite (\r
738 IN BIOS_KEYBOARD_DEV *BiosKeyboardPrivate,\r
739 IN UINT8 Data\r
740 )\r
741{\r
742 UINT32 TimeOut;\r
743 UINT32 RegEmptied;\r
744\r
745 TimeOut = 0;\r
746 RegEmptied = 0;\r
747\r
748 //\r
749 // wait for input buffer empty\r
750 //\r
751 for (TimeOut = 0; TimeOut < KEYBOARD_TIMEOUT; TimeOut += 30) {\r
752 if ((KeyReadStatusRegister (BiosKeyboardPrivate) & KBC_STSREG_VIA64_INPB) == 0) {\r
753 RegEmptied = 1;\r
754 break;\r
755 }\r
756\r
757 gBS->Stall (30);\r
758 }\r
759\r
760 if (RegEmptied == 0) {\r
761 return EFI_TIMEOUT;\r
762 }\r
763 //\r
764 // Write it\r
765 //\r
766 KeyWriteDataRegister (BiosKeyboardPrivate, Data);\r
767\r
768 return EFI_SUCCESS;\r
769}\r
770\r
771/**\r
772 Write command byte to control register of Keyboard Controller with delay and waiting for buffer-empty state.\r
773\r
774 @param BiosKeyboardPrivate Keyboard instance pointer.\r
775 @param Data Command byte to write.\r
776\r
777 @retval EFI_SUCCESS The command byte is written successfully.\r
778 @retval EFI_TIMEOUT Timeout occurred during writing.\r
779\r
780**/\r
781EFI_STATUS\r
782KeyboardCommand (\r
783 IN BIOS_KEYBOARD_DEV *BiosKeyboardPrivate,\r
784 IN UINT8 Data\r
785 )\r
786{\r
787 UINT32 TimeOut;\r
788 UINT32 RegEmptied;\r
789\r
790 TimeOut = 0;\r
791 RegEmptied = 0;\r
792\r
793 //\r
794 // Wait For Input Buffer Empty\r
795 //\r
796 for (TimeOut = 0; TimeOut < KEYBOARD_TIMEOUT; TimeOut += 30) {\r
797 if ((KeyReadStatusRegister (BiosKeyboardPrivate) & KBC_STSREG_VIA64_INPB) == 0) {\r
798 RegEmptied = 1;\r
799 break;\r
800 }\r
801\r
802 gBS->Stall (30);\r
803 }\r
804\r
805 if (RegEmptied == 0) {\r
806 return EFI_TIMEOUT;\r
807 }\r
808 //\r
809 // issue the command\r
810 //\r
811 KeyWriteCommandRegister (BiosKeyboardPrivate, Data);\r
812\r
813 //\r
814 // Wait For Input Buffer Empty again\r
815 //\r
816 RegEmptied = 0;\r
817 for (TimeOut = 0; TimeOut < KEYBOARD_TIMEOUT; TimeOut += 30) {\r
818 if ((KeyReadStatusRegister (BiosKeyboardPrivate) & KBC_STSREG_VIA64_INPB) == 0) {\r
819 RegEmptied = 1;\r
820 break;\r
821 }\r
822\r
823 gBS->Stall (30);\r
824 }\r
825\r
826 if (RegEmptied == 0) {\r
827 return EFI_TIMEOUT;\r
828 }\r
829\r
830 return EFI_SUCCESS;\r
831}\r
832\r
833/**\r
834 Wait for a specific value to be presented in\r
835 Data register of Keyboard Controller by keyboard and then read it,\r
836 used in keyboard commands ack\r
837\r
838 @param BiosKeyboardPrivate Keyboard instance pointer.\r
839 @param Value The value to be waited for\r
840 @param WaitForValueTimeOut The limit of microseconds for timeout\r
841\r
842 @retval EFI_SUCCESS The command byte is written successfully.\r
843 @retval EFI_TIMEOUT Timeout occurred during writing.\r
844\r
845**/\r
846EFI_STATUS\r
847KeyboardWaitForValue (\r
848 IN BIOS_KEYBOARD_DEV *BiosKeyboardPrivate,\r
849 IN UINT8 Value,\r
850 IN UINTN WaitForValueTimeOut\r
851 )\r
852{\r
853 UINT8 Data;\r
854 UINT32 TimeOut;\r
855 UINT32 SumTimeOut;\r
856 UINT32 GotIt;\r
857\r
858 GotIt = 0;\r
859 TimeOut = 0;\r
860 SumTimeOut = 0;\r
861\r
862 //\r
863 // Make sure the initial value of 'Data' is different from 'Value'\r
864 //\r
865 Data = 0;\r
866 if (Data == Value) {\r
867 Data = 1;\r
868 }\r
869 //\r
870 // Read from 8042 (multiple times if needed)\r
871 // until the expected value appears\r
872 // use SumTimeOut to control the iteration\r
873 //\r
874 while (1) {\r
875 //\r
876 // Perform a read\r
877 //\r
878 for (TimeOut = 0; TimeOut < KEYBOARD_TIMEOUT; TimeOut += 30) {\r
879 if ((KeyReadStatusRegister (BiosKeyboardPrivate) & KBC_STSREG_VIA64_OUTB) != 0) {\r
880 Data = KeyReadDataRegister (BiosKeyboardPrivate);\r
881 break;\r
882 }\r
883\r
884 gBS->Stall (30);\r
885 }\r
886\r
887 SumTimeOut += TimeOut;\r
888\r
889 if (Data == Value) {\r
890 GotIt = 1;\r
891 break;\r
892 }\r
893\r
894 if (SumTimeOut >= WaitForValueTimeOut) {\r
895 break;\r
896 }\r
897 }\r
898 //\r
899 // Check results\r
900 //\r
901 if (GotIt != 0) {\r
902 return EFI_SUCCESS;\r
903 } else {\r
904 return EFI_TIMEOUT;\r
905 }\r
906\r
907}\r
908\r
909/**\r
910 Reads the next keystroke from the input device. The WaitForKey Event can \r
911 be used to test for existance of a keystroke via WaitForEvent () call.\r
912\r
913 @param BiosKeyboardPrivate Bioskeyboard driver private structure.\r
914 @param KeyData A pointer to a buffer that is filled in with the keystroke \r
915 state data for the key that was pressed.\r
916\r
917 @retval EFI_SUCCESS The keystroke information was returned.\r
918 @retval EFI_NOT_READY There was no keystroke data availiable.\r
919 @retval EFI_DEVICE_ERROR The keystroke information was not returned due to \r
920 hardware errors.\r
921 @retval EFI_INVALID_PARAMETER KeyData is NULL. \r
922 \r
923**/\r
924EFI_STATUS\r
925KeyboardReadKeyStrokeWorker (\r
926 IN BIOS_KEYBOARD_DEV *BiosKeyboardPrivate,\r
927 OUT EFI_KEY_DATA *KeyData\r
928 )\r
929{\r
930 EFI_STATUS Status;\r
931 EFI_TPL OldTpl;\r
932 if (KeyData == NULL) {\r
933 return EFI_INVALID_PARAMETER;\r
934 }\r
935\r
936 //\r
70d3fe9d 937 // Use TimerEvent callback function to check whether there's any key pressed\r
bcecde14 938 //\r
939 \r
940 //\r
941 // Stall 1ms to give a chance to let other driver interrupt this routine for their timer event.\r
942 // Csm will be used to check whether there is a key pending, but the csm will disable all \r
943 // interrupt before switch to compatibility16, which mean all the efiCompatibility timer\r
944 // event will stop work during the compatibility16. And If a caller recursivly invoke this function, \r
945 // e.g. OS loader, other drivers which are driven by timer event will have a bad performance during this period, \r
946 // e.g. usb keyboard driver. \r
947 // Add a stall period can greatly increate other driver performance during the WaitForKey is recursivly invoked.\r
948 // 1ms delay will make little impact to the thunk keyboard driver, and user can not feel the delay at all when input.\r
949 //\r
950 gBS->Stall (1000);\r
951\r
952 OldTpl = gBS->RaiseTPL (TPL_NOTIFY);\r
953\r
954 BiosKeyboardTimerHandler (NULL, BiosKeyboardPrivate);\r
955 //\r
956 // If there's no key, just return\r
957 //\r
958 Status = CheckQueue (&BiosKeyboardPrivate->Queue);\r
959 if (EFI_ERROR (Status)) {\r
960 gBS->RestoreTPL (OldTpl);\r
961 return EFI_NOT_READY;\r
962 }\r
963\r
964 Status = Dequeue (&BiosKeyboardPrivate->Queue, KeyData);\r
965\r
966 gBS->RestoreTPL (OldTpl);\r
967\r
968 return EFI_SUCCESS;\r
969}\r
970\r
971//\r
972// EFI Simple Text In Protocol Functions\r
973//\r
974/**\r
975 Reset the Keyboard and do BAT test for it, if (ExtendedVerification == TRUE) then do some extra keyboard validations.\r
976\r
977 @param This Pointer of simple text Protocol.\r
978 @param ExtendedVerification Whether perform the extra validation of keyboard. True: perform; FALSE: skip.\r
979\r
980 @retval EFI_SUCCESS The command byte is written successfully.\r
70d3fe9d 981 @retval EFI_DEVICE_ERROR Errors occurred during resetting keyboard.\r
bcecde14 982\r
983**/\r
984EFI_STATUS\r
985EFIAPI\r
986BiosKeyboardReset (\r
987 IN EFI_SIMPLE_TEXT_INPUT_PROTOCOL *This,\r
988 IN BOOLEAN ExtendedVerification\r
989 )\r
990{\r
991 BIOS_KEYBOARD_DEV *BiosKeyboardPrivate;\r
992 EFI_STATUS Status;\r
993 EFI_TPL OldTpl;\r
994 UINT8 CommandByte;\r
995 BOOLEAN MouseEnable;\r
996 EFI_INPUT_KEY Key;\r
997\r
998 MouseEnable = FALSE;\r
999 BiosKeyboardPrivate = BIOS_KEYBOARD_DEV_FROM_THIS (This);\r
1000\r
1001 //\r
1002 // 1\r
1003 // Report reset progress code\r
1004 //\r
55d4edfb 1005 REPORT_STATUS_CODE (\r
bcecde14 1006 EFI_PROGRESS_CODE,\r
55d4edfb 1007 EFI_PERIPHERAL_KEYBOARD | EFI_P_PC_RESET\r
bcecde14 1008 );\r
1009\r
1010 //\r
1011 // Report a Progress Code for clearing the keyboard buffer\r
1012 //\r
55d4edfb 1013 REPORT_STATUS_CODE (\r
bcecde14 1014 EFI_PROGRESS_CODE,\r
55d4edfb 1015 EFI_PERIPHERAL_KEYBOARD | EFI_P_KEYBOARD_PC_CLEAR_BUFFER\r
bcecde14 1016 );\r
1017\r
1018 //\r
1019 // 2\r
1020 // Raise TPL to avoid mouse operation impact\r
1021 //\r
1022 OldTpl = gBS->RaiseTPL (TPL_NOTIFY);\r
1023\r
1024 //\r
1025 //\r
1026 // Exhaust output buffer data\r
1027 //\r
1028 do {\r
1029 Status = BiosKeyboardReadKeyStroke (\r
1030 This,\r
1031 &Key\r
1032 );\r
1033 } while (!EFI_ERROR (Status));\r
1034 //\r
1035 // 3\r
1036 // check for KBC itself firstly for setted-up already or not by reading SYSF (bit2) of status register via 64H\r
1037 // if not skip step 4&5 and jump to step 6 to selftest KBC and report this\r
1038 // else go step 4\r
1039 //\r
4ccfd305 1040 if (!PcdGetBool (PcdFastPS2Detection)) {\r
1041 if ((KeyReadStatusRegister (BiosKeyboardPrivate) & KBC_STSREG_VIA64_SYSF) != 0) {\r
1042 //\r
1043 // 4\r
1044 // CheckMouseStatus to decide enable it later or not\r
1045 //\r
1046 //\r
1047 // Read the command byte of KBC\r
1048 //\r
1049 Status = KeyboardCommand (\r
1050 BiosKeyboardPrivate,\r
1051 KBC_CMDREG_VIA64_CMDBYTE_R\r
1052 );\r
1053 \r
1054 if (EFI_ERROR (Status)) {\r
1055 Status = EFI_DEVICE_ERROR;\r
1056 goto Exit;\r
1057 }\r
1058 \r
1059 Status = KeyboardRead (\r
1060 BiosKeyboardPrivate,\r
1061 &CommandByte\r
1062 );\r
1063 \r
1064 if (EFI_ERROR (Status)) {\r
1065 Status = EFI_DEVICE_ERROR;\r
1066 goto Exit;\r
1067 }\r
1068 //\r
1069 // Check mouse enabled or not before\r
1070 //\r
1071 if ((CommandByte & KB_CMMBYTE_DISABLE_AUX) != 0) {\r
1072 MouseEnable = FALSE;\r
1073 } else {\r
1074 MouseEnable = TRUE;\r
1075 }\r
1076 //\r
1077 // 5\r
1078 // disable mouse (via KBC) and Keyborad device\r
1079 //\r
1080 Status = KeyboardCommand (\r
1081 BiosKeyboardPrivate,\r
1082 KBC_CMDREG_VIA64_AUX_DISABLE\r
1083 );\r
1084 \r
1085 if (EFI_ERROR (Status)) {\r
1086 Status = EFI_DEVICE_ERROR;\r
1087 goto Exit;\r
1088 }\r
1089 \r
1090 Status = KeyboardCommand (\r
1091 BiosKeyboardPrivate,\r
1092 KBC_CMDREG_VIA64_KB_DISABLE\r
1093 );\r
1094 \r
1095 if (EFI_ERROR (Status)) {\r
1096 Status = EFI_DEVICE_ERROR;\r
1097 goto Exit;\r
1098 }\r
bcecde14 1099 } else {\r
4ccfd305 1100 //\r
1101 // 6\r
1102 // KBC Self Test\r
1103 //\r
1104 //\r
1105 // Report a Progress Code for performing a self test on the keyboard controller\r
1106 // \r
1107 REPORT_STATUS_CODE (\r
1108 EFI_PROGRESS_CODE,\r
1109 EFI_PERIPHERAL_KEYBOARD | EFI_P_KEYBOARD_PC_SELF_TEST\r
1110 );\r
1111 \r
1112 Status = KeyboardCommand (\r
1113 BiosKeyboardPrivate,\r
1114 KBC_CMDREG_VIA64_KBC_SLFTEST\r
1115 );\r
1116 if (EFI_ERROR (Status)) {\r
1117 Status = EFI_DEVICE_ERROR;\r
1118 goto Exit;\r
1119 }\r
1120 \r
1121 Status = KeyboardWaitForValue (\r
1122 BiosKeyboardPrivate,\r
1123 KBC_CMDECHO_KBCSLFTEST_OK,\r
1124 KEYBOARD_WAITFORVALUE_TIMEOUT\r
1125 );\r
1126 if (EFI_ERROR (Status)) {\r
1127 Status = EFI_DEVICE_ERROR;\r
1128 goto Exit;\r
1129 }\r
bcecde14 1130 }\r
1131 }\r
1132 //\r
1133 // 7\r
1134 // Disable Mouse interface, enable Keyboard interface and declare selftest success\r
1135 //\r
1136 // Mouse device will block keyboard interface before it be configured, so we should disable mouse first.\r
1137 //\r
1138 Status = KeyboardCommand (\r
1139 BiosKeyboardPrivate,\r
1140 KBC_CMDREG_VIA64_CMDBYTE_W\r
1141 );\r
1142\r
1143 if (EFI_ERROR (Status)) {\r
1144 Status = EFI_DEVICE_ERROR;\r
1145 goto Exit;\r
1146 }\r
1147\r
1148 //\r
1149 // Write 8042 Command Byte, set System Flag\r
1150 // While at the same time:\r
1151 // 1. disable mouse interface,\r
1152 // 2. enable kbd interface,\r
1153 // 3. enable PC/XT kbd translation mode\r
1154 // 4. enable mouse and kbd interrupts\r
1155 //\r
1156 //Command Byte bits:\r
1157 // 7: Reserved\r
1158 // 6: PC/XT translation mode\r
1159 // 5: Disable Auxiliary device interface\r
1160 // 4: Disable keyboard interface\r
1161 // 3: Reserved\r
1162 // 2: System Flag\r
1163 // 1: Enable Auxiliary device interrupt\r
1164 // 0: Enable Keyboard interrupt\r
1165 //\r
1166 CommandByte = 0;\r
1167 Status = KeyboardWrite (\r
1168 BiosKeyboardPrivate,\r
1169 (UINT8) ((CommandByte &\r
1170 (~KB_CMMBYTE_DISABLE_KB)) |\r
1171 KB_CMMBYTE_KSCAN2UNI_COV |\r
1172 KB_CMMBYTE_ENABLE_AUXINT |\r
1173 KB_CMMBYTE_ENABLE_KBINT |\r
1174 KB_CMMBYTE_SLFTEST_SUCC |\r
1175 KB_CMMBYTE_DISABLE_AUX)\r
1176 );\r
1177\r
1178 //\r
70d3fe9d
GL
1179 // For resetting keyboard is not mandatory before booting OS and sometimes keyboard responses very slow,\r
1180 // so we only do the real resetting for keyboard when user asks, and normally during booting an OS, it's skipped.\r
bcecde14 1181 // Call CheckKeyboardConnect() to check whether keyboard is connected, if it is not connected,\r
1182 // Real reset will not do.\r
1183 //\r
1184 if (ExtendedVerification && CheckKeyboardConnect (BiosKeyboardPrivate)) {\r
1185 //\r
1186 // 8\r
1187 // Send keyboard reset command then read ACK\r
1188 //\r
1189 Status = KeyboardWrite (\r
1190 BiosKeyboardPrivate,\r
1191 KBC_INPBUF_VIA60_KBRESET\r
1192 );\r
1193\r
1194 if (EFI_ERROR (Status)) {\r
1195 Status = EFI_DEVICE_ERROR;\r
1196 goto Exit;\r
1197 }\r
1198\r
1199 Status = KeyboardWaitForValue (\r
1200 BiosKeyboardPrivate,\r
1201 KBC_CMDECHO_ACK,\r
1202 KEYBOARD_WAITFORVALUE_TIMEOUT\r
1203 );\r
1204\r
1205 if (EFI_ERROR (Status)) {\r
1206 Status = EFI_DEVICE_ERROR;\r
1207 goto Exit;\r
1208 }\r
1209 //\r
1210 // 9\r
1211 // Wait for keyboard return test OK.\r
1212 //\r
1213 Status = KeyboardWaitForValue (\r
1214 BiosKeyboardPrivate,\r
1215 KBC_CMDECHO_BATTEST_OK,\r
1216 KEYBOARD_WAITFORVALUE_TIMEOUT\r
1217 );\r
1218\r
1219 if (EFI_ERROR (Status)) {\r
1220 Status = EFI_DEVICE_ERROR;\r
1221 goto Exit;\r
1222 }\r
1223 //\r
1224 // 10\r
1225 // set keyboard scan code set = 02 (standard configuration)\r
1226 //\r
1227 Status = KeyboardWrite (\r
1228 BiosKeyboardPrivate,\r
1229 KBC_INPBUF_VIA60_KBSCODE\r
1230 );\r
1231 if (EFI_ERROR (Status)) {\r
1232 Status = EFI_DEVICE_ERROR;\r
1233 goto Exit;\r
1234 }\r
1235\r
1236 Status = KeyboardWaitForValue (\r
1237 BiosKeyboardPrivate,\r
1238 KBC_CMDECHO_ACK,\r
1239 KEYBOARD_WAITFORVALUE_TIMEOUT\r
1240 );\r
1241\r
1242 if (EFI_ERROR (Status)) {\r
1243 Status = EFI_DEVICE_ERROR;\r
1244 goto Exit;\r
1245 }\r
1246\r
1247 Status = KeyboardWrite (\r
1248 BiosKeyboardPrivate,\r
1249 KBC_INPBUF_VIA60_SCODESET2\r
1250 );\r
1251 if (EFI_ERROR (Status)) {\r
1252 Status = EFI_DEVICE_ERROR;\r
1253 goto Exit;\r
1254 }\r
1255\r
1256 Status = KeyboardWaitForValue (\r
1257 BiosKeyboardPrivate,\r
1258 KBC_CMDECHO_ACK,\r
1259 KEYBOARD_WAITFORVALUE_TIMEOUT\r
1260 );\r
1261\r
1262 if (EFI_ERROR (Status)) {\r
1263 Status = EFI_DEVICE_ERROR;\r
1264 goto Exit;\r
1265 }\r
1266 //\r
1267 // 11\r
1268 // enable keyboard itself (not via KBC) by writing CMD F4 via 60H\r
1269 //\r
1270 Status = KeyboardWrite (\r
1271 BiosKeyboardPrivate,\r
1272 KBC_INPBUF_VIA60_KBEN\r
1273 );\r
1274 if (EFI_ERROR (Status)) {\r
1275 Status = EFI_DEVICE_ERROR;\r
1276 goto Exit;\r
1277 }\r
1278\r
1279 Status = KeyboardWaitForValue (\r
1280 BiosKeyboardPrivate,\r
1281 KBC_CMDECHO_ACK,\r
1282 KEYBOARD_WAITFORVALUE_TIMEOUT\r
1283 );\r
1284\r
1285 if (EFI_ERROR (Status)) {\r
1286 Status = EFI_DEVICE_ERROR;\r
1287 goto Exit;\r
1288 }\r
1289 //\r
1290 // 12\r
1291 // Additional validation, do it as follow:\r
1292 // 1). check for status register of PARE && TIM via 64H\r
1293 // 2). perform KB checking by writing ABh via 64H\r
1294 //\r
1295 if ((KeyReadStatusRegister (BiosKeyboardPrivate) & (KBC_STSREG_VIA64_PARE | KBC_STSREG_VIA64_TIM)) != 0) {\r
1296 Status = EFI_DEVICE_ERROR;\r
1297 goto Exit;\r
1298 }\r
1299\r
1300 Status = KeyboardCommand (\r
1301 BiosKeyboardPrivate,\r
1302 KBC_CMDREG_VIA64_KB_CKECK\r
1303 );\r
1304 if (EFI_ERROR (Status)) {\r
1305 Status = EFI_DEVICE_ERROR;\r
1306 goto Exit;\r
1307 }\r
1308\r
1309 Status = KeyboardWaitForValue (\r
1310 BiosKeyboardPrivate,\r
1311 KBC_CMDECHO_KBCHECK_OK,\r
1312 KEYBOARD_WAITFORVALUE_TIMEOUT\r
1313 );\r
1314\r
1315 if (EFI_ERROR (Status)) {\r
1316 Status = EFI_DEVICE_ERROR;\r
1317 goto Exit;\r
1318 }\r
1319 }\r
1320 //\r
1321 // 13\r
1322 // Done for validating keyboard. Enable keyboard (via KBC)\r
1323 // and recover the command byte to proper value\r
1324 //\r
4ccfd305 1325 if (!PcdGetBool (PcdFastPS2Detection)) {\r
1326 Status = KeyboardCommand (\r
1327 BiosKeyboardPrivate,\r
1328 KBC_CMDREG_VIA64_KB_ENABLE\r
1329 );\r
1330 \r
1331 if (EFI_ERROR (Status)) {\r
1332 Status = EFI_DEVICE_ERROR;\r
1333 goto Exit;\r
1334 }\r
bcecde14 1335 }\r
1336\r
1337 //\r
1338 // 14\r
1339 // conditionally enable mouse (via KBC)\r
1340 //\r
1341 if (MouseEnable) {\r
1342 Status = KeyboardCommand (\r
1343 BiosKeyboardPrivate,\r
1344 KBC_CMDREG_VIA64_AUX_ENABLE\r
1345 );\r
1346\r
1347 if (EFI_ERROR (Status)) {\r
1348 Status = EFI_DEVICE_ERROR;\r
1349\r
1350 }\r
1351 }\r
1352\r
1353Exit:\r
1354 //\r
1355 // 15\r
1356 // resume priority of task level\r
1357 //\r
1358 gBS->RestoreTPL (OldTpl);\r
1359\r
1360 return Status;\r
1361\r
1362}\r
1363\r
1364/**\r
1365 Read out the scan code of the key that has just been stroked.\r
1366\r
1367 @param This Pointer of simple text Protocol.\r
1368 @param Key Pointer for store the key that read out.\r
1369\r
1370 @retval EFI_SUCCESS The key is read out successfully.\r
1371 @retval other The key reading failed.\r
1372\r
1373**/\r
1374EFI_STATUS\r
1375EFIAPI\r
1376BiosKeyboardReadKeyStroke (\r
1377 IN EFI_SIMPLE_TEXT_INPUT_PROTOCOL *This,\r
1378 OUT EFI_INPUT_KEY *Key\r
1379 )\r
1380{\r
1381 BIOS_KEYBOARD_DEV *BiosKeyboardPrivate;\r
1382 EFI_STATUS Status;\r
1383 EFI_KEY_DATA KeyData;\r
1384\r
1385 BiosKeyboardPrivate = BIOS_KEYBOARD_DEV_FROM_THIS (This);\r
1386\r
1387 Status = KeyboardReadKeyStrokeWorker (BiosKeyboardPrivate, &KeyData);\r
1388 if (EFI_ERROR (Status)) {\r
1389 return Status;\r
1390 }\r
1391\r
9891f791
RN
1392 //\r
1393 // Convert the Ctrl+[a-z] to Ctrl+[1-26]\r
1394 //\r
1395 if ((KeyData.KeyState.KeyShiftState & (EFI_LEFT_CONTROL_PRESSED | EFI_RIGHT_CONTROL_PRESSED)) != 0) {\r
1396 if (KeyData.Key.UnicodeChar >= L'a' && KeyData.Key.UnicodeChar <= L'z') {\r
1397 KeyData.Key.UnicodeChar = (CHAR16) (KeyData.Key.UnicodeChar - L'a' + 1);\r
1398 } else if (KeyData.Key.UnicodeChar >= L'A' && KeyData.Key.UnicodeChar <= L'Z') {\r
1399 KeyData.Key.UnicodeChar = (CHAR16) (KeyData.Key.UnicodeChar - L'A' + 1);\r
1400 }\r
1401 }\r
1402\r
bcecde14 1403 CopyMem (Key, &KeyData.Key, sizeof (EFI_INPUT_KEY)); \r
1404\r
1405 return EFI_SUCCESS;\r
1406}\r
1407\r
1408/**\r
1409 Waiting on the keyboard event, if there's any key pressed by the user, signal the event\r
1410\r
1411 @param Event The event that be siganlled when any key has been stroked.\r
1412 @param Context Pointer of the protocol EFI_SIMPLE_TEXT_INPUT_PROTOCOL.\r
1413\r
1414**/\r
1415VOID\r
1416EFIAPI\r
1417BiosKeyboardWaitForKey (\r
1418 IN EFI_EVENT Event,\r
1419 IN VOID *Context\r
1420 )\r
1421{\r
1422 //\r
1423 // Stall 1ms to give a chance to let other driver interrupt this routine for their timer event.\r
1424 // Csm will be used to check whether there is a key pending, but the csm will disable all\r
1425 // interrupt before switch to compatibility16, which mean all the efiCompatibility timer\r
1426 // event will stop work during the compatibility16. And If a caller recursivly invoke this function,\r
1427 // e.g. UI setup or Shell, other drivers which are driven by timer event will have a bad performance during this period,\r
1428 // e.g. usb keyboard driver.\r
1429 // Add a stall period can greatly increate other driver performance during the WaitForKey is recursivly invoked.\r
1430 // 1ms delay will make little impact to the thunk keyboard driver, and user can not feel the delay at all when input.\r
1431 //\r
1432 gBS->Stall (1000);\r
1433 //\r
70d3fe9d 1434 // Use TimerEvent callback function to check whether there's any key pressed\r
bcecde14 1435 //\r
1436 BiosKeyboardTimerHandler (NULL, BIOS_KEYBOARD_DEV_FROM_THIS (Context));\r
1437\r
1438 if (!EFI_ERROR (BiosKeyboardCheckForKey (Context))) {\r
1439 gBS->SignalEvent (Event);\r
1440 }\r
1441}\r
1442\r
1443/**\r
1444 Check key buffer to get the key stroke status.\r
1445\r
1446 @param This Pointer of the protocol EFI_SIMPLE_TEXT_IN_PROTOCOL.\r
1447 \r
1448 @retval EFI_SUCCESS A key is being pressed now.\r
1449 @retval Other No key is now pressed.\r
1450\r
1451**/\r
1452EFI_STATUS\r
1453EFIAPI\r
1454BiosKeyboardCheckForKey (\r
1455 IN EFI_SIMPLE_TEXT_INPUT_PROTOCOL *This\r
1456 )\r
1457{\r
1458 BIOS_KEYBOARD_DEV *BiosKeyboardPrivate;\r
1459\r
1460 BiosKeyboardPrivate = BIOS_KEYBOARD_DEV_FROM_THIS (This);\r
1461\r
1462 return CheckQueue (&BiosKeyboardPrivate->Queue);\r
1463}\r
1464//\r
1465// Private worker functions\r
1466//\r
1467#define TABLE_END 0x0\r
1468\r
1469typedef struct _CONVERT_TABLE_ENTRY {\r
1470 UINT16 ScanCode;\r
1471 UINT16 EfiScanCode;\r
1472} CONVERT_TABLE_ENTRY;\r
1473\r
1474CONVERT_TABLE_ENTRY mConvertTable[] = {\r
1475 {\r
1476 0x47,\r
1477 SCAN_HOME\r
1478 },\r
1479 {\r
1480 0x48,\r
1481 SCAN_UP\r
1482 },\r
1483 {\r
1484 0x49,\r
1485 SCAN_PAGE_UP\r
1486 },\r
1487 {\r
1488 0x4b,\r
1489 SCAN_LEFT\r
1490 },\r
1491 {\r
1492 0x4d,\r
1493 SCAN_RIGHT\r
1494 },\r
1495 {\r
1496 0x4f,\r
1497 SCAN_END\r
1498 },\r
1499 {\r
1500 0x50,\r
1501 SCAN_DOWN\r
1502 },\r
1503 {\r
1504 0x51,\r
1505 SCAN_PAGE_DOWN\r
1506 },\r
1507 {\r
1508 0x52,\r
1509 SCAN_INSERT\r
1510 },\r
1511 {\r
1512 0x53,\r
1513 SCAN_DELETE\r
1514 },\r
1515 //\r
1516 // Function Keys are only valid if KeyChar == 0x00\r
1517 // This function does not require KeyChar to be 0x00\r
1518 //\r
1519 {\r
1520 0x3b,\r
1521 SCAN_F1\r
1522 },\r
1523 {\r
1524 0x3c,\r
1525 SCAN_F2\r
1526 },\r
1527 {\r
1528 0x3d,\r
1529 SCAN_F3\r
1530 },\r
1531 {\r
1532 0x3e,\r
1533 SCAN_F4\r
1534 },\r
1535 {\r
1536 0x3f,\r
1537 SCAN_F5\r
1538 },\r
1539 {\r
1540 0x40,\r
1541 SCAN_F6\r
1542 },\r
1543 {\r
1544 0x41,\r
1545 SCAN_F7\r
1546 },\r
1547 {\r
1548 0x42,\r
1549 SCAN_F8\r
1550 },\r
1551 {\r
1552 0x43,\r
1553 SCAN_F9\r
1554 },\r
1555 {\r
1556 0x44,\r
1557 SCAN_F10\r
1558 },\r
1559 {\r
1560 0x85,\r
1561 SCAN_F11\r
1562 },\r
1563 {\r
1564 0x86,\r
1565 SCAN_F12\r
1566 },\r
1567 //\r
1568 // Convert ALT + Fn keys\r
1569 //\r
1570 {\r
1571 0x68,\r
1572 SCAN_F1\r
1573 },\r
1574 {\r
1575 0x69,\r
1576 SCAN_F2\r
1577 },\r
1578 {\r
1579 0x6a,\r
1580 SCAN_F3\r
1581 },\r
1582 {\r
1583 0x6b,\r
1584 SCAN_F4\r
1585 },\r
1586 {\r
1587 0x6c,\r
1588 SCAN_F5\r
1589 },\r
1590 {\r
1591 0x6d,\r
1592 SCAN_F6\r
1593 },\r
1594 {\r
1595 0x6e,\r
1596 SCAN_F7\r
1597 },\r
1598 {\r
1599 0x6f,\r
1600 SCAN_F8\r
1601 },\r
1602 {\r
1603 0x70,\r
1604 SCAN_F9\r
1605 },\r
1606 {\r
1607 0x71,\r
1608 SCAN_F10\r
1609 },\r
1610 {\r
1611 TABLE_END,\r
1612 SCAN_NULL\r
1613 },\r
1614};\r
1615\r
1616/**\r
1617 Convert unicode combined with scan code of key to the counterpart of EFIScancode of it.\r
1618\r
1619 @param KeyChar Unicode of key.\r
1620 @param ScanCode Scan code of key.\r
1621\r
1622 @return The value of EFI Scancode for the key. \r
1623 @retval SCAN_NULL No corresponding value in the EFI convert table is found for the key.\r
1624\r
1625**/\r
1626UINT16\r
1627ConvertToEFIScanCode (\r
1628 IN CHAR16 KeyChar,\r
1629 IN UINT16 ScanCode\r
1630 )\r
1631{\r
1632 UINT16 EfiScanCode;\r
1633 UINT16 Index;\r
1634\r
1635 if (KeyChar == CHAR_ESC) {\r
1636 EfiScanCode = SCAN_ESC;\r
1637 } else if (KeyChar == 0x00 || KeyChar == 0xe0) {\r
1638 //\r
1639 // Movement & Function Keys\r
1640 //\r
1641 for (Index = 0; (Index < sizeof (mConvertTable) / sizeof (CONVERT_TABLE_ENTRY)) && (mConvertTable[Index].ScanCode != TABLE_END); Index += 1) {\r
1642 if (ScanCode == mConvertTable[Index].ScanCode) {\r
1643 return mConvertTable[Index].EfiScanCode;\r
1644 }\r
1645 }\r
1646 //\r
1647 // Reach Table end, return default value\r
1648 //\r
1649 return SCAN_NULL;\r
1650 } else {\r
1651 return SCAN_NULL;\r
1652 }\r
1653\r
1654 return EfiScanCode;\r
1655}\r
1656\r
1657/**\r
1658 Check whether there is Ps/2 Keyboard device in system by 0xF4 Keyboard Command\r
1659 If Keyboard receives 0xF4, it will respond with 'ACK'. If it doesn't respond, the device\r
1660 should not be in system. \r
1661\r
1662 @param BiosKeyboardPrivate Keyboard Private Data Struture\r
1663\r
1664 @retval TRUE Keyboard in System.\r
1665 @retval FALSE Keyboard not in System.\r
1666\r
1667**/\r
1668BOOLEAN\r
1669CheckKeyboardConnect (\r
1670 IN BIOS_KEYBOARD_DEV *BiosKeyboardPrivate\r
1671 )\r
1672{\r
1673 EFI_STATUS Status;\r
1674\r
1675 Status = EFI_SUCCESS;\r
1676 //\r
1677 // enable keyboard itself and wait for its ack\r
1678 // If can't receive ack, Keyboard should not be connected.\r
1679 //\r
4ccfd305 1680 if (!PcdGetBool (PcdFastPS2Detection)) {\r
1681 Status = KeyboardWrite (\r
1682 BiosKeyboardPrivate,\r
1683 KBC_INPBUF_VIA60_KBEN\r
1684 );\r
1685 if (EFI_ERROR (Status)) {\r
1686 DEBUG ((EFI_D_ERROR, "[KBD]CheckKeyboardConnect - Keyboard enable failed!\n"));\r
1687 REPORT_STATUS_CODE (\r
1688 EFI_ERROR_CODE | EFI_ERROR_MINOR,\r
1689 EFI_PERIPHERAL_KEYBOARD | EFI_P_EC_CONTROLLER_ERROR\r
1690 );\r
1691 return FALSE;\r
1692 }\r
bcecde14 1693\r
4ccfd305 1694 Status = KeyboardWaitForValue (\r
1695 BiosKeyboardPrivate,\r
1696 KBC_CMDECHO_ACK,\r
1697 KEYBOARD_WAITFORVALUE_TIMEOUT\r
1698 );\r
bcecde14 1699\r
4ccfd305 1700 if (EFI_ERROR (Status)) {\r
1701 DEBUG ((EFI_D_ERROR, "[KBD]CheckKeyboardConnect - Timeout!\n"));\r
1702 REPORT_STATUS_CODE (\r
1703 EFI_ERROR_CODE | EFI_ERROR_MINOR,\r
1704 EFI_PERIPHERAL_KEYBOARD | EFI_P_EC_CONTROLLER_ERROR\r
1705 );\r
1706 return FALSE;\r
1707 }\r
1708 return TRUE;\r
1709 } else {\r
1710 return TRUE;\r
bcecde14 1711 }\r
bcecde14 1712}\r
1713\r
1714/**\r
1715 Timer event handler: read a series of key stroke from 8042\r
1716 and put them into memory key buffer. \r
1717 It is registered as running under TPL_NOTIFY\r
1718 \r
1719 @param Event The timer event\r
1720 @param Context A BIOS_KEYBOARD_DEV pointer\r
1721\r
1722**/\r
1723VOID\r
1724EFIAPI\r
1725BiosKeyboardTimerHandler (\r
1726 IN EFI_EVENT Event,\r
1727 IN VOID *Context\r
1728 )\r
1729{\r
1730 EFI_TPL OldTpl;\r
1731 BIOS_KEYBOARD_DEV *BiosKeyboardPrivate;\r
1732 EFI_IA32_REGISTER_SET Regs;\r
1733 UINT8 KbFlag1; // 0040h:0017h - KEYBOARD - STATUS FLAGS 1\r
1734 UINT8 KbFlag2; // 0040h:0018h - KEYBOARD - STATUS FLAGS 2\r
1735 EFI_KEY_DATA KeyData;\r
1736 LIST_ENTRY *Link;\r
1737 BIOS_KEYBOARD_CONSOLE_IN_EX_NOTIFY *CurrentNotify;\r
1738\r
1739 BiosKeyboardPrivate = Context;\r
1740\r
1741 //\r
1742 // Enter critical section\r
1743 //\r
1744 OldTpl = gBS->RaiseTPL (TPL_NOTIFY);\r
1745\r
1746 //\r
1747 // if there is no key present, just return\r
1748 //\r
1749 if (BiosKeyboardPrivate->ExtendedKeyboard) {\r
1750 Regs.H.AH = 0x11;\r
1751 } else {\r
1752 Regs.H.AH = 0x01;\r
1753 }\r
1754\r
1755 BiosKeyboardPrivate->LegacyBios->Int86 (\r
1756 BiosKeyboardPrivate->LegacyBios,\r
1757 0x16,\r
1758 &Regs\r
1759 );\r
1760 if (Regs.X.Flags.ZF != 0) {\r
1761 gBS->RestoreTPL (OldTpl);\r
1762 return;\r
1763 } \r
1764\r
1765 //\r
1766 // Read the key\r
1767 //\r
1768 if (BiosKeyboardPrivate->ExtendedKeyboard) {\r
1769 Regs.H.AH = 0x10;\r
1770 } else {\r
1771 Regs.H.AH = 0x00;\r
1772 }\r
1773\r
1774 BiosKeyboardPrivate->LegacyBios->Int86 (\r
1775 BiosKeyboardPrivate->LegacyBios,\r
1776 0x16,\r
1777 &Regs\r
1778 );\r
1779\r
1780 KeyData.Key.ScanCode = (UINT16) Regs.H.AH;\r
1781 KeyData.Key.UnicodeChar = (UINT16) Regs.H.AL;\r
f6c014fb 1782 DEBUG ((\r
1783 EFI_D_INFO,\r
1784 "[KBD]INT16 returns EFI_INPUT_KEY.ScanCode - %x, EFI_INPUT_KEY.UnicodeChar - %x\n",\r
1785 KeyData.Key.ScanCode,\r
1786 KeyData.Key.UnicodeChar\r
1787 ));\r
1788 \r
bcecde14 1789 KeyData.KeyState.KeyShiftState = EFI_SHIFT_STATE_VALID;\r
1790 KeyData.KeyState.KeyToggleState = EFI_TOGGLE_STATE_VALID;\r
1791 //\r
1792 // Leagcy Bios use Int 9 which is IRQ1 interrupt handler to get keystroke scancode to KB buffer in BDA (BIOS DATE AREA), then \r
1793 // Int 16 depend KB buffer and some key bits in BDA to translate the scancode to ASCII code, and return both the scancode and ASCII \r
1794 // code to Int 16 caller. This translation process works well if the Int 9 could response user input in time. But in Tiano enviorment, the Int 9 \r
1795 // will be disabled after the thunk call finish, which means if user crazy input during int 9 being disabled, some keystrokes will be lost when \r
1796 // KB device own hardware buffer overflows. And if the lost keystroke code is CTRL or ALT or SHIFT release code, these function key flags bit \r
1797 // in BDA will not be updated. So the Int 16 will believe the CTRL or ALT or SHIFT is still pressed, and Int 16 will translate later scancode \r
70d3fe9d 1798 // to wrong ASCII code. We can increase the Thunk frequence to let Int 9 response in time, but this way will much hurt other drivers\r
bcecde14 1799 // performance, like USB.\r
1800 //\r
1801 // 1. If CTRL or ALT release code is missed, all later input keys will be translated to wrong ASCII codes which the Tiano cannot support. In \r
1802 // this case, the KB input seems fail to work, and user input is blocked. To solve the problem, we can help to clear the CTRL or ALT flag in BDA \r
1803 // after every Int 16 finish. Thus persist to press CTRL or ALT has same effection as only press one time. It is Ok, since user not often use the \r
1804 // CTRL and ALT.\r
1805 //\r
1806 // 2. If SHIFT release code is missed, all later lowercase input will become capital. This is ugly, but not block user input. If user press the lost \r
1807 // SHIFT again, the lowercase will come back to normal. Since user often use the SHIFT, it is not reasonable to help to clear the SHIFT flag in BDA,\r
1808 // which will let persist to press SHIFT has same effection as only press one time. \r
1809 //\r
1810 //0040h:0017h - KEYBOARD - STATUS FLAGS 1\r
1811 // 7 INSert active\r
1812 // 6 Caps Lock active\r
1813 // 5 Num Lock active\r
1814 // 4 Scroll Lock active\r
1815 // 3 either Alt pressed\r
1816 // 2 either Ctrl pressed\r
1817 // 1 Left Shift pressed\r
1818 // 0 Right Shift pressed\r
1819\r
1820\r
1821 //\r
1822 // Clear the CTRL and ALT BDA flag\r
1823 //\r
1824 KbFlag1 = *((UINT8 *) (UINTN) 0x417); // read the STATUS FLAGS 1\r
1825 KbFlag2 = *((UINT8 *) (UINTN) 0x418); // read STATUS FLAGS 2\r
1826\r
f6c014fb 1827 DEBUG_CODE (\r
1828 {\r
1829 if ((KbFlag1 & KB_CAPS_LOCK_BIT) == KB_CAPS_LOCK_BIT) {\r
1830 DEBUG ((EFI_D_INFO, "[KBD]Caps Lock Key is pressed.\n"));\r
1831 }\r
1832 if ((KbFlag1 & KB_NUM_LOCK_BIT) == KB_NUM_LOCK_BIT) {\r
1833 DEBUG ((EFI_D_INFO, "[KBD]Num Lock Key is pressed.\n"));\r
1834 }\r
1835 if ((KbFlag1 & KB_SCROLL_LOCK_BIT) == KB_SCROLL_LOCK_BIT) {\r
1836 DEBUG ((EFI_D_INFO, "[KBD]Scroll Lock Key is pressed.\n"));\r
1837 } \r
1838 if ((KbFlag1 & KB_ALT_PRESSED) == KB_ALT_PRESSED) {\r
1839 if ((KbFlag2 & KB_LEFT_ALT_PRESSED) == KB_LEFT_ALT_PRESSED) {\r
1840 DEBUG ((EFI_D_INFO, "[KBD]Left Alt Key is pressed.\n"));\r
1841 } else {\r
1842 DEBUG ((EFI_D_INFO, "[KBD]Right Alt Key is pressed.\n"));\r
1843 }\r
1844 } \r
1845 if ((KbFlag1 & KB_CTRL_PRESSED) == KB_CTRL_PRESSED) {\r
1846 if ((KbFlag2 & KB_LEFT_CTRL_PRESSED) == KB_LEFT_CTRL_PRESSED) {\r
1847 DEBUG ((EFI_D_INFO, "[KBD]Left Ctrl Key is pressed.\n"));\r
1848 } else {\r
1849 DEBUG ((EFI_D_INFO, "[KBD]Right Ctrl Key is pressed.\n"));\r
1850 }\r
1851 } \r
1852 if ((KbFlag1 & KB_LEFT_SHIFT_PRESSED) == KB_LEFT_SHIFT_PRESSED) {\r
1853 DEBUG ((EFI_D_INFO, "[KBD]Left Shift Key is pressed.\n"));\r
1854 }\r
1855 if ((KbFlag1 & KB_RIGHT_SHIFT_PRESSED) == KB_RIGHT_SHIFT_PRESSED) {\r
1856 DEBUG ((EFI_D_INFO, "[KBD]Right Shift Key is pressed.\n"));\r
1857 }\r
1858 }\r
1859 );\r
1860\r
bcecde14 1861 //\r
1862 // Record toggle state\r
1863 //\r
1864 if ((KbFlag1 & KB_CAPS_LOCK_BIT) == KB_CAPS_LOCK_BIT) {\r
1865 KeyData.KeyState.KeyToggleState |= EFI_CAPS_LOCK_ACTIVE;\r
1866 }\r
1867 if ((KbFlag1 & KB_NUM_LOCK_BIT) == KB_NUM_LOCK_BIT) {\r
1868 KeyData.KeyState.KeyToggleState |= EFI_NUM_LOCK_ACTIVE;\r
1869 }\r
1870 if ((KbFlag1 & KB_SCROLL_LOCK_BIT) == KB_SCROLL_LOCK_BIT) {\r
1871 KeyData.KeyState.KeyToggleState |= EFI_SCROLL_LOCK_ACTIVE;\r
1872 }\r
1873 //\r
1874 // Record shift state\r
1875 // BUGBUG: Need add Menu key and Left/Right Logo key state in the future\r
1876 // \r
1877 if ((KbFlag1 & KB_ALT_PRESSED) == KB_ALT_PRESSED) {\r
1878 KeyData.KeyState.KeyShiftState |= ((KbFlag2 & KB_LEFT_ALT_PRESSED) == KB_LEFT_ALT_PRESSED) ? EFI_LEFT_ALT_PRESSED : EFI_RIGHT_ALT_PRESSED;\r
1879 } \r
1880 if ((KbFlag1 & KB_CTRL_PRESSED) == KB_CTRL_PRESSED) {\r
1881 KeyData.KeyState.KeyShiftState |= ((KbFlag2 & KB_LEFT_CTRL_PRESSED) == KB_LEFT_CTRL_PRESSED) ? EFI_LEFT_CONTROL_PRESSED : EFI_RIGHT_CONTROL_PRESSED;\r
1882 } \r
1883 if ((KbFlag1 & KB_LEFT_SHIFT_PRESSED) == KB_LEFT_SHIFT_PRESSED) {\r
1884 KeyData.KeyState.KeyShiftState |= EFI_LEFT_SHIFT_PRESSED;\r
1885 }\r
1886 if ((KbFlag1 & KB_RIGHT_SHIFT_PRESSED) == KB_RIGHT_SHIFT_PRESSED) {\r
1887 KeyData.KeyState.KeyShiftState |= EFI_RIGHT_SHIFT_PRESSED;\r
1888 }\r
1889\r
1890 //\r
1891 // Clear left alt and left ctrl BDA flag\r
1892 //\r
1893 KbFlag2 &= ~(KB_LEFT_ALT_PRESSED | KB_LEFT_CTRL_PRESSED);\r
1894 *((UINT8 *) (UINTN) 0x418) = KbFlag2;\r
1895 KbFlag1 &= ~0x0C; \r
1896 *((UINT8 *) (UINTN) 0x417) = KbFlag1; \r
1897\r
1898 \r
1899 //\r
1900 // Output EFI input key and shift/toggle state\r
1901 //\r
1902 if (KeyData.Key.UnicodeChar == CHAR_NULL || KeyData.Key.UnicodeChar == CHAR_SCANCODE || KeyData.Key.UnicodeChar == CHAR_ESC) {\r
1903 KeyData.Key.ScanCode = ConvertToEFIScanCode (KeyData.Key.UnicodeChar, KeyData.Key.ScanCode);\r
1904 KeyData.Key.UnicodeChar = CHAR_NULL;\r
1905 } else {\r
1906 KeyData.Key.ScanCode = SCAN_NULL;\r
1907 }\r
1908\r
1909 //\r
1910 // CSM16 has converted the Ctrl+[a-z] to [1-26], converted it back.\r
1911 //\r
1912 if ((KeyData.KeyState.KeyShiftState & (EFI_LEFT_CONTROL_PRESSED | EFI_RIGHT_CONTROL_PRESSED)) != 0) {\r
1913 if (KeyData.Key.UnicodeChar >= 1 && KeyData.Key.UnicodeChar <= 26) {\r
1914 if (((KeyData.KeyState.KeyShiftState & (EFI_LEFT_SHIFT_PRESSED | EFI_RIGHT_SHIFT_PRESSED)) != 0) ==\r
1915 ((KeyData.KeyState.KeyToggleState & EFI_CAPS_LOCK_ACTIVE) != 0)\r
1916 ) {\r
1917 KeyData.Key.UnicodeChar = (UINT16) (KeyData.Key.UnicodeChar + L'a' - 1);\r
1918 } else {\r
1919 KeyData.Key.UnicodeChar = (UINT16) (KeyData.Key.UnicodeChar + L'A' - 1);\r
1920 }\r
1921 }\r
1922 }\r
1923\r
f6c014fb 1924 DEBUG ((\r
1925 EFI_D_INFO,\r
1926 "[KBD]Convert to EFI Scan Code, EFI_INPUT_KEY.ScanCode - %x, EFI_INPUT_KEY.UnicodeChar - %x\n",\r
1927 KeyData.Key.ScanCode,\r
1928 KeyData.Key.UnicodeChar\r
1929 ));\r
1930\r
bcecde14 1931 //\r
1932 // Need not return associated shift state if a class of printable characters that\r
1933 // are normally adjusted by shift modifiers.\r
1934 // e.g. Shift Key + 'f' key = 'F'; Shift Key + 'F' key = 'f'.\r
1935 //\r
1936 if ((KeyData.Key.UnicodeChar >= L'A' && KeyData.Key.UnicodeChar <= L'Z') ||\r
1937 (KeyData.Key.UnicodeChar >= L'a' && KeyData.Key.UnicodeChar <= L'z')\r
1938 ) {\r
f6c014fb 1939 DEBUG ((EFI_D_INFO, "[KBD]Shift key with a~z are pressed, remove shift state in EFI_KEY_STATE.\n"));\r
bcecde14 1940 KeyData.KeyState.KeyShiftState &= ~(EFI_LEFT_SHIFT_PRESSED | EFI_RIGHT_SHIFT_PRESSED);\r
1941 }\r
1942\r
1943 //\r
1944 // Invoke notification functions if exist\r
1945 //\r
1946 for (Link = BiosKeyboardPrivate->NotifyList.ForwardLink; Link != &BiosKeyboardPrivate->NotifyList; Link = Link->ForwardLink) {\r
1947 CurrentNotify = CR (\r
1948 Link, \r
1949 BIOS_KEYBOARD_CONSOLE_IN_EX_NOTIFY, \r
1950 NotifyEntry, \r
1951 BIOS_KEYBOARD_CONSOLE_IN_EX_NOTIFY_SIGNATURE\r
1952 );\r
1953 if (IsKeyRegistered (&CurrentNotify->KeyData, &KeyData)) { \r
1954 CurrentNotify->KeyNotificationFn (&KeyData);\r
1955 }\r
1956 }\r
1957\r
bcecde14 1958 Enqueue (&BiosKeyboardPrivate->Queue, &KeyData);\r
1959 //\r
1960 // Leave critical section and return\r
1961 //\r
1962 gBS->RestoreTPL (OldTpl);\r
1963\r
1964 return ; \r
1965}\r
1966\r
1967/**\r
1968 Free keyboard notify list.\r
1969\r
1970 @param ListHead The list head\r
1971\r
1972 @retval EFI_SUCCESS Free the notify list successfully\r
1973 @retval EFI_INVALID_PARAMETER ListHead is invalid.\r
1974\r
1975**/\r
1976EFI_STATUS\r
1977BiosKeyboardFreeNotifyList (\r
1978 IN OUT LIST_ENTRY *ListHead\r
1979 )\r
1980{\r
1981 BIOS_KEYBOARD_CONSOLE_IN_EX_NOTIFY *NotifyNode;\r
1982\r
1983 if (ListHead == NULL) {\r
1984 return EFI_INVALID_PARAMETER;\r
1985 }\r
1986 while (!IsListEmpty (ListHead)) {\r
1987 NotifyNode = CR (\r
1988 ListHead->ForwardLink, \r
1989 BIOS_KEYBOARD_CONSOLE_IN_EX_NOTIFY, \r
1990 NotifyEntry, \r
1991 BIOS_KEYBOARD_CONSOLE_IN_EX_NOTIFY_SIGNATURE\r
1992 );\r
1993 RemoveEntryList (ListHead->ForwardLink);\r
1994 gBS->FreePool (NotifyNode);\r
1995 }\r
1996\r
1997 return EFI_SUCCESS;\r
1998}\r
1999\r
2000/**\r
2001 Check if key is registered.\r
2002\r
2003 @param RegsiteredData A pointer to a buffer that is filled in with the keystroke \r
2004 state data for the key that was registered.\r
2005 @param InputData A pointer to a buffer that is filled in with the keystroke \r
2006 state data for the key that was pressed.\r
2007\r
2008 @retval TRUE Key be pressed matches a registered key.\r
2009 @retval FLASE Match failed. \r
2010 \r
2011**/\r
2012BOOLEAN\r
2013IsKeyRegistered (\r
2014 IN EFI_KEY_DATA *RegsiteredData,\r
2015 IN EFI_KEY_DATA *InputData\r
2016 )\r
2017{\r
2018 ASSERT (RegsiteredData != NULL && InputData != NULL);\r
2019 \r
2020 if ((RegsiteredData->Key.ScanCode != InputData->Key.ScanCode) ||\r
2021 (RegsiteredData->Key.UnicodeChar != InputData->Key.UnicodeChar)) {\r
2022 return FALSE; \r
2023 } \r
2024 \r
2025 //\r
2026 // Assume KeyShiftState/KeyToggleState = 0 in Registered key data means these state could be ignored.\r
2027 //\r
2028 if (RegsiteredData->KeyState.KeyShiftState != 0 &&\r
2029 RegsiteredData->KeyState.KeyShiftState != InputData->KeyState.KeyShiftState) {\r
2030 return FALSE; \r
2031 } \r
2032 if (RegsiteredData->KeyState.KeyToggleState != 0 &&\r
2033 RegsiteredData->KeyState.KeyToggleState != InputData->KeyState.KeyToggleState) {\r
2034 return FALSE; \r
2035 } \r
2036 \r
2037 return TRUE;\r
2038\r
2039}\r
2040\r
2041/**\r
2042 Waiting on the keyboard event, if there's any key pressed by the user, signal the event\r
2043\r
2044 @param Event The event that be siganlled when any key has been stroked.\r
2045 @param Context Pointer of the protocol EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.\r
2046 \r
2047**/\r
2048VOID\r
2049EFIAPI\r
2050BiosKeyboardWaitForKeyEx (\r
2051 IN EFI_EVENT Event,\r
2052 IN VOID *Context\r
2053 )\r
2054{ \r
2055 BIOS_KEYBOARD_DEV *BiosKeyboardPrivate;\r
2056 \r
2057 BiosKeyboardPrivate = TEXT_INPUT_EX_BIOS_KEYBOARD_DEV_FROM_THIS (Context); \r
2058 BiosKeyboardWaitForKey (Event, &BiosKeyboardPrivate->SimpleTextIn);\r
2059\r
2060}\r
2061\r
2062/**\r
2063 Reset the input device and optionaly run diagnostics\r
2064 \r
2065 @param This Protocol instance pointer.\r
2066 @param ExtendedVerification Driver may perform diagnostics on reset.\r
2067\r
2068 @retval EFI_SUCCESS The device was reset.\r
2069 @retval EFI_DEVICE_ERROR The device is not functioning properly and could \r
2070 not be reset.\r
2071\r
2072**/\r
2073EFI_STATUS\r
2074EFIAPI\r
2075BiosKeyboardResetEx (\r
2076 IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *This,\r
2077 IN BOOLEAN ExtendedVerification\r
2078 )\r
2079{\r
2080 BIOS_KEYBOARD_DEV *BiosKeyboardPrivate;\r
2081 EFI_STATUS Status;\r
2082 EFI_TPL OldTpl;\r
2083 \r
2084 BiosKeyboardPrivate = TEXT_INPUT_EX_BIOS_KEYBOARD_DEV_FROM_THIS (This); \r
2085\r
2086 Status = BiosKeyboardPrivate->SimpleTextIn.Reset (\r
2087 &BiosKeyboardPrivate->SimpleTextIn, \r
2088 ExtendedVerification\r
2089 );\r
2090 if (EFI_ERROR (Status)) {\r
2091 return EFI_DEVICE_ERROR;\r
2092 }\r
2093\r
2094 OldTpl = gBS->RaiseTPL (TPL_NOTIFY);\r
2095\r
2096 gBS->RestoreTPL (OldTpl);\r
2097 \r
2098 return EFI_SUCCESS;\r
2099\r
2100}\r
2101\r
2102/**\r
2103 Reads the next keystroke from the input device. The WaitForKey Event can \r
2104 be used to test for existance of a keystroke via WaitForEvent () call.\r
2105\r
2106 @param This Protocol instance pointer.\r
2107 @param KeyData A pointer to a buffer that is filled in with the keystroke \r
2108 state data for the key that was pressed.\r
2109 \r
2110 @retval EFI_SUCCESS The keystroke information was returned.\r
2111 @retval EFI_NOT_READY There was no keystroke data availiable.\r
2112 @retval EFI_DEVICE_ERROR The keystroke information was not returned due to \r
2113 hardware errors.\r
2114 @retval EFI_INVALID_PARAMETER KeyData is NULL. \r
2115 \r
2116**/\r
2117EFI_STATUS\r
2118EFIAPI\r
2119BiosKeyboardReadKeyStrokeEx (\r
2120 IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *This,\r
2121 OUT EFI_KEY_DATA *KeyData\r
2122 )\r
2123{\r
2124 BIOS_KEYBOARD_DEV *BiosKeyboardPrivate;\r
2125\r
2126 if (KeyData == NULL) {\r
2127 return EFI_INVALID_PARAMETER;\r
2128 }\r
2129 \r
2130 BiosKeyboardPrivate = TEXT_INPUT_EX_BIOS_KEYBOARD_DEV_FROM_THIS (This);\r
2131\r
2132 return KeyboardReadKeyStrokeWorker (BiosKeyboardPrivate, KeyData);\r
2133 \r
2134}\r
2135\r
2136/**\r
2137 Set certain state for the input device.\r
2138\r
2139 @param This Protocol instance pointer.\r
2140 @param KeyToggleState A pointer to the EFI_KEY_TOGGLE_STATE to set the \r
2141 state for the input device.\r
2142\r
2143 @retval EFI_SUCCESS The device state was set successfully.\r
2144 @retval EFI_DEVICE_ERROR The device is not functioning correctly and could \r
2145 not have the setting adjusted.\r
2146 @retval EFI_UNSUPPORTED The device does not have the ability to set its state.\r
2147 @retval EFI_INVALID_PARAMETER KeyToggleState is NULL. \r
2148\r
2149**/ \r
2150EFI_STATUS\r
2151EFIAPI\r
2152BiosKeyboardSetState (\r
2153 IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *This,\r
2154 IN EFI_KEY_TOGGLE_STATE *KeyToggleState\r
2155 )\r
2156{\r
2157 EFI_STATUS Status;\r
2158 BIOS_KEYBOARD_DEV *BiosKeyboardPrivate;\r
2159 EFI_TPL OldTpl;\r
2160 EFI_LEGACY_BIOS_PROTOCOL *LegacyBios;\r
2161 UINT8 Command;\r
2162\r
2163 if (KeyToggleState == NULL) {\r
2164 return EFI_INVALID_PARAMETER;\r
2165 }\r
2166\r
5829afe3 2167 //\r
2168 // Thunk keyboard driver doesn't support partial keystroke.\r
2169 //\r
2170 if ((*KeyToggleState & EFI_TOGGLE_STATE_VALID) != EFI_TOGGLE_STATE_VALID ||\r
2171 (*KeyToggleState & EFI_KEY_STATE_EXPOSED) == EFI_KEY_STATE_EXPOSED\r
2172 ) {\r
bcecde14 2173 return EFI_UNSUPPORTED;\r
2174 }\r
2175\r
2176 BiosKeyboardPrivate = TEXT_INPUT_EX_BIOS_KEYBOARD_DEV_FROM_THIS (This);\r
2177 //\r
2178 // See if the Legacy BIOS Protocol is available\r
2179 //\r
2180 Status = gBS->LocateProtocol (\r
2181 &gEfiLegacyBiosProtocolGuid,\r
2182 NULL,\r
2183 (VOID **) &LegacyBios\r
2184 );\r
2185\r
2186 ASSERT_EFI_ERROR (Status);\r
2187 //\r
2188 // Enter critical section\r
2189 //\r
2190 OldTpl = gBS->RaiseTPL (TPL_NOTIFY);\r
2191\r
2192 Command = 0;\r
2193 if ((*KeyToggleState & EFI_CAPS_LOCK_ACTIVE) == EFI_CAPS_LOCK_ACTIVE) {\r
2194 Command |= 4;\r
2195 }\r
2196 if ((*KeyToggleState & EFI_NUM_LOCK_ACTIVE) == EFI_NUM_LOCK_ACTIVE) {\r
2197 Command |= 2;\r
2198 }\r
2199 if ((*KeyToggleState & EFI_SCROLL_LOCK_ACTIVE) == EFI_SCROLL_LOCK_ACTIVE) {\r
2200 Command |= 1;\r
2201 }\r
2202\r
2203 Status = KeyboardWrite (BiosKeyboardPrivate, 0xed);\r
2204 if (EFI_ERROR (Status)) {\r
0927c81d
DC
2205 Status = EFI_DEVICE_ERROR;\r
2206 goto Exit;\r
bcecde14 2207 } \r
2208 Status = KeyboardWaitForValue (BiosKeyboardPrivate, 0xfa, KEYBOARD_WAITFORVALUE_TIMEOUT);\r
2209 if (EFI_ERROR (Status)) {\r
0927c81d
DC
2210 Status = EFI_DEVICE_ERROR;\r
2211 goto Exit;\r
bcecde14 2212 }\r
2213 Status = KeyboardWrite (BiosKeyboardPrivate, Command);\r
2214 if (EFI_ERROR (Status)) {\r
0927c81d
DC
2215 Status = EFI_DEVICE_ERROR;\r
2216 goto Exit;\r
bcecde14 2217 } \r
2218 //\r
2219 // Call Legacy BIOS Protocol to set whatever is necessary\r
2220 //\r
2221 LegacyBios->UpdateKeyboardLedStatus (LegacyBios, Command);\r
2222\r
2223 Status = EFI_SUCCESS;\r
2224\r
0927c81d 2225Exit:\r
bcecde14 2226 //\r
2227 // Leave critical section and return\r
2228 //\r
2229 gBS->RestoreTPL (OldTpl);\r
2230\r
2231 return Status;\r
2232\r
2233}\r
2234\r
2235/**\r
2236 Register a notification function for a particular keystroke for the input device.\r
2237\r
2238 @param This Protocol instance pointer.\r
2239 @param KeyData A pointer to a buffer that is filled in with the keystroke \r
2240 information data for the key that was pressed.\r
2241 @param KeyNotificationFunction Points to the function to be called when the key \r
2242 sequence is typed specified by KeyData. \r
2243 @param NotifyHandle Points to the unique handle assigned to the registered notification. \r
2244\r
2245 \r
2246 @retval EFI_SUCCESS The notification function was registered successfully.\r
2247 @retval EFI_OUT_OF_RESOURCES Unable to allocate resources for necesssary data structures.\r
2248 @retval EFI_INVALID_PARAMETER KeyData or NotifyHandle is NULL.\r
2249 \r
2250**/ \r
2251EFI_STATUS\r
2252EFIAPI\r
2253BiosKeyboardRegisterKeyNotify (\r
2254 IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *This,\r
2255 IN EFI_KEY_DATA *KeyData,\r
2256 IN EFI_KEY_NOTIFY_FUNCTION KeyNotificationFunction,\r
402e4a9d 2257 OUT VOID **NotifyHandle\r
bcecde14 2258 )\r
2259{\r
2260 EFI_STATUS Status;\r
2261 BIOS_KEYBOARD_DEV *BiosKeyboardPrivate;\r
2262 EFI_TPL OldTpl;\r
2263 BIOS_KEYBOARD_CONSOLE_IN_EX_NOTIFY *NewNotify;\r
2264 LIST_ENTRY *Link;\r
2265 BIOS_KEYBOARD_CONSOLE_IN_EX_NOTIFY *CurrentNotify; \r
2266\r
2267 if (KeyData == NULL || NotifyHandle == NULL || KeyNotificationFunction == NULL) {\r
2268 return EFI_INVALID_PARAMETER;\r
2269 }\r
2270\r
2271 BiosKeyboardPrivate = TEXT_INPUT_EX_BIOS_KEYBOARD_DEV_FROM_THIS (This);\r
2272\r
2273 //\r
2274 // Enter critical section\r
2275 //\r
2276 OldTpl = gBS->RaiseTPL (TPL_NOTIFY);\r
2277\r
2278 //\r
2279 // Return EFI_SUCCESS if the (KeyData, NotificationFunction) is already registered.\r
2280 //\r
2281 for (Link = BiosKeyboardPrivate->NotifyList.ForwardLink; Link != &BiosKeyboardPrivate->NotifyList; Link = Link->ForwardLink) {\r
2282 CurrentNotify = CR (\r
2283 Link, \r
2284 BIOS_KEYBOARD_CONSOLE_IN_EX_NOTIFY, \r
2285 NotifyEntry, \r
2286 BIOS_KEYBOARD_CONSOLE_IN_EX_NOTIFY_SIGNATURE\r
2287 );\r
2288 if (IsKeyRegistered (&CurrentNotify->KeyData, KeyData)) { \r
2289 if (CurrentNotify->KeyNotificationFn == KeyNotificationFunction) {\r
402e4a9d 2290 *NotifyHandle = CurrentNotify;\r
bcecde14 2291 Status = EFI_SUCCESS;\r
2292 goto Exit;\r
2293 }\r
2294 } \r
2295 }\r
2296\r
2297 //\r
2298 // Allocate resource to save the notification function\r
2299 //\r
2300 \r
2301 NewNotify = (BIOS_KEYBOARD_CONSOLE_IN_EX_NOTIFY *) AllocateZeroPool (sizeof (BIOS_KEYBOARD_CONSOLE_IN_EX_NOTIFY));\r
2302 if (NewNotify == NULL) {\r
2303 Status = EFI_OUT_OF_RESOURCES;\r
2304 goto Exit;\r
2305 }\r
2306\r
2307 NewNotify->Signature = BIOS_KEYBOARD_CONSOLE_IN_EX_NOTIFY_SIGNATURE;\r
2308 NewNotify->KeyNotificationFn = KeyNotificationFunction;\r
bcecde14 2309 CopyMem (&NewNotify->KeyData, KeyData, sizeof (EFI_KEY_DATA));\r
2310 InsertTailList (&BiosKeyboardPrivate->NotifyList, &NewNotify->NotifyEntry);\r
2311\r
402e4a9d 2312 *NotifyHandle = NewNotify;\r
bcecde14 2313 Status = EFI_SUCCESS;\r
2314 \r
2315Exit:\r
2316 //\r
2317 // Leave critical section and return\r
2318 //\r
2319 gBS->RestoreTPL (OldTpl);\r
2320 return Status; \r
2321}\r
2322\r
2323/**\r
2324 Remove a registered notification function from a particular keystroke.\r
2325\r
2326 @param This Protocol instance pointer. \r
2327 @param NotificationHandle The handle of the notification function being unregistered.\r
2328 \r
2329 @retval EFI_SUCCESS The notification function was unregistered successfully.\r
2330 @retval EFI_INVALID_PARAMETER The NotificationHandle is invalid.\r
2331 \r
2332**/ \r
2333EFI_STATUS\r
2334EFIAPI\r
2335BiosKeyboardUnregisterKeyNotify (\r
2336 IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *This,\r
402e4a9d 2337 IN VOID *NotificationHandle\r
bcecde14 2338 )\r
2339{\r
2340 EFI_STATUS Status;\r
2341 BIOS_KEYBOARD_DEV *BiosKeyboardPrivate;\r
2342 EFI_TPL OldTpl;\r
2343 LIST_ENTRY *Link;\r
2344 BIOS_KEYBOARD_CONSOLE_IN_EX_NOTIFY *CurrentNotify;\r
2345\r
2346 //\r
2347 // Check incoming notification handle\r
2348 //\r
2349 if (NotificationHandle == NULL) {\r
2350 return EFI_INVALID_PARAMETER;\r
2351 }\r
2352\r
2353 if (((BIOS_KEYBOARD_CONSOLE_IN_EX_NOTIFY *) NotificationHandle)->Signature != BIOS_KEYBOARD_CONSOLE_IN_EX_NOTIFY_SIGNATURE) {\r
2354 return EFI_INVALID_PARAMETER;\r
2355 } \r
2356 \r
2357 BiosKeyboardPrivate = TEXT_INPUT_EX_BIOS_KEYBOARD_DEV_FROM_THIS (This);\r
2358 \r
2359 //\r
2360 // Enter critical section\r
2361 //\r
402e4a9d 2362 OldTpl = gBS->RaiseTPL (TPL_NOTIFY);\r
bcecde14 2363\r
2364 for (Link = BiosKeyboardPrivate->NotifyList.ForwardLink; Link != &BiosKeyboardPrivate->NotifyList; Link = Link->ForwardLink) {\r
2365 CurrentNotify = CR (\r
2366 Link, \r
2367 BIOS_KEYBOARD_CONSOLE_IN_EX_NOTIFY, \r
2368 NotifyEntry, \r
2369 BIOS_KEYBOARD_CONSOLE_IN_EX_NOTIFY_SIGNATURE\r
2370 ); \r
402e4a9d 2371 if (CurrentNotify == NotificationHandle) {\r
bcecde14 2372 //\r
2373 // Remove the notification function from NotifyList and free resources\r
2374 //\r
2375 RemoveEntryList (&CurrentNotify->NotifyEntry); \r
2376\r
2377 Status = EFI_SUCCESS;\r
2378 goto Exit;\r
2379 }\r
2380 }\r
2381 \r
2382 //\r
2383 // Can not find the specified Notification Handle\r
2384 //\r
2385 Status = EFI_INVALID_PARAMETER;\r
2386\r
2387Exit:\r
2388 //\r
2389 // Leave critical section and return\r
2390 //\r
2391 gBS->RestoreTPL (OldTpl);\r
2392 return Status;\r
2393}\r
2394\r
2395/**\r
2396 The user Entry Point for module BiosKeyboard. The user code starts with this function.\r
2397\r
2398 @param[in] ImageHandle The firmware allocated handle for the EFI image.\r
2399 @param[in] SystemTable A pointer to the EFI System Table.\r
2400\r
2401 @retval EFI_SUCCESS The entry point is executed successfully.\r
2402 @retval other Some error occurs when executing this entry point.\r
2403\r
2404**/\r
2405EFI_STATUS\r
2406EFIAPI\r
2407InitializeBiosKeyboard(\r
2408 IN EFI_HANDLE ImageHandle,\r
2409 IN EFI_SYSTEM_TABLE *SystemTable\r
2410 )\r
2411{\r
2412 EFI_STATUS Status;\r
2413\r
2414 //\r
2415 // Install driver model protocol(s).\r
2416 //\r
2417 Status = EfiLibInstallDriverBindingComponentName2 (\r
2418 ImageHandle,\r
2419 SystemTable,\r
2420 &gBiosKeyboardDriverBinding,\r
2421 ImageHandle,\r
2422 &gBiosKeyboardComponentName,\r
2423 &gBiosKeyboardComponentName2\r
2424 );\r
2425 ASSERT_EFI_ERROR (Status);\r
2426\r
2427 return Status;\r
2428}\r