kbd.c 8.0 KB

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