keyboard.c 36 KB

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