at_keyboard.c 8.1 KB

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