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.
15 * 11-11-96: SAK should now work in the raw mode (Martin Mares)
17 * Modified to provide 'generic' keyboard support by Hamish Macdonald
18 * Merge with the m68k keyboard driver and split-off of the PC low-level
19 * parts by Geert Uytterhoeven, May 1997
21 * 27-05-97: Added support for the Magic SysRq Key (Martin Mares)
24 #include <linux/config.h>
25 #include <linux/sched.h>
26 #include <linux/tty.h>
27 #include <linux/tty_flip.h>
29 #include <linux/string.h>
30 #include <linux/random.h>
31 #include <linux/init.h>
33 #include <asm/keyboard.h>
34 #include <asm/bitops.h>
36 #include <linux/kbd_kern.h>
37 #include <linux/kbd_diacr.h>
38 #include <linux/vt_kern.h>
39 #include <linux/kbd_ll.h>
40 #include <linux/sysrq.h>
42 #define SIZE(x) (sizeof(x)/sizeof((x)[0]))
45 #define KBD_DEFMODE ((1 << VC_REPEAT) | (1 << VC_META))
50 * Some laptops take the 789uiojklm,. keys as number pad when NumLock
51 * is on. This seems a good reason to start with NumLock off.
60 extern void ctrl_alt_del(void);
61 extern void reset_vc(unsigned int new_console
);
62 extern void scrollback(int);
63 extern void scrollfront(int);
65 struct wait_queue
* keypress_wait
= NULL
;
68 int keyboard_wait_for_keypress(struct console
*co
)
70 sleep_on(&keypress_wait
);
75 * global state includes the following, and various static variables
76 * in this module: prev_scancode, shift_state, diacr, npadch, dead_key_next.
77 * (last_console is now a global variable)
80 /* shift state counters.. */
81 static unsigned char k_down
[NR_SHIFT
] = {0, };
82 /* keyboard key bitmap */
83 #define BITS_PER_LONG (8*sizeof(unsigned long))
84 static unsigned long key_down
[256/BITS_PER_LONG
] = { 0, };
86 static int dead_key_next
= 0;
88 * In order to retrieve the shift_state (for the mouse server), either
89 * the variable must be global, or a new procedure must be created to
90 * return the value. I chose the former way.
93 static int npadch
= -1; /* -1 or number assembled on pad */
94 static unsigned char diacr
= 0;
95 static char rep
= 0; /* flag telling character repeat */
96 struct kbd_struct kbd_table
[MAX_NR_CONSOLES
];
97 static struct tty_struct
**ttytab
;
98 static struct kbd_struct
* kbd
= kbd_table
;
99 static struct tty_struct
* tty
= NULL
;
101 void compute_shiftstate(void);
103 typedef void (*k_hand
)(unsigned char value
, char up_flag
);
104 typedef void (k_handfn
)(unsigned char value
, char up_flag
);
107 do_self
, do_fn
, do_spec
, do_pad
, do_dead
, do_cons
, do_cur
, do_shift
,
108 do_meta
, do_ascii
, do_lock
, do_lowercase
, do_slock
, do_ignore
;
110 static k_hand key_handler
[16] = {
111 do_self
, do_fn
, do_spec
, do_pad
, do_dead
, do_cons
, do_cur
, do_shift
,
112 do_meta
, do_ascii
, do_lock
, do_lowercase
, do_slock
,
113 do_ignore
, do_ignore
, do_ignore
116 /* Key types processed even in raw modes */
118 #define TYPES_ALLOWED_IN_RAW_MODE ((1 << KT_SPEC) | (1 << KT_SHIFT))
120 typedef void (*void_fnp
)(void);
121 typedef void (void_fn
)(void);
123 static void_fn do_null
, enter
, show_ptregs
, send_intr
, lastcons
, caps_toggle
,
124 num
, hold
, scroll_forw
, scroll_back
, boot_it
, caps_on
, compose
,
125 SAK
, decr_console
, incr_console
, spawn_console
, bare_num
;
127 static void_fnp spec_fn_table
[] = {
128 do_null
, enter
, show_ptregs
, show_mem
,
129 show_state
, send_intr
, lastcons
, caps_toggle
,
130 num
, hold
, scroll_forw
, scroll_back
,
131 boot_it
, caps_on
, compose
, SAK
,
132 decr_console
, incr_console
, spawn_console
, bare_num
135 #define SPECIALS_ALLOWED_IN_RAW_MODE (1 << KVAL(K_SAK))
137 /* maximum values each key_handler can handle */
138 const int max_vals
[] = {
139 255, SIZE(func_table
) - 1, SIZE(spec_fn_table
) - 1, NR_PAD
- 1,
140 NR_DEAD
- 1, 255, 3, NR_SHIFT
- 1,
141 255, NR_ASCII
- 1, NR_LOCK
- 1, 255,
145 const int NR_TYPES
= SIZE(max_vals
);
147 static void put_queue(int);
148 static unsigned char handle_diacr(unsigned char);
150 /* kbd_pt_regs - set by keyboard_interrupt(), used by show_ptregs() */
151 struct pt_regs
* kbd_pt_regs
;
153 #ifdef CONFIG_MAGIC_SYSRQ
154 static int sysrq_pressed
;
158 * Many other routines do put_queue, but I think either
159 * they produce ASCII, or they produce some user-assigned
160 * string, and in both cases we might assume that it is
163 void to_utf8(ushort c
) {
165 put_queue(c
); /* 0******* */
166 else if (c
< 0x800) {
167 put_queue(0xc0 | (c
>> 6)); /* 110***** 10****** */
168 put_queue(0x80 | (c
& 0x3f));
170 put_queue(0xe0 | (c
>> 12)); /* 1110**** 10****** 10****** */
171 put_queue(0x80 | ((c
>> 6) & 0x3f));
172 put_queue(0x80 | (c
& 0x3f));
174 /* UTF-8 is defined for words of up to 31 bits,
175 but we need only 16 bits here */
179 * Translation of escaped scancodes to keycodes.
180 * This is now user-settable (for machines were it makes sense).
183 int setkeycode(unsigned int scancode
, unsigned int keycode
)
185 return kbd_setkeycode(scancode
, keycode
);
188 int getkeycode(unsigned int scancode
)
190 return kbd_getkeycode(scancode
);
193 void handle_scancode(unsigned char scancode
)
195 unsigned char keycode
;
196 char up_flag
; /* 0 or 0200 */
199 do_poke_blanked_console
= 1;
201 add_keyboard_randomness(scancode
);
203 tty
= ttytab
[fg_console
];
204 kbd
= kbd_table
+ fg_console
;
205 if ((raw_mode
= (kbd
->kbdmode
== VC_RAW
))) {
207 /* we do not return yet, because we want to maintain
208 the key_down array, so that we have the correct
209 values when finishing RAW mode or when changing VT's */
212 if (!kbd_pretranslate(scancode
, raw_mode
))
215 * Convert scancode to keycode
217 up_flag
= (scancode
& 0200);
220 if (!kbd_translate(scancode
, &keycode
, raw_mode
))
224 * At this point the variable `keycode' contains the keycode.
225 * Note: the keycode must not be 0 (++Geert: on m68k 0 is valid).
226 * We keep track of the up/down status of the key, and
227 * return the keycode if in MEDIUMRAW mode.
232 if(!test_and_clear_bit(keycode
, key_down
))
233 up_flag
= kbd_unexpected_up(keycode
);
235 rep
= test_and_set_bit(keycode
, key_down
);
237 #ifdef CONFIG_MAGIC_SYSRQ /* Handle the SysRq Hack */
238 if (keycode
== SYSRQ_KEY
) {
239 sysrq_pressed
= !up_flag
;
241 } else if (sysrq_pressed
) {
243 handle_sysrq(kbd_sysrq_xlate
[keycode
], kbd_pt_regs
, kbd
, tty
);
248 if (kbd
->kbdmode
== VC_MEDIUMRAW
) {
249 /* soon keycodes will require more than one byte */
250 put_queue(keycode
+ up_flag
);
251 raw_mode
= 1; /* Most key classes will be ignored */
255 * Small change in philosophy: earlier we defined repetition by
256 * rep = keycode == prev_keycode;
257 * prev_keycode = keycode;
258 * but now by the fact that the depressed key was down already.
259 * Does this ever make a difference? Yes.
263 * Repeat a key only if the input buffers are empty or the
264 * characters get echoed locally. This makes key repeat usable
265 * with slow applications and under heavy loads.
268 (vc_kbd_mode(kbd
,VC_REPEAT
) && tty
&&
269 (L_ECHO(tty
) || (tty
->driver
.chars_in_buffer(tty
) == 0)))) {
273 /* the XOR below used to be an OR */
274 int shift_final
= shift_state
^ kbd
->lockstate
^ kbd
->slockstate
;
275 ushort
*key_map
= key_maps
[shift_final
];
277 if (key_map
!= NULL
) {
278 keysym
= key_map
[keycode
];
283 if (raw_mode
&& ! (TYPES_ALLOWED_IN_RAW_MODE
& (1 << type
)))
285 if (type
== KT_LETTER
) {
287 if (vc_kbd_led(kbd
, VC_CAPSLOCK
)) {
288 key_map
= key_maps
[shift_final
^ (1<<KG_SHIFT
)];
290 keysym
= key_map
[keycode
];
293 (*key_handler
[type
])(keysym
& 0xff, up_flag
);
294 if (type
!= KT_SLOCK
)
297 /* maybe only if (kbd->kbdmode == VC_UNICODE) ? */
298 if (!up_flag
&& !raw_mode
)
303 /* we have at least to update shift_state */
304 #if 1 /* how? two almost equivalent choices follow */
305 compute_shiftstate();
307 keysym
= U(plain_map
[keycode
]);
309 if (type
== KT_SHIFT
)
310 (*key_handler
[type
])(keysym
& 0xff, up_flag
);
317 static void put_queue(int ch
)
319 wake_up(&keypress_wait
);
321 tty_insert_flip_char(tty
, ch
, 0);
322 tty_schedule_flip(tty
);
326 static void puts_queue(char *cp
)
328 wake_up(&keypress_wait
);
333 tty_insert_flip_char(tty
, *cp
, 0);
336 tty_schedule_flip(tty
);
339 static void applkey(int key
, char mode
)
341 static char buf
[] = { 0x1b, 'O', 0x00, 0x00 };
343 buf
[1] = (mode
? 'O' : '[');
348 static void enter(void)
351 if (vc_kbd_mode(kbd
,VC_CRLF
))
355 static void caps_toggle(void)
359 chg_vc_kbd_led(kbd
, VC_CAPSLOCK
);
362 static void caps_on(void)
366 set_vc_kbd_led(kbd
, VC_CAPSLOCK
);
369 static void show_ptregs(void)
372 show_regs(kbd_pt_regs
);
375 static void hold(void)
381 * Note: SCROLLOCK will be set (cleared) by stop_tty (start_tty);
382 * these routines are also activated by ^S/^Q.
383 * (And SCROLLOCK can also be set by the ioctl KDSKBLED.)
391 static void num(void)
393 if (vc_kbd_mode(kbd
,VC_APPLIC
))
400 * Bind this to Shift-NumLock if you work in application keypad mode
401 * but want to be able to change the NumLock flag.
402 * Bind this to NumLock if you prefer that the NumLock key always
403 * changes the NumLock flag.
405 static void bare_num(void)
408 chg_vc_kbd_led(kbd
,VC_NUMLOCK
);
411 static void lastcons(void)
413 /* switch to the last used console, ChN */
414 set_console(last_console
);
417 static void decr_console(void)
421 for (i
= fg_console
-1; i
!= fg_console
; i
--) {
423 i
= MAX_NR_CONSOLES
-1;
424 if (vc_cons_allocated(i
))
430 static void incr_console(void)
434 for (i
= fg_console
+1; i
!= fg_console
; i
++) {
435 if (i
== MAX_NR_CONSOLES
)
437 if (vc_cons_allocated(i
))
443 static void send_intr(void)
447 tty_insert_flip_char(tty
, 0, TTY_BREAK
);
448 tty_schedule_flip(tty
);
451 static void scroll_forw(void)
456 static void scroll_back(void)
461 static void boot_it(void)
466 static void compose(void)
471 int spawnpid
, spawnsig
;
473 static void spawn_console(void)
476 if(kill_proc(spawnpid
, spawnsig
, 1))
480 static void SAK(void)
483 * SAK should also work in all raw modes and reset
488 reset_vc(fg_console
);
490 do_unblank_screen(); /* not in interrupt routine? */
494 static void do_ignore(unsigned char value
, char up_flag
)
498 static void do_null()
500 compute_shiftstate();
503 static void do_spec(unsigned char value
, char up_flag
)
507 if (value
>= SIZE(spec_fn_table
))
509 if ((kbd
->kbdmode
== VC_RAW
|| kbd
->kbdmode
== VC_MEDIUMRAW
) &&
510 !(SPECIALS_ALLOWED_IN_RAW_MODE
& (1 << value
)))
512 spec_fn_table
[value
]();
515 static void do_lowercase(unsigned char value
, char up_flag
)
517 printk(KERN_ERR
"keyboard.c: do_lowercase was called - impossible\n");
520 static void do_self(unsigned char value
, char up_flag
)
523 return; /* no action, if this is a key release */
526 value
= handle_diacr(value
);
543 static unsigned char ret_diacr
[NR_DEAD
] =
544 {A_GRAVE
, A_ACUTE
, A_CFLEX
, A_TILDE
, A_DIAER
, A_CEDIL
};
546 /* If a dead key pressed twice, output a character corresponding to it, */
547 /* otherwise just remember the dead key. */
549 static void do_dead(unsigned char value
, char up_flag
)
554 value
= ret_diacr
[value
];
555 if (diacr
== value
) { /* pressed twice */
564 /* If space is pressed, return the character corresponding the pending */
565 /* dead key, otherwise try to combine the two. */
567 unsigned char handle_diacr(unsigned char ch
)
576 for (i
= 0; i
< accent_table_size
; i
++) {
577 if (accent_table
[i
].diacr
== d
&& accent_table
[i
].base
== ch
)
578 return accent_table
[i
].result
;
585 static void do_cons(unsigned char value
, char up_flag
)
592 static void do_fn(unsigned char value
, char up_flag
)
596 if (value
< SIZE(func_table
)) {
597 if (func_table
[value
])
598 puts_queue(func_table
[value
]);
600 printk(KERN_ERR
"do_fn called with value=%d\n", value
);
603 static void do_pad(unsigned char value
, char up_flag
)
605 static const char *pad_chars
= "0123456789+-*/\015,.?()";
606 static const char *app_map
= "pqrstuvwxylSRQMnnmPQ";
609 return; /* no action, if this is a key release */
611 /* kludge... shift forces cursor/number keys */
612 if (vc_kbd_mode(kbd
,VC_APPLIC
) && !k_down
[KG_SHIFT
]) {
613 applkey(app_map
[value
], 1);
617 if (!vc_kbd_led(kbd
,VC_NUMLOCK
))
621 do_fn(KVAL(K_REMOVE
), 0);
624 do_fn(KVAL(K_INSERT
), 0);
627 do_fn(KVAL(K_SELECT
), 0);
630 do_cur(KVAL(K_DOWN
), 0);
633 do_fn(KVAL(K_PGDN
), 0);
636 do_cur(KVAL(K_LEFT
), 0);
639 do_cur(KVAL(K_RIGHT
), 0);
642 do_fn(KVAL(K_FIND
), 0);
645 do_cur(KVAL(K_UP
), 0);
648 do_fn(KVAL(K_PGUP
), 0);
651 applkey('G', vc_kbd_mode(kbd
, VC_APPLIC
));
655 put_queue(pad_chars
[value
]);
656 if (value
== KVAL(K_PENTER
) && vc_kbd_mode(kbd
, VC_CRLF
))
660 static void do_cur(unsigned char value
, char up_flag
)
662 static const char *cur_chars
= "BDCA";
666 applkey(cur_chars
[value
], vc_kbd_mode(kbd
,VC_CKMODE
));
669 static void do_shift(unsigned char value
, char up_flag
)
671 int old_state
= shift_state
;
677 a CapsShift key acts like Shift but undoes CapsLock */
678 if (value
== KVAL(K_CAPSSHIFT
)) {
679 value
= KVAL(K_SHIFT
);
681 clr_vc_kbd_led(kbd
, VC_CAPSLOCK
);
685 /* handle the case that two shift or control
686 keys are depressed simultaneously */
693 shift_state
|= (1 << value
);
695 shift_state
&= ~ (1 << value
);
698 if (up_flag
&& shift_state
!= old_state
&& npadch
!= -1) {
699 if (kbd
->kbdmode
== VC_UNICODE
)
700 to_utf8(npadch
& 0xffff);
702 put_queue(npadch
& 0xff);
707 /* called after returning from RAW mode or when changing consoles -
708 recompute k_down[] and shift_state from key_down[] */
709 /* maybe called when keymap is undefined, so that shiftkey release is seen */
710 void compute_shiftstate(void)
712 int i
, j
, k
, sym
, val
;
715 for(i
=0; i
< SIZE(k_down
); i
++)
718 for(i
=0; i
< SIZE(key_down
); i
++)
719 if(key_down
[i
]) { /* skip this word if not a single bit on */
721 for(j
=0; j
<BITS_PER_LONG
; j
++,k
++)
722 if(test_bit(k
, key_down
)) {
723 sym
= U(plain_map
[k
]);
724 if(KTYP(sym
) == KT_SHIFT
) {
726 if (val
== KVAL(K_CAPSSHIFT
))
729 shift_state
|= (1<<val
);
735 static void do_meta(unsigned char value
, char up_flag
)
740 if (vc_kbd_mode(kbd
, VC_META
)) {
744 put_queue(value
| 0x80);
747 static void do_ascii(unsigned char value
, char up_flag
)
754 if (value
< 10) /* decimal input of code, while Alt depressed */
756 else { /* hexadecimal input of code, while AltGr depressed */
764 npadch
= npadch
* base
+ value
;
767 static void do_lock(unsigned char value
, char up_flag
)
771 chg_vc_kbd_lock(kbd
, value
);
774 static void do_slock(unsigned char value
, char up_flag
)
778 chg_vc_kbd_slock(kbd
, value
);
782 * The leds display either (i) the status of NumLock, CapsLock, ScrollLock,
783 * or (ii) whatever pattern of lights people want to show using KDSETLED,
784 * or (iii) specified bits of specified words in kernel memory.
787 static unsigned char ledstate
= 0xff; /* undefined */
788 static unsigned char ledioctl
;
790 unsigned char getledstate(void) {
794 void setledstate(struct kbd_struct
*kbd
, unsigned int led
) {
797 kbd
->ledmode
= LED_SHOW_IOCTL
;
799 kbd
->ledmode
= LED_SHOW_FLAGS
;
803 static struct ledptr
{
806 unsigned char valid
:1;
809 void register_leds(int console
, unsigned int led
,
810 unsigned int *addr
, unsigned int mask
) {
811 struct kbd_struct
*kbd
= kbd_table
+ console
;
813 ledptrs
[led
].addr
= addr
;
814 ledptrs
[led
].mask
= mask
;
815 ledptrs
[led
].valid
= 1;
816 kbd
->ledmode
= LED_SHOW_MEM
;
818 kbd
->ledmode
= LED_SHOW_FLAGS
;
821 static inline unsigned char getleds(void){
822 struct kbd_struct
*kbd
= kbd_table
+ fg_console
;
825 if (kbd
->ledmode
== LED_SHOW_IOCTL
)
827 leds
= kbd
->ledflagstate
;
828 if (kbd
->ledmode
== LED_SHOW_MEM
) {
829 if (ledptrs
[0].valid
) {
830 if (*ledptrs
[0].addr
& ledptrs
[0].mask
)
835 if (ledptrs
[1].valid
) {
836 if (*ledptrs
[1].addr
& ledptrs
[1].mask
)
841 if (ledptrs
[2].valid
) {
842 if (*ledptrs
[2].addr
& ledptrs
[2].mask
)
852 * This routine is the bottom half of the keyboard interrupt
853 * routine, and runs with all interrupts enabled. It does
854 * console changing, led setting and copy_to_cooked, which can
855 * take a reasonably long time.
857 * Aside from timing (which isn't really that important for
858 * keyboard interrupts as they happen often), using the software
859 * interrupt routines for this thing allows us to easily mask
860 * this when we don't want any of the above to happen. Not yet
861 * used, but this allows for easy and efficient race-condition
862 * prevention later on.
864 static void kbd_bh(void)
866 unsigned char leds
= getleds();
868 if (leds
!= ledstate
) {
874 __initfunc(int kbd_init(void))
877 struct kbd_struct kbd0
;
878 extern struct tty_driver console_driver
;
880 kbd0
.ledflagstate
= kbd0
.default_ledflagstate
= KBD_DEFLEDS
;
881 kbd0
.ledmode
= LED_SHOW_FLAGS
;
882 kbd0
.lockstate
= KBD_DEFLOCK
;
884 kbd0
.modeflags
= KBD_DEFMODE
;
885 kbd0
.kbdmode
= VC_XLATE
;
887 for (i
= 0 ; i
< MAX_NR_CONSOLES
; i
++)
890 ttytab
= console_driver
.table
;
893 init_bh(KEYBOARD_BH
, kbd_bh
);
894 mark_bh(KEYBOARD_BH
);