keyboard.c 33 KB

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