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