keyboard.c 50 KB

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