keyboard.c 30 KB

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