keyboard.c 50 KB

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