kdb_keyboard.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*
  2. * Kernel Debugger Architecture Dependent Console I/O handler
  3. *
  4. * This file is subject to the terms and conditions of the GNU General Public
  5. * License.
  6. *
  7. * Copyright (c) 1999-2006 Silicon Graphics, Inc. All Rights Reserved.
  8. * Copyright (c) 2009 Wind River Systems, Inc. All Rights Reserved.
  9. */
  10. #include <linux/kdb.h>
  11. #include <linux/keyboard.h>
  12. #include <linux/ctype.h>
  13. #include <linux/io.h>
  14. /* Keyboard Controller Registers on normal PCs. */
  15. #define KBD_STATUS_REG 0x64 /* Status register (R) */
  16. #define KBD_DATA_REG 0x60 /* Keyboard data register (R/W) */
  17. /* Status Register Bits */
  18. #define KBD_STAT_OBF 0x01 /* Keyboard output buffer full */
  19. #define KBD_STAT_MOUSE_OBF 0x20 /* Mouse output buffer full */
  20. static int kbd_exists;
  21. static int kbd_last_ret;
  22. /*
  23. * Check if the keyboard controller has a keypress for us.
  24. * Some parts (Enter Release, LED change) are still blocking polled here,
  25. * but hopefully they are all short.
  26. */
  27. int kdb_get_kbd_char(void)
  28. {
  29. int scancode, scanstatus;
  30. static int shift_lock; /* CAPS LOCK state (0-off, 1-on) */
  31. static int shift_key; /* Shift next keypress */
  32. static int ctrl_key;
  33. u_short keychar;
  34. if (KDB_FLAG(NO_I8042) || KDB_FLAG(NO_VT_CONSOLE) ||
  35. (inb(KBD_STATUS_REG) == 0xff && inb(KBD_DATA_REG) == 0xff)) {
  36. kbd_exists = 0;
  37. return -1;
  38. }
  39. kbd_exists = 1;
  40. if ((inb(KBD_STATUS_REG) & KBD_STAT_OBF) == 0)
  41. return -1;
  42. /*
  43. * Fetch the scancode
  44. */
  45. scancode = inb(KBD_DATA_REG);
  46. scanstatus = inb(KBD_STATUS_REG);
  47. /*
  48. * Ignore mouse events.
  49. */
  50. if (scanstatus & KBD_STAT_MOUSE_OBF)
  51. return -1;
  52. /*
  53. * Ignore release, trigger on make
  54. * (except for shift keys, where we want to
  55. * keep the shift state so long as the key is
  56. * held down).
  57. */
  58. if (((scancode&0x7f) == 0x2a) || ((scancode&0x7f) == 0x36)) {
  59. /*
  60. * Next key may use shift table
  61. */
  62. if ((scancode & 0x80) == 0)
  63. shift_key = 1;
  64. else
  65. shift_key = 0;
  66. return -1;
  67. }
  68. if ((scancode&0x7f) == 0x1d) {
  69. /*
  70. * Left ctrl key
  71. */
  72. if ((scancode & 0x80) == 0)
  73. ctrl_key = 1;
  74. else
  75. ctrl_key = 0;
  76. return -1;
  77. }
  78. if ((scancode & 0x80) != 0) {
  79. if (scancode == 0x9c)
  80. kbd_last_ret = 0;
  81. return -1;
  82. }
  83. scancode &= 0x7f;
  84. /*
  85. * Translate scancode
  86. */
  87. if (scancode == 0x3a) {
  88. /*
  89. * Toggle caps lock
  90. */
  91. shift_lock ^= 1;
  92. #ifdef KDB_BLINK_LED
  93. kdb_toggleled(0x4);
  94. #endif
  95. return -1;
  96. }
  97. if (scancode == 0x0e) {
  98. /*
  99. * Backspace
  100. */
  101. return 8;
  102. }
  103. /* Special Key */
  104. switch (scancode) {
  105. case 0xF: /* Tab */
  106. return 9;
  107. case 0x53: /* Del */
  108. return 4;
  109. case 0x47: /* Home */
  110. return 1;
  111. case 0x4F: /* End */
  112. return 5;
  113. case 0x4B: /* Left */
  114. return 2;
  115. case 0x48: /* Up */
  116. return 16;
  117. case 0x50: /* Down */
  118. return 14;
  119. case 0x4D: /* Right */
  120. return 6;
  121. }
  122. if (scancode == 0xe0)
  123. return -1;
  124. /*
  125. * For Japanese 86/106 keyboards
  126. * See comment in drivers/char/pc_keyb.c.
  127. * - Masahiro Adegawa
  128. */
  129. if (scancode == 0x73)
  130. scancode = 0x59;
  131. else if (scancode == 0x7d)
  132. scancode = 0x7c;
  133. if (!shift_lock && !shift_key && !ctrl_key) {
  134. keychar = plain_map[scancode];
  135. } else if ((shift_lock || shift_key) && key_maps[1]) {
  136. keychar = key_maps[1][scancode];
  137. } else if (ctrl_key && key_maps[4]) {
  138. keychar = key_maps[4][scancode];
  139. } else {
  140. keychar = 0x0020;
  141. kdb_printf("Unknown state/scancode (%d)\n", scancode);
  142. }
  143. keychar &= 0x0fff;
  144. if (keychar == '\t')
  145. keychar = ' ';
  146. switch (KTYP(keychar)) {
  147. case KT_LETTER:
  148. case KT_LATIN:
  149. if (isprint(keychar))
  150. break; /* printable characters */
  151. fallthrough;
  152. case KT_SPEC:
  153. if (keychar == K_ENTER)
  154. break;
  155. fallthrough;
  156. default:
  157. return -1; /* ignore unprintables */
  158. }
  159. if (scancode == 0x1c) {
  160. kbd_last_ret = 1;
  161. return 13;
  162. }
  163. return keychar & 0xff;
  164. }
  165. EXPORT_SYMBOL_GPL(kdb_get_kbd_char);
  166. /*
  167. * Best effort cleanup of ENTER break codes on leaving KDB. Called on
  168. * exiting KDB, when we know we processed an ENTER or KP ENTER scan
  169. * code.
  170. */
  171. void kdb_kbd_cleanup_state(void)
  172. {
  173. int scancode, scanstatus;
  174. /*
  175. * Nothing to clean up, since either
  176. * ENTER was never pressed, or has already
  177. * gotten cleaned up.
  178. */
  179. if (!kbd_last_ret)
  180. return;
  181. kbd_last_ret = 0;
  182. /*
  183. * Enter key. Need to absorb the break code here, lest it gets
  184. * leaked out if we exit KDB as the result of processing 'g'.
  185. *
  186. * This has several interesting implications:
  187. * + Need to handle KP ENTER, which has break code 0xe0 0x9c.
  188. * + Need to handle repeat ENTER and repeat KP ENTER. Repeats
  189. * only get a break code at the end of the repeated
  190. * sequence. This means we can't propagate the repeated key
  191. * press, and must swallow it away.
  192. * + Need to handle possible PS/2 mouse input.
  193. * + Need to handle mashed keys.
  194. */
  195. while (1) {
  196. while ((inb(KBD_STATUS_REG) & KBD_STAT_OBF) == 0)
  197. cpu_relax();
  198. /*
  199. * Fetch the scancode.
  200. */
  201. scancode = inb(KBD_DATA_REG);
  202. scanstatus = inb(KBD_STATUS_REG);
  203. /*
  204. * Skip mouse input.
  205. */
  206. if (scanstatus & KBD_STAT_MOUSE_OBF)
  207. continue;
  208. /*
  209. * If we see 0xe0, this is either a break code for KP
  210. * ENTER, or a repeat make for KP ENTER. Either way,
  211. * since the second byte is equivalent to an ENTER,
  212. * skip the 0xe0 and try again.
  213. *
  214. * If we see 0x1c, this must be a repeat ENTER or KP
  215. * ENTER (and we swallowed 0xe0 before). Try again.
  216. *
  217. * We can also see make and break codes for other keys
  218. * mashed before or after pressing ENTER. Thus, if we
  219. * see anything other than 0x9c, we have to try again.
  220. *
  221. * Note, if you held some key as ENTER was depressed,
  222. * that break code would get leaked out.
  223. */
  224. if (scancode != 0x9c)
  225. continue;
  226. return;
  227. }
  228. }