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