kbd.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. #include <linux/kd.h>
  2. //#include <linux/kbd_ll.h>
  3. #include <linux/kbd_kern.h>
  4. /*
  5. * Translation of escaped scancodes to keycodes.
  6. * This is now user-settable.
  7. * The keycodes 1-88,96-111,119 are fairly standard, and
  8. * should probably not be changed - changing might confuse X.
  9. * X also interprets scancode 0x5d (KEY_Begin).
  10. *
  11. * For 1-88 keycode equals scancode.
  12. */
  13. #define E0_KPENTER 96
  14. #define E0_RCTRL 97
  15. #define E0_KPSLASH 98
  16. #define E0_PRSCR 99
  17. #define E0_RALT 100
  18. #define E0_BREAK 101 /* (control-pause) */
  19. #define E0_HOME 102
  20. #define E0_UP 103
  21. #define E0_PGUP 104
  22. #define E0_LEFT 105
  23. #define E0_RIGHT 106
  24. #define E0_END 107
  25. #define E0_DOWN 108
  26. #define E0_PGDN 109
  27. #define E0_INS 110
  28. #define E0_DEL 111
  29. /* for USB 106 keyboard */
  30. #define E0_YEN 124
  31. #define E0_BACKSLASH 89
  32. #define E1_PAUSE 119
  33. /*
  34. * The keycodes below are randomly located in 89-95,112-118,120-127.
  35. * They could be thrown away (and all occurrences below replaced by 0),
  36. * but that would force many users to use the `setkeycodes' utility, where
  37. * they needed not before. It does not matter that there are duplicates, as
  38. * long as no duplication occurs for any single keyboard.
  39. */
  40. #define SC_LIM 89
  41. #define FOCUS_PF1 85 /* actual code! */
  42. #define FOCUS_PF2 89
  43. #define FOCUS_PF3 90
  44. #define FOCUS_PF4 91
  45. #define FOCUS_PF5 92
  46. #define FOCUS_PF6 93
  47. #define FOCUS_PF7 94
  48. #define FOCUS_PF8 95
  49. #define FOCUS_PF9 120
  50. #define FOCUS_PF10 121
  51. #define FOCUS_PF11 122
  52. #define FOCUS_PF12 123
  53. #define JAP_86 124
  54. /* tfj@olivia.ping.dk:
  55. * The four keys are located over the numeric keypad, and are
  56. * labelled A1-A4. It's an rc930 keyboard, from
  57. * Regnecentralen/RC International, Now ICL.
  58. * Scancodes: 59, 5a, 5b, 5c.
  59. */
  60. #define RGN1 124
  61. #define RGN2 125
  62. #define RGN3 126
  63. #define RGN4 127
  64. static unsigned char high_keys[128 - SC_LIM] = {
  65. RGN1, RGN2, RGN3, RGN4, 0, 0, 0, /* 0x59-0x5f */
  66. 0, 0, 0, 0, 0, 0, 0, 0, /* 0x60-0x67 */
  67. 0, 0, 0, 0, 0, FOCUS_PF11, 0, FOCUS_PF12, /* 0x68-0x6f */
  68. 0, 0, 0, FOCUS_PF2, FOCUS_PF9, 0, 0, FOCUS_PF3, /* 0x70-0x77 */
  69. FOCUS_PF4, FOCUS_PF5, FOCUS_PF6, FOCUS_PF7, /* 0x78-0x7b */
  70. FOCUS_PF8, JAP_86, FOCUS_PF10, 0 /* 0x7c-0x7f */
  71. };
  72. /* BTC */
  73. #define E0_MACRO 112
  74. /* LK450 */
  75. #define E0_F13 113
  76. #define E0_F14 114
  77. #define E0_HELP 115
  78. #define E0_DO 116
  79. #define E0_F17 117
  80. #define E0_KPMINPLUS 118
  81. /*
  82. * My OmniKey generates e0 4c for the "OMNI" key and the
  83. * right alt key does nada. [kkoller@nyx10.cs.du.edu]
  84. */
  85. #define E0_OK 124
  86. /*
  87. * New microsoft keyboard is rumoured to have
  88. * e0 5b (left window button), e0 5c (right window button),
  89. * e0 5d (menu button). [or: LBANNER, RBANNER, RMENU]
  90. * [or: Windows_L, Windows_R, TaskMan]
  91. */
  92. #define E0_MSLW 125
  93. #define E0_MSRW 126
  94. #define E0_MSTM 127
  95. static unsigned char e0_keys[128] = {
  96. 0, 0, 0, 0, 0, 0, 0, 0, /* 0x00-0x07 */
  97. 0, 0, 0, 0, 0, 0, 0, 0, /* 0x08-0x0f */
  98. 0, 0, 0, 0, 0, 0, 0, 0, /* 0x10-0x17 */
  99. 0, 0, 0, 0, E0_KPENTER, E0_RCTRL, 0, 0, /* 0x18-0x1f */
  100. 0, 0, 0, 0, 0, 0, 0, 0, /* 0x20-0x27 */
  101. 0, 0, 0, 0, 0, 0, 0, 0, /* 0x28-0x2f */
  102. 0, 0, 0, 0, 0, E0_KPSLASH, 0, E0_PRSCR, /* 0x30-0x37 */
  103. E0_RALT, 0, 0, 0, 0, E0_F13, E0_F14, E0_HELP, /* 0x38-0x3f */
  104. E0_DO, E0_F17, 0, 0, 0, 0, E0_BREAK, E0_HOME, /* 0x40-0x47 */
  105. E0_UP, E0_PGUP, 0, E0_LEFT, E0_OK, E0_RIGHT, E0_KPMINPLUS, E0_END, /* 0x48-0x4f */
  106. E0_DOWN, E0_PGDN, E0_INS, E0_DEL, 0, 0, 0, 0, /* 0x50-0x57 */
  107. 0, 0, 0, E0_MSLW, E0_MSRW, E0_MSTM, 0, 0, /* 0x58-0x5f */
  108. 0, 0, 0, 0, 0, 0, 0, 0, /* 0x60-0x67 */
  109. 0, 0, 0, 0, 0, 0, 0, E0_MACRO, /* 0x68-0x6f */
  110. //0, 0, 0, 0, 0, 0, 0, 0, /* 0x70-0x77 */
  111. 0, 0, 0, 0, 0, E0_BACKSLASH, 0, 0, /* 0x70-0x77 */
  112. 0, 0, 0, E0_YEN, 0, 0, 0, 0 /* 0x78-0x7f */
  113. };
  114. static int gen_setkeycode(unsigned int scancode, unsigned int keycode)
  115. {
  116. if (scancode < SC_LIM || scancode > 255 || keycode > 127)
  117. return -EINVAL;
  118. if (scancode < 128)
  119. high_keys[scancode - SC_LIM] = keycode;
  120. else
  121. e0_keys[scancode - 128] = keycode;
  122. return 0;
  123. }
  124. static int gen_getkeycode(unsigned int scancode)
  125. {
  126. return
  127. (scancode < SC_LIM || scancode > 255) ? -EINVAL :
  128. (scancode <
  129. 128) ? high_keys[scancode - SC_LIM] : e0_keys[scancode - 128];
  130. }
  131. static int
  132. gen_translate(unsigned char scancode, unsigned char *keycode, char raw_mode)
  133. {
  134. static int prev_scancode;
  135. /* special prefix scancodes.. */
  136. if (scancode == 0xe0 || scancode == 0xe1) {
  137. prev_scancode = scancode;
  138. return 0;
  139. }
  140. /* 0xFF is sent by a few keyboards, ignore it. 0x00 is error */
  141. if (scancode == 0x00 || scancode == 0xff) {
  142. prev_scancode = 0;
  143. return 0;
  144. }
  145. scancode &= 0x7f;
  146. if (prev_scancode) {
  147. /*
  148. * usually it will be 0xe0, but a Pause key generates
  149. * e1 1d 45 e1 9d c5 when pressed, and nothing when released
  150. */
  151. if (prev_scancode != 0xe0) {
  152. if (prev_scancode == 0xe1 && scancode == 0x1d) {
  153. prev_scancode = 0x100;
  154. return 0;
  155. }
  156. else if (prev_scancode == 0x100
  157. && scancode == 0x45) {
  158. *keycode = E1_PAUSE;
  159. prev_scancode = 0;
  160. } else {
  161. #ifdef KBD_REPORT_UNKN
  162. if (!raw_mode)
  163. printk(KERN_INFO
  164. "keyboard: unknown e1 escape sequence\n");
  165. #endif
  166. prev_scancode = 0;
  167. return 0;
  168. }
  169. } else {
  170. prev_scancode = 0;
  171. /*
  172. * The keyboard maintains its own internal caps lock and
  173. * num lock statuses. In caps lock mode E0 AA precedes make
  174. * code and E0 2A follows break code. In num lock mode,
  175. * E0 2A precedes make code and E0 AA follows break code.
  176. * We do our own book-keeping, so we will just ignore these.
  177. */
  178. /*
  179. * For my keyboard there is no caps lock mode, but there are
  180. * both Shift-L and Shift-R modes. The former mode generates
  181. * E0 2A / E0 AA pairs, the latter E0 B6 / E0 36 pairs.
  182. * So, we should also ignore the latter. - aeb@cwi.nl
  183. */
  184. if (scancode == 0x2a || scancode == 0x36)
  185. return 0;
  186. if (e0_keys[scancode])
  187. *keycode = e0_keys[scancode];
  188. else {
  189. #ifdef KBD_REPORT_UNKN
  190. if (!raw_mode)
  191. printk(KERN_INFO
  192. "keyboard: unknown scancode e0 %02x\n",
  193. scancode);
  194. #endif
  195. return 0;
  196. }
  197. }
  198. } else if (scancode >= SC_LIM) {
  199. /* This happens with the FOCUS 9000 keyboard
  200. Its keys PF1..PF12 are reported to generate
  201. 55 73 77 78 79 7a 7b 7c 74 7e 6d 6f
  202. Moreover, unless repeated, they do not generate
  203. key-down events, so we have to zero up_flag below */
  204. /* Also, Japanese 86/106 keyboards are reported to
  205. generate 0x73 and 0x7d for \ - and \ | respectively. */
  206. /* Also, some Brazilian keyboard is reported to produce
  207. 0x73 and 0x7e for \ ? and KP-dot, respectively. */
  208. *keycode = high_keys[scancode - SC_LIM];
  209. if (!*keycode) {
  210. if (!raw_mode) {
  211. #ifdef KBD_REPORT_UNKN
  212. printk(KERN_INFO
  213. "keyboard: unrecognized scancode (%02x)"
  214. " - ignored\n", scancode);
  215. #endif
  216. }
  217. return 0;
  218. }
  219. } else
  220. *keycode = scancode;
  221. return 1;
  222. }
  223. static char gen_unexpected_up(unsigned char keycode)
  224. {
  225. /* unexpected, but this can happen: maybe this was a key release for a
  226. FOCUS 9000 PF key; if we want to see it, we have to clear up_flag */
  227. if (keycode >= SC_LIM || keycode == 85)
  228. return 0;
  229. else
  230. return 0200;
  231. }
  232. /*
  233. * These are the default mappings
  234. */
  235. int (*k_setkeycode)(unsigned int, unsigned int) = gen_setkeycode;
  236. int (*k_getkeycode)(unsigned int) = gen_getkeycode;
  237. int (*k_translate)(unsigned char, unsigned char *, char) = gen_translate;
  238. char (*k_unexpected_up)(unsigned char) = gen_unexpected_up;
  239. void (*k_leds)(unsigned char);
  240. /* Simple translation table for the SysRq keys */
  241. #ifdef CONFIG_MAGIC_SYSRQ
  242. static unsigned char gen_sysrq_xlate[128] =
  243. "\000\0331234567890-=\177\t" /* 0x00 - 0x0f */
  244. "qwertyuiop[]\r\000as" /* 0x10 - 0x1f */
  245. "dfghjkl;'`\000\\zxcv" /* 0x20 - 0x2f */
  246. "bnm,./\000*\000 \000\201\202\203\204\205" /* 0x30 - 0x3f */
  247. "\206\207\210\211\212\000\000789-456+1" /* 0x40 - 0x4f */
  248. "230\177\000\000\213\214\000\000\000\000\000\000\000\000\000\000" /* 0x50 - 0x5f */
  249. "\r\000/"; /* 0x60 - 0x6f */
  250. unsigned char *k_sysrq_xlate = gen_sysrq_xlate;
  251. int k_sysrq_key = 0x54;
  252. #endif