keyboard.c 33 KB

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