keyboard.c 52 KB

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