Ps2KbdTextIn.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733
  1. /** @file
  2. Routines implements SIMPLE_TEXT_IN protocol's interfaces based on 8042 interfaces
  3. provided by Ps2KbdCtrller.c.
  4. Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
  5. SPDX-License-Identifier: BSD-2-Clause-Patent
  6. **/
  7. #include "Ps2Keyboard.h"
  8. /**
  9. Check whether the EFI key buffer is empty.
  10. @param Queue Pointer to instance of EFI_KEY_QUEUE.
  11. @retval TRUE The EFI key buffer is empty.
  12. @retval FALSE The EFI key buffer isn't empty.
  13. **/
  14. BOOLEAN
  15. IsEfikeyBufEmpty (
  16. IN EFI_KEY_QUEUE *Queue
  17. )
  18. {
  19. return (BOOLEAN) (Queue->Head == Queue->Tail);
  20. }
  21. /**
  22. Read & remove one key data from the EFI key buffer.
  23. @param Queue Pointer to instance of EFI_KEY_QUEUE.
  24. @param KeyData Receive the key data.
  25. @retval EFI_SUCCESS The key data is popped successfully.
  26. @retval EFI_NOT_READY There is no key data available.
  27. **/
  28. EFI_STATUS
  29. PopEfikeyBufHead (
  30. IN EFI_KEY_QUEUE *Queue,
  31. OUT EFI_KEY_DATA *KeyData OPTIONAL
  32. )
  33. {
  34. if (IsEfikeyBufEmpty (Queue)) {
  35. return EFI_NOT_READY;
  36. }
  37. //
  38. // Retrieve and remove the values
  39. //
  40. if (KeyData != NULL) {
  41. CopyMem (KeyData, &Queue->Buffer[Queue->Head], sizeof (EFI_KEY_DATA));
  42. }
  43. Queue->Head = (Queue->Head + 1) % KEYBOARD_EFI_KEY_MAX_COUNT;
  44. return EFI_SUCCESS;
  45. }
  46. /**
  47. Push one key data to the EFI key buffer.
  48. @param Queue Pointer to instance of EFI_KEY_QUEUE.
  49. @param KeyData The key data to push.
  50. **/
  51. VOID
  52. PushEfikeyBufTail (
  53. IN EFI_KEY_QUEUE *Queue,
  54. IN EFI_KEY_DATA *KeyData
  55. )
  56. {
  57. if ((Queue->Tail + 1) % KEYBOARD_EFI_KEY_MAX_COUNT == Queue->Head) {
  58. //
  59. // If Queue is full, pop the one from head.
  60. //
  61. PopEfikeyBufHead (Queue, NULL);
  62. }
  63. CopyMem (&Queue->Buffer[Queue->Tail], KeyData, sizeof (EFI_KEY_DATA));
  64. Queue->Tail = (Queue->Tail + 1) % KEYBOARD_EFI_KEY_MAX_COUNT;
  65. }
  66. /**
  67. Judge whether is a registed key
  68. @param RegsiteredData A pointer to a buffer that is filled in with the keystroke
  69. state data for the key that was registered.
  70. @param InputData A pointer to a buffer that is filled in with the keystroke
  71. state data for the key that was pressed.
  72. @retval TRUE Key be pressed matches a registered key.
  73. @retval FLASE Match failed.
  74. **/
  75. BOOLEAN
  76. IsKeyRegistered (
  77. IN EFI_KEY_DATA *RegsiteredData,
  78. IN EFI_KEY_DATA *InputData
  79. )
  80. {
  81. ASSERT (RegsiteredData != NULL && InputData != NULL);
  82. if ((RegsiteredData->Key.ScanCode != InputData->Key.ScanCode) ||
  83. (RegsiteredData->Key.UnicodeChar != InputData->Key.UnicodeChar)) {
  84. return FALSE;
  85. }
  86. //
  87. // Assume KeyShiftState/KeyToggleState = 0 in Registered key data means these state could be ignored.
  88. //
  89. if (RegsiteredData->KeyState.KeyShiftState != 0 &&
  90. RegsiteredData->KeyState.KeyShiftState != InputData->KeyState.KeyShiftState) {
  91. return FALSE;
  92. }
  93. if (RegsiteredData->KeyState.KeyToggleState != 0 &&
  94. RegsiteredData->KeyState.KeyToggleState != InputData->KeyState.KeyToggleState) {
  95. return FALSE;
  96. }
  97. return TRUE;
  98. }
  99. /**
  100. Reads the next keystroke from the input device. The WaitForKey Event can
  101. be used to test for existance of a keystroke via WaitForEvent () call.
  102. @param ConsoleInDev Ps2 Keyboard private structure
  103. @param KeyData A pointer to a buffer that is filled in with the keystroke
  104. state data for the key that was pressed.
  105. @retval EFI_SUCCESS The keystroke information was returned.
  106. @retval EFI_NOT_READY There was no keystroke data availiable.
  107. @retval EFI_DEVICE_ERROR The keystroke information was not returned due to
  108. hardware errors.
  109. @retval EFI_INVALID_PARAMETER KeyData is NULL.
  110. **/
  111. EFI_STATUS
  112. KeyboardReadKeyStrokeWorker (
  113. IN KEYBOARD_CONSOLE_IN_DEV *ConsoleInDev,
  114. OUT EFI_KEY_DATA *KeyData
  115. )
  116. {
  117. EFI_STATUS Status;
  118. EFI_TPL OldTpl;
  119. if (KeyData == NULL) {
  120. return EFI_INVALID_PARAMETER;
  121. }
  122. //
  123. // Enter critical section
  124. //
  125. OldTpl = gBS->RaiseTPL (TPL_NOTIFY);
  126. KeyboardTimerHandler (NULL, ConsoleInDev);
  127. if (ConsoleInDev->KeyboardErr) {
  128. Status = EFI_DEVICE_ERROR;
  129. } else {
  130. Status = PopEfikeyBufHead (&ConsoleInDev->EfiKeyQueue, KeyData);
  131. if (Status == EFI_NOT_READY) {
  132. ZeroMem (&KeyData->Key, sizeof (KeyData->Key));
  133. InitializeKeyState (ConsoleInDev, &KeyData->KeyState);
  134. }
  135. }
  136. gBS->RestoreTPL (OldTpl);
  137. return Status;
  138. }
  139. /**
  140. Perform 8042 controller and keyboard initialization which implement SIMPLE_TEXT_IN.Reset()
  141. @param This Pointer to instance of EFI_SIMPLE_TEXT_INPUT_PROTOCOL
  142. @param ExtendedVerification Indicate that the driver may perform a more
  143. exhaustive verification operation of the device during
  144. reset, now this par is ignored in this driver
  145. **/
  146. EFI_STATUS
  147. EFIAPI
  148. KeyboardEfiReset (
  149. IN EFI_SIMPLE_TEXT_INPUT_PROTOCOL *This,
  150. IN BOOLEAN ExtendedVerification
  151. )
  152. {
  153. EFI_STATUS Status;
  154. KEYBOARD_CONSOLE_IN_DEV *ConsoleIn;
  155. EFI_TPL OldTpl;
  156. ConsoleIn = KEYBOARD_CONSOLE_IN_DEV_FROM_THIS (This);
  157. if (ConsoleIn->KeyboardErr) {
  158. return EFI_DEVICE_ERROR;
  159. }
  160. REPORT_STATUS_CODE_WITH_DEVICE_PATH (
  161. EFI_PROGRESS_CODE,
  162. EFI_PERIPHERAL_KEYBOARD | EFI_P_PC_RESET,
  163. ConsoleIn->DevicePath
  164. );
  165. //
  166. // Enter critical section
  167. //
  168. OldTpl = gBS->RaiseTPL (TPL_NOTIFY);
  169. //
  170. // Call InitKeyboard to initialize the keyboard
  171. //
  172. Status = InitKeyboard (ConsoleIn, ExtendedVerification);
  173. if (EFI_ERROR (Status)) {
  174. //
  175. // Leave critical section and return
  176. //
  177. gBS->RestoreTPL (OldTpl);
  178. return EFI_DEVICE_ERROR;
  179. }
  180. //
  181. // Leave critical section and return
  182. //
  183. gBS->RestoreTPL (OldTpl);
  184. //
  185. // Report the status If a stuck key was detected
  186. //
  187. if (KeyReadStatusRegister (ConsoleIn) & 0x01) {
  188. REPORT_STATUS_CODE_WITH_DEVICE_PATH (
  189. EFI_ERROR_CODE | EFI_ERROR_MINOR,
  190. EFI_PERIPHERAL_KEYBOARD | EFI_P_KEYBOARD_EC_STUCK_KEY,
  191. ConsoleIn->DevicePath
  192. );
  193. }
  194. //
  195. // Report the status If keyboard is locked
  196. //
  197. if ((KeyReadStatusRegister (ConsoleIn) & 0x10) == 0) {
  198. REPORT_STATUS_CODE_WITH_DEVICE_PATH (
  199. EFI_ERROR_CODE | EFI_ERROR_MINOR,
  200. EFI_PERIPHERAL_KEYBOARD | EFI_P_KEYBOARD_EC_LOCKED,
  201. ConsoleIn->DevicePath
  202. );
  203. }
  204. return EFI_SUCCESS;
  205. }
  206. /**
  207. Retrieve key values for driver user which implement SIMPLE_TEXT_IN.ReadKeyStroke().
  208. @param This Pointer to instance of EFI_SIMPLE_TEXT_INPUT_PROTOCOL
  209. @param Key The output buffer for key value
  210. @retval EFI_SUCCESS success to read key stroke
  211. **/
  212. EFI_STATUS
  213. EFIAPI
  214. KeyboardReadKeyStroke (
  215. IN EFI_SIMPLE_TEXT_INPUT_PROTOCOL *This,
  216. OUT EFI_INPUT_KEY *Key
  217. )
  218. {
  219. EFI_STATUS Status;
  220. KEYBOARD_CONSOLE_IN_DEV *ConsoleIn;
  221. EFI_KEY_DATA KeyData;
  222. ConsoleIn = KEYBOARD_CONSOLE_IN_DEV_FROM_THIS (This);
  223. //
  224. // Considering if the partial keystroke is enabled, there maybe a partial
  225. // keystroke in the queue, so here skip the partial keystroke and get the
  226. // next key from the queue
  227. //
  228. while (1) {
  229. //
  230. // If there is no pending key, then return.
  231. //
  232. Status = KeyboardReadKeyStrokeWorker (ConsoleIn, &KeyData);
  233. if (EFI_ERROR (Status)) {
  234. return Status;
  235. }
  236. //
  237. // If it is partial keystroke, skip it.
  238. //
  239. if (KeyData.Key.ScanCode == SCAN_NULL && KeyData.Key.UnicodeChar == CHAR_NULL) {
  240. continue;
  241. }
  242. //
  243. // Translate the CTRL-Alpha characters to their corresponding control value
  244. // (ctrl-a = 0x0001 through ctrl-Z = 0x001A)
  245. //
  246. if ((KeyData.KeyState.KeyShiftState & (EFI_LEFT_CONTROL_PRESSED | EFI_RIGHT_CONTROL_PRESSED)) != 0) {
  247. if (KeyData.Key.UnicodeChar >= L'a' && KeyData.Key.UnicodeChar <= L'z') {
  248. KeyData.Key.UnicodeChar = (CHAR16) (KeyData.Key.UnicodeChar - L'a' + 1);
  249. } else if (KeyData.Key.UnicodeChar >= L'A' && KeyData.Key.UnicodeChar <= L'Z') {
  250. KeyData.Key.UnicodeChar = (CHAR16) (KeyData.Key.UnicodeChar - L'A' + 1);
  251. }
  252. }
  253. CopyMem (Key, &KeyData.Key, sizeof (EFI_INPUT_KEY));
  254. return EFI_SUCCESS;
  255. }
  256. }
  257. /**
  258. Event notification function for SIMPLE_TEXT_IN.WaitForKey event
  259. Signal the event if there is key available
  260. @param Event the event object
  261. @param Context waitting context
  262. **/
  263. VOID
  264. EFIAPI
  265. KeyboardWaitForKey (
  266. IN EFI_EVENT Event,
  267. IN VOID *Context
  268. )
  269. {
  270. EFI_TPL OldTpl;
  271. KEYBOARD_CONSOLE_IN_DEV *ConsoleIn;
  272. EFI_KEY_DATA KeyData;
  273. ConsoleIn = (KEYBOARD_CONSOLE_IN_DEV *) Context;
  274. //
  275. // Enter critical section
  276. //
  277. OldTpl = gBS->RaiseTPL (TPL_NOTIFY);
  278. KeyboardTimerHandler (NULL, ConsoleIn);
  279. if (!ConsoleIn->KeyboardErr) {
  280. //
  281. // WaitforKey doesn't suppor the partial key.
  282. // Considering if the partial keystroke is enabled, there maybe a partial
  283. // keystroke in the queue, so here skip the partial keystroke and get the
  284. // next key from the queue
  285. //
  286. while (!IsEfikeyBufEmpty (&ConsoleIn->EfiKeyQueue)) {
  287. CopyMem (
  288. &KeyData,
  289. &(ConsoleIn->EfiKeyQueue.Buffer[ConsoleIn->EfiKeyQueue.Head]),
  290. sizeof (EFI_KEY_DATA)
  291. );
  292. if (KeyData.Key.ScanCode == SCAN_NULL && KeyData.Key.UnicodeChar == CHAR_NULL) {
  293. PopEfikeyBufHead (&ConsoleIn->EfiKeyQueue, &KeyData);
  294. continue;
  295. }
  296. //
  297. // if there is pending value key, signal the event.
  298. //
  299. gBS->SignalEvent (Event);
  300. break;
  301. }
  302. }
  303. //
  304. // Leave critical section and return
  305. //
  306. gBS->RestoreTPL (OldTpl);
  307. }
  308. /**
  309. Event notification function for SIMPLE_TEXT_INPUT_EX_PROTOCOL.WaitForKeyEx event
  310. Signal the event if there is key available
  311. @param Event event object
  312. @param Context waiting context
  313. **/
  314. VOID
  315. EFIAPI
  316. KeyboardWaitForKeyEx (
  317. IN EFI_EVENT Event,
  318. IN VOID *Context
  319. )
  320. {
  321. KeyboardWaitForKey (Event, Context);
  322. }
  323. /**
  324. Reset the input device and optionaly run diagnostics
  325. @param This Protocol instance pointer.
  326. @param ExtendedVerification Driver may perform diagnostics on reset.
  327. @retval EFI_SUCCESS The device was reset.
  328. @retval EFI_DEVICE_ERROR The device is not functioning properly and could
  329. not be reset.
  330. **/
  331. EFI_STATUS
  332. EFIAPI
  333. KeyboardEfiResetEx (
  334. IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *This,
  335. IN BOOLEAN ExtendedVerification
  336. )
  337. {
  338. KEYBOARD_CONSOLE_IN_DEV *ConsoleInDev;
  339. ConsoleInDev = TEXT_INPUT_EX_KEYBOARD_CONSOLE_IN_DEV_FROM_THIS (This);
  340. return ConsoleInDev->ConIn.Reset (
  341. &ConsoleInDev->ConIn,
  342. ExtendedVerification
  343. );
  344. }
  345. /**
  346. Reads the next keystroke from the input device. The WaitForKey Event can
  347. be used to test for existance of a keystroke via WaitForEvent () call.
  348. @param This Protocol instance pointer.
  349. @param KeyData A pointer to a buffer that is filled in with the keystroke
  350. state data for the key that was pressed.
  351. @retval EFI_SUCCESS The keystroke information was returned.
  352. @retval EFI_NOT_READY There was no keystroke data availiable.
  353. @retval EFI_DEVICE_ERROR The keystroke information was not returned due to
  354. hardware errors.
  355. @retval EFI_INVALID_PARAMETER KeyData is NULL.
  356. **/
  357. EFI_STATUS
  358. EFIAPI
  359. KeyboardReadKeyStrokeEx (
  360. IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *This,
  361. OUT EFI_KEY_DATA *KeyData
  362. )
  363. {
  364. KEYBOARD_CONSOLE_IN_DEV *ConsoleInDev;
  365. if (KeyData == NULL) {
  366. return EFI_INVALID_PARAMETER;
  367. }
  368. ConsoleInDev = TEXT_INPUT_EX_KEYBOARD_CONSOLE_IN_DEV_FROM_THIS (This);
  369. return KeyboardReadKeyStrokeWorker (ConsoleInDev, KeyData);
  370. }
  371. /**
  372. Set certain state for the input device.
  373. @param This Protocol instance pointer.
  374. @param KeyToggleState A pointer to the EFI_KEY_TOGGLE_STATE to set the
  375. state for the input device.
  376. @retval EFI_SUCCESS The device state was set successfully.
  377. @retval EFI_DEVICE_ERROR The device is not functioning correctly and could
  378. not have the setting adjusted.
  379. @retval EFI_UNSUPPORTED The device does not have the ability to set its state.
  380. @retval EFI_INVALID_PARAMETER KeyToggleState is NULL.
  381. **/
  382. EFI_STATUS
  383. EFIAPI
  384. KeyboardSetState (
  385. IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *This,
  386. IN EFI_KEY_TOGGLE_STATE *KeyToggleState
  387. )
  388. {
  389. EFI_STATUS Status;
  390. KEYBOARD_CONSOLE_IN_DEV *ConsoleInDev;
  391. EFI_TPL OldTpl;
  392. if (KeyToggleState == NULL) {
  393. return EFI_INVALID_PARAMETER;
  394. }
  395. ConsoleInDev = TEXT_INPUT_EX_KEYBOARD_CONSOLE_IN_DEV_FROM_THIS (This);
  396. //
  397. // Enter critical section
  398. //
  399. OldTpl = gBS->RaiseTPL (TPL_NOTIFY);
  400. if (ConsoleInDev->KeyboardErr) {
  401. Status = EFI_DEVICE_ERROR;
  402. goto Exit;
  403. }
  404. if ((*KeyToggleState & EFI_TOGGLE_STATE_VALID) != EFI_TOGGLE_STATE_VALID) {
  405. Status = EFI_UNSUPPORTED;
  406. goto Exit;
  407. }
  408. //
  409. // Update the status light
  410. //
  411. ConsoleInDev->ScrollLock = FALSE;
  412. ConsoleInDev->NumLock = FALSE;
  413. ConsoleInDev->CapsLock = FALSE;
  414. ConsoleInDev->IsSupportPartialKey = FALSE;
  415. if ((*KeyToggleState & EFI_SCROLL_LOCK_ACTIVE) == EFI_SCROLL_LOCK_ACTIVE) {
  416. ConsoleInDev->ScrollLock = TRUE;
  417. }
  418. if ((*KeyToggleState & EFI_NUM_LOCK_ACTIVE) == EFI_NUM_LOCK_ACTIVE) {
  419. ConsoleInDev->NumLock = TRUE;
  420. }
  421. if ((*KeyToggleState & EFI_CAPS_LOCK_ACTIVE) == EFI_CAPS_LOCK_ACTIVE) {
  422. ConsoleInDev->CapsLock = TRUE;
  423. }
  424. if ((*KeyToggleState & EFI_KEY_STATE_EXPOSED) == EFI_KEY_STATE_EXPOSED) {
  425. ConsoleInDev->IsSupportPartialKey = TRUE;
  426. }
  427. Status = UpdateStatusLights (ConsoleInDev);
  428. if (EFI_ERROR (Status)) {
  429. Status = EFI_DEVICE_ERROR;
  430. }
  431. Exit:
  432. //
  433. // Leave critical section and return
  434. //
  435. gBS->RestoreTPL (OldTpl);
  436. return Status;
  437. }
  438. /**
  439. Register a notification function for a particular keystroke for the input device.
  440. @param This Protocol instance pointer.
  441. @param KeyData A pointer to a buffer that is filled in with the keystroke
  442. information data for the key that was pressed. If KeyData.Key,
  443. KeyData.KeyState.KeyToggleState and KeyData.KeyState.KeyShiftState are 0,
  444. then any incomplete keystroke will trigger a notification of the KeyNotificationFunction.
  445. @param KeyNotificationFunction Points to the function to be called when the key
  446. sequence is typed specified by KeyData. This notification function
  447. should be called at <=TPL_CALLBACK.
  448. @param NotifyHandle Points to the unique handle assigned to the registered notification.
  449. @retval EFI_SUCCESS The notification function was registered successfully.
  450. @retval EFI_OUT_OF_RESOURCES Unable to allocate resources for necesssary data structures.
  451. @retval EFI_INVALID_PARAMETER KeyData or NotifyHandle or KeyNotificationFunction is NULL.
  452. **/
  453. EFI_STATUS
  454. EFIAPI
  455. KeyboardRegisterKeyNotify (
  456. IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *This,
  457. IN EFI_KEY_DATA *KeyData,
  458. IN EFI_KEY_NOTIFY_FUNCTION KeyNotificationFunction,
  459. OUT VOID **NotifyHandle
  460. )
  461. {
  462. EFI_STATUS Status;
  463. KEYBOARD_CONSOLE_IN_DEV *ConsoleInDev;
  464. EFI_TPL OldTpl;
  465. LIST_ENTRY *Link;
  466. KEYBOARD_CONSOLE_IN_EX_NOTIFY *CurrentNotify;
  467. KEYBOARD_CONSOLE_IN_EX_NOTIFY *NewNotify;
  468. if (KeyData == NULL || NotifyHandle == NULL || KeyNotificationFunction == NULL) {
  469. return EFI_INVALID_PARAMETER;
  470. }
  471. ConsoleInDev = TEXT_INPUT_EX_KEYBOARD_CONSOLE_IN_DEV_FROM_THIS (This);
  472. //
  473. // Enter critical section
  474. //
  475. OldTpl = gBS->RaiseTPL (TPL_NOTIFY);
  476. //
  477. // Return EFI_SUCCESS if the (KeyData, NotificationFunction) is already registered.
  478. //
  479. for (Link = ConsoleInDev->NotifyList.ForwardLink; Link != &ConsoleInDev->NotifyList; Link = Link->ForwardLink) {
  480. CurrentNotify = CR (
  481. Link,
  482. KEYBOARD_CONSOLE_IN_EX_NOTIFY,
  483. NotifyEntry,
  484. KEYBOARD_CONSOLE_IN_EX_NOTIFY_SIGNATURE
  485. );
  486. if (IsKeyRegistered (&CurrentNotify->KeyData, KeyData)) {
  487. if (CurrentNotify->KeyNotificationFn == KeyNotificationFunction) {
  488. *NotifyHandle = CurrentNotify;
  489. Status = EFI_SUCCESS;
  490. goto Exit;
  491. }
  492. }
  493. }
  494. //
  495. // Allocate resource to save the notification function
  496. //
  497. NewNotify = (KEYBOARD_CONSOLE_IN_EX_NOTIFY *) AllocateZeroPool (sizeof (KEYBOARD_CONSOLE_IN_EX_NOTIFY));
  498. if (NewNotify == NULL) {
  499. Status = EFI_OUT_OF_RESOURCES;
  500. goto Exit;
  501. }
  502. NewNotify->Signature = KEYBOARD_CONSOLE_IN_EX_NOTIFY_SIGNATURE;
  503. NewNotify->KeyNotificationFn = KeyNotificationFunction;
  504. CopyMem (&NewNotify->KeyData, KeyData, sizeof (EFI_KEY_DATA));
  505. InsertTailList (&ConsoleInDev->NotifyList, &NewNotify->NotifyEntry);
  506. *NotifyHandle = NewNotify;
  507. Status = EFI_SUCCESS;
  508. Exit:
  509. //
  510. // Leave critical section and return
  511. //
  512. gBS->RestoreTPL (OldTpl);
  513. return Status;
  514. }
  515. /**
  516. Remove a registered notification function from a particular keystroke.
  517. @param This Protocol instance pointer.
  518. @param NotificationHandle The handle of the notification function being unregistered.
  519. @retval EFI_SUCCESS The notification function was unregistered successfully.
  520. @retval EFI_INVALID_PARAMETER The NotificationHandle is invalid.
  521. **/
  522. EFI_STATUS
  523. EFIAPI
  524. KeyboardUnregisterKeyNotify (
  525. IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *This,
  526. IN VOID *NotificationHandle
  527. )
  528. {
  529. EFI_STATUS Status;
  530. KEYBOARD_CONSOLE_IN_DEV *ConsoleInDev;
  531. EFI_TPL OldTpl;
  532. LIST_ENTRY *Link;
  533. KEYBOARD_CONSOLE_IN_EX_NOTIFY *CurrentNotify;
  534. if (NotificationHandle == NULL) {
  535. return EFI_INVALID_PARAMETER;
  536. }
  537. ConsoleInDev = TEXT_INPUT_EX_KEYBOARD_CONSOLE_IN_DEV_FROM_THIS (This);
  538. //
  539. // Enter critical section
  540. //
  541. OldTpl = gBS->RaiseTPL (TPL_NOTIFY);
  542. for (Link = ConsoleInDev->NotifyList.ForwardLink; Link != &ConsoleInDev->NotifyList; Link = Link->ForwardLink) {
  543. CurrentNotify = CR (
  544. Link,
  545. KEYBOARD_CONSOLE_IN_EX_NOTIFY,
  546. NotifyEntry,
  547. KEYBOARD_CONSOLE_IN_EX_NOTIFY_SIGNATURE
  548. );
  549. if (CurrentNotify == NotificationHandle) {
  550. //
  551. // Remove the notification function from NotifyList and free resources
  552. //
  553. RemoveEntryList (&CurrentNotify->NotifyEntry);
  554. gBS->FreePool (CurrentNotify);
  555. Status = EFI_SUCCESS;
  556. goto Exit;
  557. }
  558. }
  559. //
  560. // Can not find the specified Notification Handle
  561. //
  562. Status = EFI_INVALID_PARAMETER;
  563. Exit:
  564. //
  565. // Leave critical section and return
  566. //
  567. gBS->RestoreTPL (OldTpl);
  568. return Status;
  569. }
  570. /**
  571. Process key notify.
  572. @param Event Indicates the event that invoke this function.
  573. @param Context Indicates the calling context.
  574. **/
  575. VOID
  576. EFIAPI
  577. KeyNotifyProcessHandler (
  578. IN EFI_EVENT Event,
  579. IN VOID *Context
  580. )
  581. {
  582. EFI_STATUS Status;
  583. KEYBOARD_CONSOLE_IN_DEV *ConsoleIn;
  584. EFI_KEY_DATA KeyData;
  585. LIST_ENTRY *Link;
  586. LIST_ENTRY *NotifyList;
  587. KEYBOARD_CONSOLE_IN_EX_NOTIFY *CurrentNotify;
  588. EFI_TPL OldTpl;
  589. ConsoleIn = (KEYBOARD_CONSOLE_IN_DEV *) Context;
  590. //
  591. // Invoke notification functions.
  592. //
  593. NotifyList = &ConsoleIn->NotifyList;
  594. while (TRUE) {
  595. //
  596. // Enter critical section
  597. //
  598. OldTpl = gBS->RaiseTPL (TPL_NOTIFY);
  599. Status = PopEfikeyBufHead (&ConsoleIn->EfiKeyQueueForNotify, &KeyData);
  600. //
  601. // Leave critical section
  602. //
  603. gBS->RestoreTPL (OldTpl);
  604. if (EFI_ERROR (Status)) {
  605. break;
  606. }
  607. for (Link = GetFirstNode (NotifyList); !IsNull (NotifyList, Link); Link = GetNextNode (NotifyList, Link)) {
  608. CurrentNotify = CR (Link, KEYBOARD_CONSOLE_IN_EX_NOTIFY, NotifyEntry, KEYBOARD_CONSOLE_IN_EX_NOTIFY_SIGNATURE);
  609. if (IsKeyRegistered (&CurrentNotify->KeyData, &KeyData)) {
  610. CurrentNotify->KeyNotificationFn (&KeyData);
  611. }
  612. }
  613. }
  614. }