keyboard.c 34 KB

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