kdb_keyboard.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. /*
  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. return -1;
  80. scancode &= 0x7f;
  81. /*
  82. * Translate scancode
  83. */
  84. if (scancode == 0x3a) {
  85. /*
  86. * Toggle caps lock
  87. */
  88. shift_lock ^= 1;
  89. #ifdef KDB_BLINK_LED
  90. kdb_toggleled(0x4);
  91. #endif
  92. return -1;
  93. }
  94. if (scancode == 0x0e) {
  95. /*
  96. * Backspace
  97. */
  98. return 8;
  99. }
  100. /* Special Key */
  101. switch (scancode) {
  102. case 0xF: /* Tab */
  103. return 9;
  104. case 0x53: /* Del */
  105. return 4;
  106. case 0x47: /* Home */
  107. return 1;
  108. case 0x4F: /* End */
  109. return 5;
  110. case 0x4B: /* Left */
  111. return 2;
  112. case 0x48: /* Up */
  113. return 16;
  114. case 0x50: /* Down */
  115. return 14;
  116. case 0x4D: /* Right */
  117. return 6;
  118. }
  119. if (scancode == 0xe0)
  120. return -1;
  121. /*
  122. * For Japanese 86/106 keyboards
  123. * See comment in drivers/char/pc_keyb.c.
  124. * - Masahiro Adegawa
  125. */
  126. if (scancode == 0x73)
  127. scancode = 0x59;
  128. else if (scancode == 0x7d)
  129. scancode = 0x7c;
  130. if (!shift_lock && !shift_key && !ctrl_key) {
  131. keychar = plain_map[scancode];
  132. } else if ((shift_lock || shift_key) && key_maps[1]) {
  133. keychar = key_maps[1][scancode];
  134. } else if (ctrl_key && key_maps[4]) {
  135. keychar = key_maps[4][scancode];
  136. } else {
  137. keychar = 0x0020;
  138. kdb_printf("Unknown state/scancode (%d)\n", scancode);
  139. }
  140. keychar &= 0x0fff;
  141. if (keychar == '\t')
  142. keychar = ' ';
  143. switch (KTYP(keychar)) {
  144. case KT_LETTER:
  145. case KT_LATIN:
  146. if (isprint(keychar))
  147. break; /* printable characters */
  148. /* drop through */
  149. case KT_SPEC:
  150. if (keychar == K_ENTER)
  151. break;
  152. /* drop through */
  153. default:
  154. return -1; /* ignore unprintables */
  155. }
  156. if ((scancode & 0x7f) == 0x1c) {
  157. /*
  158. * enter key. All done. Absorb the release scancode.
  159. */
  160. while ((inb(KBD_STATUS_REG) & KBD_STAT_OBF) == 0)
  161. ;
  162. /*
  163. * Fetch the scancode
  164. */
  165. scancode = inb(KBD_DATA_REG);
  166. scanstatus = inb(KBD_STATUS_REG);
  167. while (scanstatus & KBD_STAT_MOUSE_OBF) {
  168. scancode = inb(KBD_DATA_REG);
  169. scanstatus = inb(KBD_STATUS_REG);
  170. }
  171. if (scancode != 0x9c) {
  172. /*
  173. * Wasn't an enter-release, why not?
  174. */
  175. kdb_printf("kdb: expected enter got 0x%x status 0x%x\n",
  176. scancode, scanstatus);
  177. }
  178. return 13;
  179. }
  180. return keychar & 0xff;
  181. }
  182. EXPORT_SYMBOL_GPL(kdb_get_kbd_char);