keyboard.c 33 KB

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