at_keyboard.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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. #include <grub/time.h>
  25. #include <grub/loader.h>
  26. #include <grub/ps2.h>
  27. GRUB_MOD_LICENSE ("GPLv3+");
  28. static grub_uint8_t grub_keyboard_controller_orig;
  29. static grub_uint8_t grub_keyboard_orig_set;
  30. struct grub_ps2_state ps2_state;
  31. static int ping_sent;
  32. static void
  33. grub_keyboard_controller_init (void);
  34. static void
  35. keyboard_controller_wait_until_ready (void)
  36. {
  37. while (! KEYBOARD_COMMAND_ISREADY (grub_inb (KEYBOARD_REG_STATUS)));
  38. }
  39. static grub_uint8_t
  40. wait_ack (void)
  41. {
  42. grub_uint64_t endtime;
  43. grub_uint8_t ack;
  44. endtime = grub_get_time_ms () + 20;
  45. do
  46. ack = grub_inb (KEYBOARD_REG_DATA);
  47. while (ack != GRUB_AT_ACK && ack != GRUB_AT_NACK
  48. && grub_get_time_ms () < endtime);
  49. return ack;
  50. }
  51. static int
  52. at_command (grub_uint8_t data)
  53. {
  54. unsigned i;
  55. for (i = 0; i < GRUB_AT_TRIES; i++)
  56. {
  57. grub_uint8_t ack;
  58. keyboard_controller_wait_until_ready ();
  59. grub_outb (data, KEYBOARD_REG_STATUS);
  60. ack = wait_ack ();
  61. if (ack == GRUB_AT_NACK)
  62. continue;
  63. if (ack == GRUB_AT_ACK)
  64. break;
  65. return 0;
  66. }
  67. return (i != GRUB_AT_TRIES);
  68. }
  69. static void
  70. grub_keyboard_controller_write (grub_uint8_t c)
  71. {
  72. at_command (KEYBOARD_COMMAND_WRITE);
  73. keyboard_controller_wait_until_ready ();
  74. grub_outb (c, KEYBOARD_REG_DATA);
  75. }
  76. #if defined (GRUB_MACHINE_MIPS_LOONGSON) || defined (GRUB_MACHINE_QEMU) || defined (GRUB_MACHINE_COREBOOT) || defined (GRUB_MACHINE_MIPS_QEMU_MIPS)
  77. #define USE_SCANCODE_SET 1
  78. #else
  79. #define USE_SCANCODE_SET 0
  80. #endif
  81. #if !USE_SCANCODE_SET
  82. static grub_uint8_t
  83. grub_keyboard_controller_read (void)
  84. {
  85. at_command (KEYBOARD_COMMAND_READ);
  86. keyboard_controller_wait_until_ready ();
  87. return grub_inb (KEYBOARD_REG_DATA);
  88. }
  89. #endif
  90. static int
  91. write_mode (int mode)
  92. {
  93. unsigned i;
  94. for (i = 0; i < GRUB_AT_TRIES; i++)
  95. {
  96. grub_uint8_t ack;
  97. keyboard_controller_wait_until_ready ();
  98. grub_outb (0xf0, KEYBOARD_REG_DATA);
  99. keyboard_controller_wait_until_ready ();
  100. grub_outb (mode, KEYBOARD_REG_DATA);
  101. keyboard_controller_wait_until_ready ();
  102. ack = wait_ack ();
  103. if (ack == GRUB_AT_NACK)
  104. continue;
  105. if (ack == GRUB_AT_ACK)
  106. break;
  107. return 0;
  108. }
  109. return (i != GRUB_AT_TRIES);
  110. }
  111. static int
  112. query_mode (void)
  113. {
  114. grub_uint8_t ret;
  115. int e;
  116. e = write_mode (0);
  117. if (!e)
  118. return 0;
  119. keyboard_controller_wait_until_ready ();
  120. do
  121. ret = grub_inb (KEYBOARD_REG_DATA);
  122. while (ret == GRUB_AT_ACK);
  123. /* QEMU translates the set even in no-translate mode. */
  124. if (ret == 0x43 || ret == 1)
  125. return 1;
  126. if (ret == 0x41 || ret == 2)
  127. return 2;
  128. if (ret == 0x3f || ret == 3)
  129. return 3;
  130. return 0;
  131. }
  132. static void
  133. set_scancodes (void)
  134. {
  135. /* You must have visited computer museum. Keyboard without scancode set
  136. knowledge. Assume XT. */
  137. if (!grub_keyboard_orig_set)
  138. {
  139. grub_dprintf ("atkeyb", "No sets support assumed\n");
  140. ps2_state.current_set = 1;
  141. return;
  142. }
  143. #if !USE_SCANCODE_SET
  144. ps2_state.current_set = 1;
  145. return;
  146. #else
  147. grub_keyboard_controller_write (grub_keyboard_controller_orig
  148. & ~KEYBOARD_AT_TRANSLATE);
  149. write_mode (2);
  150. ps2_state.current_set = query_mode ();
  151. grub_dprintf ("atkeyb", "returned set %d\n", ps2_state.current_set);
  152. if (ps2_state.current_set == 2)
  153. return;
  154. write_mode (1);
  155. ps2_state.current_set = query_mode ();
  156. grub_dprintf ("atkeyb", "returned set %d\n", ps2_state.current_set);
  157. if (ps2_state.current_set == 1)
  158. return;
  159. grub_dprintf ("atkeyb", "no supported scancode set found\n");
  160. #endif
  161. }
  162. static void
  163. keyboard_controller_led (grub_uint8_t leds)
  164. {
  165. keyboard_controller_wait_until_ready ();
  166. grub_outb (0xed, KEYBOARD_REG_DATA);
  167. keyboard_controller_wait_until_ready ();
  168. grub_outb (leds & 0x7, KEYBOARD_REG_DATA);
  169. }
  170. int
  171. grub_at_keyboard_is_alive (void)
  172. {
  173. if (ps2_state.current_set != 0)
  174. return 1;
  175. if (ping_sent
  176. && KEYBOARD_COMMAND_ISREADY (grub_inb (KEYBOARD_REG_STATUS))
  177. && grub_inb (KEYBOARD_REG_DATA) == 0x55)
  178. {
  179. grub_keyboard_controller_init ();
  180. return 1;
  181. }
  182. if (KEYBOARD_COMMAND_ISREADY (grub_inb (KEYBOARD_REG_STATUS)))
  183. {
  184. grub_outb (0xaa, KEYBOARD_REG_STATUS);
  185. ping_sent = 1;
  186. }
  187. return 0;
  188. }
  189. /* If there is a character pending, return it;
  190. otherwise return GRUB_TERM_NO_KEY. */
  191. static int
  192. grub_at_keyboard_getkey (struct grub_term_input *term __attribute__ ((unused)))
  193. {
  194. grub_uint8_t at_key;
  195. int ret;
  196. grub_uint8_t old_led;
  197. if (!grub_at_keyboard_is_alive ())
  198. return GRUB_TERM_NO_KEY;
  199. if (! KEYBOARD_ISREADY (grub_inb (KEYBOARD_REG_STATUS)))
  200. return -1;
  201. at_key = grub_inb (KEYBOARD_REG_DATA);
  202. old_led = ps2_state.led_status;
  203. ret = grub_ps2_process_incoming_byte (&ps2_state, at_key);
  204. if (old_led != ps2_state.led_status)
  205. keyboard_controller_led (ps2_state.led_status);
  206. return ret;
  207. }
  208. static void
  209. grub_keyboard_controller_init (void)
  210. {
  211. ps2_state.at_keyboard_status = 0;
  212. /* Drain input buffer. */
  213. while (1)
  214. {
  215. keyboard_controller_wait_until_ready ();
  216. if (! KEYBOARD_ISREADY (grub_inb (KEYBOARD_REG_STATUS)))
  217. break;
  218. keyboard_controller_wait_until_ready ();
  219. grub_inb (KEYBOARD_REG_DATA);
  220. }
  221. #if defined (GRUB_MACHINE_MIPS_LOONGSON) || defined (GRUB_MACHINE_MIPS_QEMU_MIPS)
  222. grub_keyboard_controller_orig = 0;
  223. grub_keyboard_orig_set = 2;
  224. #elif defined (GRUB_MACHINE_QEMU) || defined (GRUB_MACHINE_COREBOOT)
  225. /* *BSD relies on those settings. */
  226. grub_keyboard_controller_orig = KEYBOARD_AT_TRANSLATE;
  227. grub_keyboard_orig_set = 2;
  228. #else
  229. grub_keyboard_controller_orig = grub_keyboard_controller_read ();
  230. grub_keyboard_orig_set = query_mode ();
  231. #endif
  232. set_scancodes ();
  233. keyboard_controller_led (ps2_state.led_status);
  234. }
  235. static grub_err_t
  236. grub_keyboard_controller_fini (struct grub_term_input *term __attribute__ ((unused)))
  237. {
  238. if (ps2_state.current_set == 0)
  239. return GRUB_ERR_NONE;
  240. if (grub_keyboard_orig_set)
  241. write_mode (grub_keyboard_orig_set);
  242. grub_keyboard_controller_write (grub_keyboard_controller_orig);
  243. return GRUB_ERR_NONE;
  244. }
  245. static grub_err_t
  246. grub_at_fini_hw (int noreturn __attribute__ ((unused)))
  247. {
  248. return grub_keyboard_controller_fini (NULL);
  249. }
  250. static grub_err_t
  251. grub_at_restore_hw (void)
  252. {
  253. if (ps2_state.current_set == 0)
  254. return GRUB_ERR_NONE;
  255. /* Drain input buffer. */
  256. while (1)
  257. {
  258. keyboard_controller_wait_until_ready ();
  259. if (! KEYBOARD_ISREADY (grub_inb (KEYBOARD_REG_STATUS)))
  260. break;
  261. keyboard_controller_wait_until_ready ();
  262. grub_inb (KEYBOARD_REG_DATA);
  263. }
  264. set_scancodes ();
  265. keyboard_controller_led (ps2_state.led_status);
  266. return GRUB_ERR_NONE;
  267. }
  268. static struct grub_term_input grub_at_keyboard_term =
  269. {
  270. .name = "at_keyboard",
  271. .fini = grub_keyboard_controller_fini,
  272. .getkey = grub_at_keyboard_getkey
  273. };
  274. GRUB_MOD_INIT(at_keyboard)
  275. {
  276. grub_term_register_input ("at_keyboard", &grub_at_keyboard_term);
  277. grub_loader_register_preboot_hook (grub_at_fini_hw, grub_at_restore_hw,
  278. GRUB_LOADER_PREBOOT_HOOK_PRIO_CONSOLE);
  279. }
  280. GRUB_MOD_FINI(at_keyboard)
  281. {
  282. grub_keyboard_controller_fini (NULL);
  283. grub_term_unregister_input (&grub_at_keyboard_term);
  284. }