keyboard.c 33 KB

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