at_keyboard.c 8.3 KB

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