keyboard.c 33 KB

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