keyboard.c 52 KB

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