keyboard.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Written for linux by Johan Myreen as a translation from
  4. * the assembly version by Linus (with diacriticals added)
  5. *
  6. * Some additional features added by Christoph Niemann (ChN), March 1993
  7. *
  8. * Loadable keymaps by Risto Kankkunen, May 1993
  9. *
  10. * Diacriticals redone & other small changes, aeb@cwi.nl, June 1993
  11. * Added decr/incr_console, dynamic keymaps, Unicode support,
  12. * dynamic function/string keys, led setting, Sept 1994
  13. * `Sticky' modifier keys, 951006.
  14. *
  15. * 11-11-96: SAK should now work in the raw mode (Martin Mares)
  16. *
  17. * Modified to provide 'generic' keyboard support by Hamish Macdonald
  18. * Merge with the m68k keyboard driver and split-off of the PC low-level
  19. * parts by Geert Uytterhoeven, May 1997
  20. *
  21. * 27-05-97: Added support for the Magic SysRq Key (Martin Mares)
  22. * 30-07-98: Dead keys redone, aeb@cwi.nl.
  23. * 21-08-02: Converted to input API, major cleanup. (Vojtech Pavlik)
  24. */
  25. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  26. #include <linux/consolemap.h>
  27. #include <linux/init.h>
  28. #include <linux/input.h>
  29. #include <linux/jiffies.h>
  30. #include <linux/kbd_diacr.h>
  31. #include <linux/kbd_kern.h>
  32. #include <linux/leds.h>
  33. #include <linux/mm.h>
  34. #include <linux/module.h>
  35. #include <linux/nospec.h>
  36. #include <linux/notifier.h>
  37. #include <linux/reboot.h>
  38. #include <linux/sched/debug.h>
  39. #include <linux/sched/signal.h>
  40. #include <linux/slab.h>
  41. #include <linux/spinlock.h>
  42. #include <linux/string.h>
  43. #include <linux/tty_flip.h>
  44. #include <linux/tty.h>
  45. #include <linux/uaccess.h>
  46. #include <linux/vt_kern.h>
  47. #include <asm/irq_regs.h>
  48. /*
  49. * Exported functions/variables
  50. */
  51. #define KBD_DEFMODE (BIT(VC_REPEAT) | BIT(VC_META))
  52. #if defined(CONFIG_X86) || defined(CONFIG_PARISC)
  53. #include <asm/kbdleds.h>
  54. #else
  55. static inline int kbd_defleds(void)
  56. {
  57. return 0;
  58. }
  59. #endif
  60. #define KBD_DEFLOCK 0
  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. struct vt_spawn_console vt_spawn_con = {
  86. .lock = __SPIN_LOCK_UNLOCKED(vt_spawn_con.lock),
  87. .pid = NULL,
  88. .sig = 0,
  89. };
  90. /*
  91. * Internal Data.
  92. */
  93. static struct kbd_struct kbd_table[MAX_NR_CONSOLES];
  94. static struct kbd_struct *kbd = kbd_table;
  95. /* maximum values each key_handler can handle */
  96. static const unsigned char max_vals[] = {
  97. [ KT_LATIN ] = 255,
  98. [ KT_FN ] = ARRAY_SIZE(func_table) - 1,
  99. [ KT_SPEC ] = ARRAY_SIZE(fn_handler) - 1,
  100. [ KT_PAD ] = NR_PAD - 1,
  101. [ KT_DEAD ] = NR_DEAD - 1,
  102. [ KT_CONS ] = 255,
  103. [ KT_CUR ] = 3,
  104. [ KT_SHIFT ] = NR_SHIFT - 1,
  105. [ KT_META ] = 255,
  106. [ KT_ASCII ] = NR_ASCII - 1,
  107. [ KT_LOCK ] = NR_LOCK - 1,
  108. [ KT_LETTER ] = 255,
  109. [ KT_SLOCK ] = NR_LOCK - 1,
  110. [ KT_DEAD2 ] = 255,
  111. [ KT_BRL ] = NR_BRL - 1,
  112. };
  113. static const int NR_TYPES = ARRAY_SIZE(max_vals);
  114. static void kbd_bh(struct tasklet_struct *unused);
  115. static DECLARE_TASKLET_DISABLED(keyboard_tasklet, kbd_bh);
  116. static struct input_handler kbd_handler;
  117. static DEFINE_SPINLOCK(kbd_event_lock);
  118. static DEFINE_SPINLOCK(led_lock);
  119. static DEFINE_SPINLOCK(func_buf_lock); /* guard 'func_buf' and friends */
  120. static DECLARE_BITMAP(key_down, KEY_CNT); /* keyboard key bitmap */
  121. static unsigned char shift_down[NR_SHIFT]; /* shift state counters.. */
  122. static bool dead_key_next;
  123. /* Handles a number being assembled on the number pad */
  124. static bool npadch_active;
  125. static unsigned int npadch_value;
  126. static unsigned int diacr;
  127. static bool rep; /* flag telling character repeat */
  128. static int shift_state = 0;
  129. static unsigned int ledstate = -1U; /* undefined */
  130. static unsigned char ledioctl;
  131. static bool vt_switch;
  132. /*
  133. * Notifier list for console keyboard events
  134. */
  135. static ATOMIC_NOTIFIER_HEAD(keyboard_notifier_list);
  136. int register_keyboard_notifier(struct notifier_block *nb)
  137. {
  138. return atomic_notifier_chain_register(&keyboard_notifier_list, nb);
  139. }
  140. EXPORT_SYMBOL_GPL(register_keyboard_notifier);
  141. int unregister_keyboard_notifier(struct notifier_block *nb)
  142. {
  143. return atomic_notifier_chain_unregister(&keyboard_notifier_list, nb);
  144. }
  145. EXPORT_SYMBOL_GPL(unregister_keyboard_notifier);
  146. /*
  147. * Translation of scancodes to keycodes. We set them on only the first
  148. * keyboard in the list that accepts the scancode and keycode.
  149. * Explanation for not choosing the first attached keyboard anymore:
  150. * USB keyboards for example have two event devices: one for all "normal"
  151. * keys and one for extra function keys (like "volume up", "make coffee",
  152. * etc.). So this means that scancodes for the extra function keys won't
  153. * be valid for the first event device, but will be for the second.
  154. */
  155. struct getset_keycode_data {
  156. struct input_keymap_entry ke;
  157. int error;
  158. };
  159. static int getkeycode_helper(struct input_handle *handle, void *data)
  160. {
  161. struct getset_keycode_data *d = data;
  162. d->error = input_get_keycode(handle->dev, &d->ke);
  163. return d->error == 0; /* stop as soon as we successfully get one */
  164. }
  165. static int getkeycode(unsigned int scancode)
  166. {
  167. struct getset_keycode_data d = {
  168. .ke = {
  169. .flags = 0,
  170. .len = sizeof(scancode),
  171. .keycode = 0,
  172. },
  173. .error = -ENODEV,
  174. };
  175. memcpy(d.ke.scancode, &scancode, sizeof(scancode));
  176. input_handler_for_each_handle(&kbd_handler, &d, getkeycode_helper);
  177. return d.error ?: d.ke.keycode;
  178. }
  179. static int setkeycode_helper(struct input_handle *handle, void *data)
  180. {
  181. struct getset_keycode_data *d = data;
  182. d->error = input_set_keycode(handle->dev, &d->ke);
  183. return d->error == 0; /* stop as soon as we successfully set one */
  184. }
  185. static int setkeycode(unsigned int scancode, unsigned int keycode)
  186. {
  187. struct getset_keycode_data d = {
  188. .ke = {
  189. .flags = 0,
  190. .len = sizeof(scancode),
  191. .keycode = keycode,
  192. },
  193. .error = -ENODEV,
  194. };
  195. memcpy(d.ke.scancode, &scancode, sizeof(scancode));
  196. input_handler_for_each_handle(&kbd_handler, &d, setkeycode_helper);
  197. return d.error;
  198. }
  199. /*
  200. * Making beeps and bells. Note that we prefer beeps to bells, but when
  201. * shutting the sound off we do both.
  202. */
  203. static int kd_sound_helper(struct input_handle *handle, void *data)
  204. {
  205. unsigned int *hz = data;
  206. struct input_dev *dev = handle->dev;
  207. if (test_bit(EV_SND, dev->evbit)) {
  208. if (test_bit(SND_TONE, dev->sndbit)) {
  209. input_inject_event(handle, EV_SND, SND_TONE, *hz);
  210. if (*hz)
  211. return 0;
  212. }
  213. if (test_bit(SND_BELL, dev->sndbit))
  214. input_inject_event(handle, EV_SND, SND_BELL, *hz ? 1 : 0);
  215. }
  216. return 0;
  217. }
  218. static void kd_nosound(struct timer_list *unused)
  219. {
  220. static unsigned int zero;
  221. input_handler_for_each_handle(&kbd_handler, &zero, kd_sound_helper);
  222. }
  223. static DEFINE_TIMER(kd_mksound_timer, kd_nosound);
  224. void kd_mksound(unsigned int hz, unsigned int ticks)
  225. {
  226. timer_delete_sync(&kd_mksound_timer);
  227. input_handler_for_each_handle(&kbd_handler, &hz, kd_sound_helper);
  228. if (hz && ticks)
  229. mod_timer(&kd_mksound_timer, jiffies + ticks);
  230. }
  231. EXPORT_SYMBOL(kd_mksound);
  232. /*
  233. * Setting the keyboard rate.
  234. */
  235. static int kbd_rate_helper(struct input_handle *handle, void *data)
  236. {
  237. struct input_dev *dev = handle->dev;
  238. struct kbd_repeat *rpt = data;
  239. if (test_bit(EV_REP, dev->evbit)) {
  240. if (rpt[0].delay > 0)
  241. input_inject_event(handle,
  242. EV_REP, REP_DELAY, rpt[0].delay);
  243. if (rpt[0].period > 0)
  244. input_inject_event(handle,
  245. EV_REP, REP_PERIOD, rpt[0].period);
  246. rpt[1].delay = dev->rep[REP_DELAY];
  247. rpt[1].period = dev->rep[REP_PERIOD];
  248. }
  249. return 0;
  250. }
  251. int kbd_rate(struct kbd_repeat *rpt)
  252. {
  253. struct kbd_repeat data[2] = { *rpt };
  254. input_handler_for_each_handle(&kbd_handler, data, kbd_rate_helper);
  255. *rpt = data[1]; /* Copy currently used settings */
  256. return 0;
  257. }
  258. /*
  259. * Helper Functions.
  260. */
  261. static void put_queue(struct vc_data *vc, int ch)
  262. {
  263. tty_insert_flip_char(&vc->port, ch, 0);
  264. tty_flip_buffer_push(&vc->port);
  265. }
  266. static void puts_queue(struct vc_data *vc, const char *cp)
  267. {
  268. tty_insert_flip_string(&vc->port, cp, strlen(cp));
  269. tty_flip_buffer_push(&vc->port);
  270. }
  271. static void applkey(struct vc_data *vc, int key, char mode)
  272. {
  273. static char buf[] = { 0x1b, 'O', 0x00, 0x00 };
  274. buf[1] = (mode ? 'O' : '[');
  275. buf[2] = key;
  276. puts_queue(vc, buf);
  277. }
  278. /*
  279. * Many other routines do put_queue, but I think either
  280. * they produce ASCII, or they produce some user-assigned
  281. * string, and in both cases we might assume that it is
  282. * in utf-8 already.
  283. */
  284. static void to_utf8(struct vc_data *vc, uint c)
  285. {
  286. if (c < 0x80)
  287. /* 0******* */
  288. put_queue(vc, c);
  289. else if (c < 0x800) {
  290. /* 110***** 10****** */
  291. put_queue(vc, 0xc0 | (c >> 6));
  292. put_queue(vc, 0x80 | (c & 0x3f));
  293. } else if (c < 0x10000) {
  294. if (c >= 0xD800 && c < 0xE000)
  295. return;
  296. if (c == 0xFFFF)
  297. return;
  298. /* 1110**** 10****** 10****** */
  299. put_queue(vc, 0xe0 | (c >> 12));
  300. put_queue(vc, 0x80 | ((c >> 6) & 0x3f));
  301. put_queue(vc, 0x80 | (c & 0x3f));
  302. } else if (c < 0x110000) {
  303. /* 11110*** 10****** 10****** 10****** */
  304. put_queue(vc, 0xf0 | (c >> 18));
  305. put_queue(vc, 0x80 | ((c >> 12) & 0x3f));
  306. put_queue(vc, 0x80 | ((c >> 6) & 0x3f));
  307. put_queue(vc, 0x80 | (c & 0x3f));
  308. }
  309. }
  310. static void put_queue_utf8(struct vc_data *vc, u32 value)
  311. {
  312. if (kbd->kbdmode == VC_UNICODE)
  313. to_utf8(vc, value);
  314. else {
  315. int c = conv_uni_to_8bit(value);
  316. if (c != -1)
  317. put_queue(vc, c);
  318. }
  319. }
  320. /* FIXME: review locking for vt.c callers */
  321. static void set_leds(void)
  322. {
  323. tasklet_schedule(&keyboard_tasklet);
  324. }
  325. /*
  326. * Called after returning from RAW mode or when changing consoles - recompute
  327. * shift_down[] and shift_state from key_down[] maybe called when keymap is
  328. * undefined, so that shiftkey release is seen. The caller must hold the
  329. * kbd_event_lock.
  330. */
  331. static void do_compute_shiftstate(void)
  332. {
  333. unsigned int k, sym, val;
  334. shift_state = 0;
  335. memset(shift_down, 0, sizeof(shift_down));
  336. for_each_set_bit(k, key_down, min(NR_KEYS, KEY_CNT)) {
  337. sym = U(key_maps[0][k]);
  338. if (KTYP(sym) != KT_SHIFT && KTYP(sym) != KT_SLOCK)
  339. continue;
  340. val = KVAL(sym);
  341. if (val == KVAL(K_CAPSSHIFT))
  342. val = KVAL(K_SHIFT);
  343. shift_down[val]++;
  344. shift_state |= BIT(val);
  345. }
  346. }
  347. /* We still have to export this method to vt.c */
  348. void vt_set_leds_compute_shiftstate(void)
  349. {
  350. /*
  351. * When VT is switched, the keyboard led needs to be set once.
  352. * Ensure that after the switch is completed, the state of the
  353. * keyboard LED is consistent with the state of the keyboard lock.
  354. */
  355. vt_switch = true;
  356. set_leds();
  357. guard(spinlock_irqsave)(&kbd_event_lock);
  358. do_compute_shiftstate();
  359. }
  360. /*
  361. * We have a combining character DIACR here, followed by the character CH.
  362. * If the combination occurs in the table, return the corresponding value.
  363. * Otherwise, if CH is a space or equals DIACR, return DIACR.
  364. * Otherwise, conclude that DIACR was not combining after all,
  365. * queue it and return CH.
  366. */
  367. static unsigned int handle_diacr(struct vc_data *vc, unsigned int ch)
  368. {
  369. unsigned int d = diacr;
  370. unsigned int i;
  371. diacr = 0;
  372. if ((d & ~0xff) == BRL_UC_ROW) {
  373. if ((ch & ~0xff) == BRL_UC_ROW)
  374. return d | ch;
  375. } else {
  376. for (i = 0; i < accent_table_size; i++)
  377. if (accent_table[i].diacr == d && accent_table[i].base == ch)
  378. return accent_table[i].result;
  379. }
  380. if (ch == ' ' || ch == (BRL_UC_ROW|0) || ch == d)
  381. return d;
  382. put_queue_utf8(vc, d);
  383. return ch;
  384. }
  385. /*
  386. * Special function handlers
  387. */
  388. static void fn_enter(struct vc_data *vc)
  389. {
  390. if (diacr) {
  391. put_queue_utf8(vc, diacr);
  392. diacr = 0;
  393. }
  394. put_queue(vc, '\r');
  395. if (vc_kbd_mode(kbd, VC_CRLF))
  396. put_queue(vc, '\n');
  397. }
  398. static void fn_caps_toggle(struct vc_data *vc)
  399. {
  400. if (rep)
  401. return;
  402. chg_vc_kbd_led(kbd, VC_CAPSLOCK);
  403. }
  404. static void fn_caps_on(struct vc_data *vc)
  405. {
  406. if (rep)
  407. return;
  408. set_vc_kbd_led(kbd, VC_CAPSLOCK);
  409. }
  410. static void fn_show_ptregs(struct vc_data *vc)
  411. {
  412. struct pt_regs *regs = get_irq_regs();
  413. if (regs)
  414. show_regs(regs);
  415. }
  416. static void fn_hold(struct vc_data *vc)
  417. {
  418. struct tty_struct *tty = vc->port.tty;
  419. if (rep || !tty)
  420. return;
  421. /*
  422. * Note: SCROLLOCK will be set (cleared) by stop_tty (start_tty);
  423. * these routines are also activated by ^S/^Q.
  424. * (And SCROLLOCK can also be set by the ioctl KDSKBLED.)
  425. */
  426. if (tty->flow.stopped)
  427. start_tty(tty);
  428. else
  429. stop_tty(tty);
  430. }
  431. static void fn_num(struct vc_data *vc)
  432. {
  433. if (vc_kbd_mode(kbd, VC_APPLIC))
  434. applkey(vc, 'P', 1);
  435. else
  436. fn_bare_num(vc);
  437. }
  438. /*
  439. * Bind this to Shift-NumLock if you work in application keypad mode
  440. * but want to be able to change the NumLock flag.
  441. * Bind this to NumLock if you prefer that the NumLock key always
  442. * changes the NumLock flag.
  443. */
  444. static void fn_bare_num(struct vc_data *vc)
  445. {
  446. if (!rep)
  447. chg_vc_kbd_led(kbd, VC_NUMLOCK);
  448. }
  449. static void fn_lastcons(struct vc_data *vc)
  450. {
  451. /* switch to the last used console, ChN */
  452. set_console(last_console);
  453. }
  454. static void fn_dec_console(struct vc_data *vc)
  455. {
  456. int i, cur = fg_console;
  457. /* Currently switching? Queue this next switch relative to that. */
  458. if (want_console != -1)
  459. cur = want_console;
  460. for (i = cur - 1; i != cur; i--) {
  461. if (i == -1)
  462. i = MAX_NR_CONSOLES - 1;
  463. if (vc_cons_allocated(i))
  464. break;
  465. }
  466. set_console(i);
  467. }
  468. static void fn_inc_console(struct vc_data *vc)
  469. {
  470. int i, cur = fg_console;
  471. /* Currently switching? Queue this next switch relative to that. */
  472. if (want_console != -1)
  473. cur = want_console;
  474. for (i = cur+1; i != cur; i++) {
  475. if (i == MAX_NR_CONSOLES)
  476. i = 0;
  477. if (vc_cons_allocated(i))
  478. break;
  479. }
  480. set_console(i);
  481. }
  482. static void fn_send_intr(struct vc_data *vc)
  483. {
  484. tty_insert_flip_char(&vc->port, 0, TTY_BREAK);
  485. tty_flip_buffer_push(&vc->port);
  486. }
  487. static void fn_scroll_forw(struct vc_data *vc)
  488. {
  489. scrollfront(vc, 0);
  490. }
  491. static void fn_scroll_back(struct vc_data *vc)
  492. {
  493. scrollback(vc);
  494. }
  495. static void fn_show_mem(struct vc_data *vc)
  496. {
  497. show_mem();
  498. }
  499. static void fn_show_state(struct vc_data *vc)
  500. {
  501. show_state();
  502. }
  503. static void fn_boot_it(struct vc_data *vc)
  504. {
  505. ctrl_alt_del();
  506. }
  507. static void fn_compose(struct vc_data *vc)
  508. {
  509. dead_key_next = true;
  510. }
  511. static void fn_spawn_con(struct vc_data *vc)
  512. {
  513. guard(spinlock)(&vt_spawn_con.lock);
  514. if (vt_spawn_con.pid)
  515. if (kill_pid(vt_spawn_con.pid, vt_spawn_con.sig, 1)) {
  516. put_pid(vt_spawn_con.pid);
  517. vt_spawn_con.pid = NULL;
  518. }
  519. }
  520. static void fn_SAK(struct vc_data *vc)
  521. {
  522. struct work_struct *SAK_work = &vc_cons[fg_console].SAK_work;
  523. schedule_work(SAK_work);
  524. }
  525. static void fn_null(struct vc_data *vc)
  526. {
  527. do_compute_shiftstate();
  528. }
  529. /*
  530. * Special key handlers
  531. */
  532. static void k_ignore(struct vc_data *vc, unsigned char value, char up_flag)
  533. {
  534. }
  535. static void k_spec(struct vc_data *vc, unsigned char value, char up_flag)
  536. {
  537. if (up_flag)
  538. return;
  539. if (value >= ARRAY_SIZE(fn_handler))
  540. return;
  541. if ((kbd->kbdmode == VC_RAW ||
  542. kbd->kbdmode == VC_MEDIUMRAW ||
  543. kbd->kbdmode == VC_OFF) &&
  544. value != KVAL(K_SAK))
  545. return; /* SAK is allowed even in raw mode */
  546. fn_handler[value](vc);
  547. }
  548. static void k_lowercase(struct vc_data *vc, unsigned char value, char up_flag)
  549. {
  550. pr_err("k_lowercase was called - impossible\n");
  551. }
  552. static void k_unicode(struct vc_data *vc, unsigned int value, char up_flag)
  553. {
  554. if (up_flag)
  555. return; /* no action, if this is a key release */
  556. if (diacr)
  557. value = handle_diacr(vc, value);
  558. if (dead_key_next) {
  559. dead_key_next = false;
  560. diacr = value;
  561. return;
  562. }
  563. put_queue_utf8(vc, value);
  564. }
  565. /*
  566. * Handle dead key. Note that we now may have several
  567. * dead keys modifying the same character. Very useful
  568. * for Vietnamese.
  569. */
  570. static void k_deadunicode(struct vc_data *vc, unsigned int value, char up_flag)
  571. {
  572. if (up_flag)
  573. return;
  574. diacr = (diacr ? handle_diacr(vc, value) : value);
  575. }
  576. static void k_self(struct vc_data *vc, unsigned char value, char up_flag)
  577. {
  578. k_unicode(vc, conv_8bit_to_uni(value), up_flag);
  579. }
  580. static void k_dead2(struct vc_data *vc, unsigned char value, char up_flag)
  581. {
  582. k_deadunicode(vc, value, up_flag);
  583. }
  584. /*
  585. * Obsolete - for backwards compatibility only
  586. */
  587. static void k_dead(struct vc_data *vc, unsigned char value, char up_flag)
  588. {
  589. static const unsigned char ret_diacr[NR_DEAD] = {
  590. '`', /* dead_grave */
  591. '\'', /* dead_acute */
  592. '^', /* dead_circumflex */
  593. '~', /* dead_tilda */
  594. '"', /* dead_diaeresis */
  595. ',', /* dead_cedilla */
  596. '_', /* dead_macron */
  597. 'U', /* dead_breve */
  598. '.', /* dead_abovedot */
  599. '*', /* dead_abovering */
  600. '=', /* dead_doubleacute */
  601. 'c', /* dead_caron */
  602. 'k', /* dead_ogonek */
  603. 'i', /* dead_iota */
  604. '#', /* dead_voiced_sound */
  605. 'o', /* dead_semivoiced_sound */
  606. '!', /* dead_belowdot */
  607. '?', /* dead_hook */
  608. '+', /* dead_horn */
  609. '-', /* dead_stroke */
  610. ')', /* dead_abovecomma */
  611. '(', /* dead_abovereversedcomma */
  612. ':', /* dead_doublegrave */
  613. 'n', /* dead_invertedbreve */
  614. ';', /* dead_belowcomma */
  615. '$', /* dead_currency */
  616. '@', /* dead_greek */
  617. };
  618. k_deadunicode(vc, ret_diacr[value], up_flag);
  619. }
  620. static void k_cons(struct vc_data *vc, unsigned char value, char up_flag)
  621. {
  622. if (up_flag)
  623. return;
  624. set_console(value);
  625. }
  626. static void k_fn(struct vc_data *vc, unsigned char value, char up_flag)
  627. {
  628. if (up_flag)
  629. return;
  630. if ((unsigned)value < ARRAY_SIZE(func_table)) {
  631. guard(spinlock_irqsave)(&func_buf_lock);
  632. if (func_table[value])
  633. puts_queue(vc, func_table[value]);
  634. } else
  635. pr_err("k_fn called with value=%d\n", value);
  636. }
  637. static void k_cur(struct vc_data *vc, unsigned char value, char up_flag)
  638. {
  639. static const char cur_chars[] = "BDCA";
  640. if (up_flag)
  641. return;
  642. applkey(vc, cur_chars[value], vc_kbd_mode(kbd, VC_CKMODE));
  643. }
  644. static void k_pad(struct vc_data *vc, unsigned char value, char up_flag)
  645. {
  646. static const char pad_chars[] = "0123456789+-*/\015,.?()#";
  647. static const char app_map[] = "pqrstuvwxylSRQMnnmPQS";
  648. if (up_flag)
  649. return; /* no action, if this is a key release */
  650. /* kludge... shift forces cursor/number keys */
  651. if (vc_kbd_mode(kbd, VC_APPLIC) && !shift_down[KG_SHIFT]) {
  652. applkey(vc, app_map[value], 1);
  653. return;
  654. }
  655. if (!vc_kbd_led(kbd, VC_NUMLOCK)) {
  656. switch (value) {
  657. case KVAL(K_PCOMMA):
  658. case KVAL(K_PDOT):
  659. k_fn(vc, KVAL(K_REMOVE), 0);
  660. return;
  661. case KVAL(K_P0):
  662. k_fn(vc, KVAL(K_INSERT), 0);
  663. return;
  664. case KVAL(K_P1):
  665. k_fn(vc, KVAL(K_SELECT), 0);
  666. return;
  667. case KVAL(K_P2):
  668. k_cur(vc, KVAL(K_DOWN), 0);
  669. return;
  670. case KVAL(K_P3):
  671. k_fn(vc, KVAL(K_PGDN), 0);
  672. return;
  673. case KVAL(K_P4):
  674. k_cur(vc, KVAL(K_LEFT), 0);
  675. return;
  676. case KVAL(K_P6):
  677. k_cur(vc, KVAL(K_RIGHT), 0);
  678. return;
  679. case KVAL(K_P7):
  680. k_fn(vc, KVAL(K_FIND), 0);
  681. return;
  682. case KVAL(K_P8):
  683. k_cur(vc, KVAL(K_UP), 0);
  684. return;
  685. case KVAL(K_P9):
  686. k_fn(vc, KVAL(K_PGUP), 0);
  687. return;
  688. case KVAL(K_P5):
  689. applkey(vc, 'G', vc_kbd_mode(kbd, VC_APPLIC));
  690. return;
  691. }
  692. }
  693. put_queue(vc, pad_chars[value]);
  694. if (value == KVAL(K_PENTER) && vc_kbd_mode(kbd, VC_CRLF))
  695. put_queue(vc, '\n');
  696. }
  697. static void k_shift(struct vc_data *vc, unsigned char value, char up_flag)
  698. {
  699. int old_state = shift_state;
  700. if (rep)
  701. return;
  702. /*
  703. * Mimic typewriter:
  704. * a CapsShift key acts like Shift but undoes CapsLock
  705. */
  706. if (value == KVAL(K_CAPSSHIFT)) {
  707. value = KVAL(K_SHIFT);
  708. if (!up_flag)
  709. clr_vc_kbd_led(kbd, VC_CAPSLOCK);
  710. }
  711. if (up_flag) {
  712. /*
  713. * handle the case that two shift or control
  714. * keys are depressed simultaneously
  715. */
  716. if (shift_down[value])
  717. shift_down[value]--;
  718. } else
  719. shift_down[value]++;
  720. if (shift_down[value])
  721. shift_state |= BIT(value);
  722. else
  723. shift_state &= ~BIT(value);
  724. /* kludge */
  725. if (up_flag && shift_state != old_state && npadch_active) {
  726. if (kbd->kbdmode == VC_UNICODE)
  727. to_utf8(vc, npadch_value);
  728. else
  729. put_queue(vc, npadch_value & 0xff);
  730. npadch_active = false;
  731. }
  732. }
  733. static void k_meta(struct vc_data *vc, unsigned char value, char up_flag)
  734. {
  735. if (up_flag)
  736. return;
  737. if (vc_kbd_mode(kbd, VC_META)) {
  738. put_queue(vc, '\033');
  739. put_queue(vc, value);
  740. } else
  741. put_queue(vc, value | BIT(7));
  742. }
  743. static void k_ascii(struct vc_data *vc, unsigned char value, char up_flag)
  744. {
  745. unsigned int base;
  746. if (up_flag)
  747. return;
  748. if (value < 10) {
  749. /* decimal input of code, while Alt depressed */
  750. base = 10;
  751. } else {
  752. /* hexadecimal input of code, while AltGr depressed */
  753. value -= 10;
  754. base = 16;
  755. }
  756. if (!npadch_active) {
  757. npadch_value = 0;
  758. npadch_active = true;
  759. }
  760. npadch_value = npadch_value * base + value;
  761. }
  762. static void k_lock(struct vc_data *vc, unsigned char value, char up_flag)
  763. {
  764. if (up_flag || rep)
  765. return;
  766. chg_vc_kbd_lock(kbd, value);
  767. }
  768. static void k_slock(struct vc_data *vc, unsigned char value, char up_flag)
  769. {
  770. k_shift(vc, value, up_flag);
  771. if (up_flag || rep)
  772. return;
  773. chg_vc_kbd_slock(kbd, value);
  774. /* try to make Alt, oops, AltGr and such work */
  775. if (!key_maps[kbd->lockstate ^ kbd->slockstate]) {
  776. kbd->slockstate = 0;
  777. chg_vc_kbd_slock(kbd, value);
  778. }
  779. }
  780. /* by default, 300ms interval for combination release */
  781. static unsigned brl_timeout = 300;
  782. MODULE_PARM_DESC(brl_timeout, "Braille keys release delay in ms (0 for commit on first key release)");
  783. module_param(brl_timeout, uint, 0644);
  784. static unsigned brl_nbchords = 1;
  785. MODULE_PARM_DESC(brl_nbchords, "Number of chords that produce a braille pattern (0 for dead chords)");
  786. module_param(brl_nbchords, uint, 0644);
  787. static void k_brlcommit(struct vc_data *vc, unsigned int pattern, char up_flag)
  788. {
  789. static unsigned long chords;
  790. static unsigned committed;
  791. if (!brl_nbchords)
  792. k_deadunicode(vc, BRL_UC_ROW | pattern, up_flag);
  793. else {
  794. committed |= pattern;
  795. chords++;
  796. if (chords == brl_nbchords) {
  797. k_unicode(vc, BRL_UC_ROW | committed, up_flag);
  798. chords = 0;
  799. committed = 0;
  800. }
  801. }
  802. }
  803. static void k_brl(struct vc_data *vc, unsigned char value, char up_flag)
  804. {
  805. static unsigned pressed, committing;
  806. static unsigned long releasestart;
  807. if (kbd->kbdmode != VC_UNICODE) {
  808. if (!up_flag)
  809. pr_warn("keyboard mode must be unicode for braille patterns\n");
  810. return;
  811. }
  812. if (!value) {
  813. k_unicode(vc, BRL_UC_ROW, up_flag);
  814. return;
  815. }
  816. if (value > 8)
  817. return;
  818. if (!up_flag) {
  819. pressed |= BIT(value - 1);
  820. if (!brl_timeout)
  821. committing = pressed;
  822. } else if (brl_timeout) {
  823. if (!committing ||
  824. time_after(jiffies,
  825. releasestart + msecs_to_jiffies(brl_timeout))) {
  826. committing = pressed;
  827. releasestart = jiffies;
  828. }
  829. pressed &= ~BIT(value - 1);
  830. if (!pressed && committing) {
  831. k_brlcommit(vc, committing, 0);
  832. committing = 0;
  833. }
  834. } else {
  835. if (committing) {
  836. k_brlcommit(vc, committing, 0);
  837. committing = 0;
  838. }
  839. pressed &= ~BIT(value - 1);
  840. }
  841. }
  842. #if IS_ENABLED(CONFIG_INPUT_LEDS) && IS_ENABLED(CONFIG_LEDS_TRIGGERS)
  843. struct kbd_led_trigger {
  844. struct led_trigger trigger;
  845. unsigned int mask;
  846. };
  847. static int kbd_led_trigger_activate(struct led_classdev *cdev)
  848. {
  849. struct kbd_led_trigger *trigger =
  850. container_of(cdev->trigger, struct kbd_led_trigger, trigger);
  851. tasklet_disable(&keyboard_tasklet);
  852. if (ledstate != -1U)
  853. led_set_brightness(cdev, ledstate & trigger->mask ? LED_FULL : LED_OFF);
  854. tasklet_enable(&keyboard_tasklet);
  855. return 0;
  856. }
  857. #define KBD_LED_TRIGGER(_led_bit, _name) { \
  858. .trigger = { \
  859. .name = _name, \
  860. .activate = kbd_led_trigger_activate, \
  861. }, \
  862. .mask = BIT(_led_bit), \
  863. }
  864. #define KBD_LOCKSTATE_TRIGGER(_led_bit, _name) \
  865. KBD_LED_TRIGGER((_led_bit) + 8, _name)
  866. static struct kbd_led_trigger kbd_led_triggers[] = {
  867. KBD_LED_TRIGGER(VC_SCROLLOCK, "kbd-scrolllock"),
  868. KBD_LED_TRIGGER(VC_NUMLOCK, "kbd-numlock"),
  869. KBD_LED_TRIGGER(VC_CAPSLOCK, "kbd-capslock"),
  870. KBD_LED_TRIGGER(VC_KANALOCK, "kbd-kanalock"),
  871. KBD_LOCKSTATE_TRIGGER(VC_SHIFTLOCK, "kbd-shiftlock"),
  872. KBD_LOCKSTATE_TRIGGER(VC_ALTGRLOCK, "kbd-altgrlock"),
  873. KBD_LOCKSTATE_TRIGGER(VC_CTRLLOCK, "kbd-ctrllock"),
  874. KBD_LOCKSTATE_TRIGGER(VC_ALTLOCK, "kbd-altlock"),
  875. KBD_LOCKSTATE_TRIGGER(VC_SHIFTLLOCK, "kbd-shiftllock"),
  876. KBD_LOCKSTATE_TRIGGER(VC_SHIFTRLOCK, "kbd-shiftrlock"),
  877. KBD_LOCKSTATE_TRIGGER(VC_CTRLLLOCK, "kbd-ctrlllock"),
  878. KBD_LOCKSTATE_TRIGGER(VC_CTRLRLOCK, "kbd-ctrlrlock"),
  879. };
  880. static void kbd_propagate_led_state(unsigned int old_state,
  881. unsigned int new_state)
  882. {
  883. struct kbd_led_trigger *trigger;
  884. unsigned int changed = old_state ^ new_state;
  885. int i;
  886. for (i = 0; i < ARRAY_SIZE(kbd_led_triggers); i++) {
  887. trigger = &kbd_led_triggers[i];
  888. if (changed & trigger->mask)
  889. led_trigger_event(&trigger->trigger,
  890. new_state & trigger->mask ?
  891. LED_FULL : LED_OFF);
  892. }
  893. }
  894. static int kbd_update_leds_helper(struct input_handle *handle, void *data)
  895. {
  896. unsigned int led_state = *(unsigned int *)data;
  897. if (test_bit(EV_LED, handle->dev->evbit))
  898. kbd_propagate_led_state(~led_state, led_state);
  899. return 0;
  900. }
  901. static void kbd_init_leds(void)
  902. {
  903. int error;
  904. int i;
  905. for (i = 0; i < ARRAY_SIZE(kbd_led_triggers); i++) {
  906. error = led_trigger_register(&kbd_led_triggers[i].trigger);
  907. if (error)
  908. pr_err("error %d while registering trigger %s\n",
  909. error, kbd_led_triggers[i].trigger.name);
  910. }
  911. }
  912. #else
  913. static int kbd_update_leds_helper(struct input_handle *handle, void *data)
  914. {
  915. unsigned int leds = *(unsigned int *)data;
  916. if (test_bit(EV_LED, handle->dev->evbit)) {
  917. input_inject_event(handle, EV_LED, LED_SCROLLL, !!(leds & BIT(0)));
  918. input_inject_event(handle, EV_LED, LED_NUML, !!(leds & BIT(1)));
  919. input_inject_event(handle, EV_LED, LED_CAPSL, !!(leds & BIT(2)));
  920. input_inject_event(handle, EV_SYN, SYN_REPORT, 0);
  921. }
  922. return 0;
  923. }
  924. static void kbd_propagate_led_state(unsigned int old_state,
  925. unsigned int new_state)
  926. {
  927. input_handler_for_each_handle(&kbd_handler, &new_state,
  928. kbd_update_leds_helper);
  929. }
  930. static void kbd_init_leds(void)
  931. {
  932. }
  933. #endif
  934. /*
  935. * The leds display either (i) the status of NumLock, CapsLock, ScrollLock,
  936. * or (ii) whatever pattern of lights people want to show using KDSETLED,
  937. * or (iii) specified bits of specified words in kernel memory.
  938. */
  939. static unsigned char getledstate(void)
  940. {
  941. return ledstate & 0xff;
  942. }
  943. void setledstate(struct kbd_struct *kb, unsigned int led)
  944. {
  945. guard(spinlock_irqsave)(&led_lock);
  946. if (!(led & ~7)) {
  947. ledioctl = led;
  948. kb->ledmode = LED_SHOW_IOCTL;
  949. } else
  950. kb->ledmode = LED_SHOW_FLAGS;
  951. set_leds();
  952. }
  953. static inline unsigned char getleds(void)
  954. {
  955. struct kbd_struct *kb = kbd_table + fg_console;
  956. if (kb->ledmode == LED_SHOW_IOCTL)
  957. return ledioctl;
  958. return kb->ledflagstate;
  959. }
  960. /**
  961. * vt_get_leds - helper for braille console
  962. * @console: console to read
  963. * @flag: flag we want to check
  964. *
  965. * Check the status of a keyboard led flag and report it back
  966. */
  967. int vt_get_leds(unsigned int console, int flag)
  968. {
  969. struct kbd_struct *kb = &kbd_table[console];
  970. guard(spinlock_irqsave)(&led_lock);
  971. return vc_kbd_led(kb, flag);
  972. }
  973. EXPORT_SYMBOL_GPL(vt_get_leds);
  974. /**
  975. * vt_set_led_state - set LED state of a console
  976. * @console: console to set
  977. * @leds: LED bits
  978. *
  979. * Set the LEDs on a console. This is a wrapper for the VT layer
  980. * so that we can keep kbd knowledge internal
  981. */
  982. void vt_set_led_state(unsigned int console, int leds)
  983. {
  984. struct kbd_struct *kb = &kbd_table[console];
  985. setledstate(kb, leds);
  986. }
  987. /**
  988. * vt_kbd_con_start - Keyboard side of console start
  989. * @console: console
  990. *
  991. * Handle console start. This is a wrapper for the VT layer
  992. * so that we can keep kbd knowledge internal
  993. *
  994. * FIXME: We eventually need to hold the kbd lock here to protect
  995. * the LED updating. We can't do it yet because fn_hold calls stop_tty
  996. * and start_tty under the kbd_event_lock, while normal tty paths
  997. * don't hold the lock. We probably need to split out an LED lock
  998. * but not during an -rc release!
  999. */
  1000. void vt_kbd_con_start(unsigned int console)
  1001. {
  1002. struct kbd_struct *kb = &kbd_table[console];
  1003. guard(spinlock_irqsave)(&led_lock);
  1004. clr_vc_kbd_led(kb, VC_SCROLLOCK);
  1005. set_leds();
  1006. }
  1007. /**
  1008. * vt_kbd_con_stop - Keyboard side of console stop
  1009. * @console: console
  1010. *
  1011. * Handle console stop. This is a wrapper for the VT layer
  1012. * so that we can keep kbd knowledge internal
  1013. */
  1014. void vt_kbd_con_stop(unsigned int console)
  1015. {
  1016. struct kbd_struct *kb = &kbd_table[console];
  1017. guard(spinlock_irqsave)(&led_lock);
  1018. set_vc_kbd_led(kb, VC_SCROLLOCK);
  1019. set_leds();
  1020. }
  1021. /*
  1022. * This is the tasklet that updates LED state of LEDs using standard
  1023. * keyboard triggers. The reason we use tasklet is that we need to
  1024. * handle the scenario when keyboard handler is not registered yet
  1025. * but we already getting updates from the VT to update led state.
  1026. */
  1027. static void kbd_bh(struct tasklet_struct *unused)
  1028. {
  1029. unsigned int leds;
  1030. scoped_guard(spinlock_irqsave, &led_lock) {
  1031. leds = getleds();
  1032. leds |= (unsigned int)kbd->lockstate << 8;
  1033. }
  1034. if (vt_switch) {
  1035. ledstate = ~leds;
  1036. vt_switch = false;
  1037. }
  1038. if (leds != ledstate) {
  1039. kbd_propagate_led_state(ledstate, leds);
  1040. ledstate = leds;
  1041. }
  1042. }
  1043. #if defined(CONFIG_X86) || defined(CONFIG_ALPHA) ||\
  1044. defined(CONFIG_MIPS) || defined(CONFIG_PPC) || defined(CONFIG_SPARC) ||\
  1045. defined(CONFIG_PARISC) || defined(CONFIG_SUPERH) ||\
  1046. (defined(CONFIG_ARM) && defined(CONFIG_KEYBOARD_ATKBD) && !defined(CONFIG_ARCH_RPC))
  1047. static inline bool kbd_is_hw_raw(const struct input_dev *dev)
  1048. {
  1049. if (!test_bit(EV_MSC, dev->evbit) || !test_bit(MSC_RAW, dev->mscbit))
  1050. return false;
  1051. return dev->id.bustype == BUS_I8042 &&
  1052. dev->id.vendor == 0x0001 && dev->id.product == 0x0001;
  1053. }
  1054. static const unsigned short x86_keycodes[256] =
  1055. { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
  1056. 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
  1057. 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
  1058. 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
  1059. 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
  1060. 80, 81, 82, 83, 84,118, 86, 87, 88,115,120,119,121,112,123, 92,
  1061. 284,285,309, 0,312, 91,327,328,329,331,333,335,336,337,338,339,
  1062. 367,288,302,304,350, 89,334,326,267,126,268,269,125,347,348,349,
  1063. 360,261,262,263,268,376,100,101,321,316,373,286,289,102,351,355,
  1064. 103,104,105,275,287,279,258,106,274,107,294,364,358,363,362,361,
  1065. 291,108,381,281,290,272,292,305,280, 99,112,257,306,359,113,114,
  1066. 264,117,271,374,379,265,266, 93, 94, 95, 85,259,375,260, 90,116,
  1067. 377,109,111,277,278,282,283,295,296,297,299,300,301,293,303,307,
  1068. 308,310,313,314,315,317,318,319,320,357,322,323,324,325,276,330,
  1069. 332,340,365,342,343,344,345,346,356,270,341,368,369,370,371,372 };
  1070. #ifdef CONFIG_SPARC
  1071. static int sparc_l1_a_state;
  1072. extern void sun_do_break(void);
  1073. #endif
  1074. static int emulate_raw(struct vc_data *vc, unsigned int keycode,
  1075. unsigned char up_flag)
  1076. {
  1077. int code;
  1078. switch (keycode) {
  1079. case KEY_PAUSE:
  1080. put_queue(vc, 0xe1);
  1081. put_queue(vc, 0x1d | up_flag);
  1082. put_queue(vc, 0x45 | up_flag);
  1083. break;
  1084. case KEY_HANGEUL:
  1085. if (!up_flag)
  1086. put_queue(vc, 0xf2);
  1087. break;
  1088. case KEY_HANJA:
  1089. if (!up_flag)
  1090. put_queue(vc, 0xf1);
  1091. break;
  1092. case KEY_SYSRQ:
  1093. /*
  1094. * Real AT keyboards (that's what we're trying
  1095. * to emulate here) emit 0xe0 0x2a 0xe0 0x37 when
  1096. * pressing PrtSc/SysRq alone, but simply 0x54
  1097. * when pressing Alt+PrtSc/SysRq.
  1098. */
  1099. if (test_bit(KEY_LEFTALT, key_down) ||
  1100. test_bit(KEY_RIGHTALT, key_down)) {
  1101. put_queue(vc, 0x54 | up_flag);
  1102. } else {
  1103. put_queue(vc, 0xe0);
  1104. put_queue(vc, 0x2a | up_flag);
  1105. put_queue(vc, 0xe0);
  1106. put_queue(vc, 0x37 | up_flag);
  1107. }
  1108. break;
  1109. default:
  1110. if (keycode > 255)
  1111. return -1;
  1112. code = x86_keycodes[keycode];
  1113. if (!code)
  1114. return -1;
  1115. if (code & 0x100)
  1116. put_queue(vc, 0xe0);
  1117. put_queue(vc, (code & 0x7f) | up_flag);
  1118. break;
  1119. }
  1120. return 0;
  1121. }
  1122. #else
  1123. static inline bool kbd_is_hw_raw(const struct input_dev *dev)
  1124. {
  1125. return false;
  1126. }
  1127. static int emulate_raw(struct vc_data *vc, unsigned int keycode, unsigned char up_flag)
  1128. {
  1129. if (keycode > 127)
  1130. return -1;
  1131. put_queue(vc, keycode | up_flag);
  1132. return 0;
  1133. }
  1134. #endif
  1135. static void kbd_rawcode(unsigned char data)
  1136. {
  1137. struct vc_data *vc = vc_cons[fg_console].d;
  1138. kbd = &kbd_table[vc->vc_num];
  1139. if (kbd->kbdmode == VC_RAW)
  1140. put_queue(vc, data);
  1141. }
  1142. static void kbd_keycode(unsigned int keycode, int down, bool hw_raw)
  1143. {
  1144. struct vc_data *vc = vc_cons[fg_console].d;
  1145. unsigned short keysym, *key_map;
  1146. unsigned char type;
  1147. bool raw_mode;
  1148. struct tty_struct *tty;
  1149. int shift_final;
  1150. struct keyboard_notifier_param param = { .vc = vc, .value = keycode, .down = down };
  1151. int rc;
  1152. tty = vc->port.tty;
  1153. if (tty && (!tty->driver_data)) {
  1154. /* No driver data? Strange. Okay we fix it then. */
  1155. tty->driver_data = vc;
  1156. }
  1157. kbd = &kbd_table[vc->vc_num];
  1158. #ifdef CONFIG_SPARC
  1159. if (keycode == KEY_STOP)
  1160. sparc_l1_a_state = down;
  1161. #endif
  1162. rep = (down == 2);
  1163. raw_mode = (kbd->kbdmode == VC_RAW);
  1164. if (raw_mode && !hw_raw)
  1165. if (emulate_raw(vc, keycode, !down << 7))
  1166. if (keycode < BTN_MISC && printk_ratelimit())
  1167. pr_warn("can't emulate rawmode for keycode %d\n",
  1168. keycode);
  1169. #ifdef CONFIG_SPARC
  1170. if (keycode == KEY_A && sparc_l1_a_state) {
  1171. sparc_l1_a_state = false;
  1172. sun_do_break();
  1173. }
  1174. #endif
  1175. if (kbd->kbdmode == VC_MEDIUMRAW) {
  1176. /*
  1177. * This is extended medium raw mode, with keys above 127
  1178. * encoded as 0, high 7 bits, low 7 bits, with the 0 bearing
  1179. * the 'up' flag if needed. 0 is reserved, so this shouldn't
  1180. * interfere with anything else. The two bytes after 0 will
  1181. * always have the up flag set not to interfere with older
  1182. * applications. This allows for 16384 different keycodes,
  1183. * which should be enough.
  1184. */
  1185. if (keycode < 128) {
  1186. put_queue(vc, keycode | (!down << 7));
  1187. } else {
  1188. put_queue(vc, !down << 7);
  1189. put_queue(vc, (keycode >> 7) | BIT(7));
  1190. put_queue(vc, keycode | BIT(7));
  1191. }
  1192. raw_mode = true;
  1193. }
  1194. assign_bit(keycode, key_down, down);
  1195. if (rep &&
  1196. (!vc_kbd_mode(kbd, VC_REPEAT) ||
  1197. (tty && !L_ECHO(tty) && tty_chars_in_buffer(tty)))) {
  1198. /*
  1199. * Don't repeat a key if the input buffers are not empty and the
  1200. * characters get aren't echoed locally. This makes key repeat
  1201. * usable with slow applications and under heavy loads.
  1202. */
  1203. return;
  1204. }
  1205. param.shift = shift_final = (shift_state | kbd->slockstate) ^ kbd->lockstate;
  1206. param.ledstate = kbd->ledflagstate;
  1207. key_map = key_maps[shift_final];
  1208. rc = atomic_notifier_call_chain(&keyboard_notifier_list,
  1209. KBD_KEYCODE, &param);
  1210. if (rc == NOTIFY_STOP || !key_map) {
  1211. atomic_notifier_call_chain(&keyboard_notifier_list,
  1212. KBD_UNBOUND_KEYCODE, &param);
  1213. do_compute_shiftstate();
  1214. kbd->slockstate = 0;
  1215. return;
  1216. }
  1217. if (keycode < NR_KEYS)
  1218. keysym = key_map[keycode];
  1219. else if (keycode >= KEY_BRL_DOT1 && keycode <= KEY_BRL_DOT8)
  1220. keysym = U(K(KT_BRL, keycode - KEY_BRL_DOT1 + 1));
  1221. else
  1222. return;
  1223. type = KTYP(keysym);
  1224. if (type < 0xf0) {
  1225. param.value = keysym;
  1226. rc = atomic_notifier_call_chain(&keyboard_notifier_list,
  1227. KBD_UNICODE, &param);
  1228. if (rc != NOTIFY_STOP)
  1229. if (down && !(raw_mode || kbd->kbdmode == VC_OFF))
  1230. k_unicode(vc, keysym, !down);
  1231. return;
  1232. }
  1233. type -= 0xf0;
  1234. if (type == KT_LETTER) {
  1235. type = KT_LATIN;
  1236. if (vc_kbd_led(kbd, VC_CAPSLOCK)) {
  1237. key_map = key_maps[shift_final ^ BIT(KG_SHIFT)];
  1238. if (key_map)
  1239. keysym = key_map[keycode];
  1240. }
  1241. }
  1242. param.value = keysym;
  1243. rc = atomic_notifier_call_chain(&keyboard_notifier_list,
  1244. KBD_KEYSYM, &param);
  1245. if (rc == NOTIFY_STOP)
  1246. return;
  1247. if ((raw_mode || kbd->kbdmode == VC_OFF) && type != KT_SPEC && type != KT_SHIFT)
  1248. return;
  1249. (*k_handler[type])(vc, KVAL(keysym), !down);
  1250. param.ledstate = kbd->ledflagstate;
  1251. atomic_notifier_call_chain(&keyboard_notifier_list, KBD_POST_KEYSYM, &param);
  1252. if (type != KT_SLOCK)
  1253. kbd->slockstate = 0;
  1254. }
  1255. static void kbd_event(struct input_handle *handle, unsigned int event_type,
  1256. unsigned int event_code, int value)
  1257. {
  1258. /* We are called with interrupts disabled, just take the lock */
  1259. scoped_guard(spinlock, &kbd_event_lock) {
  1260. if (event_type == EV_MSC && event_code == MSC_RAW &&
  1261. kbd_is_hw_raw(handle->dev))
  1262. kbd_rawcode(value);
  1263. if (event_type == EV_KEY && event_code <= KEY_MAX)
  1264. kbd_keycode(event_code, value, kbd_is_hw_raw(handle->dev));
  1265. }
  1266. tasklet_schedule(&keyboard_tasklet);
  1267. do_poke_blanked_console = 1;
  1268. schedule_console_callback();
  1269. }
  1270. static bool kbd_match(struct input_handler *handler, struct input_dev *dev)
  1271. {
  1272. if (test_bit(EV_SND, dev->evbit))
  1273. return true;
  1274. if (test_bit(EV_KEY, dev->evbit)) {
  1275. if (find_next_bit(dev->keybit, BTN_MISC, KEY_RESERVED) <
  1276. BTN_MISC)
  1277. return true;
  1278. if (find_next_bit(dev->keybit, KEY_BRL_DOT10 + 1,
  1279. KEY_BRL_DOT1) <= KEY_BRL_DOT10)
  1280. return true;
  1281. }
  1282. return false;
  1283. }
  1284. /*
  1285. * When a keyboard (or other input device) is found, the kbd_connect
  1286. * function is called. The function then looks at the device, and if it
  1287. * likes it, it can open it and get events from it. In this (kbd_connect)
  1288. * function, we should decide which VT to bind that keyboard to initially.
  1289. */
  1290. static int kbd_connect(struct input_handler *handler, struct input_dev *dev,
  1291. const struct input_device_id *id)
  1292. {
  1293. int error;
  1294. struct input_handle __free(kfree) *handle = kzalloc_obj(*handle);
  1295. if (!handle)
  1296. return -ENOMEM;
  1297. handle->dev = dev;
  1298. handle->handler = handler;
  1299. handle->name = "kbd";
  1300. error = input_register_handle(handle);
  1301. if (error)
  1302. return error;
  1303. error = input_open_device(handle);
  1304. if (error)
  1305. goto err_unregister_handle;
  1306. retain_and_null_ptr(handle);
  1307. return 0;
  1308. err_unregister_handle:
  1309. input_unregister_handle(handle);
  1310. return error;
  1311. }
  1312. static void kbd_disconnect(struct input_handle *handle)
  1313. {
  1314. input_close_device(handle);
  1315. input_unregister_handle(handle);
  1316. kfree(handle);
  1317. }
  1318. /*
  1319. * Start keyboard handler on the new keyboard by refreshing LED state to
  1320. * match the rest of the system.
  1321. */
  1322. static void kbd_start(struct input_handle *handle)
  1323. {
  1324. tasklet_disable(&keyboard_tasklet);
  1325. if (ledstate != -1U)
  1326. kbd_update_leds_helper(handle, &ledstate);
  1327. tasklet_enable(&keyboard_tasklet);
  1328. }
  1329. static const struct input_device_id kbd_ids[] = {
  1330. {
  1331. .flags = INPUT_DEVICE_ID_MATCH_EVBIT,
  1332. .evbit = { BIT_MASK(EV_KEY) },
  1333. },
  1334. {
  1335. .flags = INPUT_DEVICE_ID_MATCH_EVBIT,
  1336. .evbit = { BIT_MASK(EV_SND) },
  1337. },
  1338. { }, /* Terminating entry */
  1339. };
  1340. MODULE_DEVICE_TABLE(input, kbd_ids);
  1341. static struct input_handler kbd_handler = {
  1342. .event = kbd_event,
  1343. .match = kbd_match,
  1344. .connect = kbd_connect,
  1345. .disconnect = kbd_disconnect,
  1346. .start = kbd_start,
  1347. .name = "kbd",
  1348. .id_table = kbd_ids,
  1349. };
  1350. int __init kbd_init(void)
  1351. {
  1352. int i;
  1353. int error;
  1354. for (i = 0; i < MAX_NR_CONSOLES; i++) {
  1355. kbd_table[i].ledflagstate = kbd_defleds();
  1356. kbd_table[i].default_ledflagstate = kbd_defleds();
  1357. kbd_table[i].ledmode = LED_SHOW_FLAGS;
  1358. kbd_table[i].lockstate = KBD_DEFLOCK;
  1359. kbd_table[i].slockstate = 0;
  1360. kbd_table[i].modeflags = KBD_DEFMODE;
  1361. kbd_table[i].kbdmode = default_utf8 ? VC_UNICODE : VC_XLATE;
  1362. }
  1363. kbd_init_leds();
  1364. error = input_register_handler(&kbd_handler);
  1365. if (error)
  1366. return error;
  1367. tasklet_enable(&keyboard_tasklet);
  1368. tasklet_schedule(&keyboard_tasklet);
  1369. return 0;
  1370. }
  1371. /* Ioctl support code */
  1372. static int vt_do_kdgkbdiacr(void __user *udp)
  1373. {
  1374. struct kbdiacrs __user *a = udp;
  1375. int i, asize;
  1376. struct kbdiacr __free(kfree) *dia = kmalloc_array(MAX_DIACR, sizeof(struct kbdiacr),
  1377. GFP_KERNEL);
  1378. if (!dia)
  1379. return -ENOMEM;
  1380. /* Lock the diacriticals table, make a copy and then
  1381. copy it after we unlock */
  1382. scoped_guard(spinlock_irqsave, &kbd_event_lock) {
  1383. asize = accent_table_size;
  1384. for (i = 0; i < asize; i++) {
  1385. dia[i].diacr = conv_uni_to_8bit(accent_table[i].diacr);
  1386. dia[i].base = conv_uni_to_8bit(accent_table[i].base);
  1387. dia[i].result = conv_uni_to_8bit(accent_table[i].result);
  1388. }
  1389. }
  1390. if (put_user(asize, &a->kb_cnt))
  1391. return -EFAULT;
  1392. if (copy_to_user(a->kbdiacr, dia, asize * sizeof(struct kbdiacr)))
  1393. return -EFAULT;
  1394. return 0;
  1395. }
  1396. static int vt_do_kdgkbdiacruc(void __user *udp)
  1397. {
  1398. struct kbdiacrsuc __user *a = udp;
  1399. int asize;
  1400. void __free(kfree) *buf = kmalloc_array(MAX_DIACR, sizeof(struct kbdiacruc),
  1401. GFP_KERNEL);
  1402. if (buf == NULL)
  1403. return -ENOMEM;
  1404. /* Lock the diacriticals table, make a copy and then
  1405. copy it after we unlock */
  1406. scoped_guard(spinlock_irqsave, &kbd_event_lock) {
  1407. asize = accent_table_size;
  1408. memcpy(buf, accent_table, asize * sizeof(struct kbdiacruc));
  1409. }
  1410. if (put_user(asize, &a->kb_cnt))
  1411. return -EFAULT;
  1412. if (copy_to_user(a->kbdiacruc, buf, asize * sizeof(struct kbdiacruc)))
  1413. return -EFAULT;
  1414. return 0;
  1415. }
  1416. static int vt_do_kdskbdiacr(void __user *udp, int perm)
  1417. {
  1418. struct kbdiacrs __user *a = udp;
  1419. struct kbdiacr __free(kfree) *dia = NULL;
  1420. unsigned int ct;
  1421. int i;
  1422. if (!perm)
  1423. return -EPERM;
  1424. if (get_user(ct, &a->kb_cnt))
  1425. return -EFAULT;
  1426. if (ct >= MAX_DIACR)
  1427. return -EINVAL;
  1428. if (ct) {
  1429. dia = memdup_array_user(a->kbdiacr,
  1430. ct, sizeof(struct kbdiacr));
  1431. if (IS_ERR(dia))
  1432. return PTR_ERR(dia);
  1433. }
  1434. guard(spinlock_irqsave)(&kbd_event_lock);
  1435. accent_table_size = ct;
  1436. for (i = 0; i < ct; i++) {
  1437. accent_table[i].diacr =
  1438. conv_8bit_to_uni(dia[i].diacr);
  1439. accent_table[i].base =
  1440. conv_8bit_to_uni(dia[i].base);
  1441. accent_table[i].result =
  1442. conv_8bit_to_uni(dia[i].result);
  1443. }
  1444. return 0;
  1445. }
  1446. static int vt_do_kdskbdiacruc(void __user *udp, int perm)
  1447. {
  1448. struct kbdiacrsuc __user *a = udp;
  1449. unsigned int ct;
  1450. void __free(kfree) *buf = NULL;
  1451. if (!perm)
  1452. return -EPERM;
  1453. if (get_user(ct, &a->kb_cnt))
  1454. return -EFAULT;
  1455. if (ct >= MAX_DIACR)
  1456. return -EINVAL;
  1457. if (ct) {
  1458. buf = memdup_array_user(a->kbdiacruc,
  1459. ct, sizeof(struct kbdiacruc));
  1460. if (IS_ERR(buf))
  1461. return PTR_ERR(buf);
  1462. }
  1463. guard(spinlock_irqsave)(&kbd_event_lock);
  1464. if (ct)
  1465. memcpy(accent_table, buf,
  1466. ct * sizeof(struct kbdiacruc));
  1467. accent_table_size = ct;
  1468. return 0;
  1469. }
  1470. /**
  1471. * vt_do_diacrit - diacritical table updates
  1472. * @cmd: ioctl request
  1473. * @udp: pointer to user data for ioctl
  1474. * @perm: permissions check computed by caller
  1475. *
  1476. * Update the diacritical tables atomically and safely. Lock them
  1477. * against simultaneous keypresses
  1478. */
  1479. int vt_do_diacrit(unsigned int cmd, void __user *udp, int perm)
  1480. {
  1481. switch (cmd) {
  1482. case KDGKBDIACR:
  1483. return vt_do_kdgkbdiacr(udp);
  1484. case KDGKBDIACRUC:
  1485. return vt_do_kdgkbdiacruc(udp);
  1486. case KDSKBDIACR:
  1487. return vt_do_kdskbdiacr(udp, perm);
  1488. case KDSKBDIACRUC:
  1489. return vt_do_kdskbdiacruc(udp, perm);
  1490. }
  1491. return 0;
  1492. }
  1493. /**
  1494. * vt_do_kdskbmode - set keyboard mode ioctl
  1495. * @console: the console to use
  1496. * @arg: the requested mode
  1497. *
  1498. * Update the keyboard mode bits while holding the correct locks.
  1499. * Return 0 for success or an error code.
  1500. */
  1501. int vt_do_kdskbmode(unsigned int console, unsigned int arg)
  1502. {
  1503. struct kbd_struct *kb = &kbd_table[console];
  1504. guard(spinlock_irqsave)(&kbd_event_lock);
  1505. switch(arg) {
  1506. case K_RAW:
  1507. kb->kbdmode = VC_RAW;
  1508. return 0;
  1509. case K_MEDIUMRAW:
  1510. kb->kbdmode = VC_MEDIUMRAW;
  1511. return 0;
  1512. case K_XLATE:
  1513. kb->kbdmode = VC_XLATE;
  1514. do_compute_shiftstate();
  1515. return 0;
  1516. case K_UNICODE:
  1517. kb->kbdmode = VC_UNICODE;
  1518. do_compute_shiftstate();
  1519. return 0;
  1520. case K_OFF:
  1521. kb->kbdmode = VC_OFF;
  1522. return 0;
  1523. default:
  1524. return -EINVAL;
  1525. }
  1526. }
  1527. /**
  1528. * vt_do_kdskbmeta - set keyboard meta state
  1529. * @console: the console to use
  1530. * @arg: the requested meta state
  1531. *
  1532. * Update the keyboard meta bits while holding the correct locks.
  1533. * Return 0 for success or an error code.
  1534. */
  1535. int vt_do_kdskbmeta(unsigned int console, unsigned int arg)
  1536. {
  1537. struct kbd_struct *kb = &kbd_table[console];
  1538. guard(spinlock_irqsave)(&kbd_event_lock);
  1539. switch(arg) {
  1540. case K_METABIT:
  1541. clr_vc_kbd_mode(kb, VC_META);
  1542. return 0;
  1543. case K_ESCPREFIX:
  1544. set_vc_kbd_mode(kb, VC_META);
  1545. return 0;
  1546. default:
  1547. return -EINVAL;
  1548. }
  1549. }
  1550. int vt_do_kbkeycode_ioctl(int cmd, struct kbkeycode __user *user_kbkc, int perm)
  1551. {
  1552. struct kbkeycode tmp;
  1553. int kc;
  1554. if (copy_from_user(&tmp, user_kbkc, sizeof(struct kbkeycode)))
  1555. return -EFAULT;
  1556. switch (cmd) {
  1557. case KDGETKEYCODE:
  1558. kc = getkeycode(tmp.scancode);
  1559. if (kc < 0)
  1560. return kc;
  1561. return put_user(kc, &user_kbkc->keycode);
  1562. case KDSETKEYCODE:
  1563. if (!perm)
  1564. return -EPERM;
  1565. return setkeycode(tmp.scancode, tmp.keycode);
  1566. }
  1567. return 0;
  1568. }
  1569. static unsigned short vt_kdgkbent(unsigned char kbdmode, unsigned char idx,
  1570. unsigned char map)
  1571. {
  1572. unsigned short *key_map;
  1573. /* Ensure another thread doesn't free it under us */
  1574. guard(spinlock_irqsave)(&kbd_event_lock);
  1575. key_map = key_maps[map];
  1576. if (key_map) {
  1577. unsigned short val = U(key_map[idx]);
  1578. if (kbdmode != VC_UNICODE && KTYP(val) >= NR_TYPES)
  1579. return K_HOLE;
  1580. return val;
  1581. }
  1582. return idx ? K_HOLE : K_NOSUCHMAP;
  1583. }
  1584. static int vt_kdskbent(unsigned char kbdmode, unsigned char idx,
  1585. unsigned char map, unsigned short val)
  1586. {
  1587. unsigned short *key_map, oldval;
  1588. if (!idx && val == K_NOSUCHMAP) {
  1589. guard(spinlock_irqsave)(&kbd_event_lock);
  1590. /* deallocate map */
  1591. key_map = key_maps[map];
  1592. if (map && key_map) {
  1593. key_maps[map] = NULL;
  1594. if (key_map[0] == U(K_ALLOCATED)) {
  1595. kfree(key_map);
  1596. keymap_count--;
  1597. }
  1598. }
  1599. return 0;
  1600. }
  1601. if (KTYP(val) < NR_TYPES) {
  1602. if (KVAL(val) > max_vals[KTYP(val)])
  1603. return -EINVAL;
  1604. } else if (kbdmode != VC_UNICODE)
  1605. return -EINVAL;
  1606. /* ++Geert: non-PC keyboards may generate keycode zero */
  1607. #if !defined(__mc68000__) && !defined(__powerpc__)
  1608. /* assignment to entry 0 only tests validity of args */
  1609. if (!idx)
  1610. return 0;
  1611. #endif
  1612. unsigned short __free(kfree) *new_map = kmalloc(sizeof(plain_map), GFP_KERNEL);
  1613. if (!new_map)
  1614. return -ENOMEM;
  1615. guard(spinlock_irqsave)(&kbd_event_lock);
  1616. key_map = key_maps[map];
  1617. if (key_map == NULL) {
  1618. int j;
  1619. if (keymap_count >= MAX_NR_OF_USER_KEYMAPS && !capable(CAP_SYS_RESOURCE))
  1620. return -EPERM;
  1621. key_map = key_maps[map] = no_free_ptr(new_map);
  1622. key_map[0] = U(K_ALLOCATED);
  1623. for (j = 1; j < NR_KEYS; j++)
  1624. key_map[j] = U(K_HOLE);
  1625. keymap_count++;
  1626. }
  1627. oldval = U(key_map[idx]);
  1628. if (val == oldval)
  1629. return 0;
  1630. /* Attention Key */
  1631. if ((oldval == K_SAK || val == K_SAK) && !capable(CAP_SYS_ADMIN))
  1632. return -EPERM;
  1633. key_map[idx] = U(val);
  1634. if (!map && (KTYP(oldval) == KT_SHIFT || KTYP(val) == KT_SHIFT))
  1635. do_compute_shiftstate();
  1636. return 0;
  1637. }
  1638. int vt_do_kdsk_ioctl(int cmd, struct kbentry __user *user_kbe, int perm,
  1639. unsigned int console)
  1640. {
  1641. struct kbd_struct *kb = &kbd_table[console];
  1642. struct kbentry kbe;
  1643. if (copy_from_user(&kbe, user_kbe, sizeof(struct kbentry)))
  1644. return -EFAULT;
  1645. switch (cmd) {
  1646. case KDGKBENT:
  1647. return put_user(vt_kdgkbent(kb->kbdmode, kbe.kb_index,
  1648. kbe.kb_table),
  1649. &user_kbe->kb_value);
  1650. case KDSKBENT:
  1651. if (!perm || !capable(CAP_SYS_TTY_CONFIG))
  1652. return -EPERM;
  1653. return vt_kdskbent(kb->kbdmode, kbe.kb_index, kbe.kb_table,
  1654. kbe.kb_value);
  1655. }
  1656. return 0;
  1657. }
  1658. static char *vt_kdskbsent(char *kbs, unsigned char cur)
  1659. {
  1660. static DECLARE_BITMAP(is_kmalloc, MAX_NR_FUNC);
  1661. char *cur_f = func_table[cur];
  1662. if (cur_f && strlen(cur_f) >= strlen(kbs)) {
  1663. strcpy(cur_f, kbs);
  1664. return kbs;
  1665. }
  1666. func_table[cur] = kbs;
  1667. return __test_and_set_bit(cur, is_kmalloc) ? cur_f : NULL;
  1668. }
  1669. int vt_do_kdgkb_ioctl(int cmd, struct kbsentry __user *user_kdgkb, int perm)
  1670. {
  1671. unsigned char kb_func;
  1672. if (get_user(kb_func, &user_kdgkb->kb_func))
  1673. return -EFAULT;
  1674. kb_func = array_index_nospec(kb_func, MAX_NR_FUNC);
  1675. switch (cmd) {
  1676. case KDGKBSENT: {
  1677. /* size should have been a struct member */
  1678. ssize_t len = sizeof(user_kdgkb->kb_string);
  1679. char __free(kfree) *kbs = kmalloc(len, GFP_KERNEL);
  1680. if (!kbs)
  1681. return -ENOMEM;
  1682. scoped_guard(spinlock_irqsave, &func_buf_lock)
  1683. len = strscpy(kbs, func_table[kb_func] ? : "", len);
  1684. if (len < 0)
  1685. return -ENOSPC;
  1686. if (copy_to_user(user_kdgkb->kb_string, kbs, len + 1))
  1687. return -EFAULT;
  1688. return 0;
  1689. }
  1690. case KDSKBSENT:
  1691. if (!perm || !capable(CAP_SYS_TTY_CONFIG))
  1692. return -EPERM;
  1693. char __free(kfree) *kbs = strndup_user(user_kdgkb->kb_string,
  1694. sizeof(user_kdgkb->kb_string));
  1695. if (IS_ERR(kbs))
  1696. return PTR_ERR(kbs);
  1697. guard(spinlock_irqsave)(&func_buf_lock);
  1698. kbs = vt_kdskbsent(kbs, kb_func);
  1699. return 0;
  1700. }
  1701. return 0;
  1702. }
  1703. int vt_do_kdskled(unsigned int console, int cmd, unsigned long arg, int perm)
  1704. {
  1705. struct kbd_struct *kb = &kbd_table[console];
  1706. unsigned char ucval;
  1707. switch(cmd) {
  1708. /* the ioctls below read/set the flags usually shown in the leds */
  1709. /* don't use them - they will go away without warning */
  1710. case KDGKBLED:
  1711. scoped_guard(spinlock_irqsave, &kbd_event_lock)
  1712. ucval = kb->ledflagstate | (kb->default_ledflagstate << 4);
  1713. return put_user(ucval, (char __user *)arg);
  1714. case KDSKBLED:
  1715. if (!perm)
  1716. return -EPERM;
  1717. if (arg & ~0x77)
  1718. return -EINVAL;
  1719. scoped_guard(spinlock_irqsave, &led_lock) {
  1720. kb->ledflagstate = (arg & 7);
  1721. kb->default_ledflagstate = ((arg >> 4) & 7);
  1722. set_leds();
  1723. }
  1724. return 0;
  1725. /* the ioctls below only set the lights, not the functions */
  1726. /* for those, see KDGKBLED and KDSKBLED above */
  1727. case KDGETLED:
  1728. ucval = getledstate();
  1729. return put_user(ucval, (char __user *)arg);
  1730. case KDSETLED:
  1731. if (!perm)
  1732. return -EPERM;
  1733. setledstate(kb, arg);
  1734. return 0;
  1735. }
  1736. return -ENOIOCTLCMD;
  1737. }
  1738. int vt_do_kdgkbmode(unsigned int console)
  1739. {
  1740. struct kbd_struct *kb = &kbd_table[console];
  1741. /* This is a spot read so needs no locking */
  1742. switch (kb->kbdmode) {
  1743. case VC_RAW:
  1744. return K_RAW;
  1745. case VC_MEDIUMRAW:
  1746. return K_MEDIUMRAW;
  1747. case VC_UNICODE:
  1748. return K_UNICODE;
  1749. case VC_OFF:
  1750. return K_OFF;
  1751. default:
  1752. return K_XLATE;
  1753. }
  1754. }
  1755. /**
  1756. * vt_do_kdgkbmeta - report meta status
  1757. * @console: console to report
  1758. *
  1759. * Report the meta flag status of this console
  1760. */
  1761. int vt_do_kdgkbmeta(unsigned int console)
  1762. {
  1763. struct kbd_struct *kb = &kbd_table[console];
  1764. /* Again a spot read so no locking */
  1765. return vc_kbd_mode(kb, VC_META) ? K_ESCPREFIX : K_METABIT;
  1766. }
  1767. /**
  1768. * vt_reset_unicode - reset the unicode status
  1769. * @console: console being reset
  1770. *
  1771. * Restore the unicode console state to its default
  1772. */
  1773. void vt_reset_unicode(unsigned int console)
  1774. {
  1775. guard(spinlock_irqsave)(&kbd_event_lock);
  1776. kbd_table[console].kbdmode = default_utf8 ? VC_UNICODE : VC_XLATE;
  1777. }
  1778. /**
  1779. * vt_get_shift_state - shift bit state
  1780. *
  1781. * Report the shift bits from the keyboard state. We have to export
  1782. * this to support some oddities in the vt layer.
  1783. */
  1784. int vt_get_shift_state(void)
  1785. {
  1786. /* Don't lock as this is a transient report */
  1787. return shift_state;
  1788. }
  1789. /**
  1790. * vt_reset_keyboard - reset keyboard state
  1791. * @console: console to reset
  1792. *
  1793. * Reset the keyboard bits for a console as part of a general console
  1794. * reset event
  1795. */
  1796. void vt_reset_keyboard(unsigned int console)
  1797. {
  1798. struct kbd_struct *kb = &kbd_table[console];
  1799. guard(spinlock_irqsave)(&kbd_event_lock);
  1800. set_vc_kbd_mode(kb, VC_REPEAT);
  1801. clr_vc_kbd_mode(kb, VC_CKMODE);
  1802. clr_vc_kbd_mode(kb, VC_APPLIC);
  1803. clr_vc_kbd_mode(kb, VC_CRLF);
  1804. kb->lockstate = 0;
  1805. kb->slockstate = 0;
  1806. guard(spinlock)(&led_lock);
  1807. kb->ledmode = LED_SHOW_FLAGS;
  1808. kb->ledflagstate = kb->default_ledflagstate;
  1809. /* do not do set_leds here because this causes an endless tasklet loop
  1810. when the keyboard hasn't been initialized yet */
  1811. }
  1812. /**
  1813. * vt_get_kbd_mode_bit - read keyboard status bits
  1814. * @console: console to read from
  1815. * @bit: mode bit to read
  1816. *
  1817. * Report back a vt mode bit. We do this without locking so the
  1818. * caller must be sure that there are no synchronization needs
  1819. */
  1820. int vt_get_kbd_mode_bit(unsigned int console, int bit)
  1821. {
  1822. struct kbd_struct *kb = &kbd_table[console];
  1823. return vc_kbd_mode(kb, bit);
  1824. }
  1825. /**
  1826. * vt_set_kbd_mode_bit - read keyboard status bits
  1827. * @console: console to read from
  1828. * @bit: mode bit to read
  1829. *
  1830. * Set a vt mode bit. We do this without locking so the
  1831. * caller must be sure that there are no synchronization needs
  1832. */
  1833. void vt_set_kbd_mode_bit(unsigned int console, int bit)
  1834. {
  1835. struct kbd_struct *kb = &kbd_table[console];
  1836. guard(spinlock_irqsave)(&kbd_event_lock);
  1837. set_vc_kbd_mode(kb, bit);
  1838. }
  1839. /**
  1840. * vt_clr_kbd_mode_bit - read keyboard status bits
  1841. * @console: console to read from
  1842. * @bit: mode bit to read
  1843. *
  1844. * Report back a vt mode bit. We do this without locking so the
  1845. * caller must be sure that there are no synchronization needs
  1846. */
  1847. void vt_clr_kbd_mode_bit(unsigned int console, int bit)
  1848. {
  1849. struct kbd_struct *kb = &kbd_table[console];
  1850. guard(spinlock_irqsave)(&kbd_event_lock);
  1851. clr_vc_kbd_mode(kb, bit);
  1852. }