keyboard.c 50 KB

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