keyboard.c 53 KB

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