keyboard.c 33 KB

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