keyboard.c 33 KB

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