keyboard.c 53 KB

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