keyboard.c 50 KB

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