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