keyboard.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445
  1. /*
  2. * linux/drivers/char/keyboard.c
  3. *
  4. * Written for linux by Johan Myreen as a translation from
  5. * the assembly version by Linus (with diacriticals added)
  6. *
  7. * Some additional features added by Christoph Niemann (ChN), March 1993
  8. *
  9. * Loadable keymaps by Risto Kankkunen, May 1993
  10. *
  11. * Diacriticals redone & other small changes, aeb@cwi.nl, June 1993
  12. * Added decr/incr_console, dynamic keymaps, Unicode support,
  13. * dynamic function/string keys, led setting, Sept 1994
  14. * `Sticky' modifier keys, 951006.
  15. *
  16. * 11-11-96: SAK should now work in the raw mode (Martin Mares)
  17. *
  18. * Modified to provide 'generic' keyboard support by Hamish Macdonald
  19. * Merge with the m68k keyboard driver and split-off of the PC low-level
  20. * parts by Geert Uytterhoeven, May 1997
  21. *
  22. * 27-05-97: Added support for the Magic SysRq Key (Martin Mares)
  23. * 30-07-98: Dead keys redone, aeb@cwi.nl.
  24. * 21-08-02: Converted to input API, major cleanup. (Vojtech Pavlik)
  25. */
  26. #include <linux/consolemap.h>
  27. #include <linux/module.h>
  28. #include <linux/sched.h>
  29. #include <linux/tty.h>
  30. #include <linux/tty_flip.h>
  31. #include <linux/mm.h>
  32. #include <linux/string.h>
  33. #include <linux/init.h>
  34. #include <linux/slab.h>
  35. #include <linux/irq.h>
  36. #include <linux/kbd_kern.h>
  37. #include <linux/kbd_diacr.h>
  38. #include <linux/vt_kern.h>
  39. #include <linux/sysrq.h>
  40. #include <linux/input.h>
  41. #include <linux/reboot.h>
  42. #include <linux/notifier.h>
  43. #include <linux/jiffies.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. /*
  50. * Some laptops take the 789uiojklm,. keys as number pad when NumLock is on.
  51. * This seems a good reason to start with NumLock off. On HIL keyboards
  52. * of PARISC machines however there is no NumLock key and everyone expects the keypad
  53. * to be used for numbers.
  54. */
  55. #if defined(CONFIG_PARISC) && (defined(CONFIG_KEYBOARD_HIL) || defined(CONFIG_KEYBOARD_HIL_OLD))
  56. #define KBD_DEFLEDS (1 << VC_NUMLOCK)
  57. #else
  58. #define KBD_DEFLEDS 0
  59. #endif
  60. #define KBD_DEFLOCK 0
  61. void compute_shiftstate(void);
  62. /*
  63. * Handler Tables.
  64. */
  65. #define K_HANDLERS\
  66. k_self, k_fn, k_spec, k_pad,\
  67. k_dead, k_cons, k_cur, k_shift,\
  68. k_meta, k_ascii, k_lock, k_lowercase,\
  69. k_slock, k_dead2, k_brl, k_ignore
  70. typedef void (k_handler_fn)(struct vc_data *vc, unsigned char value,
  71. char up_flag);
  72. static k_handler_fn K_HANDLERS;
  73. k_handler_fn *k_handler[16] = { K_HANDLERS };
  74. EXPORT_SYMBOL_GPL(k_handler);
  75. #define FN_HANDLERS\
  76. fn_null, fn_enter, fn_show_ptregs, fn_show_mem,\
  77. fn_show_state, fn_send_intr, fn_lastcons, fn_caps_toggle,\
  78. fn_num, fn_hold, fn_scroll_forw, fn_scroll_back,\
  79. fn_boot_it, fn_caps_on, fn_compose, fn_SAK,\
  80. fn_dec_console, fn_inc_console, fn_spawn_con, fn_bare_num
  81. typedef void (fn_handler_fn)(struct vc_data *vc);
  82. static fn_handler_fn FN_HANDLERS;
  83. static fn_handler_fn *fn_handler[] = { FN_HANDLERS };
  84. /*
  85. * Variables exported for vt_ioctl.c
  86. */
  87. /* maximum values each key_handler can handle */
  88. const int max_vals[] = {
  89. 255, ARRAY_SIZE(func_table) - 1, ARRAY_SIZE(fn_handler) - 1, NR_PAD - 1,
  90. NR_DEAD - 1, 255, 3, NR_SHIFT - 1, 255, NR_ASCII - 1, NR_LOCK - 1,
  91. 255, NR_LOCK - 1, 255, NR_BRL - 1
  92. };
  93. const int NR_TYPES = ARRAY_SIZE(max_vals);
  94. struct kbd_struct kbd_table[MAX_NR_CONSOLES];
  95. EXPORT_SYMBOL_GPL(kbd_table);
  96. static struct kbd_struct *kbd = kbd_table;
  97. struct vt_spawn_console vt_spawn_con = {
  98. .lock = __SPIN_LOCK_UNLOCKED(vt_spawn_con.lock),
  99. .pid = NULL,
  100. .sig = 0,
  101. };
  102. /*
  103. * Variables exported for vt.c
  104. */
  105. int shift_state = 0;
  106. /*
  107. * Internal Data.
  108. */
  109. static struct input_handler kbd_handler;
  110. static DEFINE_SPINLOCK(kbd_event_lock);
  111. static unsigned long key_down[BITS_TO_LONGS(KEY_CNT)]; /* keyboard key bitmap */
  112. static unsigned char shift_down[NR_SHIFT]; /* shift state counters.. */
  113. static int dead_key_next;
  114. static int npadch = -1; /* -1 or number assembled on pad */
  115. static unsigned int diacr;
  116. static char rep; /* flag telling character repeat */
  117. static unsigned char ledstate = 0xff; /* undefined */
  118. static unsigned char ledioctl;
  119. static struct ledptr {
  120. unsigned int *addr;
  121. unsigned int mask;
  122. unsigned char valid:1;
  123. } ledptrs[3];
  124. /* Simple translation table for the SysRq keys */
  125. #ifdef CONFIG_MAGIC_SYSRQ
  126. unsigned char kbd_sysrq_xlate[KEY_MAX + 1] =
  127. "\000\0331234567890-=\177\t" /* 0x00 - 0x0f */
  128. "qwertyuiop[]\r\000as" /* 0x10 - 0x1f */
  129. "dfghjkl;'`\000\\zxcv" /* 0x20 - 0x2f */
  130. "bnm,./\000*\000 \000\201\202\203\204\205" /* 0x30 - 0x3f */
  131. "\206\207\210\211\212\000\000789-456+1" /* 0x40 - 0x4f */
  132. "230\177\000\000\213\214\000\000\000\000\000\000\000\000\000\000" /* 0x50 - 0x5f */
  133. "\r\000/"; /* 0x60 - 0x6f */
  134. static int sysrq_down;
  135. static int sysrq_alt_use;
  136. #endif
  137. static int sysrq_alt;
  138. /*
  139. * Notifier list for console keyboard events
  140. */
  141. static ATOMIC_NOTIFIER_HEAD(keyboard_notifier_list);
  142. int register_keyboard_notifier(struct notifier_block *nb)
  143. {
  144. return atomic_notifier_chain_register(&keyboard_notifier_list, nb);
  145. }
  146. EXPORT_SYMBOL_GPL(register_keyboard_notifier);
  147. int unregister_keyboard_notifier(struct notifier_block *nb)
  148. {
  149. return atomic_notifier_chain_unregister(&keyboard_notifier_list, nb);
  150. }
  151. EXPORT_SYMBOL_GPL(unregister_keyboard_notifier);
  152. /*
  153. * Translation of scancodes to keycodes. We set them on only the first
  154. * keyboard in the list that accepts the scancode and keycode.
  155. * Explanation for not choosing the first attached keyboard anymore:
  156. * USB keyboards for example have two event devices: one for all "normal"
  157. * keys and one for extra function keys (like "volume up", "make coffee",
  158. * etc.). So this means that scancodes for the extra function keys won't
  159. * be valid for the first event device, but will be for the second.
  160. */
  161. struct getset_keycode_data {
  162. unsigned int scancode;
  163. unsigned int keycode;
  164. int error;
  165. };
  166. static int getkeycode_helper(struct input_handle *handle, void *data)
  167. {
  168. struct getset_keycode_data *d = data;
  169. d->error = input_get_keycode(handle->dev, d->scancode, &d->keycode);
  170. return d->error == 0; /* stop as soon as we successfully get one */
  171. }
  172. int getkeycode(unsigned int scancode)
  173. {
  174. struct getset_keycode_data d = { scancode, 0, -ENODEV };
  175. input_handler_for_each_handle(&kbd_handler, &d, getkeycode_helper);
  176. return d.error ?: d.keycode;
  177. }
  178. static int setkeycode_helper(struct input_handle *handle, void *data)
  179. {
  180. struct getset_keycode_data *d = data;
  181. d->error = input_set_keycode(handle->dev, d->scancode, d->keycode);
  182. return d->error == 0; /* stop as soon as we successfully set one */
  183. }
  184. int setkeycode(unsigned int scancode, unsigned int keycode)
  185. {
  186. struct getset_keycode_data d = { scancode, keycode, -ENODEV };
  187. input_handler_for_each_handle(&kbd_handler, &d, setkeycode_helper);
  188. return d.error;
  189. }
  190. /*
  191. * Making beeps and bells. Note that we prefer beeps to bells, but when
  192. * shutting the sound off we do both.
  193. */
  194. static int kd_sound_helper(struct input_handle *handle, void *data)
  195. {
  196. unsigned int *hz = data;
  197. struct input_dev *dev = handle->dev;
  198. if (test_bit(EV_SND, dev->evbit)) {
  199. if (test_bit(SND_TONE, dev->sndbit)) {
  200. input_inject_event(handle, EV_SND, SND_TONE, *hz);
  201. if (*hz)
  202. return 0;
  203. }
  204. if (test_bit(SND_BELL, dev->sndbit))
  205. input_inject_event(handle, EV_SND, SND_BELL, *hz ? 1 : 0);
  206. }
  207. return 0;
  208. }
  209. static void kd_nosound(unsigned long ignored)
  210. {
  211. static unsigned int zero;
  212. input_handler_for_each_handle(&kbd_handler, &zero, kd_sound_helper);
  213. }
  214. static DEFINE_TIMER(kd_mksound_timer, kd_nosound, 0, 0);
  215. void kd_mksound(unsigned int hz, unsigned int ticks)
  216. {
  217. del_timer_sync(&kd_mksound_timer);
  218. input_handler_for_each_handle(&kbd_handler, &hz, kd_sound_helper);
  219. if (hz && ticks)
  220. mod_timer(&kd_mksound_timer, jiffies + ticks);
  221. }
  222. EXPORT_SYMBOL(kd_mksound);
  223. /*
  224. * Setting the keyboard rate.
  225. */
  226. static int kbd_rate_helper(struct input_handle *handle, void *data)
  227. {
  228. struct input_dev *dev = handle->dev;
  229. struct kbd_repeat *rep = data;
  230. if (test_bit(EV_REP, dev->evbit)) {
  231. if (rep[0].delay > 0)
  232. input_inject_event(handle,
  233. EV_REP, REP_DELAY, rep[0].delay);
  234. if (rep[0].period > 0)
  235. input_inject_event(handle,
  236. EV_REP, REP_PERIOD, rep[0].period);
  237. rep[1].delay = dev->rep[REP_DELAY];
  238. rep[1].period = dev->rep[REP_PERIOD];
  239. }
  240. return 0;
  241. }
  242. int kbd_rate(struct kbd_repeat *rep)
  243. {
  244. struct kbd_repeat data[2] = { *rep };
  245. input_handler_for_each_handle(&kbd_handler, data, kbd_rate_helper);
  246. *rep = data[1]; /* Copy currently used settings */
  247. return 0;
  248. }
  249. /*
  250. * Helper Functions.
  251. */
  252. static void put_queue(struct vc_data *vc, int ch)
  253. {
  254. struct tty_struct *tty = vc->vc_tty;
  255. if (tty) {
  256. tty_insert_flip_char(tty, ch, 0);
  257. con_schedule_flip(tty);
  258. }
  259. }
  260. static void puts_queue(struct vc_data *vc, char *cp)
  261. {
  262. struct tty_struct *tty = vc->vc_tty;
  263. if (!tty)
  264. return;
  265. while (*cp) {
  266. tty_insert_flip_char(tty, *cp, 0);
  267. cp++;
  268. }
  269. con_schedule_flip(tty);
  270. }
  271. static void applkey(struct vc_data *vc, int key, char mode)
  272. {
  273. static char buf[] = { 0x1b, 'O', 0x00, 0x00 };
  274. buf[1] = (mode ? 'O' : '[');
  275. buf[2] = key;
  276. puts_queue(vc, buf);
  277. }
  278. /*
  279. * Many other routines do put_queue, but I think either
  280. * they produce ASCII, or they produce some user-assigned
  281. * string, and in both cases we might assume that it is
  282. * in utf-8 already.
  283. */
  284. static void to_utf8(struct vc_data *vc, uint c)
  285. {
  286. if (c < 0x80)
  287. /* 0******* */
  288. put_queue(vc, c);
  289. else if (c < 0x800) {
  290. /* 110***** 10****** */
  291. put_queue(vc, 0xc0 | (c >> 6));
  292. put_queue(vc, 0x80 | (c & 0x3f));
  293. } else if (c < 0x10000) {
  294. if (c >= 0xD800 && c < 0xE000)
  295. return;
  296. if (c == 0xFFFF)
  297. return;
  298. /* 1110**** 10****** 10****** */
  299. put_queue(vc, 0xe0 | (c >> 12));
  300. put_queue(vc, 0x80 | ((c >> 6) & 0x3f));
  301. put_queue(vc, 0x80 | (c & 0x3f));
  302. } else if (c < 0x110000) {
  303. /* 11110*** 10****** 10****** 10****** */
  304. put_queue(vc, 0xf0 | (c >> 18));
  305. put_queue(vc, 0x80 | ((c >> 12) & 0x3f));
  306. put_queue(vc, 0x80 | ((c >> 6) & 0x3f));
  307. put_queue(vc, 0x80 | (c & 0x3f));
  308. }
  309. }
  310. /*
  311. * Called after returning from RAW mode or when changing consoles - recompute
  312. * shift_down[] and shift_state from key_down[] maybe called when keymap is
  313. * undefined, so that shiftkey release is seen
  314. */
  315. void compute_shiftstate(void)
  316. {
  317. unsigned int i, j, k, sym, val;
  318. shift_state = 0;
  319. memset(shift_down, 0, sizeof(shift_down));
  320. for (i = 0; i < ARRAY_SIZE(key_down); i++) {
  321. if (!key_down[i])
  322. continue;
  323. k = i * BITS_PER_LONG;
  324. for (j = 0; j < BITS_PER_LONG; j++, k++) {
  325. if (!test_bit(k, key_down))
  326. continue;
  327. sym = U(key_maps[0][k]);
  328. if (KTYP(sym) != KT_SHIFT && KTYP(sym) != KT_SLOCK)
  329. continue;
  330. val = KVAL(sym);
  331. if (val == KVAL(K_CAPSSHIFT))
  332. val = KVAL(K_SHIFT);
  333. shift_down[val]++;
  334. shift_state |= (1 << val);
  335. }
  336. }
  337. }
  338. /*
  339. * We have a combining character DIACR here, followed by the character CH.
  340. * If the combination occurs in the table, return the corresponding value.
  341. * Otherwise, if CH is a space or equals DIACR, return DIACR.
  342. * Otherwise, conclude that DIACR was not combining after all,
  343. * queue it and return CH.
  344. */
  345. static unsigned int handle_diacr(struct vc_data *vc, unsigned int ch)
  346. {
  347. unsigned int d = diacr;
  348. unsigned int i;
  349. diacr = 0;
  350. if ((d & ~0xff) == BRL_UC_ROW) {
  351. if ((ch & ~0xff) == BRL_UC_ROW)
  352. return d | ch;
  353. } else {
  354. for (i = 0; i < accent_table_size; i++)
  355. if (accent_table[i].diacr == d && accent_table[i].base == ch)
  356. return accent_table[i].result;
  357. }
  358. if (ch == ' ' || ch == (BRL_UC_ROW|0) || ch == d)
  359. return d;
  360. if (kbd->kbdmode == VC_UNICODE)
  361. to_utf8(vc, d);
  362. else {
  363. int c = conv_uni_to_8bit(d);
  364. if (c != -1)
  365. put_queue(vc, c);
  366. }
  367. return ch;
  368. }
  369. /*
  370. * Special function handlers
  371. */
  372. static void fn_enter(struct vc_data *vc)
  373. {
  374. if (diacr) {
  375. if (kbd->kbdmode == VC_UNICODE)
  376. to_utf8(vc, diacr);
  377. else {
  378. int c = conv_uni_to_8bit(diacr);
  379. if (c != -1)
  380. put_queue(vc, c);
  381. }
  382. diacr = 0;
  383. }
  384. put_queue(vc, 13);
  385. if (vc_kbd_mode(kbd, VC_CRLF))
  386. put_queue(vc, 10);
  387. }
  388. static void fn_caps_toggle(struct vc_data *vc)
  389. {
  390. if (rep)
  391. return;
  392. chg_vc_kbd_led(kbd, VC_CAPSLOCK);
  393. }
  394. static void fn_caps_on(struct vc_data *vc)
  395. {
  396. if (rep)
  397. return;
  398. set_vc_kbd_led(kbd, VC_CAPSLOCK);
  399. }
  400. static void fn_show_ptregs(struct vc_data *vc)
  401. {
  402. struct pt_regs *regs = get_irq_regs();
  403. if (regs)
  404. show_regs(regs);
  405. }
  406. static void fn_hold(struct vc_data *vc)
  407. {
  408. struct tty_struct *tty = vc->vc_tty;
  409. if (rep || !tty)
  410. return;
  411. /*
  412. * Note: SCROLLOCK will be set (cleared) by stop_tty (start_tty);
  413. * these routines are also activated by ^S/^Q.
  414. * (And SCROLLOCK can also be set by the ioctl KDSKBLED.)
  415. */
  416. if (tty->stopped)
  417. start_tty(tty);
  418. else
  419. stop_tty(tty);
  420. }
  421. static void fn_num(struct vc_data *vc)
  422. {
  423. if (vc_kbd_mode(kbd,VC_APPLIC))
  424. applkey(vc, 'P', 1);
  425. else
  426. fn_bare_num(vc);
  427. }
  428. /*
  429. * Bind this to Shift-NumLock if you work in application keypad mode
  430. * but want to be able to change the NumLock flag.
  431. * Bind this to NumLock if you prefer that the NumLock key always
  432. * changes the NumLock flag.
  433. */
  434. static void fn_bare_num(struct vc_data *vc)
  435. {
  436. if (!rep)
  437. chg_vc_kbd_led(kbd, VC_NUMLOCK);
  438. }
  439. static void fn_lastcons(struct vc_data *vc)
  440. {
  441. /* switch to the last used console, ChN */
  442. set_console(last_console);
  443. }
  444. static void fn_dec_console(struct vc_data *vc)
  445. {
  446. int i, cur = fg_console;
  447. /* Currently switching? Queue this next switch relative to that. */
  448. if (want_console != -1)
  449. cur = want_console;
  450. for (i = cur - 1; i != cur; i--) {
  451. if (i == -1)
  452. i = MAX_NR_CONSOLES - 1;
  453. if (vc_cons_allocated(i))
  454. break;
  455. }
  456. set_console(i);
  457. }
  458. static void fn_inc_console(struct vc_data *vc)
  459. {
  460. int i, cur = fg_console;
  461. /* Currently switching? Queue this next switch relative to that. */
  462. if (want_console != -1)
  463. cur = want_console;
  464. for (i = cur+1; i != cur; i++) {
  465. if (i == MAX_NR_CONSOLES)
  466. i = 0;
  467. if (vc_cons_allocated(i))
  468. break;
  469. }
  470. set_console(i);
  471. }
  472. static void fn_send_intr(struct vc_data *vc)
  473. {
  474. struct tty_struct *tty = vc->vc_tty;
  475. if (!tty)
  476. return;
  477. tty_insert_flip_char(tty, 0, TTY_BREAK);
  478. con_schedule_flip(tty);
  479. }
  480. static void fn_scroll_forw(struct vc_data *vc)
  481. {
  482. scrollfront(vc, 0);
  483. }
  484. static void fn_scroll_back(struct vc_data *vc)
  485. {
  486. scrollback(vc, 0);
  487. }
  488. static void fn_show_mem(struct vc_data *vc)
  489. {
  490. show_mem();
  491. }
  492. static void fn_show_state(struct vc_data *vc)
  493. {
  494. show_state();
  495. }
  496. static void fn_boot_it(struct vc_data *vc)
  497. {
  498. ctrl_alt_del();
  499. }
  500. static void fn_compose(struct vc_data *vc)
  501. {
  502. dead_key_next = 1;
  503. }
  504. static void fn_spawn_con(struct vc_data *vc)
  505. {
  506. spin_lock(&vt_spawn_con.lock);
  507. if (vt_spawn_con.pid)
  508. if (kill_pid(vt_spawn_con.pid, vt_spawn_con.sig, 1)) {
  509. put_pid(vt_spawn_con.pid);
  510. vt_spawn_con.pid = NULL;
  511. }
  512. spin_unlock(&vt_spawn_con.lock);
  513. }
  514. static void fn_SAK(struct vc_data *vc)
  515. {
  516. struct work_struct *SAK_work = &vc_cons[fg_console].SAK_work;
  517. schedule_work(SAK_work);
  518. }
  519. static void fn_null(struct vc_data *vc)
  520. {
  521. compute_shiftstate();
  522. }
  523. /*
  524. * Special key handlers
  525. */
  526. static void k_ignore(struct vc_data *vc, unsigned char value, char up_flag)
  527. {
  528. }
  529. static void k_spec(struct vc_data *vc, unsigned char value, char up_flag)
  530. {
  531. if (up_flag)
  532. return;
  533. if (value >= ARRAY_SIZE(fn_handler))
  534. return;
  535. if ((kbd->kbdmode == VC_RAW ||
  536. kbd->kbdmode == VC_MEDIUMRAW) &&
  537. value != KVAL(K_SAK))
  538. return; /* SAK is allowed even in raw mode */
  539. fn_handler[value](vc);
  540. }
  541. static void k_lowercase(struct vc_data *vc, unsigned char value, char up_flag)
  542. {
  543. printk(KERN_ERR "keyboard.c: k_lowercase was called - impossible\n");
  544. }
  545. static void k_unicode(struct vc_data *vc, unsigned int value, char up_flag)
  546. {
  547. if (up_flag)
  548. return; /* no action, if this is a key release */
  549. if (diacr)
  550. value = handle_diacr(vc, value);
  551. if (dead_key_next) {
  552. dead_key_next = 0;
  553. diacr = value;
  554. return;
  555. }
  556. if (kbd->kbdmode == VC_UNICODE)
  557. to_utf8(vc, value);
  558. else {
  559. int c = conv_uni_to_8bit(value);
  560. if (c != -1)
  561. put_queue(vc, c);
  562. }
  563. }
  564. /*
  565. * Handle dead key. Note that we now may have several
  566. * dead keys modifying the same character. Very useful
  567. * for Vietnamese.
  568. */
  569. static void k_deadunicode(struct vc_data *vc, unsigned int value, char up_flag)
  570. {
  571. if (up_flag)
  572. return;
  573. diacr = (diacr ? handle_diacr(vc, value) : value);
  574. }
  575. static void k_self(struct vc_data *vc, unsigned char value, char up_flag)
  576. {
  577. k_unicode(vc, conv_8bit_to_uni(value), up_flag);
  578. }
  579. static void k_dead2(struct vc_data *vc, unsigned char value, char up_flag)
  580. {
  581. k_deadunicode(vc, value, up_flag);
  582. }
  583. /*
  584. * Obsolete - for backwards compatibility only
  585. */
  586. static void k_dead(struct vc_data *vc, unsigned char value, char up_flag)
  587. {
  588. static const unsigned char ret_diacr[NR_DEAD] = {'`', '\'', '^', '~', '"', ',' };
  589. value = ret_diacr[value];
  590. k_deadunicode(vc, value, up_flag);
  591. }
  592. static void k_cons(struct vc_data *vc, unsigned char value, char up_flag)
  593. {
  594. if (up_flag)
  595. return;
  596. set_console(value);
  597. }
  598. static void k_fn(struct vc_data *vc, unsigned char value, char up_flag)
  599. {
  600. unsigned v;
  601. if (up_flag)
  602. return;
  603. v = value;
  604. if (v < ARRAY_SIZE(func_table)) {
  605. if (func_table[value])
  606. puts_queue(vc, func_table[value]);
  607. } else
  608. printk(KERN_ERR "k_fn called with value=%d\n", value);
  609. }
  610. static void k_cur(struct vc_data *vc, unsigned char value, char up_flag)
  611. {
  612. static const char cur_chars[] = "BDCA";
  613. if (up_flag)
  614. return;
  615. applkey(vc, cur_chars[value], vc_kbd_mode(kbd, VC_CKMODE));
  616. }
  617. static void k_pad(struct vc_data *vc, unsigned char value, char up_flag)
  618. {
  619. static const char pad_chars[] = "0123456789+-*/\015,.?()#";
  620. static const char app_map[] = "pqrstuvwxylSRQMnnmPQS";
  621. if (up_flag)
  622. return; /* no action, if this is a key release */
  623. /* kludge... shift forces cursor/number keys */
  624. if (vc_kbd_mode(kbd, VC_APPLIC) && !shift_down[KG_SHIFT]) {
  625. applkey(vc, app_map[value], 1);
  626. return;
  627. }
  628. if (!vc_kbd_led(kbd, VC_NUMLOCK))
  629. switch (value) {
  630. case KVAL(K_PCOMMA):
  631. case KVAL(K_PDOT):
  632. k_fn(vc, KVAL(K_REMOVE), 0);
  633. return;
  634. case KVAL(K_P0):
  635. k_fn(vc, KVAL(K_INSERT), 0);
  636. return;
  637. case KVAL(K_P1):
  638. k_fn(vc, KVAL(K_SELECT), 0);
  639. return;
  640. case KVAL(K_P2):
  641. k_cur(vc, KVAL(K_DOWN), 0);
  642. return;
  643. case KVAL(K_P3):
  644. k_fn(vc, KVAL(K_PGDN), 0);
  645. return;
  646. case KVAL(K_P4):
  647. k_cur(vc, KVAL(K_LEFT), 0);
  648. return;
  649. case KVAL(K_P6):
  650. k_cur(vc, KVAL(K_RIGHT), 0);
  651. return;
  652. case KVAL(K_P7):
  653. k_fn(vc, KVAL(K_FIND), 0);
  654. return;
  655. case KVAL(K_P8):
  656. k_cur(vc, KVAL(K_UP), 0);
  657. return;
  658. case KVAL(K_P9):
  659. k_fn(vc, KVAL(K_PGUP), 0);
  660. return;
  661. case KVAL(K_P5):
  662. applkey(vc, 'G', vc_kbd_mode(kbd, VC_APPLIC));
  663. return;
  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. printk("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. if (brl_timeout) {
  791. if (!committing ||
  792. time_after(jiffies,
  793. releasestart + msecs_to_jiffies(brl_timeout))) {
  794. committing = pressed;
  795. releasestart = jiffies;
  796. }
  797. pressed &= ~(1 << (value - 1));
  798. if (!pressed) {
  799. if (committing) {
  800. k_brlcommit(vc, committing, 0);
  801. committing = 0;
  802. }
  803. }
  804. } else {
  805. if (committing) {
  806. k_brlcommit(vc, committing, 0);
  807. committing = 0;
  808. }
  809. pressed &= ~(1 << (value - 1));
  810. }
  811. } else {
  812. pressed |= 1 << (value - 1);
  813. if (!brl_timeout)
  814. committing = pressed;
  815. }
  816. }
  817. /*
  818. * The leds display either (i) the status of NumLock, CapsLock, ScrollLock,
  819. * or (ii) whatever pattern of lights people want to show using KDSETLED,
  820. * or (iii) specified bits of specified words in kernel memory.
  821. */
  822. unsigned char getledstate(void)
  823. {
  824. return ledstate;
  825. }
  826. void setledstate(struct kbd_struct *kbd, unsigned int led)
  827. {
  828. if (!(led & ~7)) {
  829. ledioctl = led;
  830. kbd->ledmode = LED_SHOW_IOCTL;
  831. } else
  832. kbd->ledmode = LED_SHOW_FLAGS;
  833. set_leds();
  834. }
  835. static inline unsigned char getleds(void)
  836. {
  837. struct kbd_struct *kbd = kbd_table + fg_console;
  838. unsigned char leds;
  839. int i;
  840. if (kbd->ledmode == LED_SHOW_IOCTL)
  841. return ledioctl;
  842. leds = kbd->ledflagstate;
  843. if (kbd->ledmode == LED_SHOW_MEM) {
  844. for (i = 0; i < 3; i++)
  845. if (ledptrs[i].valid) {
  846. if (*ledptrs[i].addr & ledptrs[i].mask)
  847. leds |= (1 << i);
  848. else
  849. leds &= ~(1 << i);
  850. }
  851. }
  852. return leds;
  853. }
  854. static int kbd_update_leds_helper(struct input_handle *handle, void *data)
  855. {
  856. unsigned char leds = *(unsigned char *)data;
  857. if (test_bit(EV_LED, handle->dev->evbit)) {
  858. input_inject_event(handle, EV_LED, LED_SCROLLL, !!(leds & 0x01));
  859. input_inject_event(handle, EV_LED, LED_NUML, !!(leds & 0x02));
  860. input_inject_event(handle, EV_LED, LED_CAPSL, !!(leds & 0x04));
  861. input_inject_event(handle, EV_SYN, SYN_REPORT, 0);
  862. }
  863. return 0;
  864. }
  865. /*
  866. * This is the tasklet that updates LED state on all keyboards
  867. * attached to the box. The reason we use tasklet is that we
  868. * need to handle the scenario when keyboard handler is not
  869. * registered yet but we already getting updates form VT to
  870. * update led state.
  871. */
  872. static void kbd_bh(unsigned long dummy)
  873. {
  874. unsigned char leds = getleds();
  875. if (leds != ledstate) {
  876. input_handler_for_each_handle(&kbd_handler, &leds,
  877. kbd_update_leds_helper);
  878. ledstate = leds;
  879. }
  880. }
  881. DECLARE_TASKLET_DISABLED(keyboard_tasklet, kbd_bh, 0);
  882. #if defined(CONFIG_X86) || defined(CONFIG_IA64) || defined(CONFIG_ALPHA) ||\
  883. defined(CONFIG_MIPS) || defined(CONFIG_PPC) || defined(CONFIG_SPARC) ||\
  884. defined(CONFIG_PARISC) || defined(CONFIG_SUPERH) ||\
  885. (defined(CONFIG_ARM) && defined(CONFIG_KEYBOARD_ATKBD) && !defined(CONFIG_ARCH_RPC)) ||\
  886. defined(CONFIG_AVR32)
  887. #define HW_RAW(dev) (test_bit(EV_MSC, dev->evbit) && test_bit(MSC_RAW, dev->mscbit) &&\
  888. ((dev)->id.bustype == BUS_I8042) && ((dev)->id.vendor == 0x0001) && ((dev)->id.product == 0x0001))
  889. static const unsigned short x86_keycodes[256] =
  890. { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
  891. 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
  892. 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
  893. 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
  894. 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
  895. 80, 81, 82, 83, 84,118, 86, 87, 88,115,120,119,121,112,123, 92,
  896. 284,285,309, 0,312, 91,327,328,329,331,333,335,336,337,338,339,
  897. 367,288,302,304,350, 89,334,326,267,126,268,269,125,347,348,349,
  898. 360,261,262,263,268,376,100,101,321,316,373,286,289,102,351,355,
  899. 103,104,105,275,287,279,258,106,274,107,294,364,358,363,362,361,
  900. 291,108,381,281,290,272,292,305,280, 99,112,257,306,359,113,114,
  901. 264,117,271,374,379,265,266, 93, 94, 95, 85,259,375,260, 90,116,
  902. 377,109,111,277,278,282,283,295,296,297,299,300,301,293,303,307,
  903. 308,310,313,314,315,317,318,319,320,357,322,323,324,325,276,330,
  904. 332,340,365,342,343,344,345,346,356,270,341,368,369,370,371,372 };
  905. #ifdef CONFIG_SPARC
  906. static int sparc_l1_a_state = 0;
  907. extern void sun_do_break(void);
  908. #endif
  909. static int emulate_raw(struct vc_data *vc, unsigned int keycode,
  910. unsigned char up_flag)
  911. {
  912. int code;
  913. switch (keycode) {
  914. case KEY_PAUSE:
  915. put_queue(vc, 0xe1);
  916. put_queue(vc, 0x1d | up_flag);
  917. put_queue(vc, 0x45 | up_flag);
  918. break;
  919. case KEY_HANGEUL:
  920. if (!up_flag)
  921. put_queue(vc, 0xf2);
  922. break;
  923. case KEY_HANJA:
  924. if (!up_flag)
  925. put_queue(vc, 0xf1);
  926. break;
  927. case KEY_SYSRQ:
  928. /*
  929. * Real AT keyboards (that's what we're trying
  930. * to emulate here emit 0xe0 0x2a 0xe0 0x37 when
  931. * pressing PrtSc/SysRq alone, but simply 0x54
  932. * when pressing Alt+PrtSc/SysRq.
  933. */
  934. if (sysrq_alt) {
  935. put_queue(vc, 0x54 | up_flag);
  936. } else {
  937. put_queue(vc, 0xe0);
  938. put_queue(vc, 0x2a | up_flag);
  939. put_queue(vc, 0xe0);
  940. put_queue(vc, 0x37 | up_flag);
  941. }
  942. break;
  943. default:
  944. if (keycode > 255)
  945. return -1;
  946. code = x86_keycodes[keycode];
  947. if (!code)
  948. return -1;
  949. if (code & 0x100)
  950. put_queue(vc, 0xe0);
  951. put_queue(vc, (code & 0x7f) | up_flag);
  952. break;
  953. }
  954. return 0;
  955. }
  956. #else
  957. #define HW_RAW(dev) 0
  958. static int emulate_raw(struct vc_data *vc, unsigned int keycode, unsigned char up_flag)
  959. {
  960. if (keycode > 127)
  961. return -1;
  962. put_queue(vc, keycode | up_flag);
  963. return 0;
  964. }
  965. #endif
  966. static void kbd_rawcode(unsigned char data)
  967. {
  968. struct vc_data *vc = vc_cons[fg_console].d;
  969. kbd = kbd_table + vc->vc_num;
  970. if (kbd->kbdmode == VC_RAW)
  971. put_queue(vc, data);
  972. }
  973. static void kbd_keycode(unsigned int keycode, int down, int hw_raw)
  974. {
  975. struct vc_data *vc = vc_cons[fg_console].d;
  976. unsigned short keysym, *key_map;
  977. unsigned char type, raw_mode;
  978. struct tty_struct *tty;
  979. int shift_final;
  980. struct keyboard_notifier_param param = { .vc = vc, .value = keycode, .down = down };
  981. tty = vc->vc_tty;
  982. if (tty && (!tty->driver_data)) {
  983. /* No driver data? Strange. Okay we fix it then. */
  984. tty->driver_data = vc;
  985. }
  986. kbd = kbd_table + vc->vc_num;
  987. if (keycode == KEY_LEFTALT || keycode == KEY_RIGHTALT)
  988. sysrq_alt = down ? keycode : 0;
  989. #ifdef CONFIG_SPARC
  990. if (keycode == KEY_STOP)
  991. sparc_l1_a_state = down;
  992. #endif
  993. rep = (down == 2);
  994. #ifdef CONFIG_MAC_EMUMOUSEBTN
  995. if (mac_hid_mouse_emulate_buttons(1, keycode, down))
  996. return;
  997. #endif /* CONFIG_MAC_EMUMOUSEBTN */
  998. if ((raw_mode = (kbd->kbdmode == VC_RAW)) && !hw_raw)
  999. if (emulate_raw(vc, keycode, !down << 7))
  1000. if (keycode < BTN_MISC && printk_ratelimit())
  1001. printk(KERN_WARNING "keyboard.c: can't emulate rawmode for keycode %d\n", keycode);
  1002. #ifdef CONFIG_MAGIC_SYSRQ /* Handle the SysRq Hack */
  1003. if (keycode == KEY_SYSRQ && (sysrq_down || (down == 1 && sysrq_alt))) {
  1004. if (!sysrq_down) {
  1005. sysrq_down = down;
  1006. sysrq_alt_use = sysrq_alt;
  1007. }
  1008. return;
  1009. }
  1010. if (sysrq_down && !down && keycode == sysrq_alt_use)
  1011. sysrq_down = 0;
  1012. if (sysrq_down && down && !rep) {
  1013. handle_sysrq(kbd_sysrq_xlate[keycode], tty);
  1014. return;
  1015. }
  1016. #endif
  1017. #ifdef CONFIG_SPARC
  1018. if (keycode == KEY_A && sparc_l1_a_state) {
  1019. sparc_l1_a_state = 0;
  1020. sun_do_break();
  1021. }
  1022. #endif
  1023. if (kbd->kbdmode == VC_MEDIUMRAW) {
  1024. /*
  1025. * This is extended medium raw mode, with keys above 127
  1026. * encoded as 0, high 7 bits, low 7 bits, with the 0 bearing
  1027. * the 'up' flag if needed. 0 is reserved, so this shouldn't
  1028. * interfere with anything else. The two bytes after 0 will
  1029. * always have the up flag set not to interfere with older
  1030. * applications. This allows for 16384 different keycodes,
  1031. * which should be enough.
  1032. */
  1033. if (keycode < 128) {
  1034. put_queue(vc, keycode | (!down << 7));
  1035. } else {
  1036. put_queue(vc, !down << 7);
  1037. put_queue(vc, (keycode >> 7) | 0x80);
  1038. put_queue(vc, keycode | 0x80);
  1039. }
  1040. raw_mode = 1;
  1041. }
  1042. if (down)
  1043. set_bit(keycode, key_down);
  1044. else
  1045. clear_bit(keycode, key_down);
  1046. if (rep &&
  1047. (!vc_kbd_mode(kbd, VC_REPEAT) ||
  1048. (tty && !L_ECHO(tty) && tty_chars_in_buffer(tty)))) {
  1049. /*
  1050. * Don't repeat a key if the input buffers are not empty and the
  1051. * characters get aren't echoed locally. This makes key repeat
  1052. * usable with slow applications and under heavy loads.
  1053. */
  1054. return;
  1055. }
  1056. param.shift = shift_final = (shift_state | kbd->slockstate) ^ kbd->lockstate;
  1057. param.ledstate = kbd->ledflagstate;
  1058. key_map = key_maps[shift_final];
  1059. if (atomic_notifier_call_chain(&keyboard_notifier_list, KBD_KEYCODE, &param) == NOTIFY_STOP || !key_map) {
  1060. atomic_notifier_call_chain(&keyboard_notifier_list, KBD_UNBOUND_KEYCODE, &param);
  1061. compute_shiftstate();
  1062. kbd->slockstate = 0;
  1063. return;
  1064. }
  1065. if (keycode >= NR_KEYS)
  1066. if (keycode >= KEY_BRL_DOT1 && keycode <= KEY_BRL_DOT8)
  1067. keysym = U(K(KT_BRL, keycode - KEY_BRL_DOT1 + 1));
  1068. else
  1069. return;
  1070. else
  1071. keysym = key_map[keycode];
  1072. type = KTYP(keysym);
  1073. if (type < 0xf0) {
  1074. param.value = keysym;
  1075. if (atomic_notifier_call_chain(&keyboard_notifier_list, KBD_UNICODE, &param) == NOTIFY_STOP)
  1076. return;
  1077. if (down && !raw_mode)
  1078. to_utf8(vc, keysym);
  1079. return;
  1080. }
  1081. type -= 0xf0;
  1082. if (type == KT_LETTER) {
  1083. type = KT_LATIN;
  1084. if (vc_kbd_led(kbd, VC_CAPSLOCK)) {
  1085. key_map = key_maps[shift_final ^ (1 << KG_SHIFT)];
  1086. if (key_map)
  1087. keysym = key_map[keycode];
  1088. }
  1089. }
  1090. param.value = keysym;
  1091. if (atomic_notifier_call_chain(&keyboard_notifier_list, KBD_KEYSYM, &param) == NOTIFY_STOP)
  1092. return;
  1093. if (raw_mode && type != KT_SPEC && type != KT_SHIFT)
  1094. return;
  1095. (*k_handler[type])(vc, keysym & 0xff, !down);
  1096. param.ledstate = kbd->ledflagstate;
  1097. atomic_notifier_call_chain(&keyboard_notifier_list, KBD_POST_KEYSYM, &param);
  1098. if (type != KT_SLOCK)
  1099. kbd->slockstate = 0;
  1100. }
  1101. static void kbd_event(struct input_handle *handle, unsigned int event_type,
  1102. unsigned int event_code, int value)
  1103. {
  1104. /* We are called with interrupts disabled, just take the lock */
  1105. spin_lock(&kbd_event_lock);
  1106. if (event_type == EV_MSC && event_code == MSC_RAW && HW_RAW(handle->dev))
  1107. kbd_rawcode(value);
  1108. if (event_type == EV_KEY)
  1109. kbd_keycode(event_code, value, HW_RAW(handle->dev));
  1110. spin_unlock(&kbd_event_lock);
  1111. tasklet_schedule(&keyboard_tasklet);
  1112. do_poke_blanked_console = 1;
  1113. schedule_console_callback();
  1114. }
  1115. /*
  1116. * When a keyboard (or other input device) is found, the kbd_connect
  1117. * function is called. The function then looks at the device, and if it
  1118. * likes it, it can open it and get events from it. In this (kbd_connect)
  1119. * function, we should decide which VT to bind that keyboard to initially.
  1120. */
  1121. static int kbd_connect(struct input_handler *handler, struct input_dev *dev,
  1122. const struct input_device_id *id)
  1123. {
  1124. struct input_handle *handle;
  1125. int error;
  1126. int i;
  1127. for (i = KEY_RESERVED; i < BTN_MISC; i++)
  1128. if (test_bit(i, dev->keybit))
  1129. break;
  1130. if (i == BTN_MISC && !test_bit(EV_SND, dev->evbit))
  1131. return -ENODEV;
  1132. handle = kzalloc(sizeof(struct input_handle), GFP_KERNEL);
  1133. if (!handle)
  1134. return -ENOMEM;
  1135. handle->dev = dev;
  1136. handle->handler = handler;
  1137. handle->name = "kbd";
  1138. error = input_register_handle(handle);
  1139. if (error)
  1140. goto err_free_handle;
  1141. error = input_open_device(handle);
  1142. if (error)
  1143. goto err_unregister_handle;
  1144. return 0;
  1145. err_unregister_handle:
  1146. input_unregister_handle(handle);
  1147. err_free_handle:
  1148. kfree(handle);
  1149. return error;
  1150. }
  1151. static void kbd_disconnect(struct input_handle *handle)
  1152. {
  1153. input_close_device(handle);
  1154. input_unregister_handle(handle);
  1155. kfree(handle);
  1156. }
  1157. /*
  1158. * Start keyboard handler on the new keyboard by refreshing LED state to
  1159. * match the rest of the system.
  1160. */
  1161. static void kbd_start(struct input_handle *handle)
  1162. {
  1163. tasklet_disable(&keyboard_tasklet);
  1164. if (ledstate != 0xff)
  1165. kbd_update_leds_helper(handle, &ledstate);
  1166. tasklet_enable(&keyboard_tasklet);
  1167. }
  1168. static const struct input_device_id kbd_ids[] = {
  1169. {
  1170. .flags = INPUT_DEVICE_ID_MATCH_EVBIT,
  1171. .evbit = { BIT_MASK(EV_KEY) },
  1172. },
  1173. {
  1174. .flags = INPUT_DEVICE_ID_MATCH_EVBIT,
  1175. .evbit = { BIT_MASK(EV_SND) },
  1176. },
  1177. { }, /* Terminating entry */
  1178. };
  1179. MODULE_DEVICE_TABLE(input, kbd_ids);
  1180. static struct input_handler kbd_handler = {
  1181. .event = kbd_event,
  1182. .connect = kbd_connect,
  1183. .disconnect = kbd_disconnect,
  1184. .start = kbd_start,
  1185. .name = "kbd",
  1186. .id_table = kbd_ids,
  1187. };
  1188. int __init kbd_init(void)
  1189. {
  1190. int i;
  1191. int error;
  1192. for (i = 0; i < MAX_NR_CONSOLES; i++) {
  1193. kbd_table[i].ledflagstate = KBD_DEFLEDS;
  1194. kbd_table[i].default_ledflagstate = KBD_DEFLEDS;
  1195. kbd_table[i].ledmode = LED_SHOW_FLAGS;
  1196. kbd_table[i].lockstate = KBD_DEFLOCK;
  1197. kbd_table[i].slockstate = 0;
  1198. kbd_table[i].modeflags = KBD_DEFMODE;
  1199. kbd_table[i].kbdmode = default_utf8 ? VC_UNICODE : VC_XLATE;
  1200. }
  1201. error = input_register_handler(&kbd_handler);
  1202. if (error)
  1203. return error;
  1204. tasklet_enable(&keyboard_tasklet);
  1205. tasklet_schedule(&keyboard_tasklet);
  1206. return 0;
  1207. }