kdb_keyboard.c 5.5 KB

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