keyboard.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254
  1. /*
  2. * linux/drivers/char/keyboard.c
  3. *
  4. * Written for linux by Johan Myreen as a translation from
  5. * the assembly version by Linus (with diacriticals added)
  6. *
  7. * Some additional features added by Christoph Niemann (ChN), March 1993
  8. *
  9. * Loadable keymaps by Risto Kankkunen, May 1993
  10. *
  11. * Diacriticals redone & other small changes, aeb@cwi.nl, June 1993
  12. * Added decr/incr_console, dynamic keymaps, Unicode support,
  13. * dynamic function/string keys, led setting, Sept 1994
  14. * `Sticky' modifier keys, 951006.
  15. *
  16. * 11-11-96: SAK should now work in the raw mode (Martin Mares)
  17. *
  18. * Modified to provide 'generic' keyboard support by Hamish Macdonald
  19. * Merge with the m68k keyboard driver and split-off of the PC low-level
  20. * parts by Geert Uytterhoeven, May 1997
  21. *
  22. * 27-05-97: Added support for the Magic SysRq Key (Martin Mares)
  23. * 30-07-98: Dead keys redone, aeb@cwi.nl.
  24. * 21-08-02: Converted to input API, major cleanup. (Vojtech Pavlik)
  25. */
  26. #include <linux/config.h>
  27. #include <linux/module.h>
  28. #include <linux/sched.h>
  29. #include <linux/tty.h>
  30. #include <linux/tty_flip.h>
  31. #include <linux/mm.h>
  32. #include <linux/string.h>
  33. #include <linux/init.h>
  34. #include <linux/slab.h>
  35. #include <linux/kbd_kern.h>
  36. #include <linux/kbd_diacr.h>
  37. #include <linux/vt_kern.h>
  38. #include <linux/sysrq.h>
  39. #include <linux/input.h>
  40. static void kbd_disconnect(struct input_handle *handle);
  41. extern void ctrl_alt_del(void);
  42. /*
  43. * Exported functions/variables
  44. */
  45. #define KBD_DEFMODE ((1 << VC_REPEAT) | (1 << VC_META))
  46. /*
  47. * Some laptops take the 789uiojklm,. keys as number pad when NumLock is on.
  48. * This seems a good reason to start with NumLock off. On HIL keyboards
  49. * of PARISC machines however there is no NumLock key and everyone expects the keypad
  50. * to be used for numbers.
  51. */
  52. #if defined(CONFIG_PARISC) && (defined(CONFIG_KEYBOARD_HIL) || defined(CONFIG_KEYBOARD_HIL_OLD))
  53. #define KBD_DEFLEDS (1 << VC_NUMLOCK)
  54. #else
  55. #define KBD_DEFLEDS 0
  56. #endif
  57. #define KBD_DEFLOCK 0
  58. void compute_shiftstate(void);
  59. /*
  60. * Handler Tables.
  61. */
  62. #define K_HANDLERS\
  63. k_self, k_fn, k_spec, k_pad,\
  64. k_dead, k_cons, k_cur, k_shift,\
  65. k_meta, k_ascii, k_lock, k_lowercase,\
  66. k_slock, k_dead2, k_ignore, k_ignore
  67. typedef void (k_handler_fn)(struct vc_data *vc, unsigned char value,
  68. char up_flag, struct pt_regs *regs);
  69. static k_handler_fn K_HANDLERS;
  70. static k_handler_fn *k_handler[16] = { K_HANDLERS };
  71. #define FN_HANDLERS\
  72. fn_null, fn_enter, fn_show_ptregs, fn_show_mem,\
  73. fn_show_state, fn_send_intr, fn_lastcons, fn_caps_toggle,\
  74. fn_num, fn_hold, fn_scroll_forw, fn_scroll_back,\
  75. fn_boot_it, fn_caps_on, fn_compose, fn_SAK,\
  76. fn_dec_console, fn_inc_console, fn_spawn_con, fn_bare_num
  77. typedef void (fn_handler_fn)(struct vc_data *vc, struct pt_regs *regs);
  78. static fn_handler_fn FN_HANDLERS;
  79. static fn_handler_fn *fn_handler[] = { FN_HANDLERS };
  80. /*
  81. * Variables exported for vt_ioctl.c
  82. */
  83. /* maximum values each key_handler can handle */
  84. const int max_vals[] = {
  85. 255, ARRAY_SIZE(func_table) - 1, ARRAY_SIZE(fn_handler) - 1, NR_PAD - 1,
  86. NR_DEAD - 1, 255, 3, NR_SHIFT - 1, 255, NR_ASCII - 1, NR_LOCK - 1,
  87. 255, NR_LOCK - 1, 255
  88. };
  89. const int NR_TYPES = ARRAY_SIZE(max_vals);
  90. struct kbd_struct kbd_table[MAX_NR_CONSOLES];
  91. static struct kbd_struct *kbd = kbd_table;
  92. static struct kbd_struct kbd0;
  93. int spawnpid, spawnsig;
  94. /*
  95. * Variables exported for vt.c
  96. */
  97. int shift_state = 0;
  98. /*
  99. * Internal Data.
  100. */
  101. static struct input_handler kbd_handler;
  102. static unsigned long key_down[NBITS(KEY_MAX)]; /* keyboard key bitmap */
  103. static unsigned char shift_down[NR_SHIFT]; /* shift state counters.. */
  104. static int dead_key_next;
  105. static int npadch = -1; /* -1 or number assembled on pad */
  106. static unsigned char diacr;
  107. static char rep; /* flag telling character repeat */
  108. static unsigned char ledstate = 0xff; /* undefined */
  109. static unsigned char ledioctl;
  110. static struct ledptr {
  111. unsigned int *addr;
  112. unsigned int mask;
  113. unsigned char valid:1;
  114. } ledptrs[3];
  115. /* Simple translation table for the SysRq keys */
  116. #ifdef CONFIG_MAGIC_SYSRQ
  117. unsigned char kbd_sysrq_xlate[KEY_MAX + 1] =
  118. "\000\0331234567890-=\177\t" /* 0x00 - 0x0f */
  119. "qwertyuiop[]\r\000as" /* 0x10 - 0x1f */
  120. "dfghjkl;'`\000\\zxcv" /* 0x20 - 0x2f */
  121. "bnm,./\000*\000 \000\201\202\203\204\205" /* 0x30 - 0x3f */
  122. "\206\207\210\211\212\000\000789-456+1" /* 0x40 - 0x4f */
  123. "230\177\000\000\213\214\000\000\000\000\000\000\000\000\000\000" /* 0x50 - 0x5f */
  124. "\r\000/"; /* 0x60 - 0x6f */
  125. static int sysrq_down;
  126. #endif
  127. static int sysrq_alt;
  128. /*
  129. * Translation of scancodes to keycodes. We set them on only the first attached
  130. * keyboard - for per-keyboard setting, /dev/input/event is more useful.
  131. */
  132. int getkeycode(unsigned int scancode)
  133. {
  134. struct list_head * node;
  135. struct input_dev *dev = NULL;
  136. list_for_each(node,&kbd_handler.h_list) {
  137. struct input_handle * handle = to_handle_h(node);
  138. if (handle->dev->keycodesize) {
  139. dev = handle->dev;
  140. break;
  141. }
  142. }
  143. if (!dev)
  144. return -ENODEV;
  145. if (scancode >= dev->keycodemax)
  146. return -EINVAL;
  147. return INPUT_KEYCODE(dev, scancode);
  148. }
  149. int setkeycode(unsigned int scancode, unsigned int keycode)
  150. {
  151. struct list_head * node;
  152. struct input_dev *dev = NULL;
  153. unsigned int i, oldkey;
  154. list_for_each(node,&kbd_handler.h_list) {
  155. struct input_handle *handle = to_handle_h(node);
  156. if (handle->dev->keycodesize) {
  157. dev = handle->dev;
  158. break;
  159. }
  160. }
  161. if (!dev)
  162. return -ENODEV;
  163. if (scancode >= dev->keycodemax)
  164. return -EINVAL;
  165. if (keycode < 0 || keycode > KEY_MAX)
  166. return -EINVAL;
  167. if (keycode >> (dev->keycodesize * 8))
  168. return -EINVAL;
  169. oldkey = SET_INPUT_KEYCODE(dev, scancode, keycode);
  170. clear_bit(oldkey, dev->keybit);
  171. set_bit(keycode, dev->keybit);
  172. for (i = 0; i < dev->keycodemax; i++)
  173. if (INPUT_KEYCODE(dev,i) == oldkey)
  174. set_bit(oldkey, dev->keybit);
  175. return 0;
  176. }
  177. /*
  178. * Making beeps and bells.
  179. */
  180. static void kd_nosound(unsigned long ignored)
  181. {
  182. struct list_head * node;
  183. list_for_each(node,&kbd_handler.h_list) {
  184. struct input_handle *handle = to_handle_h(node);
  185. if (test_bit(EV_SND, handle->dev->evbit)) {
  186. if (test_bit(SND_TONE, handle->dev->sndbit))
  187. input_event(handle->dev, EV_SND, SND_TONE, 0);
  188. if (test_bit(SND_BELL, handle->dev->sndbit))
  189. input_event(handle->dev, EV_SND, SND_BELL, 0);
  190. }
  191. }
  192. }
  193. static DEFINE_TIMER(kd_mksound_timer, kd_nosound, 0, 0);
  194. void kd_mksound(unsigned int hz, unsigned int ticks)
  195. {
  196. struct list_head * node;
  197. del_timer(&kd_mksound_timer);
  198. if (hz) {
  199. list_for_each_prev(node,&kbd_handler.h_list) {
  200. struct input_handle *handle = to_handle_h(node);
  201. if (test_bit(EV_SND, handle->dev->evbit)) {
  202. if (test_bit(SND_TONE, handle->dev->sndbit)) {
  203. input_event(handle->dev, EV_SND, SND_TONE, hz);
  204. break;
  205. }
  206. if (test_bit(SND_BELL, handle->dev->sndbit)) {
  207. input_event(handle->dev, EV_SND, SND_BELL, 1);
  208. break;
  209. }
  210. }
  211. }
  212. if (ticks)
  213. mod_timer(&kd_mksound_timer, jiffies + ticks);
  214. } else
  215. kd_nosound(0);
  216. }
  217. /*
  218. * Setting the keyboard rate.
  219. */
  220. int kbd_rate(struct kbd_repeat *rep)
  221. {
  222. struct list_head *node;
  223. unsigned int d = 0;
  224. unsigned int p = 0;
  225. list_for_each(node,&kbd_handler.h_list) {
  226. struct input_handle *handle = to_handle_h(node);
  227. struct input_dev *dev = handle->dev;
  228. if (test_bit(EV_REP, dev->evbit)) {
  229. if (rep->delay > 0)
  230. input_event(dev, EV_REP, REP_DELAY, rep->delay);
  231. if (rep->period > 0)
  232. input_event(dev, EV_REP, REP_PERIOD, rep->period);
  233. d = dev->rep[REP_DELAY];
  234. p = dev->rep[REP_PERIOD];
  235. }
  236. }
  237. rep->delay = d;
  238. rep->period = p;
  239. return 0;
  240. }
  241. /*
  242. * Helper Functions.
  243. */
  244. static void put_queue(struct vc_data *vc, int ch)
  245. {
  246. struct tty_struct *tty = vc->vc_tty;
  247. if (tty) {
  248. tty_insert_flip_char(tty, ch, 0);
  249. con_schedule_flip(tty);
  250. }
  251. }
  252. static void puts_queue(struct vc_data *vc, char *cp)
  253. {
  254. struct tty_struct *tty = vc->vc_tty;
  255. if (!tty)
  256. return;
  257. while (*cp) {
  258. tty_insert_flip_char(tty, *cp, 0);
  259. cp++;
  260. }
  261. con_schedule_flip(tty);
  262. }
  263. static void applkey(struct vc_data *vc, int key, char mode)
  264. {
  265. static char buf[] = { 0x1b, 'O', 0x00, 0x00 };
  266. buf[1] = (mode ? 'O' : '[');
  267. buf[2] = key;
  268. puts_queue(vc, buf);
  269. }
  270. /*
  271. * Many other routines do put_queue, but I think either
  272. * they produce ASCII, or they produce some user-assigned
  273. * string, and in both cases we might assume that it is
  274. * in utf-8 already. UTF-8 is defined for words of up to 31 bits,
  275. * but we need only 16 bits here
  276. */
  277. static void to_utf8(struct vc_data *vc, ushort c)
  278. {
  279. if (c < 0x80)
  280. /* 0******* */
  281. put_queue(vc, c);
  282. else if (c < 0x800) {
  283. /* 110***** 10****** */
  284. put_queue(vc, 0xc0 | (c >> 6));
  285. put_queue(vc, 0x80 | (c & 0x3f));
  286. } else {
  287. /* 1110**** 10****** 10****** */
  288. put_queue(vc, 0xe0 | (c >> 12));
  289. put_queue(vc, 0x80 | ((c >> 6) & 0x3f));
  290. put_queue(vc, 0x80 | (c & 0x3f));
  291. }
  292. }
  293. /*
  294. * Called after returning from RAW mode or when changing consoles - recompute
  295. * shift_down[] and shift_state from key_down[] maybe called when keymap is
  296. * undefined, so that shiftkey release is seen
  297. */
  298. void compute_shiftstate(void)
  299. {
  300. unsigned int i, j, k, sym, val;
  301. shift_state = 0;
  302. memset(shift_down, 0, sizeof(shift_down));
  303. for (i = 0; i < ARRAY_SIZE(key_down); i++) {
  304. if (!key_down[i])
  305. continue;
  306. k = i * BITS_PER_LONG;
  307. for (j = 0; j < BITS_PER_LONG; j++, k++) {
  308. if (!test_bit(k, key_down))
  309. continue;
  310. sym = U(key_maps[0][k]);
  311. if (KTYP(sym) != KT_SHIFT && KTYP(sym) != KT_SLOCK)
  312. continue;
  313. val = KVAL(sym);
  314. if (val == KVAL(K_CAPSSHIFT))
  315. val = KVAL(K_SHIFT);
  316. shift_down[val]++;
  317. shift_state |= (1 << val);
  318. }
  319. }
  320. }
  321. /*
  322. * We have a combining character DIACR here, followed by the character CH.
  323. * If the combination occurs in the table, return the corresponding value.
  324. * Otherwise, if CH is a space or equals DIACR, return DIACR.
  325. * Otherwise, conclude that DIACR was not combining after all,
  326. * queue it and return CH.
  327. */
  328. static unsigned char handle_diacr(struct vc_data *vc, unsigned char ch)
  329. {
  330. int d = diacr;
  331. unsigned int i;
  332. diacr = 0;
  333. for (i = 0; i < accent_table_size; i++) {
  334. if (accent_table[i].diacr == d && accent_table[i].base == ch)
  335. return accent_table[i].result;
  336. }
  337. if (ch == ' ' || ch == d)
  338. return d;
  339. put_queue(vc, d);
  340. return ch;
  341. }
  342. /*
  343. * Special function handlers
  344. */
  345. static void fn_enter(struct vc_data *vc, struct pt_regs *regs)
  346. {
  347. if (diacr) {
  348. put_queue(vc, diacr);
  349. diacr = 0;
  350. }
  351. put_queue(vc, 13);
  352. if (vc_kbd_mode(kbd, VC_CRLF))
  353. put_queue(vc, 10);
  354. }
  355. static void fn_caps_toggle(struct vc_data *vc, struct pt_regs *regs)
  356. {
  357. if (rep)
  358. return;
  359. chg_vc_kbd_led(kbd, VC_CAPSLOCK);
  360. }
  361. static void fn_caps_on(struct vc_data *vc, struct pt_regs *regs)
  362. {
  363. if (rep)
  364. return;
  365. set_vc_kbd_led(kbd, VC_CAPSLOCK);
  366. }
  367. static void fn_show_ptregs(struct vc_data *vc, struct pt_regs *regs)
  368. {
  369. if (regs)
  370. show_regs(regs);
  371. }
  372. static void fn_hold(struct vc_data *vc, struct pt_regs *regs)
  373. {
  374. struct tty_struct *tty = vc->vc_tty;
  375. if (rep || !tty)
  376. return;
  377. /*
  378. * Note: SCROLLOCK will be set (cleared) by stop_tty (start_tty);
  379. * these routines are also activated by ^S/^Q.
  380. * (And SCROLLOCK can also be set by the ioctl KDSKBLED.)
  381. */
  382. if (tty->stopped)
  383. start_tty(tty);
  384. else
  385. stop_tty(tty);
  386. }
  387. static void fn_num(struct vc_data *vc, struct pt_regs *regs)
  388. {
  389. if (vc_kbd_mode(kbd,VC_APPLIC))
  390. applkey(vc, 'P', 1);
  391. else
  392. fn_bare_num(vc, regs);
  393. }
  394. /*
  395. * Bind this to Shift-NumLock if you work in application keypad mode
  396. * but want to be able to change the NumLock flag.
  397. * Bind this to NumLock if you prefer that the NumLock key always
  398. * changes the NumLock flag.
  399. */
  400. static void fn_bare_num(struct vc_data *vc, struct pt_regs *regs)
  401. {
  402. if (!rep)
  403. chg_vc_kbd_led(kbd, VC_NUMLOCK);
  404. }
  405. static void fn_lastcons(struct vc_data *vc, struct pt_regs *regs)
  406. {
  407. /* switch to the last used console, ChN */
  408. set_console(last_console);
  409. }
  410. static void fn_dec_console(struct vc_data *vc, struct pt_regs *regs)
  411. {
  412. int i, cur = fg_console;
  413. /* Currently switching? Queue this next switch relative to that. */
  414. if (want_console != -1)
  415. cur = want_console;
  416. for (i = cur-1; i != cur; i--) {
  417. if (i == -1)
  418. i = MAX_NR_CONSOLES-1;
  419. if (vc_cons_allocated(i))
  420. break;
  421. }
  422. set_console(i);
  423. }
  424. static void fn_inc_console(struct vc_data *vc, struct pt_regs *regs)
  425. {
  426. int i, cur = fg_console;
  427. /* Currently switching? Queue this next switch relative to that. */
  428. if (want_console != -1)
  429. cur = want_console;
  430. for (i = cur+1; i != cur; i++) {
  431. if (i == MAX_NR_CONSOLES)
  432. i = 0;
  433. if (vc_cons_allocated(i))
  434. break;
  435. }
  436. set_console(i);
  437. }
  438. static void fn_send_intr(struct vc_data *vc, struct pt_regs *regs)
  439. {
  440. struct tty_struct *tty = vc->vc_tty;
  441. if (!tty)
  442. return;
  443. tty_insert_flip_char(tty, 0, TTY_BREAK);
  444. con_schedule_flip(tty);
  445. }
  446. static void fn_scroll_forw(struct vc_data *vc, struct pt_regs *regs)
  447. {
  448. scrollfront(vc, 0);
  449. }
  450. static void fn_scroll_back(struct vc_data *vc, struct pt_regs *regs)
  451. {
  452. scrollback(vc, 0);
  453. }
  454. static void fn_show_mem(struct vc_data *vc, struct pt_regs *regs)
  455. {
  456. show_mem();
  457. }
  458. static void fn_show_state(struct vc_data *vc, struct pt_regs *regs)
  459. {
  460. show_state();
  461. }
  462. static void fn_boot_it(struct vc_data *vc, struct pt_regs *regs)
  463. {
  464. ctrl_alt_del();
  465. }
  466. static void fn_compose(struct vc_data *vc, struct pt_regs *regs)
  467. {
  468. dead_key_next = 1;
  469. }
  470. static void fn_spawn_con(struct vc_data *vc, struct pt_regs *regs)
  471. {
  472. if (spawnpid)
  473. if(kill_proc(spawnpid, spawnsig, 1))
  474. spawnpid = 0;
  475. }
  476. static void fn_SAK(struct vc_data *vc, struct pt_regs *regs)
  477. {
  478. struct tty_struct *tty = vc->vc_tty;
  479. /*
  480. * SAK should also work in all raw modes and reset
  481. * them properly.
  482. */
  483. if (tty)
  484. do_SAK(tty);
  485. reset_vc(vc);
  486. }
  487. static void fn_null(struct vc_data *vc, struct pt_regs *regs)
  488. {
  489. compute_shiftstate();
  490. }
  491. /*
  492. * Special key handlers
  493. */
  494. static void k_ignore(struct vc_data *vc, unsigned char value, char up_flag, struct pt_regs *regs)
  495. {
  496. }
  497. static void k_spec(struct vc_data *vc, unsigned char value, char up_flag, struct pt_regs *regs)
  498. {
  499. if (up_flag)
  500. return;
  501. if (value >= ARRAY_SIZE(fn_handler))
  502. return;
  503. if ((kbd->kbdmode == VC_RAW ||
  504. kbd->kbdmode == VC_MEDIUMRAW) &&
  505. value != KVAL(K_SAK))
  506. return; /* SAK is allowed even in raw mode */
  507. fn_handler[value](vc, regs);
  508. }
  509. static void k_lowercase(struct vc_data *vc, unsigned char value, char up_flag, struct pt_regs *regs)
  510. {
  511. printk(KERN_ERR "keyboard.c: k_lowercase was called - impossible\n");
  512. }
  513. static void k_self(struct vc_data *vc, unsigned char value, char up_flag, struct pt_regs *regs)
  514. {
  515. if (up_flag)
  516. return; /* no action, if this is a key release */
  517. if (diacr)
  518. value = handle_diacr(vc, value);
  519. if (dead_key_next) {
  520. dead_key_next = 0;
  521. diacr = value;
  522. return;
  523. }
  524. put_queue(vc, value);
  525. }
  526. /*
  527. * Handle dead key. Note that we now may have several
  528. * dead keys modifying the same character. Very useful
  529. * for Vietnamese.
  530. */
  531. static void k_dead2(struct vc_data *vc, unsigned char value, char up_flag, struct pt_regs *regs)
  532. {
  533. if (up_flag)
  534. return;
  535. diacr = (diacr ? handle_diacr(vc, value) : value);
  536. }
  537. /*
  538. * Obsolete - for backwards compatibility only
  539. */
  540. static void k_dead(struct vc_data *vc, unsigned char value, char up_flag, struct pt_regs *regs)
  541. {
  542. static unsigned char ret_diacr[NR_DEAD] = {'`', '\'', '^', '~', '"', ',' };
  543. value = ret_diacr[value];
  544. k_dead2(vc, value, up_flag, regs);
  545. }
  546. static void k_cons(struct vc_data *vc, unsigned char value, char up_flag, struct pt_regs *regs)
  547. {
  548. if (up_flag)
  549. return;
  550. set_console(value);
  551. }
  552. static void k_fn(struct vc_data *vc, unsigned char value, char up_flag, struct pt_regs *regs)
  553. {
  554. unsigned v;
  555. if (up_flag)
  556. return;
  557. v = value;
  558. if (v < ARRAY_SIZE(func_table)) {
  559. if (func_table[value])
  560. puts_queue(vc, func_table[value]);
  561. } else
  562. printk(KERN_ERR "k_fn called with value=%d\n", value);
  563. }
  564. static void k_cur(struct vc_data *vc, unsigned char value, char up_flag, struct pt_regs *regs)
  565. {
  566. static const char *cur_chars = "BDCA";
  567. if (up_flag)
  568. return;
  569. applkey(vc, cur_chars[value], vc_kbd_mode(kbd, VC_CKMODE));
  570. }
  571. static void k_pad(struct vc_data *vc, unsigned char value, char up_flag, struct pt_regs *regs)
  572. {
  573. static const char *pad_chars = "0123456789+-*/\015,.?()#";
  574. static const char *app_map = "pqrstuvwxylSRQMnnmPQS";
  575. if (up_flag)
  576. return; /* no action, if this is a key release */
  577. /* kludge... shift forces cursor/number keys */
  578. if (vc_kbd_mode(kbd, VC_APPLIC) && !shift_down[KG_SHIFT]) {
  579. applkey(vc, app_map[value], 1);
  580. return;
  581. }
  582. if (!vc_kbd_led(kbd, VC_NUMLOCK))
  583. switch (value) {
  584. case KVAL(K_PCOMMA):
  585. case KVAL(K_PDOT):
  586. k_fn(vc, KVAL(K_REMOVE), 0, regs);
  587. return;
  588. case KVAL(K_P0):
  589. k_fn(vc, KVAL(K_INSERT), 0, regs);
  590. return;
  591. case KVAL(K_P1):
  592. k_fn(vc, KVAL(K_SELECT), 0, regs);
  593. return;
  594. case KVAL(K_P2):
  595. k_cur(vc, KVAL(K_DOWN), 0, regs);
  596. return;
  597. case KVAL(K_P3):
  598. k_fn(vc, KVAL(K_PGDN), 0, regs);
  599. return;
  600. case KVAL(K_P4):
  601. k_cur(vc, KVAL(K_LEFT), 0, regs);
  602. return;
  603. case KVAL(K_P6):
  604. k_cur(vc, KVAL(K_RIGHT), 0, regs);
  605. return;
  606. case KVAL(K_P7):
  607. k_fn(vc, KVAL(K_FIND), 0, regs);
  608. return;
  609. case KVAL(K_P8):
  610. k_cur(vc, KVAL(K_UP), 0, regs);
  611. return;
  612. case KVAL(K_P9):
  613. k_fn(vc, KVAL(K_PGUP), 0, regs);
  614. return;
  615. case KVAL(K_P5):
  616. applkey(vc, 'G', vc_kbd_mode(kbd, VC_APPLIC));
  617. return;
  618. }
  619. put_queue(vc, pad_chars[value]);
  620. if (value == KVAL(K_PENTER) && vc_kbd_mode(kbd, VC_CRLF))
  621. put_queue(vc, 10);
  622. }
  623. static void k_shift(struct vc_data *vc, unsigned char value, char up_flag, struct pt_regs *regs)
  624. {
  625. int old_state = shift_state;
  626. if (rep)
  627. return;
  628. /*
  629. * Mimic typewriter:
  630. * a CapsShift key acts like Shift but undoes CapsLock
  631. */
  632. if (value == KVAL(K_CAPSSHIFT)) {
  633. value = KVAL(K_SHIFT);
  634. if (!up_flag)
  635. clr_vc_kbd_led(kbd, VC_CAPSLOCK);
  636. }
  637. if (up_flag) {
  638. /*
  639. * handle the case that two shift or control
  640. * keys are depressed simultaneously
  641. */
  642. if (shift_down[value])
  643. shift_down[value]--;
  644. } else
  645. shift_down[value]++;
  646. if (shift_down[value])
  647. shift_state |= (1 << value);
  648. else
  649. shift_state &= ~(1 << value);
  650. /* kludge */
  651. if (up_flag && shift_state != old_state && npadch != -1) {
  652. if (kbd->kbdmode == VC_UNICODE)
  653. to_utf8(vc, npadch & 0xffff);
  654. else
  655. put_queue(vc, npadch & 0xff);
  656. npadch = -1;
  657. }
  658. }
  659. static void k_meta(struct vc_data *vc, unsigned char value, char up_flag, struct pt_regs *regs)
  660. {
  661. if (up_flag)
  662. return;
  663. if (vc_kbd_mode(kbd, VC_META)) {
  664. put_queue(vc, '\033');
  665. put_queue(vc, value);
  666. } else
  667. put_queue(vc, value | 0x80);
  668. }
  669. static void k_ascii(struct vc_data *vc, unsigned char value, char up_flag, struct pt_regs *regs)
  670. {
  671. int base;
  672. if (up_flag)
  673. return;
  674. if (value < 10) {
  675. /* decimal input of code, while Alt depressed */
  676. base = 10;
  677. } else {
  678. /* hexadecimal input of code, while AltGr depressed */
  679. value -= 10;
  680. base = 16;
  681. }
  682. if (npadch == -1)
  683. npadch = value;
  684. else
  685. npadch = npadch * base + value;
  686. }
  687. static void k_lock(struct vc_data *vc, unsigned char value, char up_flag, struct pt_regs *regs)
  688. {
  689. if (up_flag || rep)
  690. return;
  691. chg_vc_kbd_lock(kbd, value);
  692. }
  693. static void k_slock(struct vc_data *vc, unsigned char value, char up_flag, struct pt_regs *regs)
  694. {
  695. k_shift(vc, value, up_flag, regs);
  696. if (up_flag || rep)
  697. return;
  698. chg_vc_kbd_slock(kbd, value);
  699. /* try to make Alt, oops, AltGr and such work */
  700. if (!key_maps[kbd->lockstate ^ kbd->slockstate]) {
  701. kbd->slockstate = 0;
  702. chg_vc_kbd_slock(kbd, value);
  703. }
  704. }
  705. /*
  706. * The leds display either (i) the status of NumLock, CapsLock, ScrollLock,
  707. * or (ii) whatever pattern of lights people want to show using KDSETLED,
  708. * or (iii) specified bits of specified words in kernel memory.
  709. */
  710. unsigned char getledstate(void)
  711. {
  712. return ledstate;
  713. }
  714. void setledstate(struct kbd_struct *kbd, unsigned int led)
  715. {
  716. if (!(led & ~7)) {
  717. ledioctl = led;
  718. kbd->ledmode = LED_SHOW_IOCTL;
  719. } else
  720. kbd->ledmode = LED_SHOW_FLAGS;
  721. set_leds();
  722. }
  723. static inline unsigned char getleds(void)
  724. {
  725. struct kbd_struct *kbd = kbd_table + fg_console;
  726. unsigned char leds;
  727. int i;
  728. if (kbd->ledmode == LED_SHOW_IOCTL)
  729. return ledioctl;
  730. leds = kbd->ledflagstate;
  731. if (kbd->ledmode == LED_SHOW_MEM) {
  732. for (i = 0; i < 3; i++)
  733. if (ledptrs[i].valid) {
  734. if (*ledptrs[i].addr & ledptrs[i].mask)
  735. leds |= (1 << i);
  736. else
  737. leds &= ~(1 << i);
  738. }
  739. }
  740. return leds;
  741. }
  742. /*
  743. * This routine is the bottom half of the keyboard interrupt
  744. * routine, and runs with all interrupts enabled. It does
  745. * console changing, led setting and copy_to_cooked, which can
  746. * take a reasonably long time.
  747. *
  748. * Aside from timing (which isn't really that important for
  749. * keyboard interrupts as they happen often), using the software
  750. * interrupt routines for this thing allows us to easily mask
  751. * this when we don't want any of the above to happen.
  752. * This allows for easy and efficient race-condition prevention
  753. * for kbd_refresh_leds => input_event(dev, EV_LED, ...) => ...
  754. */
  755. static void kbd_bh(unsigned long dummy)
  756. {
  757. struct list_head * node;
  758. unsigned char leds = getleds();
  759. if (leds != ledstate) {
  760. list_for_each(node,&kbd_handler.h_list) {
  761. struct input_handle * handle = to_handle_h(node);
  762. input_event(handle->dev, EV_LED, LED_SCROLLL, !!(leds & 0x01));
  763. input_event(handle->dev, EV_LED, LED_NUML, !!(leds & 0x02));
  764. input_event(handle->dev, EV_LED, LED_CAPSL, !!(leds & 0x04));
  765. input_sync(handle->dev);
  766. }
  767. }
  768. ledstate = leds;
  769. }
  770. DECLARE_TASKLET_DISABLED(keyboard_tasklet, kbd_bh, 0);
  771. /*
  772. * This allows a newly plugged keyboard to pick the LED state.
  773. */
  774. static void kbd_refresh_leds(struct input_handle *handle)
  775. {
  776. unsigned char leds = ledstate;
  777. tasklet_disable(&keyboard_tasklet);
  778. if (leds != 0xff) {
  779. input_event(handle->dev, EV_LED, LED_SCROLLL, !!(leds & 0x01));
  780. input_event(handle->dev, EV_LED, LED_NUML, !!(leds & 0x02));
  781. input_event(handle->dev, EV_LED, LED_CAPSL, !!(leds & 0x04));
  782. input_sync(handle->dev);
  783. }
  784. tasklet_enable(&keyboard_tasklet);
  785. }
  786. #if defined(CONFIG_X86) || defined(CONFIG_IA64) || defined(CONFIG_ALPHA) ||\
  787. defined(CONFIG_MIPS) || defined(CONFIG_PPC) || defined(CONFIG_SPARC32) ||\
  788. defined(CONFIG_SPARC64) || defined(CONFIG_PARISC) || defined(CONFIG_SUPERH) ||\
  789. (defined(CONFIG_ARM) && defined(CONFIG_KEYBOARD_ATKBD) && !defined(CONFIG_ARCH_RPC))
  790. #define HW_RAW(dev) (test_bit(EV_MSC, dev->evbit) && test_bit(MSC_RAW, dev->mscbit) &&\
  791. ((dev)->id.bustype == BUS_I8042) && ((dev)->id.vendor == 0x0001) && ((dev)->id.product == 0x0001))
  792. static unsigned short x86_keycodes[256] =
  793. { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
  794. 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
  795. 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
  796. 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
  797. 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
  798. 80, 81, 82, 83, 84,118, 86, 87, 88,115,120,119,121,112,123, 92,
  799. 284,285,309,298,312, 91,327,328,329,331,333,335,336,337,338,339,
  800. 367,288,302,304,350, 89,334,326,267,126,268,269,125,347,348,349,
  801. 360,261,262,263,268,376,100,101,321,316,373,286,289,102,351,355,
  802. 103,104,105,275,287,279,306,106,274,107,294,364,358,363,362,361,
  803. 291,108,381,281,290,272,292,305,280, 99,112,257,258,359,113,114,
  804. 264,117,271,374,379,265,266, 93, 94, 95, 85,259,375,260, 90,116,
  805. 377,109,111,277,278,282,283,295,296,297,299,300,301,293,303,307,
  806. 308,310,313,314,315,317,318,319,320,357,322,323,324,325,276,330,
  807. 332,340,365,342,343,344,345,346,356,270,341,368,369,370,371,372 };
  808. #ifdef CONFIG_MAC_EMUMOUSEBTN
  809. extern int mac_hid_mouse_emulate_buttons(int, int, int);
  810. #endif /* CONFIG_MAC_EMUMOUSEBTN */
  811. #if defined(CONFIG_SPARC32) || defined(CONFIG_SPARC64)
  812. static int sparc_l1_a_state = 0;
  813. extern void sun_do_break(void);
  814. #endif
  815. static int emulate_raw(struct vc_data *vc, unsigned int keycode,
  816. unsigned char up_flag)
  817. {
  818. if (keycode > 255 || !x86_keycodes[keycode])
  819. return -1;
  820. switch (keycode) {
  821. case KEY_PAUSE:
  822. put_queue(vc, 0xe1);
  823. put_queue(vc, 0x1d | up_flag);
  824. put_queue(vc, 0x45 | up_flag);
  825. return 0;
  826. case KEY_HANGUEL:
  827. if (!up_flag) put_queue(vc, 0xf1);
  828. return 0;
  829. case KEY_HANJA:
  830. if (!up_flag) put_queue(vc, 0xf2);
  831. return 0;
  832. }
  833. if (keycode == KEY_SYSRQ && sysrq_alt) {
  834. put_queue(vc, 0x54 | up_flag);
  835. return 0;
  836. }
  837. if (x86_keycodes[keycode] & 0x100)
  838. put_queue(vc, 0xe0);
  839. put_queue(vc, (x86_keycodes[keycode] & 0x7f) | up_flag);
  840. if (keycode == KEY_SYSRQ) {
  841. put_queue(vc, 0xe0);
  842. put_queue(vc, 0x37 | up_flag);
  843. }
  844. return 0;
  845. }
  846. #else
  847. #define HW_RAW(dev) 0
  848. #warning "Cannot generate rawmode keyboard for your architecture yet."
  849. static int emulate_raw(struct vc_data *vc, unsigned int keycode, unsigned char up_flag)
  850. {
  851. if (keycode > 127)
  852. return -1;
  853. put_queue(vc, keycode | up_flag);
  854. return 0;
  855. }
  856. #endif
  857. static void kbd_rawcode(unsigned char data)
  858. {
  859. struct vc_data *vc = vc_cons[fg_console].d;
  860. kbd = kbd_table + fg_console;
  861. if (kbd->kbdmode == VC_RAW)
  862. put_queue(vc, data);
  863. }
  864. static void kbd_keycode(unsigned int keycode, int down,
  865. int hw_raw, struct pt_regs *regs)
  866. {
  867. struct vc_data *vc = vc_cons[fg_console].d;
  868. unsigned short keysym, *key_map;
  869. unsigned char type, raw_mode;
  870. struct tty_struct *tty;
  871. int shift_final;
  872. tty = vc->vc_tty;
  873. if (tty && (!tty->driver_data)) {
  874. /* No driver data? Strange. Okay we fix it then. */
  875. tty->driver_data = vc;
  876. }
  877. kbd = kbd_table + fg_console;
  878. if (keycode == KEY_LEFTALT || keycode == KEY_RIGHTALT)
  879. sysrq_alt = down;
  880. #if defined(CONFIG_SPARC32) || defined(CONFIG_SPARC64)
  881. if (keycode == KEY_STOP)
  882. sparc_l1_a_state = down;
  883. #endif
  884. rep = (down == 2);
  885. #ifdef CONFIG_MAC_EMUMOUSEBTN
  886. if (mac_hid_mouse_emulate_buttons(1, keycode, down))
  887. return;
  888. #endif /* CONFIG_MAC_EMUMOUSEBTN */
  889. if ((raw_mode = (kbd->kbdmode == VC_RAW)) && !hw_raw)
  890. if (emulate_raw(vc, keycode, !down << 7))
  891. if (keycode < BTN_MISC)
  892. printk(KERN_WARNING "keyboard.c: can't emulate rawmode for keycode %d\n", keycode);
  893. #ifdef CONFIG_MAGIC_SYSRQ /* Handle the SysRq Hack */
  894. if (keycode == KEY_SYSRQ && (sysrq_down || (down == 1 && sysrq_alt))) {
  895. sysrq_down = down;
  896. return;
  897. }
  898. if (sysrq_down && down && !rep) {
  899. handle_sysrq(kbd_sysrq_xlate[keycode], regs, tty);
  900. return;
  901. }
  902. #endif
  903. #if defined(CONFIG_SPARC32) || defined(CONFIG_SPARC64)
  904. if (keycode == KEY_A && sparc_l1_a_state) {
  905. sparc_l1_a_state = 0;
  906. sun_do_break();
  907. }
  908. #endif
  909. if (kbd->kbdmode == VC_MEDIUMRAW) {
  910. /*
  911. * This is extended medium raw mode, with keys above 127
  912. * encoded as 0, high 7 bits, low 7 bits, with the 0 bearing
  913. * the 'up' flag if needed. 0 is reserved, so this shouldn't
  914. * interfere with anything else. The two bytes after 0 will
  915. * always have the up flag set not to interfere with older
  916. * applications. This allows for 16384 different keycodes,
  917. * which should be enough.
  918. */
  919. if (keycode < 128) {
  920. put_queue(vc, keycode | (!down << 7));
  921. } else {
  922. put_queue(vc, !down << 7);
  923. put_queue(vc, (keycode >> 7) | 0x80);
  924. put_queue(vc, keycode | 0x80);
  925. }
  926. raw_mode = 1;
  927. }
  928. if (down)
  929. set_bit(keycode, key_down);
  930. else
  931. clear_bit(keycode, key_down);
  932. if (rep && (!vc_kbd_mode(kbd, VC_REPEAT) || (tty &&
  933. (!L_ECHO(tty) && tty->driver->chars_in_buffer(tty))))) {
  934. /*
  935. * Don't repeat a key if the input buffers are not empty and the
  936. * characters get aren't echoed locally. This makes key repeat
  937. * usable with slow applications and under heavy loads.
  938. */
  939. return;
  940. }
  941. shift_final = (shift_state | kbd->slockstate) ^ kbd->lockstate;
  942. key_map = key_maps[shift_final];
  943. if (!key_map) {
  944. compute_shiftstate();
  945. kbd->slockstate = 0;
  946. return;
  947. }
  948. if (keycode > NR_KEYS)
  949. return;
  950. keysym = key_map[keycode];
  951. type = KTYP(keysym);
  952. if (type < 0xf0) {
  953. if (down && !raw_mode) to_utf8(vc, keysym);
  954. return;
  955. }
  956. type -= 0xf0;
  957. if (raw_mode && type != KT_SPEC && type != KT_SHIFT)
  958. return;
  959. if (type == KT_LETTER) {
  960. type = KT_LATIN;
  961. if (vc_kbd_led(kbd, VC_CAPSLOCK)) {
  962. key_map = key_maps[shift_final ^ (1 << KG_SHIFT)];
  963. if (key_map)
  964. keysym = key_map[keycode];
  965. }
  966. }
  967. (*k_handler[type])(vc, keysym & 0xff, !down, regs);
  968. if (type != KT_SLOCK)
  969. kbd->slockstate = 0;
  970. }
  971. static void kbd_event(struct input_handle *handle, unsigned int event_type,
  972. unsigned int event_code, int value)
  973. {
  974. if (event_type == EV_MSC && event_code == MSC_RAW && HW_RAW(handle->dev))
  975. kbd_rawcode(value);
  976. if (event_type == EV_KEY)
  977. kbd_keycode(event_code, value, HW_RAW(handle->dev), handle->dev->regs);
  978. tasklet_schedule(&keyboard_tasklet);
  979. do_poke_blanked_console = 1;
  980. schedule_console_callback();
  981. }
  982. static char kbd_name[] = "kbd";
  983. /*
  984. * When a keyboard (or other input device) is found, the kbd_connect
  985. * function is called. The function then looks at the device, and if it
  986. * likes it, it can open it and get events from it. In this (kbd_connect)
  987. * function, we should decide which VT to bind that keyboard to initially.
  988. */
  989. static struct input_handle *kbd_connect(struct input_handler *handler,
  990. struct input_dev *dev,
  991. struct input_device_id *id)
  992. {
  993. struct input_handle *handle;
  994. int i;
  995. for (i = KEY_RESERVED; i < BTN_MISC; i++)
  996. if (test_bit(i, dev->keybit)) break;
  997. if ((i == BTN_MISC) && !test_bit(EV_SND, dev->evbit))
  998. return NULL;
  999. if (!(handle = kmalloc(sizeof(struct input_handle), GFP_KERNEL)))
  1000. return NULL;
  1001. memset(handle, 0, sizeof(struct input_handle));
  1002. handle->dev = dev;
  1003. handle->handler = handler;
  1004. handle->name = kbd_name;
  1005. input_open_device(handle);
  1006. kbd_refresh_leds(handle);
  1007. return handle;
  1008. }
  1009. static void kbd_disconnect(struct input_handle *handle)
  1010. {
  1011. input_close_device(handle);
  1012. kfree(handle);
  1013. }
  1014. static struct input_device_id kbd_ids[] = {
  1015. {
  1016. .flags = INPUT_DEVICE_ID_MATCH_EVBIT,
  1017. .evbit = { BIT(EV_KEY) },
  1018. },
  1019. {
  1020. .flags = INPUT_DEVICE_ID_MATCH_EVBIT,
  1021. .evbit = { BIT(EV_SND) },
  1022. },
  1023. { }, /* Terminating entry */
  1024. };
  1025. MODULE_DEVICE_TABLE(input, kbd_ids);
  1026. static struct input_handler kbd_handler = {
  1027. .event = kbd_event,
  1028. .connect = kbd_connect,
  1029. .disconnect = kbd_disconnect,
  1030. .name = "kbd",
  1031. .id_table = kbd_ids,
  1032. };
  1033. int __init kbd_init(void)
  1034. {
  1035. int i;
  1036. kbd0.ledflagstate = kbd0.default_ledflagstate = KBD_DEFLEDS;
  1037. kbd0.ledmode = LED_SHOW_FLAGS;
  1038. kbd0.lockstate = KBD_DEFLOCK;
  1039. kbd0.slockstate = 0;
  1040. kbd0.modeflags = KBD_DEFMODE;
  1041. kbd0.kbdmode = VC_XLATE;
  1042. for (i = 0 ; i < MAX_NR_CONSOLES ; i++)
  1043. kbd_table[i] = kbd0;
  1044. input_register_handler(&kbd_handler);
  1045. tasklet_enable(&keyboard_tasklet);
  1046. tasklet_schedule(&keyboard_tasklet);
  1047. return 0;
  1048. }