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