at_keyboard.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2007,2008,2009 Free Software Foundation, Inc.
  4. *
  5. * GRUB is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * GRUB is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <grub/dl.h>
  19. #include <grub/at_keyboard.h>
  20. #include <grub/cpu/at_keyboard.h>
  21. #include <grub/cpu/io.h>
  22. #include <grub/misc.h>
  23. #include <grub/term.h>
  24. static short at_keyboard_status = 0;
  25. static int pending_key = -1;
  26. #define KEYBOARD_STATUS_SHIFT_L (1 << 0)
  27. #define KEYBOARD_STATUS_SHIFT_R (1 << 1)
  28. #define KEYBOARD_STATUS_ALT_L (1 << 2)
  29. #define KEYBOARD_STATUS_ALT_R (1 << 3)
  30. #define KEYBOARD_STATUS_CTRL_L (1 << 4)
  31. #define KEYBOARD_STATUS_CTRL_R (1 << 5)
  32. #define KEYBOARD_STATUS_CAPS_LOCK (1 << 6)
  33. #define KEYBOARD_STATUS_NUM_LOCK (1 << 7)
  34. static grub_uint8_t led_status;
  35. #define KEYBOARD_LED_SCROLL (1 << 0)
  36. #define KEYBOARD_LED_NUM (1 << 1)
  37. #define KEYBOARD_LED_CAPS (1 << 2)
  38. static char keyboard_map[128] =
  39. {
  40. '\0', GRUB_TERM_ESC, '1', '2', '3', '4', '5', '6',
  41. '7', '8', '9', '0', '-', '=', GRUB_TERM_BACKSPACE, GRUB_TERM_TAB,
  42. 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i',
  43. 'o', 'p', '[', ']', '\n', '\0', 'a', 's',
  44. 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';',
  45. '\'', '`', '\0', '\\', 'z', 'x', 'c', 'v',
  46. 'b', 'n', 'm', ',', '.', '/', '\0', '*',
  47. '\0', ' ', '\0', '\0', '\0', '\0', '\0', '\0',
  48. '\0', '\0', '\0', '\0', '\0', '\0', '\0', GRUB_TERM_HOME,
  49. GRUB_TERM_UP, GRUB_TERM_NPAGE, '-', GRUB_TERM_LEFT, '\0', GRUB_TERM_RIGHT, '+', GRUB_TERM_END,
  50. GRUB_TERM_DOWN, GRUB_TERM_PPAGE, '\0', GRUB_TERM_DC, '\0', '\0', '\0', '\0',
  51. '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
  52. '\0', '\0', '\0', '\0', '\0', OLPC_UP, OLPC_DOWN, OLPC_LEFT,
  53. OLPC_RIGHT
  54. };
  55. static char keyboard_map_shift[128] =
  56. {
  57. '\0', '\0', '!', '@', '#', '$', '%', '^',
  58. '&', '*', '(', ')', '_', '+', '\0', '\0',
  59. 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I',
  60. 'O', 'P', '{', '}', '\n', '\0', 'A', 'S',
  61. 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':',
  62. '\"', '~', '\0', '|', 'Z', 'X', 'C', 'V',
  63. 'B', 'N', 'M', '<', '>', '?'
  64. };
  65. static grub_uint8_t grub_keyboard_controller_orig;
  66. static void
  67. keyboard_controller_wait_until_ready (void)
  68. {
  69. while (! KEYBOARD_COMMAND_ISREADY (grub_inb (KEYBOARD_REG_STATUS)));
  70. }
  71. static void
  72. grub_keyboard_controller_write (grub_uint8_t c)
  73. {
  74. keyboard_controller_wait_until_ready ();
  75. grub_outb (KEYBOARD_COMMAND_WRITE, KEYBOARD_REG_STATUS);
  76. grub_outb (c, KEYBOARD_REG_DATA);
  77. }
  78. static grub_uint8_t
  79. grub_keyboard_controller_read (void)
  80. {
  81. keyboard_controller_wait_until_ready ();
  82. grub_outb (KEYBOARD_COMMAND_READ, KEYBOARD_REG_STATUS);
  83. return grub_inb (KEYBOARD_REG_DATA);
  84. }
  85. static void
  86. keyboard_controller_led (grub_uint8_t leds)
  87. {
  88. keyboard_controller_wait_until_ready ();
  89. grub_outb (0xed, KEYBOARD_REG_DATA);
  90. keyboard_controller_wait_until_ready ();
  91. grub_outb (leds & 0x7, KEYBOARD_REG_DATA);
  92. }
  93. /* FIXME: This should become an interrupt service routine. For now
  94. it's just used to catch events from control keys. */
  95. static void
  96. grub_keyboard_isr (char key)
  97. {
  98. char is_make = KEYBOARD_ISMAKE (key);
  99. key = KEYBOARD_SCANCODE (key);
  100. if (is_make)
  101. switch (key)
  102. {
  103. case SHIFT_L:
  104. at_keyboard_status |= KEYBOARD_STATUS_SHIFT_L;
  105. break;
  106. case SHIFT_R:
  107. at_keyboard_status |= KEYBOARD_STATUS_SHIFT_R;
  108. break;
  109. case CTRL:
  110. at_keyboard_status |= KEYBOARD_STATUS_CTRL_L;
  111. break;
  112. case ALT:
  113. at_keyboard_status |= KEYBOARD_STATUS_ALT_L;
  114. break;
  115. default:
  116. /* Skip grub_dprintf. */
  117. return;
  118. }
  119. else
  120. switch (key)
  121. {
  122. case SHIFT_L:
  123. at_keyboard_status &= ~KEYBOARD_STATUS_SHIFT_L;
  124. break;
  125. case SHIFT_R:
  126. at_keyboard_status &= ~KEYBOARD_STATUS_SHIFT_R;
  127. break;
  128. case CTRL:
  129. at_keyboard_status &= ~KEYBOARD_STATUS_CTRL_L;
  130. break;
  131. case ALT:
  132. at_keyboard_status &= ~KEYBOARD_STATUS_ALT_L;
  133. break;
  134. default:
  135. /* Skip grub_dprintf. */
  136. return;
  137. }
  138. #ifdef DEBUG_AT_KEYBOARD
  139. grub_dprintf ("atkeyb", "Control key 0x%0x was %s\n", key, is_make ? "pressed" : "unpressed");
  140. #endif
  141. }
  142. /* If there is a raw key pending, return it; otherwise return -1. */
  143. static int
  144. grub_keyboard_getkey (void)
  145. {
  146. grub_uint8_t key;
  147. if (! KEYBOARD_ISREADY (grub_inb (KEYBOARD_REG_STATUS)))
  148. return -1;
  149. key = grub_inb (KEYBOARD_REG_DATA);
  150. /* FIXME */ grub_keyboard_isr (key);
  151. if (! KEYBOARD_ISMAKE (key))
  152. return -1;
  153. return (KEYBOARD_SCANCODE (key));
  154. }
  155. /* If there is a character pending, return it; otherwise return -1. */
  156. static int
  157. grub_at_keyboard_getkey_noblock (void)
  158. {
  159. int code, key;
  160. code = grub_keyboard_getkey ();
  161. if (code == -1)
  162. return -1;
  163. #ifdef DEBUG_AT_KEYBOARD
  164. grub_dprintf ("atkeyb", "Detected key 0x%x\n", key);
  165. #endif
  166. switch (code)
  167. {
  168. case CAPS_LOCK:
  169. /* Caps lock sends scan code twice. Get the second one and discard it. */
  170. while (grub_keyboard_getkey () == -1);
  171. at_keyboard_status ^= KEYBOARD_STATUS_CAPS_LOCK;
  172. led_status ^= KEYBOARD_LED_CAPS;
  173. keyboard_controller_led (led_status);
  174. #ifdef DEBUG_AT_KEYBOARD
  175. grub_dprintf ("atkeyb", "caps_lock = %d\n", !!(at_keyboard_status & KEYBOARD_STATUS_CAPS_LOCK));
  176. #endif
  177. key = -1;
  178. break;
  179. case NUM_LOCK:
  180. /* Num lock sends scan code twice. Get the second one and discard it. */
  181. while (grub_keyboard_getkey () == -1);
  182. at_keyboard_status ^= KEYBOARD_STATUS_NUM_LOCK;
  183. led_status ^= KEYBOARD_LED_NUM;
  184. keyboard_controller_led (led_status);
  185. #ifdef DEBUG_AT_KEYBOARD
  186. grub_dprintf ("atkeyb", "num_lock = %d\n", !!(at_keyboard_status & KEYBOARD_STATUS_NUM_LOCK));
  187. #endif
  188. key = -1;
  189. break;
  190. case SCROLL_LOCK:
  191. /* For scroll lock we don't keep track of status. Only update its led. */
  192. led_status ^= KEYBOARD_LED_SCROLL;
  193. keyboard_controller_led (led_status);
  194. key = -1;
  195. break;
  196. default:
  197. if (at_keyboard_status & (KEYBOARD_STATUS_CTRL_L | KEYBOARD_STATUS_CTRL_R))
  198. key = keyboard_map[code] - 'a' + 1;
  199. else if ((at_keyboard_status & (KEYBOARD_STATUS_SHIFT_L | KEYBOARD_STATUS_SHIFT_R))
  200. && keyboard_map_shift[code])
  201. key = keyboard_map_shift[code];
  202. else
  203. key = keyboard_map[code];
  204. if (key == 0)
  205. grub_dprintf ("atkeyb", "Unknown key 0x%x detected\n", code);
  206. if (at_keyboard_status & KEYBOARD_STATUS_CAPS_LOCK)
  207. {
  208. if ((key >= 'a') && (key <= 'z'))
  209. key += 'A' - 'a';
  210. else if ((key >= 'A') && (key <= 'Z'))
  211. key += 'a' - 'A';
  212. }
  213. }
  214. return key;
  215. }
  216. static int
  217. grub_at_keyboard_checkkey (void)
  218. {
  219. if (pending_key != -1)
  220. return 1;
  221. pending_key = grub_at_keyboard_getkey_noblock ();
  222. if (pending_key != -1)
  223. return 1;
  224. return -1;
  225. }
  226. static int
  227. grub_at_keyboard_getkey (void)
  228. {
  229. int key;
  230. if (pending_key != -1)
  231. {
  232. key = pending_key;
  233. pending_key = -1;
  234. return key;
  235. }
  236. do
  237. {
  238. key = grub_at_keyboard_getkey_noblock ();
  239. } while (key == -1);
  240. return key;
  241. }
  242. static grub_err_t
  243. grub_keyboard_controller_init (void)
  244. {
  245. pending_key = -1;
  246. at_keyboard_status = 0;
  247. grub_keyboard_controller_orig = grub_keyboard_controller_read ();
  248. grub_keyboard_controller_write (grub_keyboard_controller_orig | KEYBOARD_SCANCODE_SET1);
  249. return GRUB_ERR_NONE;
  250. }
  251. static grub_err_t
  252. grub_keyboard_controller_fini (void)
  253. {
  254. grub_keyboard_controller_write (grub_keyboard_controller_orig);
  255. return GRUB_ERR_NONE;
  256. }
  257. static struct grub_term_input grub_at_keyboard_term =
  258. {
  259. .name = "at_keyboard",
  260. .init = grub_keyboard_controller_init,
  261. .fini = grub_keyboard_controller_fini,
  262. .checkkey = grub_at_keyboard_checkkey,
  263. .getkey = grub_at_keyboard_getkey,
  264. };
  265. GRUB_MOD_INIT(at_keyboard)
  266. {
  267. grub_term_register_input ("at_keyboard", &grub_at_keyboard_term);
  268. }
  269. GRUB_MOD_FINI(at_keyboard)
  270. {
  271. grub_term_unregister_input (&grub_at_keyboard_term);
  272. }