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
;
163 static struct pm_dev
*pm_kbd
= NULL
;
166 * Many other routines do put_queue, but I think either
167 * they produce ASCII, or they produce some user-assigned
168 * string, and in both cases we might assume that it is
171 void to_utf8(ushort c
) {
173 put_queue(c
); /* 0******* */
174 else if (c
< 0x800) {
175 put_queue(0xc0 | (c
>> 6)); /* 110***** 10****** */
176 put_queue(0x80 | (c
& 0x3f));
178 put_queue(0xe0 | (c
>> 12)); /* 1110**** 10****** 10****** */
179 put_queue(0x80 | ((c
>> 6) & 0x3f));
180 put_queue(0x80 | (c
& 0x3f));
182 /* UTF-8 is defined for words of up to 31 bits,
183 but we need only 16 bits here */
187 * Translation of escaped scancodes to keycodes.
188 * This is now user-settable (for machines were it makes sense).
191 int setkeycode(unsigned int scancode
, unsigned int keycode
)
193 return kbd_setkeycode(scancode
, keycode
);
196 int getkeycode(unsigned int scancode
)
198 return kbd_getkeycode(scancode
);
201 void handle_scancode(unsigned char scancode
, int down
)
203 unsigned char keycode
;
204 char up_flag
= down
? 0 : 0200;
209 do_poke_blanked_console
= 1;
210 tasklet_schedule(&console_tasklet
);
211 add_keyboard_randomness(scancode
| up_flag
);
213 tty
= ttytab
? ttytab
[fg_console
]: NULL
;
214 if (tty
&& (!tty
->driver_data
)) {
216 * We touch the tty structure via the the ttytab array
217 * without knowing whether or not tty is open, which
218 * is inherently dangerous. We currently rely on that
219 * fact that console_open sets tty->driver_data when
220 * it opens it, and clears it when it closes it.
224 kbd
= kbd_table
+ fg_console
;
225 if ((raw_mode
= (kbd
->kbdmode
== VC_RAW
))) {
226 put_queue(scancode
| up_flag
);
227 /* we do not return yet, because we want to maintain
228 the key_down array, so that we have the correct
229 values when finishing RAW mode or when changing VT's */
233 * Convert scancode to keycode
235 if (!kbd_translate(scancode
, &keycode
, raw_mode
))
239 * At this point the variable `keycode' contains the keycode.
240 * Note: the keycode must not be 0 (++Geert: on m68k 0 is valid).
241 * We keep track of the up/down status of the key, and
242 * return the keycode if in MEDIUMRAW mode.
247 if(!test_and_clear_bit(keycode
, key_down
))
248 up_flag
= kbd_unexpected_up(keycode
);
250 rep
= test_and_set_bit(keycode
, key_down
);
252 #ifdef CONFIG_MAGIC_SYSRQ /* Handle the SysRq Hack */
253 if (keycode
== SYSRQ_KEY
) {
254 sysrq_pressed
= !up_flag
;
256 } else if (sysrq_pressed
) {
258 handle_sysrq(kbd_sysrq_xlate
[keycode
], kbd_pt_regs
, kbd
, tty
);
264 if (kbd
->kbdmode
== VC_MEDIUMRAW
) {
265 /* soon keycodes will require more than one byte */
266 put_queue(keycode
+ up_flag
);
267 raw_mode
= 1; /* Most key classes will be ignored */
271 * Small change in philosophy: earlier we defined repetition by
272 * rep = keycode == prev_keycode;
273 * prev_keycode = keycode;
274 * but now by the fact that the depressed key was down already.
275 * Does this ever make a difference? Yes.
279 * Repeat a key only if the input buffers are empty or the
280 * characters get echoed locally. This makes key repeat usable
281 * with slow applications and under heavy loads.
284 (vc_kbd_mode(kbd
,VC_REPEAT
) && tty
&&
285 (L_ECHO(tty
) || (tty
->driver
.chars_in_buffer(tty
) == 0)))) {
289 /* the XOR below used to be an OR */
290 int shift_final
= (shift_state
| kbd
->slockstate
) ^
292 ushort
*key_map
= key_maps
[shift_final
];
294 if (key_map
!= NULL
) {
295 keysym
= key_map
[keycode
];
300 if (raw_mode
&& ! (TYPES_ALLOWED_IN_RAW_MODE
& (1 << type
)))
302 if (type
== KT_LETTER
) {
304 if (vc_kbd_led(kbd
, VC_CAPSLOCK
)) {
305 key_map
= key_maps
[shift_final
^ (1<<KG_SHIFT
)];
307 keysym
= key_map
[keycode
];
310 (*key_handler
[type
])(keysym
& 0xff, up_flag
);
311 if (type
!= KT_SLOCK
)
314 /* maybe only if (kbd->kbdmode == VC_UNICODE) ? */
315 if (!up_flag
&& !raw_mode
)
320 /* we have at least to update shift_state */
321 #if 1 /* how? two almost equivalent choices follow */
322 compute_shiftstate();
323 kbd
->slockstate
= 0; /* play it safe */
325 keysym
= U(plain_map
[keycode
]);
327 if (type
== KT_SHIFT
)
328 (*key_handler
[type
])(keysym
& 0xff, up_flag
);
334 #ifdef CONFIG_FORWARD_KEYBOARD
335 extern int forward_chars
;
337 void put_queue(int ch
)
339 if (forward_chars
== fg_console
+1){
340 kbd_forward_char (ch
);
342 wake_up(&keypress_wait
);
344 tty_insert_flip_char(tty
, ch
, 0);
345 con_schedule_flip(tty
);
350 void put_queue(int ch
)
352 wake_up(&keypress_wait
);
354 tty_insert_flip_char(tty
, ch
, 0);
355 con_schedule_flip(tty
);
360 static void puts_queue(char *cp
)
362 wake_up(&keypress_wait
);
367 tty_insert_flip_char(tty
, *cp
, 0);
370 con_schedule_flip(tty
);
373 static void applkey(int key
, char mode
)
375 static char buf
[] = { 0x1b, 'O', 0x00, 0x00 };
377 buf
[1] = (mode
? 'O' : '[');
382 static void enter(void)
389 if (vc_kbd_mode(kbd
,VC_CRLF
))
393 static void caps_toggle(void)
397 chg_vc_kbd_led(kbd
, VC_CAPSLOCK
);
400 static void caps_on(void)
404 set_vc_kbd_led(kbd
, VC_CAPSLOCK
);
407 static void show_ptregs(void)
410 show_regs(kbd_pt_regs
);
413 static void hold(void)
419 * Note: SCROLLOCK will be set (cleared) by stop_tty (start_tty);
420 * these routines are also activated by ^S/^Q.
421 * (And SCROLLOCK can also be set by the ioctl KDSKBLED.)
429 static void num(void)
431 if (vc_kbd_mode(kbd
,VC_APPLIC
))
438 * Bind this to Shift-NumLock if you work in application keypad mode
439 * but want to be able to change the NumLock flag.
440 * Bind this to NumLock if you prefer that the NumLock key always
441 * changes the NumLock flag.
443 static void bare_num(void)
446 chg_vc_kbd_led(kbd
,VC_NUMLOCK
);
449 static void lastcons(void)
451 /* switch to the last used console, ChN */
452 set_console(last_console
);
455 static void decr_console(void)
459 for (i
= fg_console
-1; i
!= fg_console
; i
--) {
461 i
= MAX_NR_CONSOLES
-1;
462 if (vc_cons_allocated(i
))
468 static void incr_console(void)
472 for (i
= fg_console
+1; i
!= fg_console
; i
++) {
473 if (i
== MAX_NR_CONSOLES
)
475 if (vc_cons_allocated(i
))
481 static void send_intr(void)
485 tty_insert_flip_char(tty
, 0, TTY_BREAK
);
486 con_schedule_flip(tty
);
489 static void scroll_forw(void)
494 static void scroll_back(void)
499 static void boot_it(void)
504 static void compose(void)
509 int spawnpid
, spawnsig
;
511 static void spawn_console(void)
514 if(kill_proc(spawnpid
, spawnsig
, 1))
518 static void SAK(void)
521 * SAK should also work in all raw modes and reset
526 reset_vc(fg_console
);
528 do_unblank_screen(); /* not in interrupt routine? */
532 static void do_ignore(unsigned char value
, char up_flag
)
536 static void do_null()
538 compute_shiftstate();
541 static void do_spec(unsigned char value
, char up_flag
)
545 if (value
>= SIZE(spec_fn_table
))
547 if ((kbd
->kbdmode
== VC_RAW
|| kbd
->kbdmode
== VC_MEDIUMRAW
) &&
548 !(SPECIALS_ALLOWED_IN_RAW_MODE
& (1 << value
)))
550 spec_fn_table
[value
]();
553 static void do_lowercase(unsigned char value
, char up_flag
)
555 printk(KERN_ERR
"keyboard.c: do_lowercase was called - impossible\n");
558 static void do_self(unsigned char value
, char up_flag
)
561 return; /* no action, if this is a key release */
564 value
= handle_diacr(value
);
581 static unsigned char ret_diacr
[NR_DEAD
] =
582 {A_GRAVE
, A_ACUTE
, A_CFLEX
, A_TILDE
, A_DIAER
, A_CEDIL
};
584 /* Obsolete - for backwards compatibility only */
585 static void do_dead(unsigned char value
, char up_flag
)
587 value
= ret_diacr
[value
];
588 do_dead2(value
,up_flag
);
592 * Handle dead key. Note that we now may have several
593 * dead keys modifying the same character. Very useful
596 static void do_dead2(unsigned char value
, char up_flag
)
601 diacr
= (diacr
? handle_diacr(value
) : value
);
606 * We have a combining character DIACR here, followed by the character CH.
607 * If the combination occurs in the table, return the corresponding value.
608 * Otherwise, if CH is a space or equals DIACR, return DIACR.
609 * Otherwise, conclude that DIACR was not combining after all,
610 * queue it and return CH.
612 unsigned char handle_diacr(unsigned char ch
)
619 for (i
= 0; i
< accent_table_size
; i
++) {
620 if (accent_table
[i
].diacr
== d
&& accent_table
[i
].base
== ch
)
621 return accent_table
[i
].result
;
624 if (ch
== ' ' || ch
== d
)
631 static void do_cons(unsigned char value
, char up_flag
)
638 static void do_fn(unsigned char value
, char up_flag
)
642 if (value
< SIZE(func_table
)) {
643 if (func_table
[value
])
644 puts_queue(func_table
[value
]);
646 printk(KERN_ERR
"do_fn called with value=%d\n", value
);
649 static void do_pad(unsigned char value
, char up_flag
)
651 static const char *pad_chars
= "0123456789+-*/\015,.?()";
652 static const char *app_map
= "pqrstuvwxylSRQMnnmPQ";
655 return; /* no action, if this is a key release */
657 /* kludge... shift forces cursor/number keys */
658 if (vc_kbd_mode(kbd
,VC_APPLIC
) && !k_down
[KG_SHIFT
]) {
659 applkey(app_map
[value
], 1);
663 if (!vc_kbd_led(kbd
,VC_NUMLOCK
))
667 do_fn(KVAL(K_REMOVE
), 0);
670 do_fn(KVAL(K_INSERT
), 0);
673 do_fn(KVAL(K_SELECT
), 0);
676 do_cur(KVAL(K_DOWN
), 0);
679 do_fn(KVAL(K_PGDN
), 0);
682 do_cur(KVAL(K_LEFT
), 0);
685 do_cur(KVAL(K_RIGHT
), 0);
688 do_fn(KVAL(K_FIND
), 0);
691 do_cur(KVAL(K_UP
), 0);
694 do_fn(KVAL(K_PGUP
), 0);
697 applkey('G', vc_kbd_mode(kbd
, VC_APPLIC
));
701 put_queue(pad_chars
[value
]);
702 if (value
== KVAL(K_PENTER
) && vc_kbd_mode(kbd
, VC_CRLF
))
706 static void do_cur(unsigned char value
, char up_flag
)
708 static const char *cur_chars
= "BDCA";
712 applkey(cur_chars
[value
], vc_kbd_mode(kbd
,VC_CKMODE
));
715 static void do_shift(unsigned char value
, char up_flag
)
717 int old_state
= shift_state
;
723 a CapsShift key acts like Shift but undoes CapsLock */
724 if (value
== KVAL(K_CAPSSHIFT
)) {
725 value
= KVAL(K_SHIFT
);
727 clr_vc_kbd_led(kbd
, VC_CAPSLOCK
);
731 /* handle the case that two shift or control
732 keys are depressed simultaneously */
739 shift_state
|= (1 << value
);
741 shift_state
&= ~ (1 << value
);
744 if (up_flag
&& shift_state
!= old_state
&& npadch
!= -1) {
745 if (kbd
->kbdmode
== VC_UNICODE
)
746 to_utf8(npadch
& 0xffff);
748 put_queue(npadch
& 0xff);
753 /* called after returning from RAW mode or when changing consoles -
754 recompute k_down[] and shift_state from key_down[] */
755 /* maybe called when keymap is undefined, so that shiftkey release is seen */
756 void compute_shiftstate(void)
758 int i
, j
, k
, sym
, val
;
761 for(i
=0; i
< SIZE(k_down
); i
++)
764 for(i
=0; i
< SIZE(key_down
); i
++)
765 if(key_down
[i
]) { /* skip this word if not a single bit on */
767 for(j
=0; j
<BITS_PER_LONG
; j
++,k
++)
768 if(test_bit(k
, key_down
)) {
769 sym
= U(plain_map
[k
]);
770 if(KTYP(sym
) == KT_SHIFT
|| KTYP(sym
) == KT_SLOCK
) {
772 if (val
== KVAL(K_CAPSSHIFT
))
775 shift_state
|= (1<<val
);
781 static void do_meta(unsigned char value
, char up_flag
)
786 if (vc_kbd_mode(kbd
, VC_META
)) {
790 put_queue(value
| 0x80);
793 static void do_ascii(unsigned char value
, char up_flag
)
800 if (value
< 10) /* decimal input of code, while Alt depressed */
802 else { /* hexadecimal input of code, while AltGr depressed */
810 npadch
= npadch
* base
+ value
;
813 static void do_lock(unsigned char value
, char up_flag
)
817 chg_vc_kbd_lock(kbd
, value
);
820 static void do_slock(unsigned char value
, char up_flag
)
822 do_shift(value
,up_flag
);
825 chg_vc_kbd_slock(kbd
, value
);
826 /* try to make Alt, oops, AltGr and such work */
827 if (!key_maps
[kbd
->lockstate
^ kbd
->slockstate
]) {
829 chg_vc_kbd_slock(kbd
, value
);
834 * The leds display either (i) the status of NumLock, CapsLock, ScrollLock,
835 * or (ii) whatever pattern of lights people want to show using KDSETLED,
836 * or (iii) specified bits of specified words in kernel memory.
839 static unsigned char ledstate
= 0xff; /* undefined */
840 static unsigned char ledioctl
;
842 unsigned char getledstate(void) {
846 void setledstate(struct kbd_struct
*kbd
, unsigned int led
) {
849 kbd
->ledmode
= LED_SHOW_IOCTL
;
851 kbd
->ledmode
= LED_SHOW_FLAGS
;
855 static struct ledptr
{
858 unsigned char valid
:1;
861 void register_leds(int console
, unsigned int led
,
862 unsigned int *addr
, unsigned int mask
) {
863 struct kbd_struct
*kbd
= kbd_table
+ console
;
865 ledptrs
[led
].addr
= addr
;
866 ledptrs
[led
].mask
= mask
;
867 ledptrs
[led
].valid
= 1;
868 kbd
->ledmode
= LED_SHOW_MEM
;
870 kbd
->ledmode
= LED_SHOW_FLAGS
;
873 static inline unsigned char getleds(void){
874 struct kbd_struct
*kbd
= kbd_table
+ fg_console
;
877 if (kbd
->ledmode
== LED_SHOW_IOCTL
)
879 leds
= kbd
->ledflagstate
;
880 if (kbd
->ledmode
== LED_SHOW_MEM
) {
881 if (ledptrs
[0].valid
) {
882 if (*ledptrs
[0].addr
& ledptrs
[0].mask
)
887 if (ledptrs
[1].valid
) {
888 if (*ledptrs
[1].addr
& ledptrs
[1].mask
)
893 if (ledptrs
[2].valid
) {
894 if (*ledptrs
[2].addr
& ledptrs
[2].mask
)
904 * This routine is the bottom half of the keyboard interrupt
905 * routine, and runs with all interrupts enabled. It does
906 * console changing, led setting and copy_to_cooked, which can
907 * take a reasonably long time.
909 * Aside from timing (which isn't really that important for
910 * keyboard interrupts as they happen often), using the software
911 * interrupt routines for this thing allows us to easily mask
912 * this when we don't want any of the above to happen. Not yet
913 * used, but this allows for easy and efficient race-condition
914 * prevention later on.
916 static void kbd_bh(unsigned long dummy
)
918 unsigned char leds
= getleds();
920 if (leds
!= ledstate
) {
923 if (kbd_ledfunc
) kbd_ledfunc(leds
);
927 EXPORT_SYMBOL(keyboard_tasklet
);
928 DECLARE_TASKLET_DISABLED(keyboard_tasklet
, kbd_bh
, 0);
930 int __init
kbd_init(void)
933 struct kbd_struct kbd0
;
934 extern struct tty_driver console_driver
;
936 kbd0
.ledflagstate
= kbd0
.default_ledflagstate
= KBD_DEFLEDS
;
937 kbd0
.ledmode
= LED_SHOW_FLAGS
;
938 kbd0
.lockstate
= KBD_DEFLOCK
;
940 kbd0
.modeflags
= KBD_DEFMODE
;
941 kbd0
.kbdmode
= VC_XLATE
;
943 for (i
= 0 ; i
< MAX_NR_CONSOLES
; i
++)
946 ttytab
= console_driver
.table
;
950 tasklet_enable(&keyboard_tasklet
);
951 tasklet_schedule(&keyboard_tasklet
);
953 pm_kbd
= pm_register(PM_SYS_DEV
, PM_SYS_KBC
, NULL
);