i8042.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674
  1. /*
  2. * (C) Copyright 2002 ELTEC Elektronik AG
  3. * Frank Gottschling <fgottschling@eltec.de>
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. /* i8042.c - Intel 8042 keyboard driver routines */
  24. /* includes */
  25. #include <common.h>
  26. #ifdef CONFIG_I8042_KBD
  27. #ifdef CONFIG_USE_CPCIDVI
  28. extern u8 gt_cpcidvi_in8(u32 offset);
  29. extern void gt_cpcidvi_out8(u32 offset, u8 data);
  30. #define in8(a) gt_cpcidvi_in8(a)
  31. #define out8(a, b) gt_cpcidvi_out8(a,b)
  32. #endif
  33. #include <i8042.h>
  34. /* defines */
  35. #ifdef CONFIG_CONSOLE_CURSOR
  36. extern void console_cursor (int state);
  37. static int blinkCount = CFG_CONSOLE_BLINK_COUNT;
  38. static int cursor_state = 0;
  39. #endif
  40. /* locals */
  41. static int kbd_input = -1; /* no input yet */
  42. static int kbd_mapping = KBD_US; /* default US keyboard */
  43. static int kbd_flags = NORMAL; /* after reset */
  44. static int kbd_state = 0; /* unshift code */
  45. static void kbd_conv_char (unsigned char scan_code);
  46. static void kbd_led_set (void);
  47. static void kbd_normal (unsigned char scan_code);
  48. static void kbd_shift (unsigned char scan_code);
  49. static void kbd_ctrl (unsigned char scan_code);
  50. static void kbd_num (unsigned char scan_code);
  51. static void kbd_caps (unsigned char scan_code);
  52. static void kbd_scroll (unsigned char scan_code);
  53. static void kbd_alt (unsigned char scan_code);
  54. static int kbd_input_empty (void);
  55. static int kbd_reset (void);
  56. static unsigned char kbd_fct_map [144] =
  57. { /* kbd_fct_map table for scan code */
  58. 0, AS, AS, AS, AS, AS, AS, AS, /* scan 0- 7 */
  59. AS, AS, AS, AS, AS, AS, AS, AS, /* scan 8- F */
  60. AS, AS, AS, AS, AS, AS, AS, AS, /* scan 10-17 */
  61. AS, AS, AS, AS, AS, CN, AS, AS, /* scan 18-1F */
  62. AS, AS, AS, AS, AS, AS, AS, AS, /* scan 20-27 */
  63. AS, AS, SH, AS, AS, AS, AS, AS, /* scan 28-2F */
  64. AS, AS, AS, AS, AS, AS, SH, AS, /* scan 30-37 */
  65. AS, AS, CP, 0, 0, 0, 0, 0, /* scan 38-3F */
  66. 0, 0, 0, 0, 0, NM, ST, ES, /* scan 40-47 */
  67. ES, ES, ES, ES, ES, ES, ES, ES, /* scan 48-4F */
  68. ES, ES, ES, ES, 0, 0, AS, 0, /* scan 50-57 */
  69. 0, 0, 0, 0, 0, 0, 0, 0, /* scan 58-5F */
  70. 0, 0, 0, 0, 0, 0, 0, 0, /* scan 60-67 */
  71. 0, 0, 0, 0, 0, 0, 0, 0, /* scan 68-6F */
  72. AS, 0, 0, AS, 0, 0, AS, 0, /* scan 70-77 */
  73. 0, AS, 0, 0, 0, AS, 0, 0, /* scan 78-7F */
  74. AS, CN, AS, AS, AK, ST, EX, EX, /* enhanced */
  75. AS, EX, EX, AS, EX, AS, EX, EX /* enhanced */
  76. };
  77. static unsigned char kbd_key_map [2][5][144] =
  78. {
  79. { /* US keyboard */
  80. { /* unshift code */
  81. 0, 0x1b, '1', '2', '3', '4', '5', '6', /* scan 0- 7 */
  82. '7', '8', '9', '0', '-', '=', 0x08, '\t', /* scan 8- F */
  83. 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', /* scan 10-17 */
  84. 'o', 'p', '[', ']', '\r', CN, 'a', 's', /* scan 18-1F */
  85. 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', /* scan 20-27 */
  86. '\'', '`', SH, '\\', 'z', 'x', 'c', 'v', /* scan 28-2F */
  87. 'b', 'n', 'm', ',', '.', '/', SH, '*', /* scan 30-37 */
  88. ' ', ' ', CP, 0, 0, 0, 0, 0, /* scan 38-3F */
  89. 0, 0, 0, 0, 0, NM, ST, '7', /* scan 40-47 */
  90. '8', '9', '-', '4', '5', '6', '+', '1', /* scan 48-4F */
  91. '2', '3', '0', '.', 0, 0, 0, 0, /* scan 50-57 */
  92. 0, 0, 0, 0, 0, 0, 0, 0, /* scan 58-5F */
  93. 0, 0, 0, 0, 0, 0, 0, 0, /* scan 60-67 */
  94. 0, 0, 0, 0, 0, 0, 0, 0, /* scan 68-6F */
  95. 0, 0, 0, 0, 0, 0, 0, 0, /* scan 70-77 */
  96. 0, 0, 0, 0, 0, 0, 0, 0, /* scan 78-7F */
  97. '\r', CN, '/', '*', ' ', ST, 'F', 'A', /* extended */
  98. 0, 'D', 'C', 0, 'B', 0, '@', 'P' /* extended */
  99. },
  100. { /* shift code */
  101. 0, 0x1b, '!', '@', '#', '$', '%', '^', /* scan 0- 7 */
  102. '&', '*', '(', ')', '_', '+', 0x08, '\t', /* scan 8- F */
  103. 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', /* scan 10-17 */
  104. 'O', 'P', '{', '}', '\r', CN, 'A', 'S', /* scan 18-1F */
  105. 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':', /* scan 20-27 */
  106. '"', '~', SH, '|', 'Z', 'X', 'C', 'V', /* scan 28-2F */
  107. 'B', 'N', 'M', '<', '>', '?', SH, '*', /* scan 30-37 */
  108. ' ', ' ', CP, 0, 0, 0, 0, 0, /* scan 38-3F */
  109. 0, 0, 0, 0, 0, NM, ST, '7', /* scan 40-47 */
  110. '8', '9', '-', '4', '5', '6', '+', '1', /* scan 48-4F */
  111. '2', '3', '0', '.', 0, 0, 0, 0, /* scan 50-57 */
  112. 0, 0, 0, 0, 0, 0, 0, 0, /* scan 58-5F */
  113. 0, 0, 0, 0, 0, 0, 0, 0, /* scan 60-67 */
  114. 0, 0, 0, 0, 0, 0, 0, 0, /* scan 68-6F */
  115. 0, 0, 0, 0, 0, 0, 0, 0, /* scan 70-77 */
  116. 0, 0, 0, 0, 0, 0, 0, 0, /* scan 78-7F */
  117. '\r', CN, '/', '*', ' ', ST, 'F', 'A', /* extended */
  118. 0, 'D', 'C', 0, 'B', 0, '@', 'P' /* extended */
  119. },
  120. { /* control code */
  121. 0xff, 0x1b, 0xff, 0x00, 0xff, 0xff, 0xff, 0xff, /* scan 0- 7 */
  122. 0x1e, 0xff, 0xff, 0xff, 0x1f, 0xff, 0xff, '\t', /* scan 8- F */
  123. 0x11, 0x17, 0x05, 0x12, 0x14, 0x19, 0x15, 0x09, /* scan 10-17 */
  124. 0x0f, 0x10, 0x1b, 0x1d, '\r', CN, 0x01, 0x13, /* scan 18-1F */
  125. 0x04, 0x06, 0x07, 0x08, 0x0a, 0x0b, 0x0c, 0xff, /* scan 20-27 */
  126. 0xff, 0x1c, SH, 0xff, 0x1a, 0x18, 0x03, 0x16, /* scan 28-2F */
  127. 0x02, 0x0e, 0x0d, 0xff, 0xff, 0xff, SH, 0xff, /* scan 30-37 */
  128. 0xff, 0xff, CP, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 38-3F */
  129. 0xff, 0xff, 0xff, 0xff, 0xff, NM, ST, 0xff, /* scan 40-47 */
  130. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 48-4F */
  131. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 50-57 */
  132. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 58-5F */
  133. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 60-67 */
  134. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 68-6F */
  135. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 70-77 */
  136. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 78-7F */
  137. '\r', CN, '/', '*', ' ', ST, 0xff, 0xff, /* extended */
  138. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff /* extended */
  139. },
  140. { /* non numeric code */
  141. 0, 0x1b, '1', '2', '3', '4', '5', '6', /* scan 0- 7 */
  142. '7', '8', '9', '0', '-', '=', 0x08, '\t', /* scan 8- F */
  143. 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', /* scan 10-17 */
  144. 'o', 'p', '[', ']', '\r', CN, 'a', 's', /* scan 18-1F */
  145. 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', /* scan 20-27 */
  146. '\'', '`', SH, '\\', 'z', 'x', 'c', 'v', /* scan 28-2F */
  147. 'b', 'n', 'm', ',', '.', '/', SH, '*', /* scan 30-37 */
  148. ' ', ' ', CP, 0, 0, 0, 0, 0, /* scan 38-3F */
  149. 0, 0, 0, 0, 0, NM, ST, 'w', /* scan 40-47 */
  150. 'x', 'y', 'l', 't', 'u', 'v', 'm', 'q', /* scan 48-4F */
  151. 'r', 's', 'p', 'n', 0, 0, 0, 0, /* scan 50-57 */
  152. 0, 0, 0, 0, 0, 0, 0, 0, /* scan 58-5F */
  153. 0, 0, 0, 0, 0, 0, 0, 0, /* scan 60-67 */
  154. 0, 0, 0, 0, 0, 0, 0, 0, /* scan 68-6F */
  155. 0, 0, 0, 0, 0, 0, 0, 0, /* scan 70-77 */
  156. 0, 0, 0, 0, 0, 0, 0, 0, /* scan 78-7F */
  157. '\r', CN, '/', '*', ' ', ST, 'F', 'A', /* extended */
  158. 0, 'D', 'C', 0, 'B', 0, '@', 'P' /* extended */
  159. },
  160. { /* right alt mode - not used in US keyboard */
  161. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 0 - 7 */
  162. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 8 - F */
  163. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 10 -17 */
  164. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 18 -1F */
  165. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 20 -27 */
  166. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 28 -2F */
  167. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 30 -37 */
  168. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 38 -3F */
  169. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 40 -47 */
  170. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 48 -4F */
  171. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 50 -57 */
  172. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 58 -5F */
  173. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 60 -67 */
  174. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 68 -6F */
  175. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 70 -77 */
  176. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 78 -7F */
  177. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* extended */
  178. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff /* extended */
  179. }
  180. },
  181. { /* german keyboard */
  182. { /* unshift code */
  183. 0, 0x1b, '1', '2', '3', '4', '5', '6', /* scan 0- 7 */
  184. '7', '8', '9', '0', 0xe1, '\'', 0x08, '\t', /* scan 8- F */
  185. 'q', 'w', 'e', 'r', 't', 'z', 'u', 'i', /* scan 10-17 */
  186. 'o', 'p', 0x81, '+', '\r', CN, 'a', 's', /* scan 18-1F */
  187. 'd', 'f', 'g', 'h', 'j', 'k', 'l', 0x94, /* scan 20-27 */
  188. 0x84, '^', SH, '#', 'y', 'x', 'c', 'v', /* scan 28-2F */
  189. 'b', 'n', 'm', ',', '.', '-', SH, '*', /* scan 30-37 */
  190. ' ', ' ', CP, 0, 0, 0, 0, 0, /* scan 38-3F */
  191. 0, 0, 0, 0, 0, NM, ST, '7', /* scan 40-47 */
  192. '8', '9', '-', '4', '5', '6', '+', '1', /* scan 48-4F */
  193. '2', '3', '0', ',', 0, 0, '<', 0, /* scan 50-57 */
  194. 0, 0, 0, 0, 0, 0, 0, 0, /* scan 58-5F */
  195. 0, 0, 0, 0, 0, 0, 0, 0, /* scan 60-67 */
  196. 0, 0, 0, 0, 0, 0, 0, 0, /* scan 68-6F */
  197. 0, 0, 0, 0, 0, 0, 0, 0, /* scan 70-77 */
  198. 0, 0, 0, 0, 0, 0, 0, 0, /* scan 78-7F */
  199. '\r', CN, '/', '*', ' ', ST, 'F', 'A', /* extended */
  200. 0, 'D', 'C', 0, 'B', 0, '@', 'P' /* extended */
  201. },
  202. { /* shift code */
  203. 0, 0x1b, '!', '"', 0x15, '$', '%', '&', /* scan 0- 7 */
  204. '/', '(', ')', '=', '?', '`', 0x08, '\t', /* scan 8- F */
  205. 'Q', 'W', 'E', 'R', 'T', 'Z', 'U', 'I', /* scan 10-17 */
  206. 'O', 'P', 0x9a, '*', '\r', CN, 'A', 'S', /* scan 18-1F */
  207. 'D', 'F', 'G', 'H', 'J', 'K', 'L', 0x99, /* scan 20-27 */
  208. 0x8e, 0xf8, SH, '\'', 'Y', 'X', 'C', 'V', /* scan 28-2F */
  209. 'B', 'N', 'M', ';', ':', '_', SH, '*', /* scan 30-37 */
  210. ' ', ' ', CP, 0, 0, 0, 0, 0, /* scan 38-3F */
  211. 0, 0, 0, 0, 0, NM, ST, '7', /* scan 40-47 */
  212. '8', '9', '-', '4', '5', '6', '+', '1', /* scan 48-4F */
  213. '2', '3', '0', ',', 0, 0, '>', 0, /* scan 50-57 */
  214. 0, 0, 0, 0, 0, 0, 0, 0, /* scan 58-5F */
  215. 0, 0, 0, 0, 0, 0, 0, 0, /* scan 60-67 */
  216. 0, 0, 0, 0, 0, 0, 0, 0, /* scan 68-6F */
  217. 0, 0, 0, 0, 0, 0, 0, 0, /* scan 70-77 */
  218. 0, 0, 0, 0, 0, 0, 0, 0, /* scan 78-7F */
  219. '\r', CN, '/', '*', ' ', ST, 'F', 'A', /* extended */
  220. 0, 'D', 'C', 0, 'B', 0, '@', 'P' /* extended */
  221. },
  222. { /* control code */
  223. 0xff, 0x1b, 0xff, 0x00, 0xff, 0xff, 0xff, 0xff, /* scan 0- 7 */
  224. 0x1e, 0xff, 0xff, 0xff, 0x1f, 0xff, 0xff, '\t', /* scan 8- F */
  225. 0x11, 0x17, 0x05, 0x12, 0x14, 0x19, 0x15, 0x09, /* scan 10-17 */
  226. 0x0f, 0x10, 0x1b, 0x1d, '\r', CN, 0x01, 0x13, /* scan 18-1F */
  227. 0x04, 0x06, 0x07, 0x08, 0x0a, 0x0b, 0x0c, 0xff, /* scan 20-27 */
  228. 0xff, 0x1c, SH, 0xff, 0x1a, 0x18, 0x03, 0x16, /* scan 28-2F */
  229. 0x02, 0x0e, 0x0d, 0xff, 0xff, 0xff, SH, 0xff, /* scan 30-37 */
  230. 0xff, 0xff, CP, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 38-3F */
  231. 0xff, 0xff, 0xff, 0xff, 0xff, NM, ST, 0xff, /* scan 40-47 */
  232. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 48-4F */
  233. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 50-57 */
  234. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 58-5F */
  235. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 60-67 */
  236. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 68-6F */
  237. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 70-77 */
  238. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 78-7F */
  239. '\r', CN, '/', '*', ' ', ST, 0xff, 0xff, /* extended */
  240. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff /* extended */
  241. },
  242. { /* non numeric code */
  243. 0, 0x1b, '1', '2', '3', '4', '5', '6', /* scan 0- 7 */
  244. '7', '8', '9', '0', 0xe1, '\'', 0x08, '\t', /* scan 8- F */
  245. 'q', 'w', 'e', 'r', 't', 'z', 'u', 'i', /* scan 10-17 */
  246. 'o', 'p', 0x81, '+', '\r', CN, 'a', 's', /* scan 18-1F */
  247. 'd', 'f', 'g', 'h', 'j', 'k', 'l', 0x94, /* scan 20-27 */
  248. 0x84, '^', SH, 0, 'y', 'x', 'c', 'v', /* scan 28-2F */
  249. 'b', 'n', 'm', ',', '.', '-', SH, '*', /* scan 30-37 */
  250. ' ', ' ', CP, 0, 0, 0, 0, 0, /* scan 38-3F */
  251. 0, 0, 0, 0, 0, NM, ST, 'w', /* scan 40-47 */
  252. 'x', 'y', 'l', 't', 'u', 'v', 'm', 'q', /* scan 48-4F */
  253. 'r', 's', 'p', 'n', 0, 0, '<', 0, /* scan 50-57 */
  254. 0, 0, 0, 0, 0, 0, 0, 0, /* scan 58-5F */
  255. 0, 0, 0, 0, 0, 0, 0, 0, /* scan 60-67 */
  256. 0, 0, 0, 0, 0, 0, 0, 0, /* scan 68-6F */
  257. 0, 0, 0, 0, 0, 0, 0, 0, /* scan 70-77 */
  258. 0, 0, 0, 0, 0, 0, 0, 0, /* scan 78-7F */
  259. '\r', CN, '/', '*', ' ', ST, 'F', 'A', /* extended */
  260. 0, 'D', 'C', 0, 'B', 0, '@', 'P' /* extended */
  261. },
  262. { /* Right alt mode - is used in German keyboard */
  263. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 0 - 7 */
  264. '{', '[', ']', '}', '\\', 0xff, 0xff, 0xff, /* scan 8 - F */
  265. '@', 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 10 -17 */
  266. 0xff, 0xff, 0xff, '~', 0xff, 0xff, 0xff, 0xff, /* scan 18 -1F */
  267. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 20 -27 */
  268. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 28 -2F */
  269. 0xff, 0xff, 0xe6, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 30 -37 */
  270. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 38 -3F */
  271. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 40 -47 */
  272. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 48 -4F */
  273. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, '|', 0xff, /* scan 50 -57 */
  274. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 58 -5F */
  275. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 60 -67 */
  276. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 68 -6F */
  277. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 70 -77 */
  278. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 78 -7F */
  279. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* extended */
  280. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff /* extended */
  281. }
  282. }
  283. };
  284. static unsigned char ext_key_map [] =
  285. {
  286. 0x1c, /* keypad enter */
  287. 0x1d, /* right control */
  288. 0x35, /* keypad slash */
  289. 0x37, /* print screen */
  290. 0x38, /* right alt */
  291. 0x46, /* break */
  292. 0x47, /* editpad home */
  293. 0x48, /* editpad up */
  294. 0x49, /* editpad pgup */
  295. 0x4b, /* editpad left */
  296. 0x4d, /* editpad right */
  297. 0x4f, /* editpad end */
  298. 0x50, /* editpad dn */
  299. 0x51, /* editpad pgdn */
  300. 0x52, /* editpad ins */
  301. 0x53, /* editpad del */
  302. 0x00 /* map end */
  303. };
  304. /*******************************************************************************
  305. *
  306. * i8042_kbd_init - reset keyboard and init state flags
  307. */
  308. int i8042_kbd_init (void)
  309. {
  310. int keymap, try;
  311. char *penv;
  312. #ifdef CONFIG_USE_CPCIDVI
  313. if ((penv = getenv ("console")) != NULL) {
  314. if (strncmp (penv, "serial", 7) == 0) {
  315. return -1;
  316. }
  317. }
  318. #endif
  319. /* Init keyboard device (default US layout) */
  320. keymap = KBD_US;
  321. if ((penv = getenv ("keymap")) != NULL)
  322. {
  323. if (strncmp (penv, "de", 3) == 0)
  324. keymap = KBD_GER;
  325. }
  326. for (try = 0; try < KBD_RESET_TRIES; try++)
  327. {
  328. if (kbd_reset() == 0)
  329. {
  330. kbd_mapping = keymap;
  331. kbd_flags = NORMAL;
  332. kbd_state = 0;
  333. kbd_led_set();
  334. return 0;
  335. }
  336. }
  337. return -1;
  338. }
  339. /*******************************************************************************
  340. *
  341. * i8042_tstc - test if keyboard input is available
  342. * option: cursor blinking if called in a loop
  343. */
  344. int i8042_tstc (void)
  345. {
  346. unsigned char scan_code = 0;
  347. #ifdef CONFIG_CONSOLE_CURSOR
  348. if (--blinkCount == 0)
  349. {
  350. cursor_state ^= 1;
  351. console_cursor (cursor_state);
  352. blinkCount = CFG_CONSOLE_BLINK_COUNT;
  353. udelay (10);
  354. }
  355. #endif
  356. if ((in8 (I8042_STATUS_REG) & 0x01) == 0)
  357. return 0;
  358. else
  359. {
  360. scan_code = in8 (I8042_DATA_REG);
  361. if (scan_code == 0xfa)
  362. return 0;
  363. kbd_conv_char(scan_code);
  364. if (kbd_input != -1)
  365. return 1;
  366. }
  367. return 0;
  368. }
  369. /*******************************************************************************
  370. *
  371. * i8042_getc - wait till keyboard input is available
  372. * option: turn on/off cursor while waiting
  373. */
  374. int i8042_getc (void)
  375. {
  376. int ret_chr;
  377. unsigned char scan_code;
  378. while (kbd_input == -1)
  379. {
  380. while ((in8 (I8042_STATUS_REG) & 0x01) == 0)
  381. {
  382. #ifdef CONFIG_CONSOLE_CURSOR
  383. if (--blinkCount==0)
  384. {
  385. cursor_state ^= 1;
  386. console_cursor (cursor_state);
  387. blinkCount = CFG_CONSOLE_BLINK_COUNT;
  388. }
  389. udelay (10);
  390. #endif
  391. }
  392. scan_code = in8 (I8042_DATA_REG);
  393. if (scan_code != 0xfa)
  394. kbd_conv_char (scan_code);
  395. }
  396. ret_chr = kbd_input;
  397. kbd_input = -1;
  398. return ret_chr;
  399. }
  400. /******************************************************************************/
  401. static void kbd_conv_char (unsigned char scan_code)
  402. {
  403. if (scan_code == 0xe0)
  404. {
  405. kbd_flags |= EXT;
  406. return;
  407. }
  408. /* if high bit of scan_code, set break flag */
  409. if (scan_code & 0x80)
  410. kbd_flags |= BRK;
  411. else
  412. kbd_flags &= ~BRK;
  413. if ((scan_code == 0xe1) || (kbd_flags & E1))
  414. {
  415. if (scan_code == 0xe1)
  416. {
  417. kbd_flags ^= BRK; /* reset the break flag */
  418. kbd_flags ^= E1; /* bitwise EXOR with E1 flag */
  419. }
  420. return;
  421. }
  422. scan_code &= 0x7f;
  423. if (kbd_flags & EXT)
  424. {
  425. int i;
  426. kbd_flags ^= EXT;
  427. for (i=0; ext_key_map[i]; i++)
  428. {
  429. if (ext_key_map[i] == scan_code)
  430. {
  431. scan_code = 0x80 + i;
  432. break;
  433. }
  434. }
  435. /* not found ? */
  436. if (!ext_key_map[i])
  437. return;
  438. }
  439. switch (kbd_fct_map [scan_code])
  440. {
  441. case AS: kbd_normal (scan_code);
  442. break;
  443. case SH: kbd_shift (scan_code);
  444. break;
  445. case CN: kbd_ctrl (scan_code);
  446. break;
  447. case NM: kbd_num (scan_code);
  448. break;
  449. case CP: kbd_caps (scan_code);
  450. break;
  451. case ST: kbd_scroll (scan_code);
  452. break;
  453. case AK: kbd_alt (scan_code);
  454. break;
  455. }
  456. return;
  457. }
  458. /******************************************************************************/
  459. static void kbd_normal (unsigned char scan_code)
  460. {
  461. unsigned char chr;
  462. if ((kbd_flags & BRK) == NORMAL)
  463. {
  464. chr = kbd_key_map [kbd_mapping][kbd_state][scan_code];
  465. if ((chr == 0xff) || (chr == 0x00))
  466. {
  467. return;
  468. }
  469. /* if caps lock convert upper to lower */
  470. if (((kbd_flags & CAPS) == CAPS) && (chr >= 'a' && chr <= 'z'))
  471. {
  472. chr -= 'a' - 'A';
  473. }
  474. kbd_input = chr;
  475. }
  476. }
  477. /******************************************************************************/
  478. static void kbd_shift (unsigned char scan_code)
  479. {
  480. if ((kbd_flags & BRK) == BRK)
  481. {
  482. kbd_state = AS;
  483. kbd_flags &= (~SHIFT);
  484. }
  485. else
  486. {
  487. kbd_state = SH;
  488. kbd_flags |= SHIFT;
  489. }
  490. }
  491. /******************************************************************************/
  492. static void kbd_ctrl (unsigned char scan_code)
  493. {
  494. if ((kbd_flags & BRK) == BRK)
  495. {
  496. kbd_state = AS;
  497. kbd_flags &= (~CTRL);
  498. }
  499. else
  500. {
  501. kbd_state = CN;
  502. kbd_flags |= CTRL;
  503. }
  504. }
  505. /******************************************************************************/
  506. static void kbd_caps (unsigned char scan_code)
  507. {
  508. if ((kbd_flags & BRK) == NORMAL)
  509. {
  510. kbd_flags ^= CAPS;
  511. kbd_led_set (); /* update keyboard LED */
  512. }
  513. }
  514. /******************************************************************************/
  515. static void kbd_num (unsigned char scan_code)
  516. {
  517. if ((kbd_flags & BRK) == NORMAL)
  518. {
  519. kbd_flags ^= NUM;
  520. kbd_state = (kbd_flags & NUM) ? AS : NM;
  521. kbd_led_set (); /* update keyboard LED */
  522. }
  523. }
  524. /******************************************************************************/
  525. static void kbd_scroll (unsigned char scan_code)
  526. {
  527. if ((kbd_flags & BRK) == NORMAL)
  528. {
  529. kbd_flags ^= STP;
  530. kbd_led_set (); /* update keyboard LED */
  531. if (kbd_flags & STP)
  532. kbd_input = 0x13;
  533. else
  534. kbd_input = 0x11;
  535. }
  536. }
  537. /******************************************************************************/
  538. static void kbd_alt (unsigned char scan_code)
  539. {
  540. if ((kbd_flags & BRK) == BRK)
  541. {
  542. kbd_state = AS;
  543. kbd_flags &= (~ALT);
  544. }
  545. else
  546. {
  547. kbd_state = AK;
  548. kbd_flags &= ALT;
  549. }
  550. }
  551. /******************************************************************************/
  552. static void kbd_led_set (void)
  553. {
  554. kbd_input_empty();
  555. out8 (I8042_DATA_REG, 0xed); /* SET LED command */
  556. kbd_input_empty();
  557. out8 (I8042_DATA_REG, (kbd_flags & 0x7)); /* LED bits only */
  558. }
  559. /******************************************************************************/
  560. static int kbd_input_empty (void)
  561. {
  562. int kbdTimeout = KBD_TIMEOUT;
  563. /* wait for input buf empty */
  564. while ((in8 (I8042_STATUS_REG) & 0x02) && kbdTimeout--)
  565. udelay(1000);
  566. return kbdTimeout;
  567. }
  568. /******************************************************************************/
  569. static int kbd_reset (void)
  570. {
  571. if (kbd_input_empty() == 0)
  572. return -1;
  573. out8 (I8042_DATA_REG, 0xff);
  574. udelay(250000);
  575. if (kbd_input_empty() == 0)
  576. return -1;
  577. #ifdef CONFIG_USE_CPCIDVI
  578. out8 (I8042_COMMAND_REG, 0x60);
  579. #else
  580. out8 (I8042_DATA_REG, 0x60);
  581. #endif
  582. if (kbd_input_empty() == 0)
  583. return -1;
  584. out8 (I8042_DATA_REG, 0x45);
  585. if (kbd_input_empty() == 0)
  586. return -1;
  587. out8 (I8042_COMMAND_REG, 0xae);
  588. if (kbd_input_empty() == 0)
  589. return -1;
  590. return 0;
  591. }
  592. #endif /* CONFIG_I8042_KBD */