keyboard.c 52 KB

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