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