2 * linux/drivers/char/keyboard.c
4 * Written for linux by Johan Myreen as a translation from
5 * the assembly version by Linus (with diacriticals added)
7 * Some additional features added by Christoph Niemann (ChN), March 1993
9 * Loadable keymaps by Risto Kankkunen, May 1993
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.
16 * 11-11-96: SAK should now work in the raw mode (Martin Mares)
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
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)
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>
33 #include <linux/string.h>
34 #include <linux/random.h>
35 #include <linux/init.h>
36 #include <linux/slab.h>
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>
44 static void kbd_disconnect(struct input_handle *handle);
45 extern void ctrl_alt_del(void);
48 * Exported functions/variables
51 #define KBD_DEFMODE ((1 << VC_REPEAT) | (1 << VC_META))
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.
60 #if defined(CONFIG_PARISC) && (defined(CONFIG_KEYBOARD_HIL) || defined(CONFIG_KEYBOARD_HIL_OLD))
61 #define KBD_DEFLEDS (1 << VC_NUMLOCK)
68 void compute_shiftstate(void);
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
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 };
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
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 };
97 * Variables exported for vt_ioctl.c
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
107 const int NR_TYPES = ARRAY_SIZE(max_vals);
109 struct kbd_struct kbd_table[MAX_NR_CONSOLES];
110 static struct kbd_struct *kbd = kbd_table;
111 static struct kbd_struct kbd0;
113 int spawnpid, spawnsig;
116 * Variables exported for vt.c
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 */
133 static unsigned char ledstate = 0xff; /* undefined */
134 static unsigned char ledioctl;
136 static struct ledptr {
139 unsigned char valid:1;
142 /* Simple translation table for the SysRq keys */
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;
155 static int sysrq_alt;
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.
161 int getkeycode(unsigned int scancode)
163 struct list_head * node;
164 struct input_dev *dev = NULL;
166 list_for_each(node,&kbd_handler.h_list) {
167 struct input_handle * handle = to_handle_h(node);
168 if (handle->dev->keycodesize) {
177 if (scancode < 0 || scancode >= dev->keycodemax)
180 return INPUT_KEYCODE(dev, scancode);
183 int setkeycode(unsigned int scancode, unsigned int keycode)
185 struct list_head * node;
186 struct input_dev *dev = NULL;
189 list_for_each(node,&kbd_handler.h_list) {
190 struct input_handle *handle = to_handle_h(node);
191 if (handle->dev->keycodesize) {
200 if (scancode < 0 || scancode >= dev->keycodemax)
203 oldkey = SET_INPUT_KEYCODE(dev, scancode, keycode);
205 clear_bit(oldkey, dev->keybit);
206 set_bit(keycode, dev->keybit);
208 for (i = 0; i < dev->keycodemax; i++)
209 if (INPUT_KEYCODE(dev,i) == oldkey)
210 set_bit(oldkey, dev->keybit);
216 * Making beeps and bells.
218 static void kd_nosound(unsigned long ignored)
220 struct list_head * node;
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);
233 static struct timer_list kd_mksound_timer =
234 TIMER_INITIALIZER(kd_nosound, 0, 0);
236 void kd_mksound(unsigned int hz, unsigned int ticks)
238 struct list_head * node;
240 del_timer(&kd_mksound_timer);
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);
250 if (test_bit(SND_BELL, handle->dev->sndbit)) {
251 input_event(handle->dev, EV_SND, SND_BELL, 1);
257 mod_timer(&kd_mksound_timer, jiffies + ticks);
263 * Setting the keyboard rate.
266 int kbd_rate(struct kbd_repeat *rep)
268 struct list_head *node;
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;
276 if (test_bit(EV_REP, dev->evbit)) {
278 input_event(dev, EV_REP, REP_DELAY, rep->delay);
280 input_event(dev, EV_REP, REP_PERIOD, rep->period);
281 d = dev->rep[REP_DELAY];
282 p = dev->rep[REP_PERIOD];
293 static void put_queue(struct vc_data *vc, int ch)
295 struct tty_struct *tty = vc->vc_tty;
298 tty_insert_flip_char(tty, ch, 0);
299 con_schedule_flip(tty);
303 static void puts_queue(struct vc_data *vc, char *cp)
305 struct tty_struct *tty = vc->vc_tty;
311 tty_insert_flip_char(tty, *cp, 0);
314 con_schedule_flip(tty);
317 static void applkey(struct vc_data *vc, int key, char mode)
319 static char buf[] = { 0x1b, 'O', 0x00, 0x00 };
321 buf[1] = (mode ? 'O' : '[');
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
333 void to_utf8(struct vc_data *vc, ushort c)
338 else if (c < 0x800) {
339 /* 110***** 10****** */
340 put_queue(vc, 0xc0 | (c >> 6));
341 put_queue(vc, 0x80 | (c & 0x3f));
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));
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
355 void compute_shiftstate(void)
357 int i, j, k, sym, val;
360 memset(shift_down, 0, sizeof(shift_down));
362 for (i = 0; i < ARRAY_SIZE(key_down); i++) {
367 k = i * BITS_PER_LONG;
369 for (j = 0; j < BITS_PER_LONG; j++, k++) {
371 if (!test_bit(k, key_down))
374 sym = U(key_maps[0][k]);
375 if (KTYP(sym) != KT_SHIFT && KTYP(sym) != KT_SLOCK)
379 if (val == KVAL(K_CAPSSHIFT))
383 shift_state |= (1 << val);
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.
395 unsigned char handle_diacr(struct vc_data *vc, unsigned char ch)
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;
407 if (ch == ' ' || ch == d)
415 * Special function handlers
417 static void fn_enter(struct vc_data *vc, struct pt_regs *regs)
420 put_queue(vc, diacr);
424 if (vc_kbd_mode(kbd, VC_CRLF))
428 static void fn_caps_toggle(struct vc_data *vc, struct pt_regs *regs)
432 chg_vc_kbd_led(kbd, VC_CAPSLOCK);
435 static void fn_caps_on(struct vc_data *vc, struct pt_regs *regs)
439 set_vc_kbd_led(kbd, VC_CAPSLOCK);
442 static void fn_show_ptregs(struct vc_data *vc, struct pt_regs *regs)
448 static void fn_hold(struct vc_data *vc, struct pt_regs *regs)
450 struct tty_struct *tty = vc->vc_tty;
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.)
466 static void fn_num(struct vc_data *vc, struct pt_regs *regs)
468 if (vc_kbd_mode(kbd,VC_APPLIC))
471 fn_bare_num(vc, regs);
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.
480 static void fn_bare_num(struct vc_data *vc, struct pt_regs *regs)
483 chg_vc_kbd_led(kbd, VC_NUMLOCK);
486 static void fn_lastcons(struct vc_data *vc, struct pt_regs *regs)
488 /* switch to the last used console, ChN */
489 set_console(last_console);
492 static void fn_dec_console(struct vc_data *vc, struct pt_regs *regs)
494 int i, cur = fg_console;
496 /* Currently switching? Queue this next switch relative to that. */
497 if (want_console != -1)
500 for (i = cur-1; i != cur; i--) {
502 i = MAX_NR_CONSOLES-1;
503 if (vc_cons_allocated(i))
509 static void fn_inc_console(struct vc_data *vc, struct pt_regs *regs)
511 int i, cur = fg_console;
513 /* Currently switching? Queue this next switch relative to that. */
514 if (want_console != -1)
517 for (i = cur+1; i != cur; i++) {
518 if (i == MAX_NR_CONSOLES)
520 if (vc_cons_allocated(i))
526 static void fn_send_intr(struct vc_data *vc, struct pt_regs *regs)
528 struct tty_struct *tty = vc->vc_tty;
532 tty_insert_flip_char(tty, 0, TTY_BREAK);
533 con_schedule_flip(tty);
536 static void fn_scroll_forw(struct vc_data *vc, struct pt_regs *regs)
541 static void fn_scroll_back(struct vc_data *vc, struct pt_regs *regs)
546 static void fn_show_mem(struct vc_data *vc, struct pt_regs *regs)
551 static void fn_show_state(struct vc_data *vc, struct pt_regs *regs)
556 static void fn_boot_it(struct vc_data *vc, struct pt_regs *regs)
561 static void fn_compose(struct vc_data *vc, struct pt_regs *regs)
566 static void fn_spawn_con(struct vc_data *vc, struct pt_regs *regs)
569 if(kill_proc(spawnpid, spawnsig, 1))
573 static void fn_SAK(struct vc_data *vc, struct pt_regs *regs)
575 struct tty_struct *tty = vc->vc_tty;
578 * SAK should also work in all raw modes and reset
583 reset_vc(fg_console);
586 static void fn_null(struct vc_data *vc, struct pt_regs *regs)
588 compute_shiftstate();
592 * Special key handlers
594 static void k_ignore(struct vc_data *vc, unsigned char value, char up_flag, struct pt_regs *regs)
598 static void k_spec(struct vc_data *vc, unsigned char value, char up_flag, struct pt_regs *regs)
602 if (value >= ARRAY_SIZE(fn_handler))
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);
611 static void k_lowercase(struct vc_data *vc, unsigned char value, char up_flag, struct pt_regs *regs)
613 printk(KERN_ERR "keyboard.c: k_lowercase was called - impossible\n");
616 static void k_self(struct vc_data *vc, unsigned char value, char up_flag, struct pt_regs *regs)
619 return; /* no action, if this is a key release */
622 value = handle_diacr(vc, value);
629 put_queue(vc, value);
633 * Handle dead key. Note that we now may have several
634 * dead keys modifying the same character. Very useful
637 static void k_dead2(struct vc_data *vc, unsigned char value, char up_flag, struct pt_regs *regs)
641 diacr = (diacr ? handle_diacr(vc, value) : value);
645 * Obsolete - for backwards compatibility only
647 static void k_dead(struct vc_data *vc, unsigned char value, char up_flag, struct pt_regs *regs)
649 static unsigned char ret_diacr[NR_DEAD] = {'`', '\'', '^', '~', '"', ',' };
650 value = ret_diacr[value];
651 k_dead2(vc, value, up_flag, regs);
654 static void k_cons(struct vc_data *vc, unsigned char value, char up_flag, struct pt_regs *regs)
661 static void k_fn(struct vc_data *vc, unsigned char value, char up_flag, struct pt_regs *regs)
668 if (v < ARRAY_SIZE(func_table)) {
669 if (func_table[value])
670 puts_queue(vc, func_table[value]);
672 printk(KERN_ERR "k_fn called with value=%d\n", value);
675 static void k_cur(struct vc_data *vc, unsigned char value, char up_flag, struct pt_regs *regs)
677 static const char *cur_chars = "BDCA";
681 applkey(vc, cur_chars[value], vc_kbd_mode(kbd, VC_CKMODE));
684 static void k_pad(struct vc_data *vc, unsigned char value, char up_flag, struct pt_regs *regs)
686 static const char *pad_chars = "0123456789+-*/\015,.?()#";
687 static const char *app_map = "pqrstuvwxylSRQMnnmPQS";
690 return; /* no action, if this is a key release */
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);
698 if (!vc_kbd_led(kbd, VC_NUMLOCK))
702 k_fn(vc, KVAL(K_REMOVE), 0, regs);
705 k_fn(vc, KVAL(K_INSERT), 0, regs);
708 k_fn(vc, KVAL(K_SELECT), 0, regs);
711 k_cur(vc, KVAL(K_DOWN), 0, regs);
714 k_fn(vc, KVAL(K_PGDN), 0, regs);
717 k_cur(vc, KVAL(K_LEFT), 0, regs);
720 k_cur(vc, KVAL(K_RIGHT), 0, regs);
723 k_fn(vc, KVAL(K_FIND), 0, regs);
726 k_cur(vc, KVAL(K_UP), 0, regs);
729 k_fn(vc, KVAL(K_PGUP), 0, regs);
732 applkey(vc, 'G', vc_kbd_mode(kbd, VC_APPLIC));
736 put_queue(vc, pad_chars[value]);
737 if (value == KVAL(K_PENTER) && vc_kbd_mode(kbd, VC_CRLF))
741 static void k_shift(struct vc_data *vc, unsigned char value, char up_flag, struct pt_regs *regs)
743 int old_state = shift_state;
749 * a CapsShift key acts like Shift but undoes CapsLock
751 if (value == KVAL(K_CAPSSHIFT)) {
752 value = KVAL(K_SHIFT);
754 clr_vc_kbd_led(kbd, VC_CAPSLOCK);
759 * handle the case that two shift or control
760 * keys are depressed simultaneously
762 if (shift_down[value])
767 if (shift_down[value])
768 shift_state |= (1 << value);
770 shift_state &= ~(1 << value);
773 if (up_flag && shift_state != old_state && npadch != -1) {
774 if (kbd->kbdmode == VC_UNICODE)
775 to_utf8(vc, npadch & 0xffff);
777 put_queue(vc, npadch & 0xff);
782 static void k_meta(struct vc_data *vc, unsigned char value, char up_flag, struct pt_regs *regs)
787 if (vc_kbd_mode(kbd, VC_META)) {
788 put_queue(vc, '\033');
789 put_queue(vc, value);
791 put_queue(vc, value | 0x80);
794 static void k_ascii(struct vc_data *vc, unsigned char value, char up_flag, struct pt_regs *regs)
802 /* decimal input of code, while Alt depressed */
805 /* hexadecimal input of code, while AltGr depressed */
813 npadch = npadch * base + value;
816 static void k_lock(struct vc_data *vc, unsigned char value, char up_flag, struct pt_regs *regs)
820 chg_vc_kbd_lock(kbd, value);
823 static void k_slock(struct vc_data *vc, unsigned char value, char up_flag, struct pt_regs *regs)
825 k_shift(vc, value, up_flag, regs);
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]) {
832 chg_vc_kbd_slock(kbd, value);
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.
841 unsigned char getledstate(void)
846 void setledstate(struct kbd_struct *kbd, unsigned int led)
850 kbd->ledmode = LED_SHOW_IOCTL;
852 kbd->ledmode = LED_SHOW_FLAGS;
856 void register_leds(struct kbd_struct *kbd, unsigned int led,
857 unsigned int *addr, unsigned int mask)
860 ledptrs[led].addr = addr;
861 ledptrs[led].mask = mask;
862 ledptrs[led].valid = 1;
863 kbd->ledmode = LED_SHOW_MEM;
865 kbd->ledmode = LED_SHOW_FLAGS;
868 static inline unsigned char getleds(void)
870 struct kbd_struct *kbd = kbd_table + fg_console;
874 if (kbd->ledmode == LED_SHOW_IOCTL)
877 leds = kbd->ledflagstate;
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)
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.
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, ...) => ...
905 static void kbd_bh(unsigned long dummy)
907 struct list_head * node;
908 unsigned char leds = getleds();
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);
923 DECLARE_TASKLET_DISABLED(keyboard_tasklet, kbd_bh, 0);
926 * This allows a newly plugged keyboard to pick the LED state.
928 void kbd_refresh_leds(struct input_handle *handle)
930 unsigned char leds = ledstate;
932 tasklet_disable(&keyboard_tasklet);
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);
939 tasklet_enable(&keyboard_tasklet);
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)
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))
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 };
964 #ifdef CONFIG_MAC_EMUMOUSEBTN
965 extern int mac_hid_mouse_emulate_buttons(int, int, int);
966 #endif /* CONFIG_MAC_EMUMOUSEBTN */
968 #if defined(CONFIG_SPARC32) || defined(CONFIG_SPARC64)
969 static int sparc_l1_a_state = 0;
970 extern void sun_do_break(void);
973 static int emulate_raw(struct vc_data *vc, unsigned int keycode,
974 unsigned char up_flag)
976 if (keycode > 255 || !x86_keycodes[keycode])
982 put_queue(vc, 0x1d | up_flag);
983 put_queue(vc, 0x45 | up_flag);
986 if (!up_flag) put_queue(vc, 0xf1);
989 if (!up_flag) put_queue(vc, 0xf2);
993 if (keycode == KEY_SYSRQ && sysrq_alt) {
994 put_queue(vc, 0x54 | up_flag);
998 if (x86_keycodes[keycode] & 0x100)
1001 put_queue(vc, (x86_keycodes[keycode] & 0x7f) | up_flag);
1003 if (keycode == KEY_SYSRQ) {
1004 put_queue(vc, 0xe0);
1005 put_queue(vc, 0x37 | up_flag);
1013 #define HW_RAW(dev) 0
1015 #warning "Cannot generate rawmode keyboard for your architecture yet."
1017 static int emulate_raw(struct vc_data *vc, unsigned int keycode, unsigned char up_flag)
1022 put_queue(vc, keycode | up_flag);
1027 void kbd_rawcode(unsigned char data)
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);
1035 void kbd_keycode(unsigned int keycode, int down, int hw_raw, struct pt_regs *regs)
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;
1044 add_keyboard_randomness((keycode << 1) ^ down);
1048 if (tty && (!tty->driver_data)) {
1049 /* No driver data? Strange. Okay we fix it then. */
1050 tty->driver_data = vc;
1053 kbd = kbd_table + fg_console;
1055 if (keycode == KEY_LEFTALT || keycode == KEY_RIGHTALT)
1057 #if defined(CONFIG_SPARC32) || defined(CONFIG_SPARC64)
1058 if (keycode == KEY_STOP)
1059 sparc_l1_a_state = down;
1064 #ifdef CONFIG_MAC_EMUMOUSEBTN
1065 if (mac_hid_mouse_emulate_buttons(1, keycode, down))
1067 #endif /* CONFIG_MAC_EMUMOUSEBTN */
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);
1074 #ifdef CONFIG_MAGIC_SYSRQ /* Handle the SysRq Hack */
1075 if (keycode == KEY_SYSRQ && (sysrq_down || (down == 1 && sysrq_alt))) {
1079 if (sysrq_down && down && !rep) {
1080 handle_sysrq(kbd_sysrq_xlate[keycode], regs, tty);
1084 #if defined(CONFIG_SPARC32) || defined(CONFIG_SPARC64)
1085 if (keycode == KEY_A && sparc_l1_a_state) {
1086 sparc_l1_a_state = 0;
1091 if (kbd->kbdmode == VC_MEDIUMRAW) {
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.
1101 if (keycode < 128) {
1102 put_queue(vc, keycode | (!down << 7));
1104 put_queue(vc, !down << 7);
1105 put_queue(vc, (keycode >> 7) | 0x80);
1106 put_queue(vc, keycode | 0x80);
1112 set_bit(keycode, key_down);
1114 clear_bit(keycode, key_down);
1116 if (rep && (!vc_kbd_mode(kbd, VC_REPEAT) || (tty &&
1117 (!L_ECHO(tty) && tty->driver->chars_in_buffer(tty))))) {
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.
1126 shift_final = (shift_state | kbd->slockstate) ^ kbd->lockstate;
1127 key_map = key_maps[shift_final];
1130 compute_shiftstate();
1131 kbd->slockstate = 0;
1135 if (keycode > NR_KEYS)
1138 keysym = key_map[keycode];
1139 type = KTYP(keysym);
1142 if (down && !raw_mode) to_utf8(vc, keysym);
1148 if (raw_mode && type != KT_SPEC && type != KT_SHIFT)
1151 if (type == KT_LETTER) {
1153 if (vc_kbd_led(kbd, VC_CAPSLOCK)) {
1154 key_map = key_maps[shift_final ^ (1 << KG_SHIFT)];
1156 keysym = key_map[keycode];
1160 (*k_handler[type])(vc, keysym & 0xff, !down, regs);
1162 if (type != KT_SLOCK)
1163 kbd->slockstate = 0;
1166 static void kbd_event(struct input_handle *handle, unsigned int event_type,
1167 unsigned int event_code, int value)
1169 if (event_type == EV_MSC && event_code == MSC_RAW && HW_RAW(handle->dev))
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();
1178 static char kbd_name[] = "kbd";
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.
1186 static struct input_handle *kbd_connect(struct input_handler *handler,
1187 struct input_dev *dev,
1188 struct input_device_id *id)
1190 struct input_handle *handle;
1193 for (i = KEY_RESERVED; i < BTN_MISC; i++)
1194 if (test_bit(i, dev->keybit)) break;
1196 if ((i == BTN_MISC) && !test_bit(EV_SND, dev->evbit))
1199 if (!(handle = kmalloc(sizeof(struct input_handle), GFP_KERNEL)))
1201 memset(handle, 0, sizeof(struct input_handle));
1204 handle->handler = handler;
1205 handle->name = kbd_name;
1207 input_open_device(handle);
1208 kbd_refresh_leds(handle);
1213 static void kbd_disconnect(struct input_handle *handle)
1215 input_close_device(handle);
1219 static struct input_device_id kbd_ids[] = {
1221 .flags = INPUT_DEVICE_ID_MATCH_EVBIT,
1222 .evbit = { BIT(EV_KEY) },
1226 .flags = INPUT_DEVICE_ID_MATCH_EVBIT,
1227 .evbit = { BIT(EV_SND) },
1230 { }, /* Terminating entry */
1233 MODULE_DEVICE_TABLE(input, kbd_ids);
1235 static struct input_handler kbd_handler = {
1237 .connect = kbd_connect,
1238 .disconnect = kbd_disconnect,
1240 .id_table = kbd_ids,
1243 int __init kbd_init(void)
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;
1254 for (i = 0 ; i < MAX_NR_CONSOLES ; i++)
1255 kbd_table[i] = kbd0;
1257 input_register_handler(&kbd_handler);
1259 tasklet_enable(&keyboard_tasklet);
1260 tasklet_schedule(&keyboard_tasklet);