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.
26 #include <linux/config.h>
27 #include <linux/module.h>
28 #include <linux/sched.h>
29 #include <linux/tty.h>
30 #include <linux/tty_flip.h>
32 #include <linux/string.h>
33 #include <linux/random.h>
34 #include <linux/init.h>
36 #include <asm/keyboard.h>
37 #include <asm/bitops.h>
39 #include <linux/kbd_kern.h>
40 #include <linux/kbd_diacr.h>
41 #include <linux/vt_kern.h>
42 #include <linux/kbd_ll.h>
43 #include <linux/sysrq.h>
46 #define SIZE(x) (sizeof(x)/sizeof((x)[0]))
49 #define KBD_DEFMODE ((1 << VC_REPEAT) | (1 << VC_META))
54 * Some laptops take the 789uiojklm,. keys as number pad when NumLock
55 * is on. This seems a good reason to start with NumLock off.
64 void (*kbd_ledfunc
)(unsigned int led
) = NULL
;
65 EXPORT_SYMBOL(handle_scancode
);
66 EXPORT_SYMBOL(kbd_ledfunc
);
68 extern void ctrl_alt_del(void);
70 DECLARE_WAIT_QUEUE_HEAD(keypress_wait
);
73 int keyboard_wait_for_keypress(struct console
*co
)
75 sleep_on(&keypress_wait
);
80 * global state includes the following, and various static variables
81 * in this module: prev_scancode, shift_state, diacr, npadch, dead_key_next.
82 * (last_console is now a global variable)
85 /* shift state counters.. */
86 static unsigned char k_down
[NR_SHIFT
] = {0, };
87 /* keyboard key bitmap */
88 static unsigned long key_down
[256/BITS_PER_LONG
] = { 0, };
90 static int dead_key_next
= 0;
92 * In order to retrieve the shift_state (for the mouse server), either
93 * the variable must be global, or a new procedure must be created to
94 * return the value. I chose the former way.
97 static int npadch
= -1; /* -1 or number assembled on pad */
98 static unsigned char diacr
= 0;
99 static char rep
= 0; /* flag telling character repeat */
100 struct kbd_struct kbd_table
[MAX_NR_CONSOLES
];
101 static struct tty_struct
**ttytab
;
102 static struct kbd_struct
* kbd
= kbd_table
;
103 static struct tty_struct
* tty
= NULL
;
105 void compute_shiftstate(void);
107 typedef void (*k_hand
)(unsigned char value
, char up_flag
);
108 typedef void (k_handfn
)(unsigned char value
, char up_flag
);
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
, do_dead2
,
115 static k_hand key_handler
[16] = {
116 do_self
, do_fn
, do_spec
, do_pad
, do_dead
, do_cons
, do_cur
, do_shift
,
117 do_meta
, do_ascii
, do_lock
, do_lowercase
, do_slock
, do_dead2
,
121 /* Key types processed even in raw modes */
123 #define TYPES_ALLOWED_IN_RAW_MODE ((1 << KT_SPEC) | (1 << KT_SHIFT))
125 typedef void (*void_fnp
)(void);
126 typedef void (void_fn
)(void);
128 static void_fn do_null
, enter
, show_ptregs
, send_intr
, lastcons
, caps_toggle
,
129 num
, hold
, scroll_forw
, scroll_back
, boot_it
, caps_on
, compose
,
130 SAK
, decr_console
, incr_console
, spawn_console
, bare_num
;
132 static void_fnp spec_fn_table
[] = {
133 do_null
, enter
, show_ptregs
, show_mem
,
134 show_state
, send_intr
, lastcons
, caps_toggle
,
135 num
, hold
, scroll_forw
, scroll_back
,
136 boot_it
, caps_on
, compose
, SAK
,
137 decr_console
, incr_console
, spawn_console
, bare_num
140 #define SPECIALS_ALLOWED_IN_RAW_MODE (1 << KVAL(K_SAK))
142 /* maximum values each key_handler can handle */
143 const int max_vals
[] = {
144 255, SIZE(func_table
) - 1, SIZE(spec_fn_table
) - 1, NR_PAD
- 1,
145 NR_DEAD
- 1, 255, 3, NR_SHIFT
- 1,
146 255, NR_ASCII
- 1, NR_LOCK
- 1, 255,
150 const int NR_TYPES
= SIZE(max_vals
);
152 /* N.B. drivers/macintosh/mac_keyb.c needs to call put_queue */
154 static unsigned char handle_diacr(unsigned char);
156 /* kbd_pt_regs - set by keyboard_interrupt(), used by show_ptregs() */
157 struct pt_regs
* kbd_pt_regs
;
159 #ifdef CONFIG_MAGIC_SYSRQ
160 static int sysrq_pressed
;
161 int sysrq_enabled
= 1;
164 static struct pm_dev
*pm_kbd
= NULL
;
167 * Many other routines do put_queue, but I think either
168 * they produce ASCII, or they produce some user-assigned
169 * string, and in both cases we might assume that it is
172 void to_utf8(ushort c
) {
174 put_queue(c
); /* 0******* */
175 else if (c
< 0x800) {
176 put_queue(0xc0 | (c
>> 6)); /* 110***** 10****** */
177 put_queue(0x80 | (c
& 0x3f));
179 put_queue(0xe0 | (c
>> 12)); /* 1110**** 10****** 10****** */
180 put_queue(0x80 | ((c
>> 6) & 0x3f));
181 put_queue(0x80 | (c
& 0x3f));
183 /* UTF-8 is defined for words of up to 31 bits,
184 but we need only 16 bits here */
188 * Translation of escaped scancodes to keycodes.
189 * This is now user-settable (for machines were it makes sense).
192 int setkeycode(unsigned int scancode
, unsigned int keycode
)
194 return kbd_setkeycode(scancode
, keycode
);
197 int getkeycode(unsigned int scancode
)
199 return kbd_getkeycode(scancode
);
202 void handle_scancode(unsigned char scancode
, int down
)
204 unsigned char keycode
;
205 char up_flag
= down
? 0 : 0200;
210 do_poke_blanked_console
= 1;
211 tasklet_schedule(&console_tasklet
);
212 add_keyboard_randomness(scancode
| up_flag
);
214 tty
= ttytab
? ttytab
[fg_console
]: NULL
;
215 if (tty
&& (!tty
->driver_data
)) {
217 * We touch the tty structure via the the ttytab array
218 * without knowing whether or not tty is open, which
219 * is inherently dangerous. We currently rely on that
220 * fact that console_open sets tty->driver_data when
221 * it opens it, and clears it when it closes it.
225 kbd
= kbd_table
+ fg_console
;
226 if ((raw_mode
= (kbd
->kbdmode
== VC_RAW
))) {
227 put_queue(scancode
| up_flag
);
228 /* we do not return yet, because we want to maintain
229 the key_down array, so that we have the correct
230 values when finishing RAW mode or when changing VT's */
234 * Convert scancode to keycode
236 if (!kbd_translate(scancode
, &keycode
, raw_mode
))
240 * At this point the variable `keycode' contains the keycode.
241 * Note: the keycode must not be 0 (++Geert: on m68k 0 is valid).
242 * We keep track of the up/down status of the key, and
243 * return the keycode if in MEDIUMRAW mode.
248 if(!test_and_clear_bit(keycode
, key_down
))
249 up_flag
= kbd_unexpected_up(keycode
);
251 rep
= test_and_set_bit(keycode
, key_down
);
253 #ifdef CONFIG_MAGIC_SYSRQ /* Handle the SysRq Hack */
254 if (keycode
== SYSRQ_KEY
) {
255 sysrq_pressed
= !up_flag
;
257 } else if (sysrq_pressed
) {
258 if (!up_flag
&& sysrq_enabled
) {
259 handle_sysrq(kbd_sysrq_xlate
[keycode
], kbd_pt_regs
, kbd
, tty
);
265 if (kbd
->kbdmode
== VC_MEDIUMRAW
) {
266 /* soon keycodes will require more than one byte */
267 put_queue(keycode
+ up_flag
);
268 raw_mode
= 1; /* Most key classes will be ignored */
272 * Small change in philosophy: earlier we defined repetition by
273 * rep = keycode == prev_keycode;
274 * prev_keycode = keycode;
275 * but now by the fact that the depressed key was down already.
276 * Does this ever make a difference? Yes.
280 * Repeat a key only if the input buffers are empty or the
281 * characters get echoed locally. This makes key repeat usable
282 * with slow applications and under heavy loads.
285 (vc_kbd_mode(kbd
,VC_REPEAT
) && tty
&&
286 (L_ECHO(tty
) || (tty
->driver
.chars_in_buffer(tty
) == 0)))) {
290 /* the XOR below used to be an OR */
291 int shift_final
= (shift_state
| kbd
->slockstate
) ^
293 ushort
*key_map
= key_maps
[shift_final
];
295 if (key_map
!= NULL
) {
296 keysym
= key_map
[keycode
];
301 if (raw_mode
&& ! (TYPES_ALLOWED_IN_RAW_MODE
& (1 << type
)))
303 if (type
== KT_LETTER
) {
305 if (vc_kbd_led(kbd
, VC_CAPSLOCK
)) {
306 key_map
= key_maps
[shift_final
^ (1<<KG_SHIFT
)];
308 keysym
= key_map
[keycode
];
311 (*key_handler
[type
])(keysym
& 0xff, up_flag
);
312 if (type
!= KT_SLOCK
)
315 /* maybe only if (kbd->kbdmode == VC_UNICODE) ? */
316 if (!up_flag
&& !raw_mode
)
321 /* we have at least to update shift_state */
322 #if 1 /* how? two almost equivalent choices follow */
323 compute_shiftstate();
324 kbd
->slockstate
= 0; /* play it safe */
326 keysym
= U(plain_map
[keycode
]);
328 if (type
== KT_SHIFT
)
329 (*key_handler
[type
])(keysym
& 0xff, up_flag
);
335 #ifdef CONFIG_FORWARD_KEYBOARD
336 extern int forward_chars
;
338 void put_queue(int ch
)
340 if (forward_chars
== fg_console
+1){
341 kbd_forward_char (ch
);
343 wake_up(&keypress_wait
);
345 tty_insert_flip_char(tty
, ch
, 0);
346 con_schedule_flip(tty
);
351 void put_queue(int ch
)
353 wake_up(&keypress_wait
);
355 tty_insert_flip_char(tty
, ch
, 0);
356 con_schedule_flip(tty
);
361 static void puts_queue(char *cp
)
363 wake_up(&keypress_wait
);
368 tty_insert_flip_char(tty
, *cp
, 0);
371 con_schedule_flip(tty
);
374 static void applkey(int key
, char mode
)
376 static char buf
[] = { 0x1b, 'O', 0x00, 0x00 };
378 buf
[1] = (mode
? 'O' : '[');
383 static void enter(void)
390 if (vc_kbd_mode(kbd
,VC_CRLF
))
394 static void caps_toggle(void)
398 chg_vc_kbd_led(kbd
, VC_CAPSLOCK
);
401 static void caps_on(void)
405 set_vc_kbd_led(kbd
, VC_CAPSLOCK
);
408 static void show_ptregs(void)
411 show_regs(kbd_pt_regs
);
414 static void hold(void)
420 * Note: SCROLLOCK will be set (cleared) by stop_tty (start_tty);
421 * these routines are also activated by ^S/^Q.
422 * (And SCROLLOCK can also be set by the ioctl KDSKBLED.)
430 static void num(void)
432 if (vc_kbd_mode(kbd
,VC_APPLIC
))
439 * Bind this to Shift-NumLock if you work in application keypad mode
440 * but want to be able to change the NumLock flag.
441 * Bind this to NumLock if you prefer that the NumLock key always
442 * changes the NumLock flag.
444 static void bare_num(void)
447 chg_vc_kbd_led(kbd
,VC_NUMLOCK
);
450 static void lastcons(void)
452 /* switch to the last used console, ChN */
453 set_console(last_console
);
456 static void decr_console(void)
460 for (i
= fg_console
-1; i
!= fg_console
; i
--) {
462 i
= MAX_NR_CONSOLES
-1;
463 if (vc_cons_allocated(i
))
469 static void incr_console(void)
473 for (i
= fg_console
+1; i
!= fg_console
; i
++) {
474 if (i
== MAX_NR_CONSOLES
)
476 if (vc_cons_allocated(i
))
482 static void send_intr(void)
486 tty_insert_flip_char(tty
, 0, TTY_BREAK
);
487 con_schedule_flip(tty
);
490 static void scroll_forw(void)
495 static void scroll_back(void)
500 static void boot_it(void)
505 static void compose(void)
510 int spawnpid
, spawnsig
;
512 static void spawn_console(void)
515 if(kill_proc(spawnpid
, spawnsig
, 1))
519 static void SAK(void)
522 * SAK should also work in all raw modes and reset
527 reset_vc(fg_console
);
529 do_unblank_screen(); /* not in interrupt routine? */
533 static void do_ignore(unsigned char value
, char up_flag
)
537 static void do_null()
539 compute_shiftstate();
542 static void do_spec(unsigned char value
, char up_flag
)
546 if (value
>= SIZE(spec_fn_table
))
548 if ((kbd
->kbdmode
== VC_RAW
|| kbd
->kbdmode
== VC_MEDIUMRAW
) &&
549 !(SPECIALS_ALLOWED_IN_RAW_MODE
& (1 << value
)))
551 spec_fn_table
[value
]();
554 static void do_lowercase(unsigned char value
, char up_flag
)
556 printk(KERN_ERR
"keyboard.c: do_lowercase was called - impossible\n");
559 static void do_self(unsigned char value
, char up_flag
)
562 return; /* no action, if this is a key release */
565 value
= handle_diacr(value
);
582 static unsigned char ret_diacr
[NR_DEAD
] =
583 {A_GRAVE
, A_ACUTE
, A_CFLEX
, A_TILDE
, A_DIAER
, A_CEDIL
};
585 /* Obsolete - for backwards compatibility only */
586 static void do_dead(unsigned char value
, char up_flag
)
588 value
= ret_diacr
[value
];
589 do_dead2(value
,up_flag
);
593 * Handle dead key. Note that we now may have several
594 * dead keys modifying the same character. Very useful
597 static void do_dead2(unsigned char value
, char up_flag
)
602 diacr
= (diacr
? handle_diacr(value
) : value
);
607 * We have a combining character DIACR here, followed by the character CH.
608 * If the combination occurs in the table, return the corresponding value.
609 * Otherwise, if CH is a space or equals DIACR, return DIACR.
610 * Otherwise, conclude that DIACR was not combining after all,
611 * queue it and return CH.
613 unsigned char handle_diacr(unsigned char ch
)
620 for (i
= 0; i
< accent_table_size
; i
++) {
621 if (accent_table
[i
].diacr
== d
&& accent_table
[i
].base
== ch
)
622 return accent_table
[i
].result
;
625 if (ch
== ' ' || ch
== d
)
632 static void do_cons(unsigned char value
, char up_flag
)
639 static void do_fn(unsigned char value
, char up_flag
)
643 if (value
< SIZE(func_table
)) {
644 if (func_table
[value
])
645 puts_queue(func_table
[value
]);
647 printk(KERN_ERR
"do_fn called with value=%d\n", value
);
650 static void do_pad(unsigned char value
, char up_flag
)
652 static const char *pad_chars
= "0123456789+-*/\015,.?()";
653 static const char *app_map
= "pqrstuvwxylSRQMnnmPQ";
656 return; /* no action, if this is a key release */
658 /* kludge... shift forces cursor/number keys */
659 if (vc_kbd_mode(kbd
,VC_APPLIC
) && !k_down
[KG_SHIFT
]) {
660 applkey(app_map
[value
], 1);
664 if (!vc_kbd_led(kbd
,VC_NUMLOCK
))
668 do_fn(KVAL(K_REMOVE
), 0);
671 do_fn(KVAL(K_INSERT
), 0);
674 do_fn(KVAL(K_SELECT
), 0);
677 do_cur(KVAL(K_DOWN
), 0);
680 do_fn(KVAL(K_PGDN
), 0);
683 do_cur(KVAL(K_LEFT
), 0);
686 do_cur(KVAL(K_RIGHT
), 0);
689 do_fn(KVAL(K_FIND
), 0);
692 do_cur(KVAL(K_UP
), 0);
695 do_fn(KVAL(K_PGUP
), 0);
698 applkey('G', vc_kbd_mode(kbd
, VC_APPLIC
));
702 put_queue(pad_chars
[value
]);
703 if (value
== KVAL(K_PENTER
) && vc_kbd_mode(kbd
, VC_CRLF
))
707 static void do_cur(unsigned char value
, char up_flag
)
709 static const char *cur_chars
= "BDCA";
713 applkey(cur_chars
[value
], vc_kbd_mode(kbd
,VC_CKMODE
));
716 static void do_shift(unsigned char value
, char up_flag
)
718 int old_state
= shift_state
;
724 a CapsShift key acts like Shift but undoes CapsLock */
725 if (value
== KVAL(K_CAPSSHIFT
)) {
726 value
= KVAL(K_SHIFT
);
728 clr_vc_kbd_led(kbd
, VC_CAPSLOCK
);
732 /* handle the case that two shift or control
733 keys are depressed simultaneously */
740 shift_state
|= (1 << value
);
742 shift_state
&= ~ (1 << value
);
745 if (up_flag
&& shift_state
!= old_state
&& npadch
!= -1) {
746 if (kbd
->kbdmode
== VC_UNICODE
)
747 to_utf8(npadch
& 0xffff);
749 put_queue(npadch
& 0xff);
754 /* called after returning from RAW mode or when changing consoles -
755 recompute k_down[] and shift_state from key_down[] */
756 /* maybe called when keymap is undefined, so that shiftkey release is seen */
757 void compute_shiftstate(void)
759 int i
, j
, k
, sym
, val
;
762 for(i
=0; i
< SIZE(k_down
); i
++)
765 for(i
=0; i
< SIZE(key_down
); i
++)
766 if(key_down
[i
]) { /* skip this word if not a single bit on */
768 for(j
=0; j
<BITS_PER_LONG
; j
++,k
++)
769 if(test_bit(k
, key_down
)) {
770 sym
= U(plain_map
[k
]);
771 if(KTYP(sym
) == KT_SHIFT
|| KTYP(sym
) == KT_SLOCK
) {
773 if (val
== KVAL(K_CAPSSHIFT
))
776 shift_state
|= (1<<val
);
782 static void do_meta(unsigned char value
, char up_flag
)
787 if (vc_kbd_mode(kbd
, VC_META
)) {
791 put_queue(value
| 0x80);
794 static void do_ascii(unsigned char value
, char up_flag
)
801 if (value
< 10) /* decimal input of code, while Alt depressed */
803 else { /* hexadecimal input of code, while AltGr depressed */
811 npadch
= npadch
* base
+ value
;
814 static void do_lock(unsigned char value
, char up_flag
)
818 chg_vc_kbd_lock(kbd
, value
);
821 static void do_slock(unsigned char value
, char up_flag
)
823 do_shift(value
,up_flag
);
826 chg_vc_kbd_slock(kbd
, value
);
827 /* try to make Alt, oops, AltGr and such work */
828 if (!key_maps
[kbd
->lockstate
^ kbd
->slockstate
]) {
830 chg_vc_kbd_slock(kbd
, value
);
835 * The leds display either (i) the status of NumLock, CapsLock, ScrollLock,
836 * or (ii) whatever pattern of lights people want to show using KDSETLED,
837 * or (iii) specified bits of specified words in kernel memory.
840 static unsigned char ledstate
= 0xff; /* undefined */
841 static unsigned char ledioctl
;
843 unsigned char getledstate(void) {
847 void setledstate(struct kbd_struct
*kbd
, unsigned int led
) {
850 kbd
->ledmode
= LED_SHOW_IOCTL
;
852 kbd
->ledmode
= LED_SHOW_FLAGS
;
856 static struct ledptr
{
859 unsigned char valid
:1;
862 void register_leds(int console
, unsigned int led
,
863 unsigned int *addr
, unsigned int mask
) {
864 struct kbd_struct
*kbd
= kbd_table
+ console
;
866 ledptrs
[led
].addr
= addr
;
867 ledptrs
[led
].mask
= mask
;
868 ledptrs
[led
].valid
= 1;
869 kbd
->ledmode
= LED_SHOW_MEM
;
871 kbd
->ledmode
= LED_SHOW_FLAGS
;
874 static inline unsigned char getleds(void){
875 struct kbd_struct
*kbd
= kbd_table
+ fg_console
;
878 if (kbd
->ledmode
== LED_SHOW_IOCTL
)
880 leds
= kbd
->ledflagstate
;
881 if (kbd
->ledmode
== LED_SHOW_MEM
) {
882 if (ledptrs
[0].valid
) {
883 if (*ledptrs
[0].addr
& ledptrs
[0].mask
)
888 if (ledptrs
[1].valid
) {
889 if (*ledptrs
[1].addr
& ledptrs
[1].mask
)
894 if (ledptrs
[2].valid
) {
895 if (*ledptrs
[2].addr
& ledptrs
[2].mask
)
905 * This routine is the bottom half of the keyboard interrupt
906 * routine, and runs with all interrupts enabled. It does
907 * console changing, led setting and copy_to_cooked, which can
908 * take a reasonably long time.
910 * Aside from timing (which isn't really that important for
911 * keyboard interrupts as they happen often), using the software
912 * interrupt routines for this thing allows us to easily mask
913 * this when we don't want any of the above to happen. Not yet
914 * used, but this allows for easy and efficient race-condition
915 * prevention later on.
917 static void kbd_bh(unsigned long dummy
)
919 unsigned char leds
= getleds();
921 if (leds
!= ledstate
) {
924 if (kbd_ledfunc
) kbd_ledfunc(leds
);
928 EXPORT_SYMBOL(keyboard_tasklet
);
929 DECLARE_TASKLET_DISABLED(keyboard_tasklet
, kbd_bh
, 0);
931 int __init
kbd_init(void)
934 struct kbd_struct kbd0
;
935 extern struct tty_driver console_driver
;
937 kbd0
.ledflagstate
= kbd0
.default_ledflagstate
= KBD_DEFLEDS
;
938 kbd0
.ledmode
= LED_SHOW_FLAGS
;
939 kbd0
.lockstate
= KBD_DEFLOCK
;
941 kbd0
.modeflags
= KBD_DEFMODE
;
942 kbd0
.kbdmode
= VC_XLATE
;
944 for (i
= 0 ; i
< MAX_NR_CONSOLES
; i
++)
947 ttytab
= console_driver
.table
;
951 tasklet_enable(&keyboard_tasklet
);
952 tasklet_schedule(&keyboard_tasklet
);
954 pm_kbd
= pm_register(PM_SYS_DEV
, PM_SYS_KBC
, NULL
);