keyboard.c 53 KB

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