]> git.proxmox.com Git - mirror_edk2.git/blame - IntelFrameworkModulePkg/Csm/BiosThunk/KeyboardDxe/BiosKeyboard.c
Consume the UNDI_CONFIG_TABLE defined in MdePkg and remove the local definition of...
[mirror_edk2.git] / IntelFrameworkModulePkg / Csm / BiosThunk / KeyboardDxe / BiosKeyboard.c
CommitLineData
bcecde14 1/** @file\r
2 ConsoleOut Routines that speak VGA.\r
3\r
4Copyright (c) 2006 - 2011, Intel Corporation. All rights reserved.<BR>\r
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
937 // Use TimerEvent callback funciton to check whether there's any key pressed\r
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
981 @retval EFI_DEVICE_ERROR Errors occurred during reseting keyboard.\r
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
1040 if ((KeyReadStatusRegister (BiosKeyboardPrivate) & KBC_STSREG_VIA64_SYSF) != 0) {\r
1041 //\r
1042 // 4\r
1043 // CheckMouseStatus to decide enable it later or not\r
1044 //\r
1045 //\r
1046 // Read the command byte of KBC\r
1047 //\r
1048 Status = KeyboardCommand (\r
1049 BiosKeyboardPrivate,\r
1050 KBC_CMDREG_VIA64_CMDBYTE_R\r
1051 );\r
1052\r
1053 if (EFI_ERROR (Status)) {\r
1054 Status = EFI_DEVICE_ERROR;\r
1055 goto Exit;\r
1056 }\r
1057\r
1058 Status = KeyboardRead (\r
1059 BiosKeyboardPrivate,\r
1060 &CommandByte\r
1061 );\r
1062\r
1063 if (EFI_ERROR (Status)) {\r
1064 Status = EFI_DEVICE_ERROR;\r
1065 goto Exit;\r
1066 }\r
1067 //\r
1068 // Check mouse enabled or not before\r
1069 //\r
1070 if ((CommandByte & KB_CMMBYTE_DISABLE_AUX) != 0) {\r
1071 MouseEnable = FALSE;\r
1072 } else {\r
1073 MouseEnable = TRUE;\r
1074 }\r
1075 //\r
1076 // 5\r
1077 // disable mouse (via KBC) and Keyborad device\r
1078 //\r
1079 Status = KeyboardCommand (\r
1080 BiosKeyboardPrivate,\r
1081 KBC_CMDREG_VIA64_AUX_DISABLE\r
1082 );\r
1083\r
1084 if (EFI_ERROR (Status)) {\r
1085 Status = EFI_DEVICE_ERROR;\r
1086 goto Exit;\r
1087 }\r
1088\r
1089 Status = KeyboardCommand (\r
1090 BiosKeyboardPrivate,\r
1091 KBC_CMDREG_VIA64_KB_DISABLE\r
1092 );\r
1093\r
1094 if (EFI_ERROR (Status)) {\r
1095 Status = EFI_DEVICE_ERROR;\r
1096 goto Exit;\r
1097 }\r
1098\r
1099 } else {\r
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
55d4edfb 1107 REPORT_STATUS_CODE (\r
bcecde14 1108 EFI_PROGRESS_CODE,\r
55d4edfb 1109 EFI_PERIPHERAL_KEYBOARD | EFI_P_KEYBOARD_PC_SELF_TEST\r
bcecde14 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
1130 }\r
1131 //\r
1132 // 7\r
1133 // Disable Mouse interface, enable Keyboard interface and declare selftest success\r
1134 //\r
1135 // Mouse device will block keyboard interface before it be configured, so we should disable mouse first.\r
1136 //\r
1137 Status = KeyboardCommand (\r
1138 BiosKeyboardPrivate,\r
1139 KBC_CMDREG_VIA64_CMDBYTE_W\r
1140 );\r
1141\r
1142 if (EFI_ERROR (Status)) {\r
1143 Status = EFI_DEVICE_ERROR;\r
1144 goto Exit;\r
1145 }\r
1146\r
1147 //\r
1148 // Write 8042 Command Byte, set System Flag\r
1149 // While at the same time:\r
1150 // 1. disable mouse interface,\r
1151 // 2. enable kbd interface,\r
1152 // 3. enable PC/XT kbd translation mode\r
1153 // 4. enable mouse and kbd interrupts\r
1154 //\r
1155 //Command Byte bits:\r
1156 // 7: Reserved\r
1157 // 6: PC/XT translation mode\r
1158 // 5: Disable Auxiliary device interface\r
1159 // 4: Disable keyboard interface\r
1160 // 3: Reserved\r
1161 // 2: System Flag\r
1162 // 1: Enable Auxiliary device interrupt\r
1163 // 0: Enable Keyboard interrupt\r
1164 //\r
1165 CommandByte = 0;\r
1166 Status = KeyboardWrite (\r
1167 BiosKeyboardPrivate,\r
1168 (UINT8) ((CommandByte &\r
1169 (~KB_CMMBYTE_DISABLE_KB)) |\r
1170 KB_CMMBYTE_KSCAN2UNI_COV |\r
1171 KB_CMMBYTE_ENABLE_AUXINT |\r
1172 KB_CMMBYTE_ENABLE_KBINT |\r
1173 KB_CMMBYTE_SLFTEST_SUCC |\r
1174 KB_CMMBYTE_DISABLE_AUX)\r
1175 );\r
1176\r
1177 //\r
1178 // For reseting keyboard is not mandatory before booting OS and sometimes keyboard responses very slow,\r
1179 // so we only do the real reseting for keyboard when user asks, and normally during booting an OS, it's skipped.\r
1180 // Call CheckKeyboardConnect() to check whether keyboard is connected, if it is not connected,\r
1181 // Real reset will not do.\r
1182 //\r
1183 if (ExtendedVerification && CheckKeyboardConnect (BiosKeyboardPrivate)) {\r
1184 //\r
1185 // 8\r
1186 // Send keyboard reset command then read ACK\r
1187 //\r
1188 Status = KeyboardWrite (\r
1189 BiosKeyboardPrivate,\r
1190 KBC_INPBUF_VIA60_KBRESET\r
1191 );\r
1192\r
1193 if (EFI_ERROR (Status)) {\r
1194 Status = EFI_DEVICE_ERROR;\r
1195 goto Exit;\r
1196 }\r
1197\r
1198 Status = KeyboardWaitForValue (\r
1199 BiosKeyboardPrivate,\r
1200 KBC_CMDECHO_ACK,\r
1201 KEYBOARD_WAITFORVALUE_TIMEOUT\r
1202 );\r
1203\r
1204 if (EFI_ERROR (Status)) {\r
1205 Status = EFI_DEVICE_ERROR;\r
1206 goto Exit;\r
1207 }\r
1208 //\r
1209 // 9\r
1210 // Wait for keyboard return test OK.\r
1211 //\r
1212 Status = KeyboardWaitForValue (\r
1213 BiosKeyboardPrivate,\r
1214 KBC_CMDECHO_BATTEST_OK,\r
1215 KEYBOARD_WAITFORVALUE_TIMEOUT\r
1216 );\r
1217\r
1218 if (EFI_ERROR (Status)) {\r
1219 Status = EFI_DEVICE_ERROR;\r
1220 goto Exit;\r
1221 }\r
1222 //\r
1223 // 10\r
1224 // set keyboard scan code set = 02 (standard configuration)\r
1225 //\r
1226 Status = KeyboardWrite (\r
1227 BiosKeyboardPrivate,\r
1228 KBC_INPBUF_VIA60_KBSCODE\r
1229 );\r
1230 if (EFI_ERROR (Status)) {\r
1231 Status = EFI_DEVICE_ERROR;\r
1232 goto Exit;\r
1233 }\r
1234\r
1235 Status = KeyboardWaitForValue (\r
1236 BiosKeyboardPrivate,\r
1237 KBC_CMDECHO_ACK,\r
1238 KEYBOARD_WAITFORVALUE_TIMEOUT\r
1239 );\r
1240\r
1241 if (EFI_ERROR (Status)) {\r
1242 Status = EFI_DEVICE_ERROR;\r
1243 goto Exit;\r
1244 }\r
1245\r
1246 Status = KeyboardWrite (\r
1247 BiosKeyboardPrivate,\r
1248 KBC_INPBUF_VIA60_SCODESET2\r
1249 );\r
1250 if (EFI_ERROR (Status)) {\r
1251 Status = EFI_DEVICE_ERROR;\r
1252 goto Exit;\r
1253 }\r
1254\r
1255 Status = KeyboardWaitForValue (\r
1256 BiosKeyboardPrivate,\r
1257 KBC_CMDECHO_ACK,\r
1258 KEYBOARD_WAITFORVALUE_TIMEOUT\r
1259 );\r
1260\r
1261 if (EFI_ERROR (Status)) {\r
1262 Status = EFI_DEVICE_ERROR;\r
1263 goto Exit;\r
1264 }\r
1265 //\r
1266 // 11\r
1267 // enable keyboard itself (not via KBC) by writing CMD F4 via 60H\r
1268 //\r
1269 Status = KeyboardWrite (\r
1270 BiosKeyboardPrivate,\r
1271 KBC_INPBUF_VIA60_KBEN\r
1272 );\r
1273 if (EFI_ERROR (Status)) {\r
1274 Status = EFI_DEVICE_ERROR;\r
1275 goto Exit;\r
1276 }\r
1277\r
1278 Status = KeyboardWaitForValue (\r
1279 BiosKeyboardPrivate,\r
1280 KBC_CMDECHO_ACK,\r
1281 KEYBOARD_WAITFORVALUE_TIMEOUT\r
1282 );\r
1283\r
1284 if (EFI_ERROR (Status)) {\r
1285 Status = EFI_DEVICE_ERROR;\r
1286 goto Exit;\r
1287 }\r
1288 //\r
1289 // 12\r
1290 // Additional validation, do it as follow:\r
1291 // 1). check for status register of PARE && TIM via 64H\r
1292 // 2). perform KB checking by writing ABh via 64H\r
1293 //\r
1294 if ((KeyReadStatusRegister (BiosKeyboardPrivate) & (KBC_STSREG_VIA64_PARE | KBC_STSREG_VIA64_TIM)) != 0) {\r
1295 Status = EFI_DEVICE_ERROR;\r
1296 goto Exit;\r
1297 }\r
1298\r
1299 Status = KeyboardCommand (\r
1300 BiosKeyboardPrivate,\r
1301 KBC_CMDREG_VIA64_KB_CKECK\r
1302 );\r
1303 if (EFI_ERROR (Status)) {\r
1304 Status = EFI_DEVICE_ERROR;\r
1305 goto Exit;\r
1306 }\r
1307\r
1308 Status = KeyboardWaitForValue (\r
1309 BiosKeyboardPrivate,\r
1310 KBC_CMDECHO_KBCHECK_OK,\r
1311 KEYBOARD_WAITFORVALUE_TIMEOUT\r
1312 );\r
1313\r
1314 if (EFI_ERROR (Status)) {\r
1315 Status = EFI_DEVICE_ERROR;\r
1316 goto Exit;\r
1317 }\r
1318 }\r
1319 //\r
1320 // 13\r
1321 // Done for validating keyboard. Enable keyboard (via KBC)\r
1322 // and recover the command byte to proper value\r
1323 //\r
1324 Status = KeyboardCommand (\r
1325 BiosKeyboardPrivate,\r
1326 KBC_CMDREG_VIA64_KB_ENABLE\r
1327 );\r
1328\r
1329 if (EFI_ERROR (Status)) {\r
1330 Status = EFI_DEVICE_ERROR;\r
1331 goto Exit;\r
1332 }\r
1333\r
1334 //\r
1335 // 14\r
1336 // conditionally enable mouse (via KBC)\r
1337 //\r
1338 if (MouseEnable) {\r
1339 Status = KeyboardCommand (\r
1340 BiosKeyboardPrivate,\r
1341 KBC_CMDREG_VIA64_AUX_ENABLE\r
1342 );\r
1343\r
1344 if (EFI_ERROR (Status)) {\r
1345 Status = EFI_DEVICE_ERROR;\r
1346\r
1347 }\r
1348 }\r
1349\r
1350Exit:\r
1351 //\r
1352 // 15\r
1353 // resume priority of task level\r
1354 //\r
1355 gBS->RestoreTPL (OldTpl);\r
1356\r
1357 return Status;\r
1358\r
1359}\r
1360\r
1361/**\r
1362 Read out the scan code of the key that has just been stroked.\r
1363\r
1364 @param This Pointer of simple text Protocol.\r
1365 @param Key Pointer for store the key that read out.\r
1366\r
1367 @retval EFI_SUCCESS The key is read out successfully.\r
1368 @retval other The key reading failed.\r
1369\r
1370**/\r
1371EFI_STATUS\r
1372EFIAPI\r
1373BiosKeyboardReadKeyStroke (\r
1374 IN EFI_SIMPLE_TEXT_INPUT_PROTOCOL *This,\r
1375 OUT EFI_INPUT_KEY *Key\r
1376 )\r
1377{\r
1378 BIOS_KEYBOARD_DEV *BiosKeyboardPrivate;\r
1379 EFI_STATUS Status;\r
1380 EFI_KEY_DATA KeyData;\r
1381\r
1382 BiosKeyboardPrivate = BIOS_KEYBOARD_DEV_FROM_THIS (This);\r
1383\r
1384 Status = KeyboardReadKeyStrokeWorker (BiosKeyboardPrivate, &KeyData);\r
1385 if (EFI_ERROR (Status)) {\r
1386 return Status;\r
1387 }\r
1388\r
1389 CopyMem (Key, &KeyData.Key, sizeof (EFI_INPUT_KEY)); \r
1390\r
1391 return EFI_SUCCESS;\r
1392}\r
1393\r
1394/**\r
1395 Waiting on the keyboard event, if there's any key pressed by the user, signal the event\r
1396\r
1397 @param Event The event that be siganlled when any key has been stroked.\r
1398 @param Context Pointer of the protocol EFI_SIMPLE_TEXT_INPUT_PROTOCOL.\r
1399\r
1400**/\r
1401VOID\r
1402EFIAPI\r
1403BiosKeyboardWaitForKey (\r
1404 IN EFI_EVENT Event,\r
1405 IN VOID *Context\r
1406 )\r
1407{\r
1408 //\r
1409 // Stall 1ms to give a chance to let other driver interrupt this routine for their timer event.\r
1410 // Csm will be used to check whether there is a key pending, but the csm will disable all\r
1411 // interrupt before switch to compatibility16, which mean all the efiCompatibility timer\r
1412 // event will stop work during the compatibility16. And If a caller recursivly invoke this function,\r
1413 // e.g. UI setup or Shell, other drivers which are driven by timer event will have a bad performance during this period,\r
1414 // e.g. usb keyboard driver.\r
1415 // Add a stall period can greatly increate other driver performance during the WaitForKey is recursivly invoked.\r
1416 // 1ms delay will make little impact to the thunk keyboard driver, and user can not feel the delay at all when input.\r
1417 //\r
1418 gBS->Stall (1000);\r
1419 //\r
1420 // Use TimerEvent callback funciton to check whether there's any key pressed\r
1421 //\r
1422 BiosKeyboardTimerHandler (NULL, BIOS_KEYBOARD_DEV_FROM_THIS (Context));\r
1423\r
1424 if (!EFI_ERROR (BiosKeyboardCheckForKey (Context))) {\r
1425 gBS->SignalEvent (Event);\r
1426 }\r
1427}\r
1428\r
1429/**\r
1430 Check key buffer to get the key stroke status.\r
1431\r
1432 @param This Pointer of the protocol EFI_SIMPLE_TEXT_IN_PROTOCOL.\r
1433 \r
1434 @retval EFI_SUCCESS A key is being pressed now.\r
1435 @retval Other No key is now pressed.\r
1436\r
1437**/\r
1438EFI_STATUS\r
1439EFIAPI\r
1440BiosKeyboardCheckForKey (\r
1441 IN EFI_SIMPLE_TEXT_INPUT_PROTOCOL *This\r
1442 )\r
1443{\r
1444 BIOS_KEYBOARD_DEV *BiosKeyboardPrivate;\r
1445\r
1446 BiosKeyboardPrivate = BIOS_KEYBOARD_DEV_FROM_THIS (This);\r
1447\r
1448 return CheckQueue (&BiosKeyboardPrivate->Queue);\r
1449}\r
1450//\r
1451// Private worker functions\r
1452//\r
1453#define TABLE_END 0x0\r
1454\r
1455typedef struct _CONVERT_TABLE_ENTRY {\r
1456 UINT16 ScanCode;\r
1457 UINT16 EfiScanCode;\r
1458} CONVERT_TABLE_ENTRY;\r
1459\r
1460CONVERT_TABLE_ENTRY mConvertTable[] = {\r
1461 {\r
1462 0x47,\r
1463 SCAN_HOME\r
1464 },\r
1465 {\r
1466 0x48,\r
1467 SCAN_UP\r
1468 },\r
1469 {\r
1470 0x49,\r
1471 SCAN_PAGE_UP\r
1472 },\r
1473 {\r
1474 0x4b,\r
1475 SCAN_LEFT\r
1476 },\r
1477 {\r
1478 0x4d,\r
1479 SCAN_RIGHT\r
1480 },\r
1481 {\r
1482 0x4f,\r
1483 SCAN_END\r
1484 },\r
1485 {\r
1486 0x50,\r
1487 SCAN_DOWN\r
1488 },\r
1489 {\r
1490 0x51,\r
1491 SCAN_PAGE_DOWN\r
1492 },\r
1493 {\r
1494 0x52,\r
1495 SCAN_INSERT\r
1496 },\r
1497 {\r
1498 0x53,\r
1499 SCAN_DELETE\r
1500 },\r
1501 //\r
1502 // Function Keys are only valid if KeyChar == 0x00\r
1503 // This function does not require KeyChar to be 0x00\r
1504 //\r
1505 {\r
1506 0x3b,\r
1507 SCAN_F1\r
1508 },\r
1509 {\r
1510 0x3c,\r
1511 SCAN_F2\r
1512 },\r
1513 {\r
1514 0x3d,\r
1515 SCAN_F3\r
1516 },\r
1517 {\r
1518 0x3e,\r
1519 SCAN_F4\r
1520 },\r
1521 {\r
1522 0x3f,\r
1523 SCAN_F5\r
1524 },\r
1525 {\r
1526 0x40,\r
1527 SCAN_F6\r
1528 },\r
1529 {\r
1530 0x41,\r
1531 SCAN_F7\r
1532 },\r
1533 {\r
1534 0x42,\r
1535 SCAN_F8\r
1536 },\r
1537 {\r
1538 0x43,\r
1539 SCAN_F9\r
1540 },\r
1541 {\r
1542 0x44,\r
1543 SCAN_F10\r
1544 },\r
1545 {\r
1546 0x85,\r
1547 SCAN_F11\r
1548 },\r
1549 {\r
1550 0x86,\r
1551 SCAN_F12\r
1552 },\r
1553 //\r
1554 // Convert ALT + Fn keys\r
1555 //\r
1556 {\r
1557 0x68,\r
1558 SCAN_F1\r
1559 },\r
1560 {\r
1561 0x69,\r
1562 SCAN_F2\r
1563 },\r
1564 {\r
1565 0x6a,\r
1566 SCAN_F3\r
1567 },\r
1568 {\r
1569 0x6b,\r
1570 SCAN_F4\r
1571 },\r
1572 {\r
1573 0x6c,\r
1574 SCAN_F5\r
1575 },\r
1576 {\r
1577 0x6d,\r
1578 SCAN_F6\r
1579 },\r
1580 {\r
1581 0x6e,\r
1582 SCAN_F7\r
1583 },\r
1584 {\r
1585 0x6f,\r
1586 SCAN_F8\r
1587 },\r
1588 {\r
1589 0x70,\r
1590 SCAN_F9\r
1591 },\r
1592 {\r
1593 0x71,\r
1594 SCAN_F10\r
1595 },\r
1596 {\r
1597 TABLE_END,\r
1598 SCAN_NULL\r
1599 },\r
1600};\r
1601\r
1602/**\r
1603 Convert unicode combined with scan code of key to the counterpart of EFIScancode of it.\r
1604\r
1605 @param KeyChar Unicode of key.\r
1606 @param ScanCode Scan code of key.\r
1607\r
1608 @return The value of EFI Scancode for the key. \r
1609 @retval SCAN_NULL No corresponding value in the EFI convert table is found for the key.\r
1610\r
1611**/\r
1612UINT16\r
1613ConvertToEFIScanCode (\r
1614 IN CHAR16 KeyChar,\r
1615 IN UINT16 ScanCode\r
1616 )\r
1617{\r
1618 UINT16 EfiScanCode;\r
1619 UINT16 Index;\r
1620\r
1621 if (KeyChar == CHAR_ESC) {\r
1622 EfiScanCode = SCAN_ESC;\r
1623 } else if (KeyChar == 0x00 || KeyChar == 0xe0) {\r
1624 //\r
1625 // Movement & Function Keys\r
1626 //\r
1627 for (Index = 0; (Index < sizeof (mConvertTable) / sizeof (CONVERT_TABLE_ENTRY)) && (mConvertTable[Index].ScanCode != TABLE_END); Index += 1) {\r
1628 if (ScanCode == mConvertTable[Index].ScanCode) {\r
1629 return mConvertTable[Index].EfiScanCode;\r
1630 }\r
1631 }\r
1632 //\r
1633 // Reach Table end, return default value\r
1634 //\r
1635 return SCAN_NULL;\r
1636 } else {\r
1637 return SCAN_NULL;\r
1638 }\r
1639\r
1640 return EfiScanCode;\r
1641}\r
1642\r
1643/**\r
1644 Check whether there is Ps/2 Keyboard device in system by 0xF4 Keyboard Command\r
1645 If Keyboard receives 0xF4, it will respond with 'ACK'. If it doesn't respond, the device\r
1646 should not be in system. \r
1647\r
1648 @param BiosKeyboardPrivate Keyboard Private Data Struture\r
1649\r
1650 @retval TRUE Keyboard in System.\r
1651 @retval FALSE Keyboard not in System.\r
1652\r
1653**/\r
1654BOOLEAN\r
1655CheckKeyboardConnect (\r
1656 IN BIOS_KEYBOARD_DEV *BiosKeyboardPrivate\r
1657 )\r
1658{\r
1659 EFI_STATUS Status;\r
1660\r
1661 Status = EFI_SUCCESS;\r
1662 //\r
1663 // enable keyboard itself and wait for its ack\r
1664 // If can't receive ack, Keyboard should not be connected.\r
1665 //\r
1666 Status = KeyboardWrite (\r
1667 BiosKeyboardPrivate,\r
1668 KBC_INPBUF_VIA60_KBEN\r
1669 );\r
1670 if (EFI_ERROR (Status)) {\r
f6c014fb 1671 DEBUG ((EFI_D_ERROR, "[KBD]CheckKeyboardConnect - Keyboard enable failed!\n"));\r
55d4edfb 1672 REPORT_STATUS_CODE (\r
f6c014fb 1673 EFI_ERROR_CODE | EFI_ERROR_MINOR,\r
55d4edfb 1674 EFI_PERIPHERAL_KEYBOARD | EFI_P_EC_CONTROLLER_ERROR\r
f6c014fb 1675 );\r
bcecde14 1676 return FALSE;\r
1677 }\r
1678\r
1679 Status = KeyboardWaitForValue (\r
1680 BiosKeyboardPrivate,\r
1681 KBC_CMDECHO_ACK,\r
1682 KEYBOARD_WAITFORVALUE_TIMEOUT\r
1683 );\r
1684\r
1685 if (EFI_ERROR (Status)) {\r
f6c014fb 1686 DEBUG ((EFI_D_ERROR, "[KBD]CheckKeyboardConnect - Timeout!\n"));\r
55d4edfb 1687 REPORT_STATUS_CODE (\r
f6c014fb 1688 EFI_ERROR_CODE | EFI_ERROR_MINOR,\r
55d4edfb 1689 EFI_PERIPHERAL_KEYBOARD | EFI_P_EC_CONTROLLER_ERROR\r
f6c014fb 1690 );\r
bcecde14 1691 return FALSE;\r
1692 }\r
1693\r
1694 return TRUE;\r
1695}\r
1696\r
1697/**\r
1698 Timer event handler: read a series of key stroke from 8042\r
1699 and put them into memory key buffer. \r
1700 It is registered as running under TPL_NOTIFY\r
1701 \r
1702 @param Event The timer event\r
1703 @param Context A BIOS_KEYBOARD_DEV pointer\r
1704\r
1705**/\r
1706VOID\r
1707EFIAPI\r
1708BiosKeyboardTimerHandler (\r
1709 IN EFI_EVENT Event,\r
1710 IN VOID *Context\r
1711 )\r
1712{\r
1713 EFI_TPL OldTpl;\r
1714 BIOS_KEYBOARD_DEV *BiosKeyboardPrivate;\r
1715 EFI_IA32_REGISTER_SET Regs;\r
1716 UINT8 KbFlag1; // 0040h:0017h - KEYBOARD - STATUS FLAGS 1\r
1717 UINT8 KbFlag2; // 0040h:0018h - KEYBOARD - STATUS FLAGS 2\r
1718 EFI_KEY_DATA KeyData;\r
1719 LIST_ENTRY *Link;\r
1720 BIOS_KEYBOARD_CONSOLE_IN_EX_NOTIFY *CurrentNotify;\r
1721\r
1722 BiosKeyboardPrivate = Context;\r
1723\r
1724 //\r
1725 // Enter critical section\r
1726 //\r
1727 OldTpl = gBS->RaiseTPL (TPL_NOTIFY);\r
1728\r
1729 //\r
1730 // if there is no key present, just return\r
1731 //\r
1732 if (BiosKeyboardPrivate->ExtendedKeyboard) {\r
1733 Regs.H.AH = 0x11;\r
1734 } else {\r
1735 Regs.H.AH = 0x01;\r
1736 }\r
1737\r
1738 BiosKeyboardPrivate->LegacyBios->Int86 (\r
1739 BiosKeyboardPrivate->LegacyBios,\r
1740 0x16,\r
1741 &Regs\r
1742 );\r
1743 if (Regs.X.Flags.ZF != 0) {\r
1744 gBS->RestoreTPL (OldTpl);\r
1745 return;\r
1746 } \r
1747\r
1748 //\r
1749 // Read the key\r
1750 //\r
1751 if (BiosKeyboardPrivate->ExtendedKeyboard) {\r
1752 Regs.H.AH = 0x10;\r
1753 } else {\r
1754 Regs.H.AH = 0x00;\r
1755 }\r
1756\r
1757 BiosKeyboardPrivate->LegacyBios->Int86 (\r
1758 BiosKeyboardPrivate->LegacyBios,\r
1759 0x16,\r
1760 &Regs\r
1761 );\r
1762\r
1763 KeyData.Key.ScanCode = (UINT16) Regs.H.AH;\r
1764 KeyData.Key.UnicodeChar = (UINT16) Regs.H.AL;\r
f6c014fb 1765 DEBUG ((\r
1766 EFI_D_INFO,\r
1767 "[KBD]INT16 returns EFI_INPUT_KEY.ScanCode - %x, EFI_INPUT_KEY.UnicodeChar - %x\n",\r
1768 KeyData.Key.ScanCode,\r
1769 KeyData.Key.UnicodeChar\r
1770 ));\r
1771 \r
bcecde14 1772 KeyData.KeyState.KeyShiftState = EFI_SHIFT_STATE_VALID;\r
1773 KeyData.KeyState.KeyToggleState = EFI_TOGGLE_STATE_VALID;\r
1774 //\r
1775 // Leagcy Bios use Int 9 which is IRQ1 interrupt handler to get keystroke scancode to KB buffer in BDA (BIOS DATE AREA), then \r
1776 // 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
1777 // 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
1778 // 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
1779 // 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
1780 // 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
1781 // to wrong ASCII code. We can increase the Thunk frequence to let Int 9 response in time, but this way will much hurt other dirvers \r
1782 // performance, like USB.\r
1783 //\r
1784 // 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
1785 // 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
1786 // 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
1787 // CTRL and ALT.\r
1788 //\r
1789 // 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
1790 // 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
1791 // which will let persist to press SHIFT has same effection as only press one time. \r
1792 //\r
1793 //0040h:0017h - KEYBOARD - STATUS FLAGS 1\r
1794 // 7 INSert active\r
1795 // 6 Caps Lock active\r
1796 // 5 Num Lock active\r
1797 // 4 Scroll Lock active\r
1798 // 3 either Alt pressed\r
1799 // 2 either Ctrl pressed\r
1800 // 1 Left Shift pressed\r
1801 // 0 Right Shift pressed\r
1802\r
1803\r
1804 //\r
1805 // Clear the CTRL and ALT BDA flag\r
1806 //\r
1807 KbFlag1 = *((UINT8 *) (UINTN) 0x417); // read the STATUS FLAGS 1\r
1808 KbFlag2 = *((UINT8 *) (UINTN) 0x418); // read STATUS FLAGS 2\r
1809\r
f6c014fb 1810 DEBUG_CODE (\r
1811 {\r
1812 if ((KbFlag1 & KB_CAPS_LOCK_BIT) == KB_CAPS_LOCK_BIT) {\r
1813 DEBUG ((EFI_D_INFO, "[KBD]Caps Lock Key is pressed.\n"));\r
1814 }\r
1815 if ((KbFlag1 & KB_NUM_LOCK_BIT) == KB_NUM_LOCK_BIT) {\r
1816 DEBUG ((EFI_D_INFO, "[KBD]Num Lock Key is pressed.\n"));\r
1817 }\r
1818 if ((KbFlag1 & KB_SCROLL_LOCK_BIT) == KB_SCROLL_LOCK_BIT) {\r
1819 DEBUG ((EFI_D_INFO, "[KBD]Scroll Lock Key is pressed.\n"));\r
1820 } \r
1821 if ((KbFlag1 & KB_ALT_PRESSED) == KB_ALT_PRESSED) {\r
1822 if ((KbFlag2 & KB_LEFT_ALT_PRESSED) == KB_LEFT_ALT_PRESSED) {\r
1823 DEBUG ((EFI_D_INFO, "[KBD]Left Alt Key is pressed.\n"));\r
1824 } else {\r
1825 DEBUG ((EFI_D_INFO, "[KBD]Right Alt Key is pressed.\n"));\r
1826 }\r
1827 } \r
1828 if ((KbFlag1 & KB_CTRL_PRESSED) == KB_CTRL_PRESSED) {\r
1829 if ((KbFlag2 & KB_LEFT_CTRL_PRESSED) == KB_LEFT_CTRL_PRESSED) {\r
1830 DEBUG ((EFI_D_INFO, "[KBD]Left Ctrl Key is pressed.\n"));\r
1831 } else {\r
1832 DEBUG ((EFI_D_INFO, "[KBD]Right Ctrl Key is pressed.\n"));\r
1833 }\r
1834 } \r
1835 if ((KbFlag1 & KB_LEFT_SHIFT_PRESSED) == KB_LEFT_SHIFT_PRESSED) {\r
1836 DEBUG ((EFI_D_INFO, "[KBD]Left Shift Key is pressed.\n"));\r
1837 }\r
1838 if ((KbFlag1 & KB_RIGHT_SHIFT_PRESSED) == KB_RIGHT_SHIFT_PRESSED) {\r
1839 DEBUG ((EFI_D_INFO, "[KBD]Right Shift Key is pressed.\n"));\r
1840 }\r
1841 }\r
1842 );\r
1843\r
bcecde14 1844 //\r
1845 // Record toggle state\r
1846 //\r
1847 if ((KbFlag1 & KB_CAPS_LOCK_BIT) == KB_CAPS_LOCK_BIT) {\r
1848 KeyData.KeyState.KeyToggleState |= EFI_CAPS_LOCK_ACTIVE;\r
1849 }\r
1850 if ((KbFlag1 & KB_NUM_LOCK_BIT) == KB_NUM_LOCK_BIT) {\r
1851 KeyData.KeyState.KeyToggleState |= EFI_NUM_LOCK_ACTIVE;\r
1852 }\r
1853 if ((KbFlag1 & KB_SCROLL_LOCK_BIT) == KB_SCROLL_LOCK_BIT) {\r
1854 KeyData.KeyState.KeyToggleState |= EFI_SCROLL_LOCK_ACTIVE;\r
1855 }\r
1856 //\r
1857 // Record shift state\r
1858 // BUGBUG: Need add Menu key and Left/Right Logo key state in the future\r
1859 // \r
1860 if ((KbFlag1 & KB_ALT_PRESSED) == KB_ALT_PRESSED) {\r
1861 KeyData.KeyState.KeyShiftState |= ((KbFlag2 & KB_LEFT_ALT_PRESSED) == KB_LEFT_ALT_PRESSED) ? EFI_LEFT_ALT_PRESSED : EFI_RIGHT_ALT_PRESSED;\r
1862 } \r
1863 if ((KbFlag1 & KB_CTRL_PRESSED) == KB_CTRL_PRESSED) {\r
1864 KeyData.KeyState.KeyShiftState |= ((KbFlag2 & KB_LEFT_CTRL_PRESSED) == KB_LEFT_CTRL_PRESSED) ? EFI_LEFT_CONTROL_PRESSED : EFI_RIGHT_CONTROL_PRESSED;\r
1865 } \r
1866 if ((KbFlag1 & KB_LEFT_SHIFT_PRESSED) == KB_LEFT_SHIFT_PRESSED) {\r
1867 KeyData.KeyState.KeyShiftState |= EFI_LEFT_SHIFT_PRESSED;\r
1868 }\r
1869 if ((KbFlag1 & KB_RIGHT_SHIFT_PRESSED) == KB_RIGHT_SHIFT_PRESSED) {\r
1870 KeyData.KeyState.KeyShiftState |= EFI_RIGHT_SHIFT_PRESSED;\r
1871 }\r
1872\r
1873 //\r
1874 // Clear left alt and left ctrl BDA flag\r
1875 //\r
1876 KbFlag2 &= ~(KB_LEFT_ALT_PRESSED | KB_LEFT_CTRL_PRESSED);\r
1877 *((UINT8 *) (UINTN) 0x418) = KbFlag2;\r
1878 KbFlag1 &= ~0x0C; \r
1879 *((UINT8 *) (UINTN) 0x417) = KbFlag1; \r
1880\r
1881 \r
1882 //\r
1883 // Output EFI input key and shift/toggle state\r
1884 //\r
1885 if (KeyData.Key.UnicodeChar == CHAR_NULL || KeyData.Key.UnicodeChar == CHAR_SCANCODE || KeyData.Key.UnicodeChar == CHAR_ESC) {\r
1886 KeyData.Key.ScanCode = ConvertToEFIScanCode (KeyData.Key.UnicodeChar, KeyData.Key.ScanCode);\r
1887 KeyData.Key.UnicodeChar = CHAR_NULL;\r
1888 } else {\r
1889 KeyData.Key.ScanCode = SCAN_NULL;\r
1890 }\r
1891\r
1892 //\r
1893 // CSM16 has converted the Ctrl+[a-z] to [1-26], converted it back.\r
1894 //\r
1895 if ((KeyData.KeyState.KeyShiftState & (EFI_LEFT_CONTROL_PRESSED | EFI_RIGHT_CONTROL_PRESSED)) != 0) {\r
1896 if (KeyData.Key.UnicodeChar >= 1 && KeyData.Key.UnicodeChar <= 26) {\r
1897 if (((KeyData.KeyState.KeyShiftState & (EFI_LEFT_SHIFT_PRESSED | EFI_RIGHT_SHIFT_PRESSED)) != 0) ==\r
1898 ((KeyData.KeyState.KeyToggleState & EFI_CAPS_LOCK_ACTIVE) != 0)\r
1899 ) {\r
1900 KeyData.Key.UnicodeChar = (UINT16) (KeyData.Key.UnicodeChar + L'a' - 1);\r
1901 } else {\r
1902 KeyData.Key.UnicodeChar = (UINT16) (KeyData.Key.UnicodeChar + L'A' - 1);\r
1903 }\r
1904 }\r
1905 }\r
1906\r
f6c014fb 1907 DEBUG ((\r
1908 EFI_D_INFO,\r
1909 "[KBD]Convert to EFI Scan Code, EFI_INPUT_KEY.ScanCode - %x, EFI_INPUT_KEY.UnicodeChar - %x\n",\r
1910 KeyData.Key.ScanCode,\r
1911 KeyData.Key.UnicodeChar\r
1912 ));\r
1913\r
bcecde14 1914 //\r
1915 // Need not return associated shift state if a class of printable characters that\r
1916 // are normally adjusted by shift modifiers.\r
1917 // e.g. Shift Key + 'f' key = 'F'; Shift Key + 'F' key = 'f'.\r
1918 //\r
1919 if ((KeyData.Key.UnicodeChar >= L'A' && KeyData.Key.UnicodeChar <= L'Z') ||\r
1920 (KeyData.Key.UnicodeChar >= L'a' && KeyData.Key.UnicodeChar <= L'z')\r
1921 ) {\r
f6c014fb 1922 DEBUG ((EFI_D_INFO, "[KBD]Shift key with a~z are pressed, remove shift state in EFI_KEY_STATE.\n"));\r
bcecde14 1923 KeyData.KeyState.KeyShiftState &= ~(EFI_LEFT_SHIFT_PRESSED | EFI_RIGHT_SHIFT_PRESSED);\r
1924 }\r
1925\r
1926 //\r
1927 // Invoke notification functions if exist\r
1928 //\r
1929 for (Link = BiosKeyboardPrivate->NotifyList.ForwardLink; Link != &BiosKeyboardPrivate->NotifyList; Link = Link->ForwardLink) {\r
1930 CurrentNotify = CR (\r
1931 Link, \r
1932 BIOS_KEYBOARD_CONSOLE_IN_EX_NOTIFY, \r
1933 NotifyEntry, \r
1934 BIOS_KEYBOARD_CONSOLE_IN_EX_NOTIFY_SIGNATURE\r
1935 );\r
1936 if (IsKeyRegistered (&CurrentNotify->KeyData, &KeyData)) { \r
1937 CurrentNotify->KeyNotificationFn (&KeyData);\r
1938 }\r
1939 }\r
1940\r
1941 //\r
1942 // Convert the Ctrl+[a-z] to Ctrl+[1-26]\r
1943 //\r
1944 if ((KeyData.KeyState.KeyShiftState & (EFI_LEFT_CONTROL_PRESSED | EFI_RIGHT_CONTROL_PRESSED)) != 0) {\r
1945 if (KeyData.Key.UnicodeChar >= L'a' && KeyData.Key.UnicodeChar <= L'z') {\r
1946 KeyData.Key.UnicodeChar = (UINT16) (KeyData.Key.UnicodeChar - L'a' + 1);\r
1947 } else if (KeyData.Key.UnicodeChar >= L'A' && KeyData.Key.UnicodeChar <= L'Z') {\r
1948 KeyData.Key.UnicodeChar = (UINT16) (KeyData.Key.UnicodeChar - L'A' + 1);\r
1949 }\r
1950 }\r
1951 Enqueue (&BiosKeyboardPrivate->Queue, &KeyData);\r
1952 //\r
1953 // Leave critical section and return\r
1954 //\r
1955 gBS->RestoreTPL (OldTpl);\r
1956\r
1957 return ; \r
1958}\r
1959\r
1960/**\r
1961 Free keyboard notify list.\r
1962\r
1963 @param ListHead The list head\r
1964\r
1965 @retval EFI_SUCCESS Free the notify list successfully\r
1966 @retval EFI_INVALID_PARAMETER ListHead is invalid.\r
1967\r
1968**/\r
1969EFI_STATUS\r
1970BiosKeyboardFreeNotifyList (\r
1971 IN OUT LIST_ENTRY *ListHead\r
1972 )\r
1973{\r
1974 BIOS_KEYBOARD_CONSOLE_IN_EX_NOTIFY *NotifyNode;\r
1975\r
1976 if (ListHead == NULL) {\r
1977 return EFI_INVALID_PARAMETER;\r
1978 }\r
1979 while (!IsListEmpty (ListHead)) {\r
1980 NotifyNode = CR (\r
1981 ListHead->ForwardLink, \r
1982 BIOS_KEYBOARD_CONSOLE_IN_EX_NOTIFY, \r
1983 NotifyEntry, \r
1984 BIOS_KEYBOARD_CONSOLE_IN_EX_NOTIFY_SIGNATURE\r
1985 );\r
1986 RemoveEntryList (ListHead->ForwardLink);\r
1987 gBS->FreePool (NotifyNode);\r
1988 }\r
1989\r
1990 return EFI_SUCCESS;\r
1991}\r
1992\r
1993/**\r
1994 Check if key is registered.\r
1995\r
1996 @param RegsiteredData A pointer to a buffer that is filled in with the keystroke \r
1997 state data for the key that was registered.\r
1998 @param InputData A pointer to a buffer that is filled in with the keystroke \r
1999 state data for the key that was pressed.\r
2000\r
2001 @retval TRUE Key be pressed matches a registered key.\r
2002 @retval FLASE Match failed. \r
2003 \r
2004**/\r
2005BOOLEAN\r
2006IsKeyRegistered (\r
2007 IN EFI_KEY_DATA *RegsiteredData,\r
2008 IN EFI_KEY_DATA *InputData\r
2009 )\r
2010{\r
2011 ASSERT (RegsiteredData != NULL && InputData != NULL);\r
2012 \r
2013 if ((RegsiteredData->Key.ScanCode != InputData->Key.ScanCode) ||\r
2014 (RegsiteredData->Key.UnicodeChar != InputData->Key.UnicodeChar)) {\r
2015 return FALSE; \r
2016 } \r
2017 \r
2018 //\r
2019 // Assume KeyShiftState/KeyToggleState = 0 in Registered key data means these state could be ignored.\r
2020 //\r
2021 if (RegsiteredData->KeyState.KeyShiftState != 0 &&\r
2022 RegsiteredData->KeyState.KeyShiftState != InputData->KeyState.KeyShiftState) {\r
2023 return FALSE; \r
2024 } \r
2025 if (RegsiteredData->KeyState.KeyToggleState != 0 &&\r
2026 RegsiteredData->KeyState.KeyToggleState != InputData->KeyState.KeyToggleState) {\r
2027 return FALSE; \r
2028 } \r
2029 \r
2030 return TRUE;\r
2031\r
2032}\r
2033\r
2034/**\r
2035 Waiting on the keyboard event, if there's any key pressed by the user, signal the event\r
2036\r
2037 @param Event The event that be siganlled when any key has been stroked.\r
2038 @param Context Pointer of the protocol EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.\r
2039 \r
2040**/\r
2041VOID\r
2042EFIAPI\r
2043BiosKeyboardWaitForKeyEx (\r
2044 IN EFI_EVENT Event,\r
2045 IN VOID *Context\r
2046 )\r
2047{ \r
2048 BIOS_KEYBOARD_DEV *BiosKeyboardPrivate;\r
2049 \r
2050 BiosKeyboardPrivate = TEXT_INPUT_EX_BIOS_KEYBOARD_DEV_FROM_THIS (Context); \r
2051 BiosKeyboardWaitForKey (Event, &BiosKeyboardPrivate->SimpleTextIn);\r
2052\r
2053}\r
2054\r
2055/**\r
2056 Reset the input device and optionaly run diagnostics\r
2057 \r
2058 @param This Protocol instance pointer.\r
2059 @param ExtendedVerification Driver may perform diagnostics on reset.\r
2060\r
2061 @retval EFI_SUCCESS The device was reset.\r
2062 @retval EFI_DEVICE_ERROR The device is not functioning properly and could \r
2063 not be reset.\r
2064\r
2065**/\r
2066EFI_STATUS\r
2067EFIAPI\r
2068BiosKeyboardResetEx (\r
2069 IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *This,\r
2070 IN BOOLEAN ExtendedVerification\r
2071 )\r
2072{\r
2073 BIOS_KEYBOARD_DEV *BiosKeyboardPrivate;\r
2074 EFI_STATUS Status;\r
2075 EFI_TPL OldTpl;\r
2076 \r
2077 BiosKeyboardPrivate = TEXT_INPUT_EX_BIOS_KEYBOARD_DEV_FROM_THIS (This); \r
2078\r
2079 Status = BiosKeyboardPrivate->SimpleTextIn.Reset (\r
2080 &BiosKeyboardPrivate->SimpleTextIn, \r
2081 ExtendedVerification\r
2082 );\r
2083 if (EFI_ERROR (Status)) {\r
2084 return EFI_DEVICE_ERROR;\r
2085 }\r
2086\r
2087 OldTpl = gBS->RaiseTPL (TPL_NOTIFY);\r
2088\r
2089 gBS->RestoreTPL (OldTpl);\r
2090 \r
2091 return EFI_SUCCESS;\r
2092\r
2093}\r
2094\r
2095/**\r
2096 Reads the next keystroke from the input device. The WaitForKey Event can \r
2097 be used to test for existance of a keystroke via WaitForEvent () call.\r
2098\r
2099 @param This Protocol instance pointer.\r
2100 @param KeyData A pointer to a buffer that is filled in with the keystroke \r
2101 state data for the key that was pressed.\r
2102 \r
2103 @retval EFI_SUCCESS The keystroke information was returned.\r
2104 @retval EFI_NOT_READY There was no keystroke data availiable.\r
2105 @retval EFI_DEVICE_ERROR The keystroke information was not returned due to \r
2106 hardware errors.\r
2107 @retval EFI_INVALID_PARAMETER KeyData is NULL. \r
2108 \r
2109**/\r
2110EFI_STATUS\r
2111EFIAPI\r
2112BiosKeyboardReadKeyStrokeEx (\r
2113 IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *This,\r
2114 OUT EFI_KEY_DATA *KeyData\r
2115 )\r
2116{\r
2117 BIOS_KEYBOARD_DEV *BiosKeyboardPrivate;\r
2118\r
2119 if (KeyData == NULL) {\r
2120 return EFI_INVALID_PARAMETER;\r
2121 }\r
2122 \r
2123 BiosKeyboardPrivate = TEXT_INPUT_EX_BIOS_KEYBOARD_DEV_FROM_THIS (This);\r
2124\r
2125 return KeyboardReadKeyStrokeWorker (BiosKeyboardPrivate, KeyData);\r
2126 \r
2127}\r
2128\r
2129/**\r
2130 Set certain state for the input device.\r
2131\r
2132 @param This Protocol instance pointer.\r
2133 @param KeyToggleState A pointer to the EFI_KEY_TOGGLE_STATE to set the \r
2134 state for the input device.\r
2135\r
2136 @retval EFI_SUCCESS The device state was set successfully.\r
2137 @retval EFI_DEVICE_ERROR The device is not functioning correctly and could \r
2138 not have the setting adjusted.\r
2139 @retval EFI_UNSUPPORTED The device does not have the ability to set its state.\r
2140 @retval EFI_INVALID_PARAMETER KeyToggleState is NULL. \r
2141\r
2142**/ \r
2143EFI_STATUS\r
2144EFIAPI\r
2145BiosKeyboardSetState (\r
2146 IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *This,\r
2147 IN EFI_KEY_TOGGLE_STATE *KeyToggleState\r
2148 )\r
2149{\r
2150 EFI_STATUS Status;\r
2151 BIOS_KEYBOARD_DEV *BiosKeyboardPrivate;\r
2152 EFI_TPL OldTpl;\r
2153 EFI_LEGACY_BIOS_PROTOCOL *LegacyBios;\r
2154 UINT8 Command;\r
2155\r
2156 if (KeyToggleState == NULL) {\r
2157 return EFI_INVALID_PARAMETER;\r
2158 }\r
2159\r
5829afe3 2160 //\r
2161 // Thunk keyboard driver doesn't support partial keystroke.\r
2162 //\r
2163 if ((*KeyToggleState & EFI_TOGGLE_STATE_VALID) != EFI_TOGGLE_STATE_VALID ||\r
2164 (*KeyToggleState & EFI_KEY_STATE_EXPOSED) == EFI_KEY_STATE_EXPOSED\r
2165 ) {\r
bcecde14 2166 return EFI_UNSUPPORTED;\r
2167 }\r
2168\r
2169 BiosKeyboardPrivate = TEXT_INPUT_EX_BIOS_KEYBOARD_DEV_FROM_THIS (This);\r
2170 //\r
2171 // See if the Legacy BIOS Protocol is available\r
2172 //\r
2173 Status = gBS->LocateProtocol (\r
2174 &gEfiLegacyBiosProtocolGuid,\r
2175 NULL,\r
2176 (VOID **) &LegacyBios\r
2177 );\r
2178\r
2179 ASSERT_EFI_ERROR (Status);\r
2180 //\r
2181 // Enter critical section\r
2182 //\r
2183 OldTpl = gBS->RaiseTPL (TPL_NOTIFY);\r
2184\r
2185 Command = 0;\r
2186 if ((*KeyToggleState & EFI_CAPS_LOCK_ACTIVE) == EFI_CAPS_LOCK_ACTIVE) {\r
2187 Command |= 4;\r
2188 }\r
2189 if ((*KeyToggleState & EFI_NUM_LOCK_ACTIVE) == EFI_NUM_LOCK_ACTIVE) {\r
2190 Command |= 2;\r
2191 }\r
2192 if ((*KeyToggleState & EFI_SCROLL_LOCK_ACTIVE) == EFI_SCROLL_LOCK_ACTIVE) {\r
2193 Command |= 1;\r
2194 }\r
2195\r
2196 Status = KeyboardWrite (BiosKeyboardPrivate, 0xed);\r
2197 if (EFI_ERROR (Status)) {\r
2198 return EFI_DEVICE_ERROR;\r
2199 } \r
2200 Status = KeyboardWaitForValue (BiosKeyboardPrivate, 0xfa, KEYBOARD_WAITFORVALUE_TIMEOUT);\r
2201 if (EFI_ERROR (Status)) {\r
2202 return EFI_DEVICE_ERROR;\r
2203 }\r
2204 Status = KeyboardWrite (BiosKeyboardPrivate, Command);\r
2205 if (EFI_ERROR (Status)) {\r
2206 return EFI_DEVICE_ERROR;\r
2207 } \r
2208 //\r
2209 // Call Legacy BIOS Protocol to set whatever is necessary\r
2210 //\r
2211 LegacyBios->UpdateKeyboardLedStatus (LegacyBios, Command);\r
2212\r
2213 Status = EFI_SUCCESS;\r
2214\r
2215 //\r
2216 // Leave critical section and return\r
2217 //\r
2218 gBS->RestoreTPL (OldTpl);\r
2219\r
2220 return Status;\r
2221\r
2222}\r
2223\r
2224/**\r
2225 Register a notification function for a particular keystroke for the input device.\r
2226\r
2227 @param This Protocol instance pointer.\r
2228 @param KeyData A pointer to a buffer that is filled in with the keystroke \r
2229 information data for the key that was pressed.\r
2230 @param KeyNotificationFunction Points to the function to be called when the key \r
2231 sequence is typed specified by KeyData. \r
2232 @param NotifyHandle Points to the unique handle assigned to the registered notification. \r
2233\r
2234 \r
2235 @retval EFI_SUCCESS The notification function was registered successfully.\r
2236 @retval EFI_OUT_OF_RESOURCES Unable to allocate resources for necesssary data structures.\r
2237 @retval EFI_INVALID_PARAMETER KeyData or NotifyHandle is NULL.\r
2238 \r
2239**/ \r
2240EFI_STATUS\r
2241EFIAPI\r
2242BiosKeyboardRegisterKeyNotify (\r
2243 IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *This,\r
2244 IN EFI_KEY_DATA *KeyData,\r
2245 IN EFI_KEY_NOTIFY_FUNCTION KeyNotificationFunction,\r
2246 OUT EFI_HANDLE *NotifyHandle\r
2247 )\r
2248{\r
2249 EFI_STATUS Status;\r
2250 BIOS_KEYBOARD_DEV *BiosKeyboardPrivate;\r
2251 EFI_TPL OldTpl;\r
2252 BIOS_KEYBOARD_CONSOLE_IN_EX_NOTIFY *NewNotify;\r
2253 LIST_ENTRY *Link;\r
2254 BIOS_KEYBOARD_CONSOLE_IN_EX_NOTIFY *CurrentNotify; \r
2255\r
2256 if (KeyData == NULL || NotifyHandle == NULL || KeyNotificationFunction == NULL) {\r
2257 return EFI_INVALID_PARAMETER;\r
2258 }\r
2259\r
2260 BiosKeyboardPrivate = TEXT_INPUT_EX_BIOS_KEYBOARD_DEV_FROM_THIS (This);\r
2261\r
2262 //\r
2263 // Enter critical section\r
2264 //\r
2265 OldTpl = gBS->RaiseTPL (TPL_NOTIFY);\r
2266\r
2267 //\r
2268 // Return EFI_SUCCESS if the (KeyData, NotificationFunction) is already registered.\r
2269 //\r
2270 for (Link = BiosKeyboardPrivate->NotifyList.ForwardLink; Link != &BiosKeyboardPrivate->NotifyList; Link = Link->ForwardLink) {\r
2271 CurrentNotify = CR (\r
2272 Link, \r
2273 BIOS_KEYBOARD_CONSOLE_IN_EX_NOTIFY, \r
2274 NotifyEntry, \r
2275 BIOS_KEYBOARD_CONSOLE_IN_EX_NOTIFY_SIGNATURE\r
2276 );\r
2277 if (IsKeyRegistered (&CurrentNotify->KeyData, KeyData)) { \r
2278 if (CurrentNotify->KeyNotificationFn == KeyNotificationFunction) {\r
2279 *NotifyHandle = CurrentNotify->NotifyHandle; \r
2280 Status = EFI_SUCCESS;\r
2281 goto Exit;\r
2282 }\r
2283 } \r
2284 }\r
2285\r
2286 //\r
2287 // Allocate resource to save the notification function\r
2288 //\r
2289 \r
2290 NewNotify = (BIOS_KEYBOARD_CONSOLE_IN_EX_NOTIFY *) AllocateZeroPool (sizeof (BIOS_KEYBOARD_CONSOLE_IN_EX_NOTIFY));\r
2291 if (NewNotify == NULL) {\r
2292 Status = EFI_OUT_OF_RESOURCES;\r
2293 goto Exit;\r
2294 }\r
2295\r
2296 NewNotify->Signature = BIOS_KEYBOARD_CONSOLE_IN_EX_NOTIFY_SIGNATURE;\r
2297 NewNotify->KeyNotificationFn = KeyNotificationFunction;\r
2298 NewNotify->NotifyHandle = (EFI_HANDLE) NewNotify;\r
2299 CopyMem (&NewNotify->KeyData, KeyData, sizeof (EFI_KEY_DATA));\r
2300 InsertTailList (&BiosKeyboardPrivate->NotifyList, &NewNotify->NotifyEntry);\r
2301\r
2302 *NotifyHandle = NewNotify->NotifyHandle; \r
2303 Status = EFI_SUCCESS;\r
2304 \r
2305Exit:\r
2306 //\r
2307 // Leave critical section and return\r
2308 //\r
2309 gBS->RestoreTPL (OldTpl);\r
2310 return Status; \r
2311}\r
2312\r
2313/**\r
2314 Remove a registered notification function from a particular keystroke.\r
2315\r
2316 @param This Protocol instance pointer. \r
2317 @param NotificationHandle The handle of the notification function being unregistered.\r
2318 \r
2319 @retval EFI_SUCCESS The notification function was unregistered successfully.\r
2320 @retval EFI_INVALID_PARAMETER The NotificationHandle is invalid.\r
2321 \r
2322**/ \r
2323EFI_STATUS\r
2324EFIAPI\r
2325BiosKeyboardUnregisterKeyNotify (\r
2326 IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *This,\r
2327 IN EFI_HANDLE NotificationHandle\r
2328 )\r
2329{\r
2330 EFI_STATUS Status;\r
2331 BIOS_KEYBOARD_DEV *BiosKeyboardPrivate;\r
2332 EFI_TPL OldTpl;\r
2333 LIST_ENTRY *Link;\r
2334 BIOS_KEYBOARD_CONSOLE_IN_EX_NOTIFY *CurrentNotify;\r
2335\r
2336 //\r
2337 // Check incoming notification handle\r
2338 //\r
2339 if (NotificationHandle == NULL) {\r
2340 return EFI_INVALID_PARAMETER;\r
2341 }\r
2342\r
2343 if (((BIOS_KEYBOARD_CONSOLE_IN_EX_NOTIFY *) NotificationHandle)->Signature != BIOS_KEYBOARD_CONSOLE_IN_EX_NOTIFY_SIGNATURE) {\r
2344 return EFI_INVALID_PARAMETER;\r
2345 } \r
2346 \r
2347 BiosKeyboardPrivate = TEXT_INPUT_EX_BIOS_KEYBOARD_DEV_FROM_THIS (This);\r
2348 \r
2349 //\r
2350 // Enter critical section\r
2351 //\r
2352 OldTpl = gBS->RaiseTPL (TPL_NOTIFY); \r
2353\r
2354 for (Link = BiosKeyboardPrivate->NotifyList.ForwardLink; Link != &BiosKeyboardPrivate->NotifyList; Link = Link->ForwardLink) {\r
2355 CurrentNotify = CR (\r
2356 Link, \r
2357 BIOS_KEYBOARD_CONSOLE_IN_EX_NOTIFY, \r
2358 NotifyEntry, \r
2359 BIOS_KEYBOARD_CONSOLE_IN_EX_NOTIFY_SIGNATURE\r
2360 ); \r
2361 if (CurrentNotify->NotifyHandle == NotificationHandle) {\r
2362 //\r
2363 // Remove the notification function from NotifyList and free resources\r
2364 //\r
2365 RemoveEntryList (&CurrentNotify->NotifyEntry); \r
2366\r
2367 Status = EFI_SUCCESS;\r
2368 goto Exit;\r
2369 }\r
2370 }\r
2371 \r
2372 //\r
2373 // Can not find the specified Notification Handle\r
2374 //\r
2375 Status = EFI_INVALID_PARAMETER;\r
2376\r
2377Exit:\r
2378 //\r
2379 // Leave critical section and return\r
2380 //\r
2381 gBS->RestoreTPL (OldTpl);\r
2382 return Status;\r
2383}\r
2384\r
2385/**\r
2386 The user Entry Point for module BiosKeyboard. The user code starts with this function.\r
2387\r
2388 @param[in] ImageHandle The firmware allocated handle for the EFI image.\r
2389 @param[in] SystemTable A pointer to the EFI System Table.\r
2390\r
2391 @retval EFI_SUCCESS The entry point is executed successfully.\r
2392 @retval other Some error occurs when executing this entry point.\r
2393\r
2394**/\r
2395EFI_STATUS\r
2396EFIAPI\r
2397InitializeBiosKeyboard(\r
2398 IN EFI_HANDLE ImageHandle,\r
2399 IN EFI_SYSTEM_TABLE *SystemTable\r
2400 )\r
2401{\r
2402 EFI_STATUS Status;\r
2403\r
2404 //\r
2405 // Install driver model protocol(s).\r
2406 //\r
2407 Status = EfiLibInstallDriverBindingComponentName2 (\r
2408 ImageHandle,\r
2409 SystemTable,\r
2410 &gBiosKeyboardDriverBinding,\r
2411 ImageHandle,\r
2412 &gBiosKeyboardComponentName,\r
2413 &gBiosKeyboardComponentName2\r
2414 );\r
2415 ASSERT_EFI_ERROR (Status);\r
2416\r
2417 return Status;\r
2418}\r