VirtualKeyboard.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162
  1. /** @file
  2. VirtualKeyboard driver
  3. Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
  4. Copyright (c) 2018, Linaro Ltd. All rights reserved.<BR>
  5. SPDX-License-Identifier: BSD-2-Clause-Patent
  6. **/
  7. #include "VirtualKeyboard.h"
  8. //
  9. // RAM Keyboard Driver Binding Protocol Instance
  10. //
  11. EFI_DRIVER_BINDING_PROTOCOL gVirtualKeyboardDriverBinding = {
  12. VirtualKeyboardDriverBindingSupported,
  13. VirtualKeyboardDriverBindingStart,
  14. VirtualKeyboardDriverBindingStop,
  15. 0x10,
  16. NULL,
  17. NULL
  18. };
  19. //
  20. // EFI Driver Binding Protocol Functions
  21. //
  22. /**
  23. Check whether the driver supports this device.
  24. @param This The Udriver binding protocol.
  25. @param Controller The controller handle to check.
  26. @param RemainingDevicePath The remaining device path.
  27. @retval EFI_SUCCESS The driver supports this controller.
  28. @retval other This device isn't supported.
  29. **/
  30. EFI_STATUS
  31. EFIAPI
  32. VirtualKeyboardDriverBindingSupported (
  33. IN EFI_DRIVER_BINDING_PROTOCOL *This,
  34. IN EFI_HANDLE Controller,
  35. IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
  36. )
  37. {
  38. EFI_STATUS Status;
  39. PLATFORM_VIRTUAL_KBD_PROTOCOL *PlatformVirtual;
  40. Status = gBS->OpenProtocol (
  41. Controller,
  42. &gPlatformVirtualKeyboardProtocolGuid,
  43. (VOID **)&PlatformVirtual,
  44. This->DriverBindingHandle,
  45. Controller,
  46. EFI_OPEN_PROTOCOL_BY_DRIVER
  47. );
  48. if (EFI_ERROR (Status)) {
  49. return Status;
  50. }
  51. gBS->CloseProtocol (
  52. Controller,
  53. &gPlatformVirtualKeyboardProtocolGuid,
  54. This->DriverBindingHandle,
  55. Controller
  56. );
  57. return Status;
  58. }
  59. /**
  60. Starts the device with this driver.
  61. @param This The driver binding instance.
  62. @param Controller Handle of device to bind driver to.
  63. @param RemainingDevicePath Optional parameter use to pick a specific child
  64. device to start.
  65. @retval EFI_SUCCESS The controller is controlled by the driver.
  66. @retval Other This controller cannot be started.
  67. **/
  68. EFI_STATUS
  69. EFIAPI
  70. VirtualKeyboardDriverBindingStart (
  71. IN EFI_DRIVER_BINDING_PROTOCOL *This,
  72. IN EFI_HANDLE Controller,
  73. IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
  74. )
  75. {
  76. EFI_STATUS Status;
  77. VIRTUAL_KEYBOARD_DEV *VirtualKeyboardPrivate;
  78. PLATFORM_VIRTUAL_KBD_PROTOCOL *PlatformVirtual;
  79. Status = gBS->OpenProtocol (
  80. Controller,
  81. &gPlatformVirtualKeyboardProtocolGuid,
  82. (VOID **)&PlatformVirtual,
  83. This->DriverBindingHandle,
  84. Controller,
  85. EFI_OPEN_PROTOCOL_BY_DRIVER
  86. );
  87. if (EFI_ERROR (Status)) {
  88. return Status;
  89. }
  90. //
  91. // Allocate the private device structure
  92. //
  93. VirtualKeyboardPrivate = (VIRTUAL_KEYBOARD_DEV *)AllocateZeroPool (sizeof (VIRTUAL_KEYBOARD_DEV));
  94. if (VirtualKeyboardPrivate == NULL) {
  95. Status = EFI_OUT_OF_RESOURCES;
  96. goto Done;
  97. }
  98. //
  99. // Initialize the private device structure
  100. //
  101. VirtualKeyboardPrivate->Signature = VIRTUAL_KEYBOARD_DEV_SIGNATURE;
  102. VirtualKeyboardPrivate->Handle = Controller;
  103. VirtualKeyboardPrivate->PlatformVirtual = PlatformVirtual;
  104. VirtualKeyboardPrivate->Queue.Front = 0;
  105. VirtualKeyboardPrivate->Queue.Rear = 0;
  106. VirtualKeyboardPrivate->QueueForNotify.Front = 0;
  107. VirtualKeyboardPrivate->QueueForNotify.Rear = 0;
  108. VirtualKeyboardPrivate->SimpleTextIn.Reset = VirtualKeyboardReset;
  109. VirtualKeyboardPrivate->SimpleTextIn.ReadKeyStroke = VirtualKeyboardReadKeyStroke;
  110. VirtualKeyboardPrivate->SimpleTextInputEx.Reset = VirtualKeyboardResetEx;
  111. VirtualKeyboardPrivate->SimpleTextInputEx.ReadKeyStrokeEx = VirtualKeyboardReadKeyStrokeEx;
  112. VirtualKeyboardPrivate->SimpleTextInputEx.SetState = VirtualKeyboardSetState;
  113. VirtualKeyboardPrivate->SimpleTextInputEx.RegisterKeyNotify = VirtualKeyboardRegisterKeyNotify;
  114. VirtualKeyboardPrivate->SimpleTextInputEx.UnregisterKeyNotify = VirtualKeyboardUnregisterKeyNotify;
  115. InitializeListHead (&VirtualKeyboardPrivate->NotifyList);
  116. Status = PlatformVirtual->Register ();
  117. if (EFI_ERROR (Status)) {
  118. goto Done;
  119. }
  120. //
  121. // Report that the keyboard is being enabled
  122. //
  123. REPORT_STATUS_CODE (
  124. EFI_PROGRESS_CODE,
  125. EFI_PERIPHERAL_KEYBOARD | EFI_P_PC_ENABLE
  126. );
  127. //
  128. // Setup the WaitForKey event
  129. //
  130. Status = gBS->CreateEvent (
  131. EVT_NOTIFY_WAIT,
  132. TPL_NOTIFY,
  133. VirtualKeyboardWaitForKey,
  134. &(VirtualKeyboardPrivate->SimpleTextIn),
  135. &((VirtualKeyboardPrivate->SimpleTextIn).WaitForKey)
  136. );
  137. if (EFI_ERROR (Status)) {
  138. (VirtualKeyboardPrivate->SimpleTextIn).WaitForKey = NULL;
  139. goto Done;
  140. }
  141. Status = gBS->CreateEvent (
  142. EVT_NOTIFY_WAIT,
  143. TPL_NOTIFY,
  144. VirtualKeyboardWaitForKeyEx,
  145. &(VirtualKeyboardPrivate->SimpleTextInputEx),
  146. &(VirtualKeyboardPrivate->SimpleTextInputEx.WaitForKeyEx)
  147. );
  148. if (EFI_ERROR (Status)) {
  149. VirtualKeyboardPrivate->SimpleTextInputEx.WaitForKeyEx = NULL;
  150. goto Done;
  151. }
  152. //
  153. // Setup a periodic timer, used for reading keystrokes at a fixed interval
  154. //
  155. Status = gBS->CreateEvent (
  156. EVT_TIMER | EVT_NOTIFY_SIGNAL,
  157. TPL_NOTIFY,
  158. VirtualKeyboardTimerHandler,
  159. VirtualKeyboardPrivate,
  160. &VirtualKeyboardPrivate->TimerEvent
  161. );
  162. if (EFI_ERROR (Status)) {
  163. Status = EFI_OUT_OF_RESOURCES;
  164. goto Done;
  165. }
  166. Status = gBS->SetTimer (
  167. VirtualKeyboardPrivate->TimerEvent,
  168. TimerPeriodic,
  169. KEYBOARD_TIMER_INTERVAL
  170. );
  171. if (EFI_ERROR (Status)) {
  172. Status = EFI_OUT_OF_RESOURCES;
  173. goto Done;
  174. }
  175. Status = gBS->CreateEvent (
  176. EVT_NOTIFY_SIGNAL,
  177. TPL_CALLBACK,
  178. KeyNotifyProcessHandler,
  179. VirtualKeyboardPrivate,
  180. &VirtualKeyboardPrivate->KeyNotifyProcessEvent
  181. );
  182. if (EFI_ERROR (Status)) {
  183. Status = EFI_OUT_OF_RESOURCES;
  184. goto Done;
  185. }
  186. //
  187. // Reset the keyboard device
  188. //
  189. Status = VirtualKeyboardPrivate->SimpleTextInputEx.Reset (
  190. &VirtualKeyboardPrivate->SimpleTextInputEx,
  191. FALSE
  192. );
  193. if (EFI_ERROR (Status)) {
  194. DEBUG ((DEBUG_ERROR, "[KBD]Reset Failed. Status - %r\n", Status));
  195. goto Done;
  196. }
  197. //
  198. // Install protocol interfaces for the keyboard device.
  199. //
  200. Status = gBS->InstallMultipleProtocolInterfaces (
  201. &Controller,
  202. &gEfiSimpleTextInProtocolGuid,
  203. &VirtualKeyboardPrivate->SimpleTextIn,
  204. &gEfiSimpleTextInputExProtocolGuid,
  205. &VirtualKeyboardPrivate->SimpleTextInputEx,
  206. NULL
  207. );
  208. Done:
  209. if (EFI_ERROR (Status)) {
  210. if (VirtualKeyboardPrivate != NULL) {
  211. if ((VirtualKeyboardPrivate->SimpleTextIn).WaitForKey != NULL) {
  212. gBS->CloseEvent ((VirtualKeyboardPrivate->SimpleTextIn).WaitForKey);
  213. }
  214. if ((VirtualKeyboardPrivate->SimpleTextInputEx).WaitForKeyEx != NULL) {
  215. gBS->CloseEvent (
  216. (VirtualKeyboardPrivate->SimpleTextInputEx).WaitForKeyEx
  217. );
  218. }
  219. if (VirtualKeyboardPrivate->KeyNotifyProcessEvent != NULL) {
  220. gBS->CloseEvent (VirtualKeyboardPrivate->KeyNotifyProcessEvent);
  221. }
  222. VirtualKeyboardFreeNotifyList (&VirtualKeyboardPrivate->NotifyList);
  223. if (VirtualKeyboardPrivate->TimerEvent != NULL) {
  224. gBS->CloseEvent (VirtualKeyboardPrivate->TimerEvent);
  225. }
  226. FreePool (VirtualKeyboardPrivate);
  227. }
  228. }
  229. gBS->CloseProtocol (
  230. Controller,
  231. &gPlatformVirtualKeyboardProtocolGuid,
  232. This->DriverBindingHandle,
  233. Controller
  234. );
  235. return Status;
  236. }
  237. /**
  238. Stop the device handled by this driver.
  239. @param This The driver binding protocol.
  240. @param Controller The controller to release.
  241. @param NumberOfChildren The number of handles in ChildHandleBuffer.
  242. @param ChildHandleBuffer The array of child handle.
  243. @retval EFI_SUCCESS The device was stopped.
  244. @retval EFI_DEVICE_ERROR The device could not be stopped due to a
  245. device error.
  246. @retval Others Fail to uninstall protocols attached on the
  247. device.
  248. **/
  249. EFI_STATUS
  250. EFIAPI
  251. VirtualKeyboardDriverBindingStop (
  252. IN EFI_DRIVER_BINDING_PROTOCOL *This,
  253. IN EFI_HANDLE Controller,
  254. IN UINTN NumberOfChildren,
  255. IN EFI_HANDLE *ChildHandleBuffer
  256. )
  257. {
  258. return EFI_SUCCESS;
  259. }
  260. /**
  261. Enqueue the key.
  262. @param Queue The queue to be enqueued.
  263. @param KeyData The key data to be enqueued.
  264. @retval EFI_NOT_READY The queue is full.
  265. @retval EFI_SUCCESS Successfully enqueued the key data.
  266. **/
  267. EFI_STATUS
  268. Enqueue (
  269. IN SIMPLE_QUEUE *Queue,
  270. IN EFI_KEY_DATA *KeyData
  271. )
  272. {
  273. if ((Queue->Rear + 1) % QUEUE_MAX_COUNT == Queue->Front) {
  274. return EFI_NOT_READY;
  275. }
  276. CopyMem (&Queue->Buffer[Queue->Rear], KeyData, sizeof (EFI_KEY_DATA));
  277. Queue->Rear = (Queue->Rear + 1) % QUEUE_MAX_COUNT;
  278. return EFI_SUCCESS;
  279. }
  280. /**
  281. Dequeue the key.
  282. @param Queue The queue to be dequeued.
  283. @param KeyData The key data to be dequeued.
  284. @retval EFI_NOT_READY The queue is empty.
  285. @retval EFI_SUCCESS Successfully dequeued the key data.
  286. **/
  287. EFI_STATUS
  288. Dequeue (
  289. IN SIMPLE_QUEUE *Queue,
  290. IN EFI_KEY_DATA *KeyData
  291. )
  292. {
  293. if (Queue->Front == Queue->Rear) {
  294. return EFI_NOT_READY;
  295. }
  296. CopyMem (KeyData, &Queue->Buffer[Queue->Front], sizeof (EFI_KEY_DATA));
  297. Queue->Front = (Queue->Front + 1) % QUEUE_MAX_COUNT;
  298. return EFI_SUCCESS;
  299. }
  300. /**
  301. Check whether the queue is empty.
  302. @param Queue The queue to be checked.
  303. @retval EFI_NOT_READY The queue is empty.
  304. @retval EFI_SUCCESS The queue is not empty.
  305. **/
  306. EFI_STATUS
  307. CheckQueue (
  308. IN SIMPLE_QUEUE *Queue
  309. )
  310. {
  311. if (Queue->Front == Queue->Rear) {
  312. return EFI_NOT_READY;
  313. }
  314. return EFI_SUCCESS;
  315. }
  316. /**
  317. Check key buffer to get the key stroke status.
  318. @param This Pointer of the protocol EFI_SIMPLE_TEXT_IN_PROTOCOL.
  319. @retval EFI_SUCCESS A key is being pressed now.
  320. @retval Other No key is now pressed.
  321. **/
  322. EFI_STATUS
  323. EFIAPI
  324. VirtualKeyboardCheckForKey (
  325. IN EFI_SIMPLE_TEXT_INPUT_PROTOCOL *This
  326. )
  327. {
  328. VIRTUAL_KEYBOARD_DEV *VirtualKeyboardPrivate;
  329. VirtualKeyboardPrivate = VIRTUAL_KEYBOARD_DEV_FROM_THIS (This);
  330. return CheckQueue (&VirtualKeyboardPrivate->Queue);
  331. }
  332. /**
  333. Free keyboard notify list.
  334. @param ListHead The list head
  335. @retval EFI_SUCCESS Free the notify list successfully
  336. @retval EFI_INVALID_PARAMETER ListHead is invalid.
  337. **/
  338. EFI_STATUS
  339. VirtualKeyboardFreeNotifyList (
  340. IN OUT LIST_ENTRY *ListHead
  341. )
  342. {
  343. VIRTUAL_KEYBOARD_CONSOLE_IN_EX_NOTIFY *NotifyNode;
  344. if (ListHead == NULL) {
  345. return EFI_INVALID_PARAMETER;
  346. }
  347. while (!IsListEmpty (ListHead)) {
  348. NotifyNode = CR (
  349. ListHead->ForwardLink,
  350. VIRTUAL_KEYBOARD_CONSOLE_IN_EX_NOTIFY,
  351. NotifyEntry,
  352. VIRTUAL_KEYBOARD_CONSOLE_IN_EX_NOTIFY_SIGNATURE
  353. );
  354. RemoveEntryList (ListHead->ForwardLink);
  355. gBS->FreePool (NotifyNode);
  356. }
  357. return EFI_SUCCESS;
  358. }
  359. /**
  360. Judge whether is a registed key
  361. @param RegsiteredData A pointer to a buffer that is filled in with
  362. the keystroke state data for the key that was
  363. registered.
  364. @param InputData A pointer to a buffer that is filled in with
  365. the keystroke state data for the key that was
  366. pressed.
  367. @retval TRUE Key be pressed matches a registered key.
  368. @retval FALSE Match failed.
  369. **/
  370. BOOLEAN
  371. IsKeyRegistered (
  372. IN EFI_KEY_DATA *RegsiteredData,
  373. IN EFI_KEY_DATA *InputData
  374. )
  375. {
  376. ASSERT (RegsiteredData != NULL && InputData != NULL);
  377. if ((RegsiteredData->Key.ScanCode != InputData->Key.ScanCode) ||
  378. (RegsiteredData->Key.UnicodeChar != InputData->Key.UnicodeChar))
  379. {
  380. return FALSE;
  381. }
  382. //
  383. // Assume KeyShiftState/KeyToggleState = 0 in Registered key data means
  384. // these state could be ignored.
  385. //
  386. if ((RegsiteredData->KeyState.KeyShiftState != 0) &&
  387. (RegsiteredData->KeyState.KeyShiftState != InputData->KeyState.KeyShiftState))
  388. {
  389. return FALSE;
  390. }
  391. if ((RegsiteredData->KeyState.KeyToggleState != 0) &&
  392. (RegsiteredData->KeyState.KeyToggleState != InputData->KeyState.KeyToggleState))
  393. {
  394. return FALSE;
  395. }
  396. return TRUE;
  397. }
  398. /**
  399. Event notification function for SIMPLE_TEXT_IN.WaitForKey event
  400. Signal the event if there is key available
  401. @param Event the event object
  402. @param Context waiting context
  403. **/
  404. VOID
  405. EFIAPI
  406. VirtualKeyboardWaitForKey (
  407. IN EFI_EVENT Event,
  408. IN VOID *Context
  409. )
  410. {
  411. //
  412. // Stall 1ms to give a chance to let other driver interrupt this routine
  413. // for their timer event.
  414. // e.g. UI setup or Shell, other drivers which are driven by timer event
  415. // will have a bad performance during this period,
  416. // e.g. usb keyboard driver.
  417. // Add a stall period can greatly increate other driver performance during
  418. // the WaitForKey is recursivly invoked. 1ms delay will make little impact
  419. // to the thunk keyboard driver, and user can not feel the delay at all when
  420. // input.
  421. //
  422. gBS->Stall (1000);
  423. //
  424. // Use TimerEvent callback function to check whether there's any key pressed
  425. //
  426. VirtualKeyboardTimerHandler (NULL, VIRTUAL_KEYBOARD_DEV_FROM_THIS (Context));
  427. if (!EFI_ERROR (VirtualKeyboardCheckForKey (Context))) {
  428. gBS->SignalEvent (Event);
  429. }
  430. }
  431. /**
  432. Event notification function for SIMPLE_TEXT_INPUT_EX_PROTOCOL.WaitForKeyEx
  433. event. Signal the event if there is key available
  434. @param Event event object
  435. @param Context waiting context
  436. **/
  437. VOID
  438. EFIAPI
  439. VirtualKeyboardWaitForKeyEx (
  440. IN EFI_EVENT Event,
  441. IN VOID *Context
  442. )
  443. {
  444. VIRTUAL_KEYBOARD_DEV *VirtualKeyboardPrivate;
  445. VirtualKeyboardPrivate = TEXT_INPUT_EX_VIRTUAL_KEYBOARD_DEV_FROM_THIS (Context);
  446. VirtualKeyboardWaitForKey (Event, &VirtualKeyboardPrivate->SimpleTextIn);
  447. }
  448. //
  449. // EFI Simple Text In Protocol Functions
  450. //
  451. /**
  452. Reset the Keyboard and do BAT test for it, if (ExtendedVerification == TRUE)
  453. then do some extra keyboard validations.
  454. @param This Pointer of simple text Protocol.
  455. @param ExtendedVerification Whether perform the extra validation of
  456. keyboard. True: perform; FALSE: skip.
  457. @retval EFI_SUCCESS The command byte is written successfully.
  458. @retval EFI_DEVICE_ERROR Errors occurred during resetting keyboard.
  459. **/
  460. EFI_STATUS
  461. EFIAPI
  462. VirtualKeyboardReset (
  463. IN EFI_SIMPLE_TEXT_INPUT_PROTOCOL *This,
  464. IN BOOLEAN ExtendedVerification
  465. )
  466. {
  467. VIRTUAL_KEYBOARD_DEV *VirtualKeyboardPrivate;
  468. EFI_STATUS Status;
  469. EFI_TPL OldTpl;
  470. VirtualKeyboardPrivate = VIRTUAL_KEYBOARD_DEV_FROM_THIS (This);
  471. //
  472. // Raise TPL to avoid mouse operation impact
  473. //
  474. OldTpl = gBS->RaiseTPL (TPL_NOTIFY);
  475. if (VirtualKeyboardPrivate->PlatformVirtual &&
  476. VirtualKeyboardPrivate->PlatformVirtual->Reset)
  477. {
  478. Status = VirtualKeyboardPrivate->PlatformVirtual->Reset ();
  479. } else {
  480. Status = EFI_INVALID_PARAMETER;
  481. }
  482. //
  483. // resume priority of task level
  484. //
  485. gBS->RestoreTPL (OldTpl);
  486. return Status;
  487. }
  488. /**
  489. Reset the input device and optionally run diagnostics
  490. @param This Protocol instance pointer.
  491. @param ExtendedVerification Driver may perform diagnostics on reset.
  492. @retval EFI_SUCCESS The device was reset.
  493. @retval EFI_DEVICE_ERROR The device is not functioning properly and
  494. could not be reset.
  495. **/
  496. EFI_STATUS
  497. EFIAPI
  498. VirtualKeyboardResetEx (
  499. IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *This,
  500. IN BOOLEAN ExtendedVerification
  501. )
  502. {
  503. VIRTUAL_KEYBOARD_DEV *VirtualKeyboardPrivate;
  504. EFI_STATUS Status;
  505. EFI_TPL OldTpl;
  506. VirtualKeyboardPrivate = TEXT_INPUT_EX_VIRTUAL_KEYBOARD_DEV_FROM_THIS (This);
  507. Status = VirtualKeyboardPrivate->SimpleTextIn.Reset (
  508. &VirtualKeyboardPrivate->SimpleTextIn,
  509. ExtendedVerification
  510. );
  511. if (EFI_ERROR (Status)) {
  512. return EFI_DEVICE_ERROR;
  513. }
  514. OldTpl = gBS->RaiseTPL (TPL_NOTIFY);
  515. gBS->RestoreTPL (OldTpl);
  516. return EFI_SUCCESS;
  517. }
  518. /**
  519. Reads the next keystroke from the input device. The WaitForKey Event can
  520. be used to test for existence of a keystroke via WaitForEvent () call.
  521. @param VirtualKeyboardPrivate Virtualkeyboard driver private structure.
  522. @param KeyData A pointer to a buffer that is filled in
  523. with the keystroke state data for the key
  524. that was pressed.
  525. @retval EFI_SUCCESS The keystroke information was returned.
  526. @retval EFI_NOT_READY There was no keystroke data available.
  527. @retval EFI_DEVICE_ERROR The keystroke information was not returned
  528. due to hardware errors.
  529. @retval EFI_INVALID_PARAMETER KeyData is NULL.
  530. **/
  531. EFI_STATUS
  532. KeyboardReadKeyStrokeWorker (
  533. IN VIRTUAL_KEYBOARD_DEV *VirtualKeyboardPrivate,
  534. OUT EFI_KEY_DATA *KeyData
  535. )
  536. {
  537. EFI_STATUS Status;
  538. EFI_TPL OldTpl;
  539. if (KeyData == NULL) {
  540. return EFI_INVALID_PARAMETER;
  541. }
  542. //
  543. // Use TimerEvent callback function to check whether there's any key pressed
  544. //
  545. //
  546. // Stall 1ms to give a chance to let other driver interrupt this routine for
  547. // their timer event.
  548. // e.g. OS loader, other drivers which are driven by timer event will have a
  549. // bad performance during this period,
  550. // e.g. usb keyboard driver.
  551. // Add a stall period can greatly increate other driver performance during
  552. // the WaitForKey is recursivly invoked. 1ms delay will make little impact
  553. // to the thunk keyboard driver, and user can not feel the delay at all when
  554. // input.
  555. //
  556. gBS->Stall (1000);
  557. OldTpl = gBS->RaiseTPL (TPL_NOTIFY);
  558. VirtualKeyboardTimerHandler (NULL, VirtualKeyboardPrivate);
  559. //
  560. // If there's no key, just return
  561. //
  562. Status = CheckQueue (&VirtualKeyboardPrivate->Queue);
  563. if (EFI_ERROR (Status)) {
  564. gBS->RestoreTPL (OldTpl);
  565. return EFI_NOT_READY;
  566. }
  567. Status = Dequeue (&VirtualKeyboardPrivate->Queue, KeyData);
  568. gBS->RestoreTPL (OldTpl);
  569. return EFI_SUCCESS;
  570. }
  571. /**
  572. Read out the scan code of the key that has just been stroked.
  573. @param This Pointer of simple text Protocol.
  574. @param Key Pointer for store the key that read out.
  575. @retval EFI_SUCCESS The key is read out successfully.
  576. @retval other The key reading failed.
  577. **/
  578. EFI_STATUS
  579. EFIAPI
  580. VirtualKeyboardReadKeyStroke (
  581. IN EFI_SIMPLE_TEXT_INPUT_PROTOCOL *This,
  582. OUT EFI_INPUT_KEY *Key
  583. )
  584. {
  585. VIRTUAL_KEYBOARD_DEV *VirtualKeyboardPrivate;
  586. EFI_STATUS Status;
  587. EFI_KEY_DATA KeyData;
  588. VirtualKeyboardPrivate = VIRTUAL_KEYBOARD_DEV_FROM_THIS (This);
  589. Status = KeyboardReadKeyStrokeWorker (VirtualKeyboardPrivate, &KeyData);
  590. if (EFI_ERROR (Status)) {
  591. return Status;
  592. }
  593. //
  594. // Convert the Ctrl+[a-z] to Ctrl+[1-26]
  595. //
  596. if ((KeyData.KeyState.KeyShiftState & (EFI_LEFT_CONTROL_PRESSED | EFI_RIGHT_CONTROL_PRESSED)) != 0) {
  597. if ((KeyData.Key.UnicodeChar >= L'a') &&
  598. (KeyData.Key.UnicodeChar <= L'z'))
  599. {
  600. KeyData.Key.UnicodeChar = (CHAR16)(KeyData.Key.UnicodeChar - L'a' + 1);
  601. } else if ((KeyData.Key.UnicodeChar >= L'A') &&
  602. (KeyData.Key.UnicodeChar <= L'Z'))
  603. {
  604. KeyData.Key.UnicodeChar = (CHAR16)(KeyData.Key.UnicodeChar - L'A' + 1);
  605. }
  606. }
  607. CopyMem (Key, &KeyData.Key, sizeof (EFI_INPUT_KEY));
  608. return EFI_SUCCESS;
  609. }
  610. /**
  611. Reads the next keystroke from the input device. The WaitForKey Event can
  612. be used to test for existence of a keystroke via WaitForEvent () call.
  613. @param This Protocol instance pointer.
  614. @param KeyData A pointer to a buffer that is filled in with the
  615. keystroke state data for the key that was pressed.
  616. @retval EFI_SUCCESS The keystroke information was returned.
  617. @retval EFI_NOT_READY There was no keystroke data available.
  618. @retval EFI_DEVICE_ERROR The keystroke information was not returned
  619. due to hardware errors.
  620. @retval EFI_INVALID_PARAMETER KeyData is NULL.
  621. **/
  622. EFI_STATUS
  623. EFIAPI
  624. VirtualKeyboardReadKeyStrokeEx (
  625. IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *This,
  626. OUT EFI_KEY_DATA *KeyData
  627. )
  628. {
  629. VIRTUAL_KEYBOARD_DEV *VirtualKeyboardPrivate;
  630. if (KeyData == NULL) {
  631. return EFI_INVALID_PARAMETER;
  632. }
  633. VirtualKeyboardPrivate = TEXT_INPUT_EX_VIRTUAL_KEYBOARD_DEV_FROM_THIS (This);
  634. return KeyboardReadKeyStrokeWorker (VirtualKeyboardPrivate, KeyData);
  635. }
  636. /**
  637. Set certain state for the input device.
  638. @param This Protocol instance pointer.
  639. @param KeyToggleState A pointer to the EFI_KEY_TOGGLE_STATE to set the
  640. state for the input device.
  641. @retval EFI_SUCCESS The device state was set successfully.
  642. @retval EFI_DEVICE_ERROR The device is not functioning correctly and
  643. could not have the setting adjusted.
  644. @retval EFI_UNSUPPORTED The device does not have the ability to set
  645. its state.
  646. @retval EFI_INVALID_PARAMETER KeyToggleState is NULL.
  647. **/
  648. EFI_STATUS
  649. EFIAPI
  650. VirtualKeyboardSetState (
  651. IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *This,
  652. IN EFI_KEY_TOGGLE_STATE *KeyToggleState
  653. )
  654. {
  655. if (KeyToggleState == NULL) {
  656. return EFI_INVALID_PARAMETER;
  657. }
  658. return EFI_SUCCESS;
  659. }
  660. /**
  661. Register a notification function for a particular keystroke for the
  662. input device.
  663. @param This Protocol instance pointer.
  664. @param KeyData A pointer to a buffer that is filled in with
  665. the keystroke information data for the key
  666. that was pressed.
  667. @param KeyNotificationFunction Points to the function to be called when the
  668. key sequence is typed specified by KeyData.
  669. @param NotifyHandle Points to the unique handle assigned to the
  670. registered notification.
  671. @retval EFI_SUCCESS The notification function was registered
  672. successfully.
  673. @retval EFI_OUT_OF_RESOURCES Unable to allocate resources for necessary
  674. data structures.
  675. @retval EFI_INVALID_PARAMETER KeyData or NotifyHandle is NULL.
  676. **/
  677. EFI_STATUS
  678. EFIAPI
  679. VirtualKeyboardRegisterKeyNotify (
  680. IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *This,
  681. IN EFI_KEY_DATA *KeyData,
  682. IN EFI_KEY_NOTIFY_FUNCTION KeyNotificationFunction,
  683. OUT VOID **NotifyHandle
  684. )
  685. {
  686. EFI_STATUS Status;
  687. VIRTUAL_KEYBOARD_DEV *VirtualKeyboardPrivate;
  688. EFI_TPL OldTpl;
  689. VIRTUAL_KEYBOARD_CONSOLE_IN_EX_NOTIFY *NewNotify;
  690. LIST_ENTRY *Link;
  691. VIRTUAL_KEYBOARD_CONSOLE_IN_EX_NOTIFY *CurrentNotify;
  692. if ((KeyData == NULL) ||
  693. (NotifyHandle == NULL) ||
  694. (KeyNotificationFunction == NULL))
  695. {
  696. return EFI_INVALID_PARAMETER;
  697. }
  698. VirtualKeyboardPrivate = TEXT_INPUT_EX_VIRTUAL_KEYBOARD_DEV_FROM_THIS (This);
  699. //
  700. // Enter critical section
  701. //
  702. OldTpl = gBS->RaiseTPL (TPL_NOTIFY);
  703. //
  704. // Return EFI_SUCCESS if the (KeyData, NotificationFunction) is already
  705. // registered.
  706. //
  707. for (Link = VirtualKeyboardPrivate->NotifyList.ForwardLink;
  708. Link != &VirtualKeyboardPrivate->NotifyList;
  709. Link = Link->ForwardLink)
  710. {
  711. CurrentNotify = CR (
  712. Link,
  713. VIRTUAL_KEYBOARD_CONSOLE_IN_EX_NOTIFY,
  714. NotifyEntry,
  715. VIRTUAL_KEYBOARD_CONSOLE_IN_EX_NOTIFY_SIGNATURE
  716. );
  717. if (IsKeyRegistered (&CurrentNotify->KeyData, KeyData)) {
  718. if (CurrentNotify->KeyNotificationFn == KeyNotificationFunction) {
  719. *NotifyHandle = CurrentNotify;
  720. Status = EFI_SUCCESS;
  721. goto Exit;
  722. }
  723. }
  724. }
  725. //
  726. // Allocate resource to save the notification function
  727. //
  728. NewNotify = (VIRTUAL_KEYBOARD_CONSOLE_IN_EX_NOTIFY *)AllocateZeroPool (sizeof (VIRTUAL_KEYBOARD_CONSOLE_IN_EX_NOTIFY));
  729. if (NewNotify == NULL) {
  730. Status = EFI_OUT_OF_RESOURCES;
  731. goto Exit;
  732. }
  733. NewNotify->Signature = VIRTUAL_KEYBOARD_CONSOLE_IN_EX_NOTIFY_SIGNATURE;
  734. NewNotify->KeyNotificationFn = KeyNotificationFunction;
  735. CopyMem (&NewNotify->KeyData, KeyData, sizeof (EFI_KEY_DATA));
  736. InsertTailList (&VirtualKeyboardPrivate->NotifyList, &NewNotify->NotifyEntry);
  737. *NotifyHandle = NewNotify;
  738. Status = EFI_SUCCESS;
  739. Exit:
  740. //
  741. // Leave critical section and return
  742. //
  743. gBS->RestoreTPL (OldTpl);
  744. return Status;
  745. }
  746. /**
  747. Remove a registered notification function from a particular keystroke.
  748. @param This Protocol instance pointer.
  749. @param NotificationHandle The handle of the notification function
  750. being unregistered.
  751. @retval EFI_SUCCESS The notification function was unregistered
  752. successfully.
  753. @retval EFI_INVALID_PARAMETER The NotificationHandle is invalid.
  754. **/
  755. EFI_STATUS
  756. EFIAPI
  757. VirtualKeyboardUnregisterKeyNotify (
  758. IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *This,
  759. IN VOID *NotificationHandle
  760. )
  761. {
  762. EFI_STATUS Status;
  763. VIRTUAL_KEYBOARD_DEV *VirtualKeyboardPrivate;
  764. EFI_TPL OldTpl;
  765. LIST_ENTRY *Link;
  766. VIRTUAL_KEYBOARD_CONSOLE_IN_EX_NOTIFY *CurrentNotify;
  767. //
  768. // Check incoming notification handle
  769. //
  770. if (NotificationHandle == NULL) {
  771. return EFI_INVALID_PARAMETER;
  772. }
  773. if (((VIRTUAL_KEYBOARD_CONSOLE_IN_EX_NOTIFY *)NotificationHandle)->Signature !=
  774. VIRTUAL_KEYBOARD_CONSOLE_IN_EX_NOTIFY_SIGNATURE)
  775. {
  776. return EFI_INVALID_PARAMETER;
  777. }
  778. VirtualKeyboardPrivate = TEXT_INPUT_EX_VIRTUAL_KEYBOARD_DEV_FROM_THIS (This);
  779. //
  780. // Enter critical section
  781. //
  782. OldTpl = gBS->RaiseTPL (TPL_NOTIFY);
  783. for (Link = VirtualKeyboardPrivate->NotifyList.ForwardLink;
  784. Link != &VirtualKeyboardPrivate->NotifyList;
  785. Link = Link->ForwardLink)
  786. {
  787. CurrentNotify = CR (
  788. Link,
  789. VIRTUAL_KEYBOARD_CONSOLE_IN_EX_NOTIFY,
  790. NotifyEntry,
  791. VIRTUAL_KEYBOARD_CONSOLE_IN_EX_NOTIFY_SIGNATURE
  792. );
  793. if (CurrentNotify == NotificationHandle) {
  794. //
  795. // Remove the notification function from NotifyList and free resources
  796. //
  797. RemoveEntryList (&CurrentNotify->NotifyEntry);
  798. Status = EFI_SUCCESS;
  799. goto Exit;
  800. }
  801. }
  802. //
  803. // Can not find the specified Notification Handle
  804. //
  805. Status = EFI_INVALID_PARAMETER;
  806. Exit:
  807. //
  808. // Leave critical section and return
  809. //
  810. gBS->RestoreTPL (OldTpl);
  811. return Status;
  812. }
  813. /**
  814. Timer event handler: read a series of scancodes from 8042
  815. and put them into memory scancode buffer.
  816. it read as much scancodes to either fill
  817. the memory buffer or empty the keyboard buffer.
  818. It is registered as running under TPL_NOTIFY
  819. @param Event The timer event
  820. @param Context A KEYBOARD_CONSOLE_IN_DEV pointer
  821. **/
  822. VOID
  823. EFIAPI
  824. VirtualKeyboardTimerHandler (
  825. IN EFI_EVENT Event,
  826. IN VOID *Context
  827. )
  828. {
  829. EFI_TPL OldTpl;
  830. LIST_ENTRY *Link;
  831. EFI_KEY_DATA KeyData;
  832. VIRTUAL_KEYBOARD_CONSOLE_IN_EX_NOTIFY *CurrentNotify;
  833. VIRTUAL_KEYBOARD_DEV *VirtualKeyboardPrivate;
  834. VIRTUAL_KBD_KEY VirtualKey;
  835. VirtualKeyboardPrivate = Context;
  836. //
  837. // Enter critical section
  838. //
  839. OldTpl = gBS->RaiseTPL (TPL_NOTIFY);
  840. if (VirtualKeyboardPrivate->PlatformVirtual &&
  841. VirtualKeyboardPrivate->PlatformVirtual->Query)
  842. {
  843. if (VirtualKeyboardPrivate->PlatformVirtual->Query (&VirtualKey) ==
  844. FALSE)
  845. {
  846. goto Exit;
  847. }
  848. // Found key
  849. KeyData.Key.ScanCode = VirtualKey.Key.ScanCode;
  850. KeyData.Key.UnicodeChar = VirtualKey.Key.UnicodeChar;
  851. KeyData.KeyState.KeyShiftState = EFI_SHIFT_STATE_VALID;
  852. KeyData.KeyState.KeyToggleState = EFI_TOGGLE_STATE_VALID;
  853. if (VirtualKeyboardPrivate->PlatformVirtual->Clear) {
  854. VirtualKeyboardPrivate->PlatformVirtual->Clear (&VirtualKey);
  855. }
  856. } else {
  857. goto Exit;
  858. }
  859. //
  860. // Signal KeyNotify process event if this key pressed matches any key registered.
  861. //
  862. for (Link = VirtualKeyboardPrivate->NotifyList.ForwardLink;
  863. Link != &VirtualKeyboardPrivate->NotifyList;
  864. Link = Link->ForwardLink)
  865. {
  866. CurrentNotify = CR (
  867. Link,
  868. VIRTUAL_KEYBOARD_CONSOLE_IN_EX_NOTIFY,
  869. NotifyEntry,
  870. VIRTUAL_KEYBOARD_CONSOLE_IN_EX_NOTIFY_SIGNATURE
  871. );
  872. if (IsKeyRegistered (&CurrentNotify->KeyData, &KeyData)) {
  873. //
  874. // The key notification function needs to run at TPL_CALLBACK
  875. // while current TPL is TPL_NOTIFY. It will be invoked in
  876. // KeyNotifyProcessHandler() which runs at TPL_CALLBACK.
  877. //
  878. Enqueue (&VirtualKeyboardPrivate->QueueForNotify, &KeyData);
  879. gBS->SignalEvent (VirtualKeyboardPrivate->KeyNotifyProcessEvent);
  880. break;
  881. }
  882. }
  883. Enqueue (&VirtualKeyboardPrivate->Queue, &KeyData);
  884. Exit:
  885. //
  886. // Leave critical section and return
  887. //
  888. gBS->RestoreTPL (OldTpl);
  889. }
  890. /**
  891. Process key notify.
  892. @param Event Indicates the event that invoke this function.
  893. @param Context Indicates the calling context.
  894. **/
  895. VOID
  896. EFIAPI
  897. KeyNotifyProcessHandler (
  898. IN EFI_EVENT Event,
  899. IN VOID *Context
  900. )
  901. {
  902. EFI_STATUS Status;
  903. VIRTUAL_KEYBOARD_DEV *VirtualKeyboardPrivate;
  904. EFI_KEY_DATA KeyData;
  905. LIST_ENTRY *Link;
  906. LIST_ENTRY *NotifyList;
  907. VIRTUAL_KEYBOARD_CONSOLE_IN_EX_NOTIFY *CurrentNotify;
  908. EFI_TPL OldTpl;
  909. VirtualKeyboardPrivate = (VIRTUAL_KEYBOARD_DEV *)Context;
  910. //
  911. // Invoke notification functions.
  912. //
  913. NotifyList = &VirtualKeyboardPrivate->NotifyList;
  914. while (TRUE) {
  915. //
  916. // Enter critical section
  917. //
  918. OldTpl = gBS->RaiseTPL (TPL_NOTIFY);
  919. Status = Dequeue (&VirtualKeyboardPrivate->QueueForNotify, &KeyData);
  920. //
  921. // Leave critical section
  922. //
  923. gBS->RestoreTPL (OldTpl);
  924. if (EFI_ERROR (Status)) {
  925. break;
  926. }
  927. for (Link = GetFirstNode (NotifyList);
  928. !IsNull (NotifyList, Link);
  929. Link = GetNextNode (NotifyList, Link))
  930. {
  931. CurrentNotify = CR (
  932. Link,
  933. VIRTUAL_KEYBOARD_CONSOLE_IN_EX_NOTIFY,
  934. NotifyEntry,
  935. VIRTUAL_KEYBOARD_CONSOLE_IN_EX_NOTIFY_SIGNATURE
  936. );
  937. if (IsKeyRegistered (&CurrentNotify->KeyData, &KeyData)) {
  938. CurrentNotify->KeyNotificationFn (&KeyData);
  939. }
  940. }
  941. }
  942. }
  943. /**
  944. The user Entry Point for module VirtualKeyboard. The user code starts with
  945. this function.
  946. @param[in] ImageHandle The firmware allocated handle for the EFI image.
  947. @param[in] SystemTable A pointer to the EFI System Table.
  948. @retval EFI_SUCCESS The entry point is executed successfully.
  949. @retval other Some error occurs when executing this entry point.
  950. **/
  951. EFI_STATUS
  952. EFIAPI
  953. InitializeVirtualKeyboard (
  954. IN EFI_HANDLE ImageHandle,
  955. IN EFI_SYSTEM_TABLE *SystemTable
  956. )
  957. {
  958. EFI_STATUS Status;
  959. //
  960. // Install driver model protocol(s).
  961. //
  962. Status = EfiLibInstallDriverBindingComponentName2 (
  963. ImageHandle,
  964. SystemTable,
  965. &gVirtualKeyboardDriverBinding,
  966. ImageHandle,
  967. &gVirtualKeyboardComponentName,
  968. &gVirtualKeyboardComponentName2
  969. );
  970. ASSERT_EFI_ERROR (Status);
  971. return Status;
  972. }