keyboard.c 34 KB

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