keyboard.c 52 KB

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