keyboard.c 34 KB

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