keyboard.c 32 KB

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