keyboard.c 50 KB

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