keyboard.c 33 KB

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