keyboard.c 53 KB

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