Ps2Keyboard.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. /** @file
  2. PS/2 keyboard driver header file
  3. Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #ifndef _PS2KEYBOARD_H_
  7. #define _PS2KEYBOARD_H_
  8. #include <Uefi.h>
  9. #include <Protocol/SuperIo.h>
  10. #include <Protocol/SimpleTextIn.h>
  11. #include <Protocol/SimpleTextInEx.h>
  12. #include <Protocol/DevicePath.h>
  13. #include <Protocol/Ps2Policy.h>
  14. #include <Library/IoLib.h>
  15. #include <Library/DevicePathLib.h>
  16. #include <Library/UefiDriverEntryPoint.h>
  17. #include <Library/UefiLib.h>
  18. #include <Library/UefiBootServicesTableLib.h>
  19. #include <Library/ReportStatusCodeLib.h>
  20. #include <Library/DebugLib.h>
  21. #include <Library/UefiRuntimeServicesTableLib.h>
  22. #include <Library/MemoryAllocationLib.h>
  23. #include <Library/BaseLib.h>
  24. #include <Library/BaseMemoryLib.h>
  25. #include <Library/TimerLib.h>
  26. #include <Library/PcdLib.h>
  27. //
  28. // Global Variables
  29. //
  30. extern EFI_DRIVER_BINDING_PROTOCOL gKeyboardControllerDriver;
  31. extern EFI_COMPONENT_NAME_PROTOCOL gPs2KeyboardComponentName;
  32. extern EFI_COMPONENT_NAME2_PROTOCOL gPs2KeyboardComponentName2;
  33. //
  34. // Driver Private Data
  35. //
  36. #define KEYBOARD_CONSOLE_IN_DEV_SIGNATURE SIGNATURE_32 ('k', 'k', 'e', 'y')
  37. #define KEYBOARD_CONSOLE_IN_EX_NOTIFY_SIGNATURE SIGNATURE_32 ('k', 'c', 'e', 'n')
  38. typedef struct _KEYBOARD_CONSOLE_IN_EX_NOTIFY {
  39. UINTN Signature;
  40. EFI_KEY_DATA KeyData;
  41. EFI_KEY_NOTIFY_FUNCTION KeyNotificationFn;
  42. LIST_ENTRY NotifyEntry;
  43. } KEYBOARD_CONSOLE_IN_EX_NOTIFY;
  44. #define KEYBOARD_SCAN_CODE_MAX_COUNT 32
  45. typedef struct {
  46. UINT8 Buffer[KEYBOARD_SCAN_CODE_MAX_COUNT];
  47. UINTN Head;
  48. UINTN Tail;
  49. } SCAN_CODE_QUEUE;
  50. #define KEYBOARD_EFI_KEY_MAX_COUNT 256
  51. typedef struct {
  52. EFI_KEY_DATA Buffer[KEYBOARD_EFI_KEY_MAX_COUNT];
  53. UINTN Head;
  54. UINTN Tail;
  55. } EFI_KEY_QUEUE;
  56. typedef struct {
  57. UINTN Signature;
  58. EFI_HANDLE Handle;
  59. EFI_SIMPLE_TEXT_INPUT_PROTOCOL ConIn;
  60. EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL ConInEx;
  61. EFI_EVENT TimerEvent;
  62. UINT32 DataRegisterAddress;
  63. UINT32 StatusRegisterAddress;
  64. UINT32 CommandRegisterAddress;
  65. BOOLEAN LeftCtrl;
  66. BOOLEAN RightCtrl;
  67. BOOLEAN LeftAlt;
  68. BOOLEAN RightAlt;
  69. BOOLEAN LeftShift;
  70. BOOLEAN RightShift;
  71. BOOLEAN LeftLogo;
  72. BOOLEAN RightLogo;
  73. BOOLEAN Menu;
  74. BOOLEAN SysReq;
  75. BOOLEAN CapsLock;
  76. BOOLEAN NumLock;
  77. BOOLEAN ScrollLock;
  78. BOOLEAN IsSupportPartialKey;
  79. //
  80. // Queue storing key scancodes
  81. //
  82. SCAN_CODE_QUEUE ScancodeQueue;
  83. EFI_KEY_QUEUE EfiKeyQueue;
  84. EFI_KEY_QUEUE EfiKeyQueueForNotify;
  85. //
  86. // Error state
  87. //
  88. BOOLEAN KeyboardErr;
  89. EFI_UNICODE_STRING_TABLE *ControllerNameTable;
  90. EFI_DEVICE_PATH_PROTOCOL *DevicePath;
  91. //
  92. // Notification Function List
  93. //
  94. LIST_ENTRY NotifyList;
  95. EFI_EVENT KeyNotifyProcessEvent;
  96. } KEYBOARD_CONSOLE_IN_DEV;
  97. #define KEYBOARD_CONSOLE_IN_DEV_FROM_THIS(a) CR (a, KEYBOARD_CONSOLE_IN_DEV, ConIn, KEYBOARD_CONSOLE_IN_DEV_SIGNATURE)
  98. #define TEXT_INPUT_EX_KEYBOARD_CONSOLE_IN_DEV_FROM_THIS(a) \
  99. CR (a, \
  100. KEYBOARD_CONSOLE_IN_DEV, \
  101. ConInEx, \
  102. KEYBOARD_CONSOLE_IN_DEV_SIGNATURE \
  103. )
  104. #define TABLE_END 0x0
  105. //
  106. // Driver entry point
  107. //
  108. /**
  109. The user Entry Point for module Ps2Keyboard. The user code starts with this function.
  110. @param[in] ImageHandle The firmware allocated handle for the EFI image.
  111. @param[in] SystemTable A pointer to the EFI System Table.
  112. @retval EFI_SUCCESS The entry point is executed successfully.
  113. @retval other Some error occurs when executing this entry point.
  114. **/
  115. EFI_STATUS
  116. EFIAPI
  117. InstallPs2KeyboardDriver (
  118. IN EFI_HANDLE ImageHandle,
  119. IN EFI_SYSTEM_TABLE *SystemTable
  120. );
  121. #define KEYBOARD_8042_DATA_REGISTER 0x60
  122. #define KEYBOARD_8042_STATUS_REGISTER 0x64
  123. #define KEYBOARD_8042_COMMAND_REGISTER 0x64
  124. #define KEYBOARD_KBEN 0xF4
  125. #define KEYBOARD_CMDECHO_ACK 0xFA
  126. #define KEYBOARD_MAX_TRY 256 // 256
  127. #define KEYBOARD_TIMEOUT 65536 // 0.07s
  128. #define KEYBOARD_WAITFORVALUE_TIMEOUT 1000000 // 1s
  129. #define KEYBOARD_BAT_TIMEOUT 4000000 // 4s
  130. #define KEYBOARD_TIMER_INTERVAL 200000 // 0.02s
  131. #define SCANCODE_EXTENDED0 0xE0
  132. #define SCANCODE_EXTENDED1 0xE1
  133. #define SCANCODE_CTRL_MAKE 0x1D
  134. #define SCANCODE_CTRL_BREAK 0x9D
  135. #define SCANCODE_ALT_MAKE 0x38
  136. #define SCANCODE_ALT_BREAK 0xB8
  137. #define SCANCODE_LEFT_SHIFT_MAKE 0x2A
  138. #define SCANCODE_LEFT_SHIFT_BREAK 0xAA
  139. #define SCANCODE_RIGHT_SHIFT_MAKE 0x36
  140. #define SCANCODE_RIGHT_SHIFT_BREAK 0xB6
  141. #define SCANCODE_CAPS_LOCK_MAKE 0x3A
  142. #define SCANCODE_NUM_LOCK_MAKE 0x45
  143. #define SCANCODE_SCROLL_LOCK_MAKE 0x46
  144. #define SCANCODE_DELETE_MAKE 0x53
  145. #define SCANCODE_LEFT_LOGO_MAKE 0x5B //GUI key defined in Keyboard scan code
  146. #define SCANCODE_LEFT_LOGO_BREAK 0xDB
  147. #define SCANCODE_RIGHT_LOGO_MAKE 0x5C
  148. #define SCANCODE_RIGHT_LOGO_BREAK 0xDC
  149. #define SCANCODE_MENU_MAKE 0x5D //APPS key defined in Keyboard scan code
  150. #define SCANCODE_MENU_BREAK 0xDD
  151. #define SCANCODE_SYS_REQ_MAKE 0x37
  152. #define SCANCODE_SYS_REQ_BREAK 0xB7
  153. #define SCANCODE_SYS_REQ_MAKE_WITH_ALT 0x54
  154. #define SCANCODE_SYS_REQ_BREAK_WITH_ALT 0xD4
  155. #define SCANCODE_MAX_MAKE 0x60
  156. #define KEYBOARD_STATUS_REGISTER_HAS_OUTPUT_DATA BIT0 ///< 0 - Output register has no data; 1 - Output register has data
  157. #define KEYBOARD_STATUS_REGISTER_HAS_INPUT_DATA BIT1 ///< 0 - Input register has no data; 1 - Input register has data
  158. #define KEYBOARD_STATUS_REGISTER_SYSTEM_FLAG BIT2 ///< Set to 0 after power on reset
  159. #define KEYBOARD_STATUS_REGISTER_INPUT_DATA_TYPE BIT3 ///< 0 - Data in input register is data; 1 - Data in input register is command
  160. #define KEYBOARD_STATUS_REGISTER_ENABLE_FLAG BIT4 ///< 0 - Keyboard is disable; 1 - Keyboard is enable
  161. #define KEYBOARD_STATUS_REGISTER_TRANSMIT_TIMEOUT BIT5 ///< 0 - Transmit is complete without timeout; 1 - Transmit is timeout without complete
  162. #define KEYBOARD_STATUS_REGISTER_RECEIVE_TIMEOUT BIT6 ///< 0 - Receive is complete without timeout; 1 - Receive is timeout without complete
  163. #define KEYBOARD_STATUS_REGISTER_PARITY BIT7 ///< 0 - Odd parity; 1 - Even parity
  164. #define KEYBOARD_8042_COMMAND_READ 0x20
  165. #define KEYBOARD_8042_COMMAND_WRITE 0x60
  166. #define KEYBOARD_8042_COMMAND_DISABLE_MOUSE_INTERFACE 0xA7
  167. #define KEYBOARD_8042_COMMAND_ENABLE_MOUSE_INTERFACE 0xA8
  168. #define KEYBOARD_8042_COMMAND_CONTROLLER_SELF_TEST 0xAA
  169. #define KEYBOARD_8042_COMMAND_KEYBOARD_INTERFACE_SELF_TEST 0xAB
  170. #define KEYBOARD_8042_COMMAND_DISABLE_KEYBOARD_INTERFACE 0xAD
  171. #define KEYBOARD_8048_COMMAND_CLEAR_OUTPUT_DATA 0xF4
  172. #define KEYBOARD_8048_COMMAND_RESET 0xFF
  173. #define KEYBOARD_8048_COMMAND_SELECT_SCAN_CODE_SET 0xF0
  174. #define KEYBOARD_8048_RETURN_8042_BAT_SUCCESS 0xAA
  175. #define KEYBOARD_8048_RETURN_8042_BAT_ERROR 0xFC
  176. #define KEYBOARD_8048_RETURN_8042_ACK 0xFA
  177. //
  178. // Keyboard Controller Status
  179. //
  180. #define KBC_PARE 0x80 // Parity Error
  181. #define KBC_TIM 0x40 // General Time Out
  182. //
  183. // Other functions that are used among .c files
  184. //
  185. /**
  186. Show keyboard status lights according to
  187. indicators in ConsoleIn.
  188. @param ConsoleIn Pointer to instance of KEYBOARD_CONSOLE_IN_DEV
  189. @return status
  190. **/
  191. EFI_STATUS
  192. UpdateStatusLights (
  193. IN KEYBOARD_CONSOLE_IN_DEV *ConsoleIn
  194. );
  195. /**
  196. write key to keyboard.
  197. @param ConsoleIn Pointer to instance of KEYBOARD_CONSOLE_IN_DEV
  198. @param Data value wanted to be written
  199. @retval EFI_TIMEOUT - GC_TODO: Add description for return value
  200. @retval EFI_SUCCESS - GC_TODO: Add description for return value
  201. **/
  202. EFI_STATUS
  203. KeyboardRead (
  204. IN KEYBOARD_CONSOLE_IN_DEV *ConsoleIn,
  205. OUT UINT8 *Data
  206. );
  207. /**
  208. Get scancode from scancode buffer and translate into EFI-scancode and unicode defined by EFI spec.
  209. The function is always called in TPL_NOTIFY.
  210. @param ConsoleIn KEYBOARD_CONSOLE_IN_DEV instance pointer
  211. **/
  212. VOID
  213. KeyGetchar (
  214. IN OUT KEYBOARD_CONSOLE_IN_DEV *ConsoleIn
  215. );
  216. /**
  217. Process key notify.
  218. @param Event Indicates the event that invoke this function.
  219. @param Context Indicates the calling context.
  220. **/
  221. VOID
  222. EFIAPI
  223. KeyNotifyProcessHandler (
  224. IN EFI_EVENT Event,
  225. IN VOID *Context
  226. );
  227. /**
  228. Perform 8042 controller and keyboard Initialization.
  229. If ExtendedVerification is TRUE, do additional test for
  230. the keyboard interface
  231. @param ConsoleIn - KEYBOARD_CONSOLE_IN_DEV instance pointer
  232. @param ExtendedVerification - indicates a thorough initialization
  233. @retval EFI_DEVICE_ERROR Fail to init keyboard
  234. @retval EFI_SUCCESS Success to init keyboard
  235. **/
  236. EFI_STATUS
  237. InitKeyboard (
  238. IN OUT KEYBOARD_CONSOLE_IN_DEV *ConsoleIn,
  239. IN BOOLEAN ExtendedVerification
  240. );
  241. /**
  242. Timer event handler: read a series of scancodes from 8042
  243. and put them into memory scancode buffer.
  244. it read as much scancodes to either fill
  245. the memory buffer or empty the keyboard buffer.
  246. It is registered as running under TPL_NOTIFY
  247. @param Event - The timer event
  248. @param Context - A KEYBOARD_CONSOLE_IN_DEV pointer
  249. **/
  250. VOID
  251. EFIAPI
  252. KeyboardTimerHandler (
  253. IN EFI_EVENT Event,
  254. IN VOID *Context
  255. );
  256. /**
  257. logic reset keyboard
  258. Implement SIMPLE_TEXT_IN.Reset()
  259. Perform 8042 controller and keyboard initialization
  260. @param This Pointer to instance of EFI_SIMPLE_TEXT_INPUT_PROTOCOL
  261. @param ExtendedVerification Indicate that the driver may perform a more
  262. exhaustive verification operation of the device during
  263. reset, now this par is ignored in this driver
  264. **/
  265. EFI_STATUS
  266. EFIAPI
  267. KeyboardEfiReset (
  268. IN EFI_SIMPLE_TEXT_INPUT_PROTOCOL *This,
  269. IN BOOLEAN ExtendedVerification
  270. );
  271. /**
  272. Implement SIMPLE_TEXT_IN.ReadKeyStroke().
  273. Retrieve key values for driver user.
  274. @param This Pointer to instance of EFI_SIMPLE_TEXT_INPUT_PROTOCOL
  275. @param Key The output buffer for key value
  276. @retval EFI_SUCCESS success to read key stroke
  277. **/
  278. EFI_STATUS
  279. EFIAPI
  280. KeyboardReadKeyStroke (
  281. IN EFI_SIMPLE_TEXT_INPUT_PROTOCOL *This,
  282. OUT EFI_INPUT_KEY *Key
  283. );
  284. /**
  285. Event notification function for SIMPLE_TEXT_IN.WaitForKey event
  286. Signal the event if there is key available
  287. @param Event the event object
  288. @param Context waitting context
  289. **/
  290. VOID
  291. EFIAPI
  292. KeyboardWaitForKey (
  293. IN EFI_EVENT Event,
  294. IN VOID *Context
  295. );
  296. /**
  297. Read status register.
  298. @param ConsoleIn Pointer to instance of KEYBOARD_CONSOLE_IN_DEV
  299. @return value in status register
  300. **/
  301. UINT8
  302. KeyReadStatusRegister (
  303. IN KEYBOARD_CONSOLE_IN_DEV *ConsoleIn
  304. );
  305. /**
  306. Check whether there is Ps/2 Keyboard device in system by 0xF4 Keyboard Command
  307. If Keyboard receives 0xF4, it will respond with 'ACK'. If it doesn't respond, the device
  308. should not be in system.
  309. @param[in] ConsoleIn Pointer to instance of KEYBOARD_CONSOLE_IN_DEV
  310. @retval TRUE Keyboard in System.
  311. @retval FALSE Keyboard not in System.
  312. **/
  313. BOOLEAN
  314. EFIAPI
  315. CheckKeyboardConnect (
  316. IN KEYBOARD_CONSOLE_IN_DEV *ConsoleIn
  317. );
  318. /**
  319. Event notification function for SIMPLE_TEXT_INPUT_EX_PROTOCOL.WaitForKeyEx event
  320. Signal the event if there is key available
  321. @param Event event object
  322. @param Context waiting context
  323. **/
  324. VOID
  325. EFIAPI
  326. KeyboardWaitForKeyEx (
  327. IN EFI_EVENT Event,
  328. IN VOID *Context
  329. );
  330. //
  331. // Simple Text Input Ex protocol function prototypes
  332. //
  333. /**
  334. Reset the input device and optionaly run diagnostics
  335. @param This - Protocol instance pointer.
  336. @param ExtendedVerification - Driver may perform diagnostics on reset.
  337. @retval EFI_SUCCESS - The device was reset.
  338. @retval EFI_DEVICE_ERROR - The device is not functioning properly and could
  339. not be reset.
  340. **/
  341. EFI_STATUS
  342. EFIAPI
  343. KeyboardEfiResetEx (
  344. IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *This,
  345. IN BOOLEAN ExtendedVerification
  346. );
  347. /**
  348. Reads the next keystroke from the input device. The WaitForKey Event can
  349. be used to test for existance of a keystroke via WaitForEvent () call.
  350. @param This - Protocol instance pointer.
  351. @param KeyData - A pointer to a buffer that is filled in with the keystroke
  352. state data for the key that was pressed.
  353. @retval EFI_SUCCESS - The keystroke information was returned.
  354. @retval EFI_NOT_READY - There was no keystroke data availiable.
  355. @retval EFI_DEVICE_ERROR - The keystroke information was not returned due to
  356. hardware errors.
  357. @retval EFI_INVALID_PARAMETER - KeyData is NULL.
  358. **/
  359. EFI_STATUS
  360. EFIAPI
  361. KeyboardReadKeyStrokeEx (
  362. IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *This,
  363. OUT EFI_KEY_DATA *KeyData
  364. );
  365. /**
  366. Set certain state for the input device.
  367. @param This - Protocol instance pointer.
  368. @param KeyToggleState - A pointer to the EFI_KEY_TOGGLE_STATE to set the
  369. state for the input device.
  370. @retval EFI_SUCCESS - The device state was set successfully.
  371. @retval EFI_DEVICE_ERROR - The device is not functioning correctly and could
  372. not have the setting adjusted.
  373. @retval EFI_UNSUPPORTED - The device does not have the ability to set its state.
  374. @retval EFI_INVALID_PARAMETER - KeyToggleState is NULL.
  375. **/
  376. EFI_STATUS
  377. EFIAPI
  378. KeyboardSetState (
  379. IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *This,
  380. IN EFI_KEY_TOGGLE_STATE *KeyToggleState
  381. );
  382. /**
  383. Register a notification function for a particular keystroke for the input device.
  384. @param This - Protocol instance pointer.
  385. @param KeyData - A pointer to a buffer that is filled in with the keystroke
  386. information data for the key that was pressed. If KeyData.Key,
  387. KeyData.KeyState.KeyToggleState and KeyData.KeyState.KeyShiftState are 0,
  388. then any incomplete keystroke will trigger a notification of the KeyNotificationFunction.
  389. @param KeyNotificationFunction - Points to the function to be called when the key
  390. sequence is typed specified by KeyData. This notification function
  391. should be called at <=TPL_CALLBACK.
  392. @param NotifyHandle - Points to the unique handle assigned to the registered notification.
  393. @retval EFI_SUCCESS - The notification function was registered successfully.
  394. @retval EFI_OUT_OF_RESOURCES - Unable to allocate resources for necesssary data structures.
  395. @retval EFI_INVALID_PARAMETER - KeyData or NotifyHandle is NULL.
  396. **/
  397. EFI_STATUS
  398. EFIAPI
  399. KeyboardRegisterKeyNotify (
  400. IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *This,
  401. IN EFI_KEY_DATA *KeyData,
  402. IN EFI_KEY_NOTIFY_FUNCTION KeyNotificationFunction,
  403. OUT VOID **NotifyHandle
  404. );
  405. /**
  406. Remove a registered notification function from a particular keystroke.
  407. @param This - Protocol instance pointer.
  408. @param NotificationHandle - The handle of the notification function being unregistered.
  409. @retval EFI_SUCCESS - The notification function was unregistered successfully.
  410. @retval EFI_INVALID_PARAMETER - The NotificationHandle is invalid.
  411. @retval EFI_NOT_FOUND - Can not find the matching entry in database.
  412. **/
  413. EFI_STATUS
  414. EFIAPI
  415. KeyboardUnregisterKeyNotify (
  416. IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *This,
  417. IN VOID *NotificationHandle
  418. );
  419. /**
  420. Push one key data to the EFI key buffer.
  421. @param Queue Pointer to instance of EFI_KEY_QUEUE.
  422. @param KeyData The key data to push.
  423. **/
  424. VOID
  425. PushEfikeyBufTail (
  426. IN EFI_KEY_QUEUE *Queue,
  427. IN EFI_KEY_DATA *KeyData
  428. );
  429. /**
  430. Judge whether is a registed key
  431. @param RegsiteredData A pointer to a buffer that is filled in with the keystroke
  432. state data for the key that was registered.
  433. @param InputData A pointer to a buffer that is filled in with the keystroke
  434. state data for the key that was pressed.
  435. @retval TRUE Key be pressed matches a registered key.
  436. @retval FLASE Match failed.
  437. **/
  438. BOOLEAN
  439. IsKeyRegistered (
  440. IN EFI_KEY_DATA *RegsiteredData,
  441. IN EFI_KEY_DATA *InputData
  442. );
  443. /**
  444. Initialize the key state.
  445. @param ConsoleIn The KEYBOARD_CONSOLE_IN_DEV instance.
  446. @param KeyState A pointer to receive the key state information.
  447. **/
  448. VOID
  449. InitializeKeyState (
  450. IN KEYBOARD_CONSOLE_IN_DEV *ConsoleIn,
  451. OUT EFI_KEY_STATE *KeyState
  452. );
  453. #endif