VirtualKeyboard.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143
  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 FLASE 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. return FALSE;
  380. }
  381. //
  382. // Assume KeyShiftState/KeyToggleState = 0 in Registered key data means
  383. // these state could be ignored.
  384. //
  385. if ((RegsiteredData->KeyState.KeyShiftState != 0) &&
  386. (RegsiteredData->KeyState.KeyShiftState != InputData->KeyState.KeyShiftState)) {
  387. return FALSE;
  388. }
  389. if ((RegsiteredData->KeyState.KeyToggleState != 0) &&
  390. (RegsiteredData->KeyState.KeyToggleState != InputData->KeyState.KeyToggleState)) {
  391. return FALSE;
  392. }
  393. return TRUE;
  394. }
  395. /**
  396. Event notification function for SIMPLE_TEXT_IN.WaitForKey event
  397. Signal the event if there is key available
  398. @param Event the event object
  399. @param Context waitting context
  400. **/
  401. VOID
  402. EFIAPI
  403. VirtualKeyboardWaitForKey (
  404. IN EFI_EVENT Event,
  405. IN VOID *Context
  406. )
  407. {
  408. //
  409. // Stall 1ms to give a chance to let other driver interrupt this routine
  410. // for their timer event.
  411. // e.g. UI setup or Shell, other drivers which are driven by timer event
  412. // will have a bad performance during this period,
  413. // e.g. usb keyboard driver.
  414. // Add a stall period can greatly increate other driver performance during
  415. // the WaitForKey is recursivly invoked. 1ms delay will make little impact
  416. // to the thunk keyboard driver, and user can not feel the delay at all when
  417. // input.
  418. //
  419. gBS->Stall (1000);
  420. //
  421. // Use TimerEvent callback function to check whether there's any key pressed
  422. //
  423. VirtualKeyboardTimerHandler (NULL, VIRTUAL_KEYBOARD_DEV_FROM_THIS (Context));
  424. if (!EFI_ERROR (VirtualKeyboardCheckForKey (Context))) {
  425. gBS->SignalEvent (Event);
  426. }
  427. }
  428. /**
  429. Event notification function for SIMPLE_TEXT_INPUT_EX_PROTOCOL.WaitForKeyEx
  430. event. Signal the event if there is key available
  431. @param Event event object
  432. @param Context waiting context
  433. **/
  434. VOID
  435. EFIAPI
  436. VirtualKeyboardWaitForKeyEx (
  437. IN EFI_EVENT Event,
  438. IN VOID *Context
  439. )
  440. {
  441. VIRTUAL_KEYBOARD_DEV *VirtualKeyboardPrivate;
  442. VirtualKeyboardPrivate = TEXT_INPUT_EX_VIRTUAL_KEYBOARD_DEV_FROM_THIS (Context);
  443. VirtualKeyboardWaitForKey (Event, &VirtualKeyboardPrivate->SimpleTextIn);
  444. }
  445. //
  446. // EFI Simple Text In Protocol Functions
  447. //
  448. /**
  449. Reset the Keyboard and do BAT test for it, if (ExtendedVerification == TRUE)
  450. then do some extra keyboard validations.
  451. @param This Pointer of simple text Protocol.
  452. @param ExtendedVerification Whether perform the extra validation of
  453. keyboard. True: perform; FALSE: skip.
  454. @retval EFI_SUCCESS The command byte is written successfully.
  455. @retval EFI_DEVICE_ERROR Errors occurred during resetting keyboard.
  456. **/
  457. EFI_STATUS
  458. EFIAPI
  459. VirtualKeyboardReset (
  460. IN EFI_SIMPLE_TEXT_INPUT_PROTOCOL *This,
  461. IN BOOLEAN ExtendedVerification
  462. )
  463. {
  464. VIRTUAL_KEYBOARD_DEV *VirtualKeyboardPrivate;
  465. EFI_STATUS Status;
  466. EFI_TPL OldTpl;
  467. VirtualKeyboardPrivate = VIRTUAL_KEYBOARD_DEV_FROM_THIS (This);
  468. //
  469. // Raise TPL to avoid mouse operation impact
  470. //
  471. OldTpl = gBS->RaiseTPL (TPL_NOTIFY);
  472. if (VirtualKeyboardPrivate->PlatformVirtual &&
  473. VirtualKeyboardPrivate->PlatformVirtual->Reset) {
  474. Status = VirtualKeyboardPrivate->PlatformVirtual->Reset ();
  475. } else {
  476. Status = EFI_INVALID_PARAMETER;
  477. }
  478. //
  479. // resume priority of task level
  480. //
  481. gBS->RestoreTPL (OldTpl);
  482. return Status;
  483. }
  484. /**
  485. Reset the input device and optionaly run diagnostics
  486. @param This Protocol instance pointer.
  487. @param ExtendedVerification Driver may perform diagnostics on reset.
  488. @retval EFI_SUCCESS The device was reset.
  489. @retval EFI_DEVICE_ERROR The device is not functioning properly and
  490. could not be reset.
  491. **/
  492. EFI_STATUS
  493. EFIAPI
  494. VirtualKeyboardResetEx (
  495. IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *This,
  496. IN BOOLEAN ExtendedVerification
  497. )
  498. {
  499. VIRTUAL_KEYBOARD_DEV *VirtualKeyboardPrivate;
  500. EFI_STATUS Status;
  501. EFI_TPL OldTpl;
  502. VirtualKeyboardPrivate = TEXT_INPUT_EX_VIRTUAL_KEYBOARD_DEV_FROM_THIS (This);
  503. Status = VirtualKeyboardPrivate->SimpleTextIn.Reset (
  504. &VirtualKeyboardPrivate->SimpleTextIn,
  505. ExtendedVerification
  506. );
  507. if (EFI_ERROR (Status)) {
  508. return EFI_DEVICE_ERROR;
  509. }
  510. OldTpl = gBS->RaiseTPL (TPL_NOTIFY);
  511. gBS->RestoreTPL (OldTpl);
  512. return EFI_SUCCESS;
  513. }
  514. /**
  515. Reads the next keystroke from the input device. The WaitForKey Event can
  516. be used to test for existance of a keystroke via WaitForEvent () call.
  517. @param VirtualKeyboardPrivate Virtualkeyboard driver private structure.
  518. @param KeyData A pointer to a buffer that is filled in
  519. with the keystroke state data for the key
  520. that was pressed.
  521. @retval EFI_SUCCESS The keystroke information was returned.
  522. @retval EFI_NOT_READY There was no keystroke data availiable.
  523. @retval EFI_DEVICE_ERROR The keystroke information was not returned
  524. due to hardware errors.
  525. @retval EFI_INVALID_PARAMETER KeyData is NULL.
  526. **/
  527. EFI_STATUS
  528. KeyboardReadKeyStrokeWorker (
  529. IN VIRTUAL_KEYBOARD_DEV *VirtualKeyboardPrivate,
  530. OUT EFI_KEY_DATA *KeyData
  531. )
  532. {
  533. EFI_STATUS Status;
  534. EFI_TPL OldTpl;
  535. if (KeyData == NULL) {
  536. return EFI_INVALID_PARAMETER;
  537. }
  538. //
  539. // Use TimerEvent callback function to check whether there's any key pressed
  540. //
  541. //
  542. // Stall 1ms to give a chance to let other driver interrupt this routine for
  543. // their timer event.
  544. // e.g. OS loader, other drivers which are driven by timer event will have a
  545. // bad performance during this period,
  546. // e.g. usb keyboard driver.
  547. // Add a stall period can greatly increate other driver performance during
  548. // the WaitForKey is recursivly invoked. 1ms delay will make little impact
  549. // to the thunk keyboard driver, and user can not feel the delay at all when
  550. // input.
  551. //
  552. gBS->Stall (1000);
  553. OldTpl = gBS->RaiseTPL (TPL_NOTIFY);
  554. VirtualKeyboardTimerHandler (NULL, VirtualKeyboardPrivate);
  555. //
  556. // If there's no key, just return
  557. //
  558. Status = CheckQueue (&VirtualKeyboardPrivate->Queue);
  559. if (EFI_ERROR (Status)) {
  560. gBS->RestoreTPL (OldTpl);
  561. return EFI_NOT_READY;
  562. }
  563. Status = Dequeue (&VirtualKeyboardPrivate->Queue, KeyData);
  564. gBS->RestoreTPL (OldTpl);
  565. return EFI_SUCCESS;
  566. }
  567. /**
  568. Read out the scan code of the key that has just been stroked.
  569. @param This Pointer of simple text Protocol.
  570. @param Key Pointer for store the key that read out.
  571. @retval EFI_SUCCESS The key is read out successfully.
  572. @retval other The key reading failed.
  573. **/
  574. EFI_STATUS
  575. EFIAPI
  576. VirtualKeyboardReadKeyStroke (
  577. IN EFI_SIMPLE_TEXT_INPUT_PROTOCOL *This,
  578. OUT EFI_INPUT_KEY *Key
  579. )
  580. {
  581. VIRTUAL_KEYBOARD_DEV *VirtualKeyboardPrivate;
  582. EFI_STATUS Status;
  583. EFI_KEY_DATA KeyData;
  584. VirtualKeyboardPrivate = VIRTUAL_KEYBOARD_DEV_FROM_THIS (This);
  585. Status = KeyboardReadKeyStrokeWorker (VirtualKeyboardPrivate, &KeyData);
  586. if (EFI_ERROR (Status)) {
  587. return Status;
  588. }
  589. //
  590. // Convert the Ctrl+[a-z] to Ctrl+[1-26]
  591. //
  592. if ((KeyData.KeyState.KeyShiftState & (EFI_LEFT_CONTROL_PRESSED | EFI_RIGHT_CONTROL_PRESSED)) != 0) {
  593. if (KeyData.Key.UnicodeChar >= L'a' &&
  594. KeyData.Key.UnicodeChar <= L'z') {
  595. KeyData.Key.UnicodeChar = (CHAR16) (KeyData.Key.UnicodeChar - L'a' + 1);
  596. } else if (KeyData.Key.UnicodeChar >= L'A' &&
  597. KeyData.Key.UnicodeChar <= L'Z') {
  598. KeyData.Key.UnicodeChar = (CHAR16) (KeyData.Key.UnicodeChar - L'A' + 1);
  599. }
  600. }
  601. CopyMem (Key, &KeyData.Key, sizeof (EFI_INPUT_KEY));
  602. return EFI_SUCCESS;
  603. }
  604. /**
  605. Reads the next keystroke from the input device. The WaitForKey Event can
  606. be used to test for existance of a keystroke via WaitForEvent () call.
  607. @param This Protocol instance pointer.
  608. @param KeyData A pointer to a buffer that is filled in with the
  609. keystroke state data for the key that was pressed.
  610. @retval EFI_SUCCESS The keystroke information was returned.
  611. @retval EFI_NOT_READY There was no keystroke data availiable.
  612. @retval EFI_DEVICE_ERROR The keystroke information was not returned
  613. due to hardware errors.
  614. @retval EFI_INVALID_PARAMETER KeyData is NULL.
  615. **/
  616. EFI_STATUS
  617. EFIAPI
  618. VirtualKeyboardReadKeyStrokeEx (
  619. IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *This,
  620. OUT EFI_KEY_DATA *KeyData
  621. )
  622. {
  623. VIRTUAL_KEYBOARD_DEV *VirtualKeyboardPrivate;
  624. if (KeyData == NULL) {
  625. return EFI_INVALID_PARAMETER;
  626. }
  627. VirtualKeyboardPrivate = TEXT_INPUT_EX_VIRTUAL_KEYBOARD_DEV_FROM_THIS (This);
  628. return KeyboardReadKeyStrokeWorker (VirtualKeyboardPrivate, KeyData);
  629. }
  630. /**
  631. Set certain state for the input device.
  632. @param This Protocol instance pointer.
  633. @param KeyToggleState A pointer to the EFI_KEY_TOGGLE_STATE to set the
  634. state for the input device.
  635. @retval EFI_SUCCESS The device state was set successfully.
  636. @retval EFI_DEVICE_ERROR The device is not functioning correctly and
  637. could not have the setting adjusted.
  638. @retval EFI_UNSUPPORTED The device does not have the ability to set
  639. its state.
  640. @retval EFI_INVALID_PARAMETER KeyToggleState is NULL.
  641. **/
  642. EFI_STATUS
  643. EFIAPI
  644. VirtualKeyboardSetState (
  645. IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *This,
  646. IN EFI_KEY_TOGGLE_STATE *KeyToggleState
  647. )
  648. {
  649. if (KeyToggleState == NULL) {
  650. return EFI_INVALID_PARAMETER;
  651. }
  652. return EFI_SUCCESS;
  653. }
  654. /**
  655. Register a notification function for a particular keystroke for the
  656. input device.
  657. @param This Protocol instance pointer.
  658. @param KeyData A pointer to a buffer that is filled in with
  659. the keystroke information data for the key
  660. that was pressed.
  661. @param KeyNotificationFunction Points to the function to be called when the
  662. key sequence is typed specified by KeyData.
  663. @param NotifyHandle Points to the unique handle assigned to the
  664. registered notification.
  665. @retval EFI_SUCCESS The notification function was registered
  666. successfully.
  667. @retval EFI_OUT_OF_RESOURCES Unable to allocate resources for necesssary
  668. data structures.
  669. @retval EFI_INVALID_PARAMETER KeyData or NotifyHandle is NULL.
  670. **/
  671. EFI_STATUS
  672. EFIAPI
  673. VirtualKeyboardRegisterKeyNotify (
  674. IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *This,
  675. IN EFI_KEY_DATA *KeyData,
  676. IN EFI_KEY_NOTIFY_FUNCTION KeyNotificationFunction,
  677. OUT VOID **NotifyHandle
  678. )
  679. {
  680. EFI_STATUS Status;
  681. VIRTUAL_KEYBOARD_DEV *VirtualKeyboardPrivate;
  682. EFI_TPL OldTpl;
  683. VIRTUAL_KEYBOARD_CONSOLE_IN_EX_NOTIFY *NewNotify;
  684. LIST_ENTRY *Link;
  685. VIRTUAL_KEYBOARD_CONSOLE_IN_EX_NOTIFY *CurrentNotify;
  686. if (KeyData == NULL ||
  687. NotifyHandle == NULL ||
  688. KeyNotificationFunction == NULL) {
  689. return EFI_INVALID_PARAMETER;
  690. }
  691. VirtualKeyboardPrivate = TEXT_INPUT_EX_VIRTUAL_KEYBOARD_DEV_FROM_THIS (This);
  692. //
  693. // Enter critical section
  694. //
  695. OldTpl = gBS->RaiseTPL (TPL_NOTIFY);
  696. //
  697. // Return EFI_SUCCESS if the (KeyData, NotificationFunction) is already
  698. // registered.
  699. //
  700. for (Link = VirtualKeyboardPrivate->NotifyList.ForwardLink;
  701. Link != &VirtualKeyboardPrivate->NotifyList;
  702. Link = Link->ForwardLink) {
  703. CurrentNotify = CR (
  704. Link,
  705. VIRTUAL_KEYBOARD_CONSOLE_IN_EX_NOTIFY,
  706. NotifyEntry,
  707. VIRTUAL_KEYBOARD_CONSOLE_IN_EX_NOTIFY_SIGNATURE
  708. );
  709. if (IsKeyRegistered (&CurrentNotify->KeyData, KeyData)) {
  710. if (CurrentNotify->KeyNotificationFn == KeyNotificationFunction) {
  711. *NotifyHandle = CurrentNotify;
  712. Status = EFI_SUCCESS;
  713. goto Exit;
  714. }
  715. }
  716. }
  717. //
  718. // Allocate resource to save the notification function
  719. //
  720. NewNotify = (VIRTUAL_KEYBOARD_CONSOLE_IN_EX_NOTIFY *) AllocateZeroPool (sizeof (VIRTUAL_KEYBOARD_CONSOLE_IN_EX_NOTIFY));
  721. if (NewNotify == NULL) {
  722. Status = EFI_OUT_OF_RESOURCES;
  723. goto Exit;
  724. }
  725. NewNotify->Signature = VIRTUAL_KEYBOARD_CONSOLE_IN_EX_NOTIFY_SIGNATURE;
  726. NewNotify->KeyNotificationFn = KeyNotificationFunction;
  727. CopyMem (&NewNotify->KeyData, KeyData, sizeof (EFI_KEY_DATA));
  728. InsertTailList (&VirtualKeyboardPrivate->NotifyList, &NewNotify->NotifyEntry);
  729. *NotifyHandle = NewNotify;
  730. Status = EFI_SUCCESS;
  731. Exit:
  732. //
  733. // Leave critical section and return
  734. //
  735. gBS->RestoreTPL (OldTpl);
  736. return Status;
  737. }
  738. /**
  739. Remove a registered notification function from a particular keystroke.
  740. @param This Protocol instance pointer.
  741. @param NotificationHandle The handle of the notification function
  742. being unregistered.
  743. @retval EFI_SUCCESS The notification function was unregistered
  744. successfully.
  745. @retval EFI_INVALID_PARAMETER The NotificationHandle is invalid.
  746. **/
  747. EFI_STATUS
  748. EFIAPI
  749. VirtualKeyboardUnregisterKeyNotify (
  750. IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *This,
  751. IN VOID *NotificationHandle
  752. )
  753. {
  754. EFI_STATUS Status;
  755. VIRTUAL_KEYBOARD_DEV *VirtualKeyboardPrivate;
  756. EFI_TPL OldTpl;
  757. LIST_ENTRY *Link;
  758. VIRTUAL_KEYBOARD_CONSOLE_IN_EX_NOTIFY *CurrentNotify;
  759. //
  760. // Check incoming notification handle
  761. //
  762. if (NotificationHandle == NULL) {
  763. return EFI_INVALID_PARAMETER;
  764. }
  765. if (((VIRTUAL_KEYBOARD_CONSOLE_IN_EX_NOTIFY *) NotificationHandle)->Signature !=
  766. VIRTUAL_KEYBOARD_CONSOLE_IN_EX_NOTIFY_SIGNATURE) {
  767. return EFI_INVALID_PARAMETER;
  768. }
  769. VirtualKeyboardPrivate = TEXT_INPUT_EX_VIRTUAL_KEYBOARD_DEV_FROM_THIS (This);
  770. //
  771. // Enter critical section
  772. //
  773. OldTpl = gBS->RaiseTPL (TPL_NOTIFY);
  774. for (Link = VirtualKeyboardPrivate->NotifyList.ForwardLink;
  775. Link != &VirtualKeyboardPrivate->NotifyList;
  776. Link = Link->ForwardLink) {
  777. CurrentNotify = CR (
  778. Link,
  779. VIRTUAL_KEYBOARD_CONSOLE_IN_EX_NOTIFY,
  780. NotifyEntry,
  781. VIRTUAL_KEYBOARD_CONSOLE_IN_EX_NOTIFY_SIGNATURE
  782. );
  783. if (CurrentNotify == NotificationHandle) {
  784. //
  785. // Remove the notification function from NotifyList and free resources
  786. //
  787. RemoveEntryList (&CurrentNotify->NotifyEntry);
  788. Status = EFI_SUCCESS;
  789. goto Exit;
  790. }
  791. }
  792. //
  793. // Can not find the specified Notification Handle
  794. //
  795. Status = EFI_INVALID_PARAMETER;
  796. Exit:
  797. //
  798. // Leave critical section and return
  799. //
  800. gBS->RestoreTPL (OldTpl);
  801. return Status;
  802. }
  803. /**
  804. Timer event handler: read a series of scancodes from 8042
  805. and put them into memory scancode buffer.
  806. it read as much scancodes to either fill
  807. the memory buffer or empty the keyboard buffer.
  808. It is registered as running under TPL_NOTIFY
  809. @param Event The timer event
  810. @param Context A KEYBOARD_CONSOLE_IN_DEV pointer
  811. **/
  812. VOID
  813. EFIAPI
  814. VirtualKeyboardTimerHandler (
  815. IN EFI_EVENT Event,
  816. IN VOID *Context
  817. )
  818. {
  819. EFI_TPL OldTpl;
  820. LIST_ENTRY *Link;
  821. EFI_KEY_DATA KeyData;
  822. VIRTUAL_KEYBOARD_CONSOLE_IN_EX_NOTIFY *CurrentNotify;
  823. VIRTUAL_KEYBOARD_DEV *VirtualKeyboardPrivate;
  824. VIRTUAL_KBD_KEY VirtualKey;
  825. VirtualKeyboardPrivate = Context;
  826. //
  827. // Enter critical section
  828. //
  829. OldTpl = gBS->RaiseTPL (TPL_NOTIFY);
  830. if (VirtualKeyboardPrivate->PlatformVirtual &&
  831. VirtualKeyboardPrivate->PlatformVirtual->Query) {
  832. if (VirtualKeyboardPrivate->PlatformVirtual->Query (&VirtualKey) ==
  833. FALSE) {
  834. goto Exit;
  835. }
  836. // Found key
  837. KeyData.Key.ScanCode = VirtualKey.Key.ScanCode;
  838. KeyData.Key.UnicodeChar = VirtualKey.Key.UnicodeChar;
  839. KeyData.KeyState.KeyShiftState = EFI_SHIFT_STATE_VALID;
  840. KeyData.KeyState.KeyToggleState = EFI_TOGGLE_STATE_VALID;
  841. if (VirtualKeyboardPrivate->PlatformVirtual->Clear) {
  842. VirtualKeyboardPrivate->PlatformVirtual->Clear (&VirtualKey);
  843. }
  844. } else {
  845. goto Exit;
  846. }
  847. //
  848. // Signal KeyNotify process event if this key pressed matches any key registered.
  849. //
  850. for (Link = VirtualKeyboardPrivate->NotifyList.ForwardLink;
  851. Link != &VirtualKeyboardPrivate->NotifyList;
  852. Link = Link->ForwardLink) {
  853. CurrentNotify = CR (
  854. Link,
  855. VIRTUAL_KEYBOARD_CONSOLE_IN_EX_NOTIFY,
  856. NotifyEntry,
  857. VIRTUAL_KEYBOARD_CONSOLE_IN_EX_NOTIFY_SIGNATURE
  858. );
  859. if (IsKeyRegistered (&CurrentNotify->KeyData, &KeyData)) {
  860. //
  861. // The key notification function needs to run at TPL_CALLBACK
  862. // while current TPL is TPL_NOTIFY. It will be invoked in
  863. // KeyNotifyProcessHandler() which runs at TPL_CALLBACK.
  864. //
  865. Enqueue (&VirtualKeyboardPrivate->QueueForNotify, &KeyData);
  866. gBS->SignalEvent (VirtualKeyboardPrivate->KeyNotifyProcessEvent);
  867. break;
  868. }
  869. }
  870. Enqueue (&VirtualKeyboardPrivate->Queue, &KeyData);
  871. Exit:
  872. //
  873. // Leave critical section and return
  874. //
  875. gBS->RestoreTPL (OldTpl);
  876. }
  877. /**
  878. Process key notify.
  879. @param Event Indicates the event that invoke this function.
  880. @param Context Indicates the calling context.
  881. **/
  882. VOID
  883. EFIAPI
  884. KeyNotifyProcessHandler (
  885. IN EFI_EVENT Event,
  886. IN VOID *Context
  887. )
  888. {
  889. EFI_STATUS Status;
  890. VIRTUAL_KEYBOARD_DEV *VirtualKeyboardPrivate;
  891. EFI_KEY_DATA KeyData;
  892. LIST_ENTRY *Link;
  893. LIST_ENTRY *NotifyList;
  894. VIRTUAL_KEYBOARD_CONSOLE_IN_EX_NOTIFY *CurrentNotify;
  895. EFI_TPL OldTpl;
  896. VirtualKeyboardPrivate = (VIRTUAL_KEYBOARD_DEV *) Context;
  897. //
  898. // Invoke notification functions.
  899. //
  900. NotifyList = &VirtualKeyboardPrivate->NotifyList;
  901. while (TRUE) {
  902. //
  903. // Enter critical section
  904. //
  905. OldTpl = gBS->RaiseTPL (TPL_NOTIFY);
  906. Status = Dequeue (&VirtualKeyboardPrivate->QueueForNotify, &KeyData);
  907. //
  908. // Leave critical section
  909. //
  910. gBS->RestoreTPL (OldTpl);
  911. if (EFI_ERROR (Status)) {
  912. break;
  913. }
  914. for (Link = GetFirstNode (NotifyList);
  915. !IsNull (NotifyList, Link);
  916. Link = GetNextNode (NotifyList, Link)) {
  917. CurrentNotify = CR (Link,
  918. VIRTUAL_KEYBOARD_CONSOLE_IN_EX_NOTIFY,
  919. NotifyEntry,
  920. VIRTUAL_KEYBOARD_CONSOLE_IN_EX_NOTIFY_SIGNATURE
  921. );
  922. if (IsKeyRegistered (&CurrentNotify->KeyData, &KeyData)) {
  923. CurrentNotify->KeyNotificationFn (&KeyData);
  924. }
  925. }
  926. }
  927. }
  928. /**
  929. The user Entry Point for module VirtualKeyboard. The user code starts with
  930. this function.
  931. @param[in] ImageHandle The firmware allocated handle for the EFI image.
  932. @param[in] SystemTable A pointer to the EFI System Table.
  933. @retval EFI_SUCCESS The entry point is executed successfully.
  934. @retval other Some error occurs when executing this entry point.
  935. **/
  936. EFI_STATUS
  937. EFIAPI
  938. InitializeVirtualKeyboard(
  939. IN EFI_HANDLE ImageHandle,
  940. IN EFI_SYSTEM_TABLE *SystemTable
  941. )
  942. {
  943. EFI_STATUS Status;
  944. //
  945. // Install driver model protocol(s).
  946. //
  947. Status = EfiLibInstallDriverBindingComponentName2 (
  948. ImageHandle,
  949. SystemTable,
  950. &gVirtualKeyboardDriverBinding,
  951. ImageHandle,
  952. &gVirtualKeyboardComponentName,
  953. &gVirtualKeyboardComponentName2
  954. );
  955. ASSERT_EFI_ERROR (Status);
  956. return Status;
  957. }