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>
45 #define SIZE(x) (sizeof(x)/sizeof((x)[0]))
48 #define KBD_DEFMODE ((1 << VC_REPEAT) | (1 << VC_META))
53 * Some laptops take the 789uiojklm,. keys as number pad when NumLock
54 * is on. This seems a good reason to start with NumLock off.
63 EXPORT_SYMBOL(handle_scancode
);
65 extern void ctrl_alt_del(void);
67 DECLARE_WAIT_QUEUE_HEAD(keypress_wait
);
70 int keyboard_wait_for_keypress(struct console
*co
)
72 sleep_on(&keypress_wait
);
77 * global state includes the following, and various static variables
78 * in this module: prev_scancode, shift_state, diacr, npadch, dead_key_next.
79 * (last_console is now a global variable)
82 /* shift state counters.. */
83 static unsigned char k_down
[NR_SHIFT
] = {0, };
84 /* keyboard key bitmap */
85 static unsigned long key_down
[256/BITS_PER_LONG
] = { 0, };
87 static int dead_key_next
= 0;
89 * In order to retrieve the shift_state (for the mouse server), either
90 * the variable must be global, or a new procedure must be created to
91 * return the value. I chose the former way.
94 static int npadch
= -1; /* -1 or number assembled on pad */
95 static unsigned char diacr
= 0;
96 static char rep
= 0; /* flag telling character repeat */
97 struct kbd_struct kbd_table
[MAX_NR_CONSOLES
];
98 static struct tty_struct
**ttytab
;
99 static struct kbd_struct
* kbd
= kbd_table
;
100 static struct tty_struct
* tty
= NULL
;
102 void compute_shiftstate(void);
104 typedef void (*k_hand
)(unsigned char value
, char up_flag
);
105 typedef void (k_handfn
)(unsigned char value
, char up_flag
);
108 do_self
, do_fn
, do_spec
, do_pad
, do_dead
, do_cons
, do_cur
, do_shift
,
109 do_meta
, do_ascii
, do_lock
, do_lowercase
, do_slock
, do_dead2
,
112 static k_hand key_handler
[16] = {
113 do_self
, do_fn
, do_spec
, do_pad
, do_dead
, do_cons
, do_cur
, do_shift
,
114 do_meta
, do_ascii
, do_lock
, do_lowercase
, do_slock
, do_dead2
,
118 /* Key types processed even in raw modes */
120 #define TYPES_ALLOWED_IN_RAW_MODE ((1 << KT_SPEC) | (1 << KT_SHIFT))
122 typedef void (*void_fnp
)(void);
123 typedef void (void_fn
)(void);
125 static void_fn do_null
, enter
, show_ptregs
, send_intr
, lastcons
, caps_toggle
,
126 num
, hold
, scroll_forw
, scroll_back
, boot_it
, caps_on
, compose
,
127 SAK
, decr_console
, incr_console
, spawn_console
, bare_num
;
129 static void_fnp spec_fn_table
[] = {
130 do_null
, enter
, show_ptregs
, show_mem
,
131 show_state
, send_intr
, lastcons
, caps_toggle
,
132 num
, hold
, scroll_forw
, scroll_back
,
133 boot_it
, caps_on
, compose
, SAK
,
134 decr_console
, incr_console
, spawn_console
, bare_num
137 #define SPECIALS_ALLOWED_IN_RAW_MODE (1 << KVAL(K_SAK))
139 /* maximum values each key_handler can handle */
140 const int max_vals
[] = {
141 255, SIZE(func_table
) - 1, SIZE(spec_fn_table
) - 1, NR_PAD
- 1,
142 NR_DEAD
- 1, 255, 3, NR_SHIFT
- 1,
143 255, NR_ASCII
- 1, NR_LOCK
- 1, 255,
147 const int NR_TYPES
= SIZE(max_vals
);
149 /* N.B. drivers/macintosh/mac_keyb.c needs to call put_queue */
151 static unsigned char handle_diacr(unsigned char);
153 /* kbd_pt_regs - set by keyboard_interrupt(), used by show_ptregs() */
154 struct pt_regs
* kbd_pt_regs
;
156 #ifdef CONFIG_MAGIC_SYSRQ
157 static int sysrq_pressed
;
158 int sysrq_enabled
= 1;
162 * Many other routines do put_queue, but I think either
163 * they produce ASCII, or they produce some user-assigned
164 * string, and in both cases we might assume that it is
167 void to_utf8(ushort c
) {
169 put_queue(c
); /* 0******* */
170 else if (c
< 0x800) {
171 put_queue(0xc0 | (c
>> 6)); /* 110***** 10****** */
172 put_queue(0x80 | (c
& 0x3f));
174 put_queue(0xe0 | (c
>> 12)); /* 1110**** 10****** 10****** */
175 put_queue(0x80 | ((c
>> 6) & 0x3f));
176 put_queue(0x80 | (c
& 0x3f));
178 /* UTF-8 is defined for words of up to 31 bits,
179 but we need only 16 bits here */
183 * Translation of escaped scancodes to keycodes.
184 * This is now user-settable (for machines were it makes sense).
187 int setkeycode(unsigned int scancode
, unsigned int keycode
)
189 return kbd_setkeycode(scancode
, keycode
);
192 int getkeycode(unsigned int scancode
)
194 return kbd_getkeycode(scancode
);
197 void handle_scancode(unsigned char scancode
, int down
)
199 unsigned char keycode
;
200 char up_flag
= down
? 0 : 0200;
203 do_poke_blanked_console
= 1;
205 add_keyboard_randomness(scancode
| up_flag
);
207 tty
= ttytab
? ttytab
[fg_console
]: NULL
;
208 if (tty
&& (!tty
->driver_data
)) {
210 * We touch the tty structure via the the ttytab array
211 * without knowing whether or not tty is open, which
212 * is inherently dangerous. We currently rely on that
213 * fact that console_open sets tty->driver_data when
214 * it opens it, and clears it when it closes it.
218 kbd
= kbd_table
+ fg_console
;
219 if ((raw_mode
= (kbd
->kbdmode
== VC_RAW
))) {
220 put_queue(scancode
| up_flag
);
221 /* we do not return yet, because we want to maintain
222 the key_down array, so that we have the correct
223 values when finishing RAW mode or when changing VT's */
227 * Convert scancode to keycode
229 if (!kbd_translate(scancode
, &keycode
, raw_mode
))
233 * At this point the variable `keycode' contains the keycode.
234 * Note: the keycode must not be 0 (++Geert: on m68k 0 is valid).
235 * We keep track of the up/down status of the key, and
236 * return the keycode if in MEDIUMRAW mode.
241 if(!test_and_clear_bit(keycode
, key_down
))
242 up_flag
= kbd_unexpected_up(keycode
);
244 rep
= test_and_set_bit(keycode
, key_down
);
246 #ifdef CONFIG_MAGIC_SYSRQ /* Handle the SysRq Hack */
247 if (keycode
== SYSRQ_KEY
) {
248 sysrq_pressed
= !up_flag
;
250 } else if (sysrq_pressed
) {
251 if (!up_flag
&& sysrq_enabled
) {
252 handle_sysrq(kbd_sysrq_xlate
[keycode
], kbd_pt_regs
, kbd
, tty
);
258 if (kbd
->kbdmode
== VC_MEDIUMRAW
) {
259 /* soon keycodes will require more than one byte */
260 put_queue(keycode
+ up_flag
);
261 raw_mode
= 1; /* Most key classes will be ignored */
265 * Small change in philosophy: earlier we defined repetition by
266 * rep = keycode == prev_keycode;
267 * prev_keycode = keycode;
268 * but now by the fact that the depressed key was down already.
269 * Does this ever make a difference? Yes.
273 * Repeat a key only if the input buffers are empty or the
274 * characters get echoed locally. This makes key repeat usable
275 * with slow applications and under heavy loads.
278 (vc_kbd_mode(kbd
,VC_REPEAT
) && tty
&&
279 (L_ECHO(tty
) || (tty
->driver
.chars_in_buffer(tty
) == 0)))) {
283 /* the XOR below used to be an OR */
284 int shift_final
= (shift_state
| kbd
->slockstate
) ^
286 ushort
*key_map
= key_maps
[shift_final
];
288 if (key_map
!= NULL
) {
289 keysym
= key_map
[keycode
];
294 if (raw_mode
&& ! (TYPES_ALLOWED_IN_RAW_MODE
& (1 << type
)))
296 if (type
== KT_LETTER
) {
298 if (vc_kbd_led(kbd
, VC_CAPSLOCK
)) {
299 key_map
= key_maps
[shift_final
^ (1<<KG_SHIFT
)];
301 keysym
= key_map
[keycode
];
304 (*key_handler
[type
])(keysym
& 0xff, up_flag
);
305 if (type
!= KT_SLOCK
)
308 /* maybe only if (kbd->kbdmode == VC_UNICODE) ? */
309 if (!up_flag
&& !raw_mode
)
314 /* we have at least to update shift_state */
315 #if 1 /* how? two almost equivalent choices follow */
316 compute_shiftstate();
317 kbd
->slockstate
= 0; /* play it safe */
319 keysym
= U(plain_map
[keycode
]);
321 if (type
== KT_SHIFT
)
322 (*key_handler
[type
])(keysym
& 0xff, up_flag
);
329 void put_queue(int ch
)
331 wake_up(&keypress_wait
);
333 tty_insert_flip_char(tty
, ch
, 0);
334 con_schedule_flip(tty
);
338 static void puts_queue(char *cp
)
340 wake_up(&keypress_wait
);
345 tty_insert_flip_char(tty
, *cp
, 0);
348 con_schedule_flip(tty
);
351 static void applkey(int key
, char mode
)
353 static char buf
[] = { 0x1b, 'O', 0x00, 0x00 };
355 buf
[1] = (mode
? 'O' : '[');
360 static void enter(void)
367 if (vc_kbd_mode(kbd
,VC_CRLF
))
371 static void caps_toggle(void)
375 chg_vc_kbd_led(kbd
, VC_CAPSLOCK
);
378 static void caps_on(void)
382 set_vc_kbd_led(kbd
, VC_CAPSLOCK
);
385 static void show_ptregs(void)
388 show_regs(kbd_pt_regs
);
391 static void hold(void)
397 * Note: SCROLLOCK will be set (cleared) by stop_tty (start_tty);
398 * these routines are also activated by ^S/^Q.
399 * (And SCROLLOCK can also be set by the ioctl KDSKBLED.)
407 static void num(void)
409 if (vc_kbd_mode(kbd
,VC_APPLIC
))
416 * Bind this to Shift-NumLock if you work in application keypad mode
417 * but want to be able to change the NumLock flag.
418 * Bind this to NumLock if you prefer that the NumLock key always
419 * changes the NumLock flag.
421 static void bare_num(void)
424 chg_vc_kbd_led(kbd
,VC_NUMLOCK
);
427 static void lastcons(void)
429 /* switch to the last used console, ChN */
430 set_console(last_console
);
433 static void decr_console(void)
437 for (i
= fg_console
-1; i
!= fg_console
; i
--) {
439 i
= MAX_NR_CONSOLES
-1;
440 if (vc_cons_allocated(i
))
446 static void incr_console(void)
450 for (i
= fg_console
+1; i
!= fg_console
; i
++) {
451 if (i
== MAX_NR_CONSOLES
)
453 if (vc_cons_allocated(i
))
459 static void send_intr(void)
463 tty_insert_flip_char(tty
, 0, TTY_BREAK
);
464 con_schedule_flip(tty
);
467 static void scroll_forw(void)
472 static void scroll_back(void)
477 static void boot_it(void)
482 static void compose(void)
487 int spawnpid
, spawnsig
;
489 static void spawn_console(void)
492 if(kill_proc(spawnpid
, spawnsig
, 1))
496 static void SAK(void)
499 * SAK should also work in all raw modes and reset
504 reset_vc(fg_console
);
506 do_unblank_screen(); /* not in interrupt routine? */
510 static void do_ignore(unsigned char value
, char up_flag
)
514 static void do_null()
516 compute_shiftstate();
519 static void do_spec(unsigned char value
, char up_flag
)
523 if (value
>= SIZE(spec_fn_table
))
525 if ((kbd
->kbdmode
== VC_RAW
|| kbd
->kbdmode
== VC_MEDIUMRAW
) &&
526 !(SPECIALS_ALLOWED_IN_RAW_MODE
& (1 << value
)))
528 spec_fn_table
[value
]();
531 static void do_lowercase(unsigned char value
, char up_flag
)
533 printk(KERN_ERR
"keyboard.c: do_lowercase was called - impossible\n");
536 static void do_self(unsigned char value
, char up_flag
)
539 return; /* no action, if this is a key release */
542 value
= handle_diacr(value
);
559 static unsigned char ret_diacr
[NR_DEAD
] =
560 {A_GRAVE
, A_ACUTE
, A_CFLEX
, A_TILDE
, A_DIAER
, A_CEDIL
};
562 /* Obsolete - for backwards compatibility only */
563 static void do_dead(unsigned char value
, char up_flag
)
565 value
= ret_diacr
[value
];
566 do_dead2(value
,up_flag
);
570 * Handle dead key. Note that we now may have several
571 * dead keys modifying the same character. Very useful
574 static void do_dead2(unsigned char value
, char up_flag
)
579 diacr
= (diacr
? handle_diacr(value
) : value
);
584 * We have a combining character DIACR here, followed by the character CH.
585 * If the combination occurs in the table, return the corresponding value.
586 * Otherwise, if CH is a space or equals DIACR, return DIACR.
587 * Otherwise, conclude that DIACR was not combining after all,
588 * queue it and return CH.
590 unsigned char handle_diacr(unsigned char ch
)
597 for (i
= 0; i
< accent_table_size
; i
++) {
598 if (accent_table
[i
].diacr
== d
&& accent_table
[i
].base
== ch
)
599 return accent_table
[i
].result
;
602 if (ch
== ' ' || ch
== d
)
609 static void do_cons(unsigned char value
, char up_flag
)
616 static void do_fn(unsigned char value
, char up_flag
)
620 if (value
< SIZE(func_table
)) {
621 if (func_table
[value
])
622 puts_queue(func_table
[value
]);
624 printk(KERN_ERR
"do_fn called with value=%d\n", value
);
627 static void do_pad(unsigned char value
, char up_flag
)
629 static const char *pad_chars
= "0123456789+-*/\015,.?()";
630 static const char *app_map
= "pqrstuvwxylSRQMnnmPQ";
633 return; /* no action, if this is a key release */
635 /* kludge... shift forces cursor/number keys */
636 if (vc_kbd_mode(kbd
,VC_APPLIC
) && !k_down
[KG_SHIFT
]) {
637 applkey(app_map
[value
], 1);
641 if (!vc_kbd_led(kbd
,VC_NUMLOCK
))
645 do_fn(KVAL(K_REMOVE
), 0);
648 do_fn(KVAL(K_INSERT
), 0);
651 do_fn(KVAL(K_SELECT
), 0);
654 do_cur(KVAL(K_DOWN
), 0);
657 do_fn(KVAL(K_PGDN
), 0);
660 do_cur(KVAL(K_LEFT
), 0);
663 do_cur(KVAL(K_RIGHT
), 0);
666 do_fn(KVAL(K_FIND
), 0);
669 do_cur(KVAL(K_UP
), 0);
672 do_fn(KVAL(K_PGUP
), 0);
675 applkey('G', vc_kbd_mode(kbd
, VC_APPLIC
));
679 put_queue(pad_chars
[value
]);
680 if (value
== KVAL(K_PENTER
) && vc_kbd_mode(kbd
, VC_CRLF
))
684 static void do_cur(unsigned char value
, char up_flag
)
686 static const char *cur_chars
= "BDCA";
690 applkey(cur_chars
[value
], vc_kbd_mode(kbd
,VC_CKMODE
));
693 static void do_shift(unsigned char value
, char up_flag
)
695 int old_state
= shift_state
;
701 a CapsShift key acts like Shift but undoes CapsLock */
702 if (value
== KVAL(K_CAPSSHIFT
)) {
703 value
= KVAL(K_SHIFT
);
705 clr_vc_kbd_led(kbd
, VC_CAPSLOCK
);
709 /* handle the case that two shift or control
710 keys are depressed simultaneously */
717 shift_state
|= (1 << value
);
719 shift_state
&= ~ (1 << value
);
722 if (up_flag
&& shift_state
!= old_state
&& npadch
!= -1) {
723 if (kbd
->kbdmode
== VC_UNICODE
)
724 to_utf8(npadch
& 0xffff);
726 put_queue(npadch
& 0xff);
731 /* called after returning from RAW mode or when changing consoles -
732 recompute k_down[] and shift_state from key_down[] */
733 /* maybe called when keymap is undefined, so that shiftkey release is seen */
734 void compute_shiftstate(void)
736 int i
, j
, k
, sym
, val
;
739 for(i
=0; i
< SIZE(k_down
); i
++)
742 for(i
=0; i
< SIZE(key_down
); i
++)
743 if(key_down
[i
]) { /* skip this word if not a single bit on */
745 for(j
=0; j
<BITS_PER_LONG
; j
++,k
++)
746 if(test_bit(k
, key_down
)) {
747 sym
= U(plain_map
[k
]);
748 if(KTYP(sym
) == KT_SHIFT
|| KTYP(sym
) == KT_SLOCK
) {
750 if (val
== KVAL(K_CAPSSHIFT
))
753 shift_state
|= (1<<val
);
759 static void do_meta(unsigned char value
, char up_flag
)
764 if (vc_kbd_mode(kbd
, VC_META
)) {
768 put_queue(value
| 0x80);
771 static void do_ascii(unsigned char value
, char up_flag
)
778 if (value
< 10) /* decimal input of code, while Alt depressed */
780 else { /* hexadecimal input of code, while AltGr depressed */
788 npadch
= npadch
* base
+ value
;
791 static void do_lock(unsigned char value
, char up_flag
)
795 chg_vc_kbd_lock(kbd
, value
);
798 static void do_slock(unsigned char value
, char up_flag
)
800 do_shift(value
,up_flag
);
803 chg_vc_kbd_slock(kbd
, value
);
804 /* try to make Alt, oops, AltGr and such work */
805 if (!key_maps
[kbd
->lockstate
^ kbd
->slockstate
]) {
807 chg_vc_kbd_slock(kbd
, value
);
812 * The leds display either (i) the status of NumLock, CapsLock, ScrollLock,
813 * or (ii) whatever pattern of lights people want to show using KDSETLED,
814 * or (iii) specified bits of specified words in kernel memory.
817 static unsigned char ledstate
= 0xff; /* undefined */
818 static unsigned char ledioctl
;
820 unsigned char getledstate(void) {
824 void setledstate(struct kbd_struct
*kbd
, unsigned int led
) {
827 kbd
->ledmode
= LED_SHOW_IOCTL
;
829 kbd
->ledmode
= LED_SHOW_FLAGS
;
833 static struct ledptr
{
836 unsigned char valid
:1;
839 void register_leds(int console
, unsigned int led
,
840 unsigned int *addr
, unsigned int mask
) {
841 struct kbd_struct
*kbd
= kbd_table
+ console
;
843 ledptrs
[led
].addr
= addr
;
844 ledptrs
[led
].mask
= mask
;
845 ledptrs
[led
].valid
= 1;
846 kbd
->ledmode
= LED_SHOW_MEM
;
848 kbd
->ledmode
= LED_SHOW_FLAGS
;
851 static inline unsigned char getleds(void){
852 struct kbd_struct
*kbd
= kbd_table
+ fg_console
;
855 if (kbd
->ledmode
== LED_SHOW_IOCTL
)
857 leds
= kbd
->ledflagstate
;
858 if (kbd
->ledmode
== LED_SHOW_MEM
) {
859 if (ledptrs
[0].valid
) {
860 if (*ledptrs
[0].addr
& ledptrs
[0].mask
)
865 if (ledptrs
[1].valid
) {
866 if (*ledptrs
[1].addr
& ledptrs
[1].mask
)
871 if (ledptrs
[2].valid
) {
872 if (*ledptrs
[2].addr
& ledptrs
[2].mask
)
882 * This routine is the bottom half of the keyboard interrupt
883 * routine, and runs with all interrupts enabled. It does
884 * console changing, led setting and copy_to_cooked, which can
885 * take a reasonably long time.
887 * Aside from timing (which isn't really that important for
888 * keyboard interrupts as they happen often), using the software
889 * interrupt routines for this thing allows us to easily mask
890 * this when we don't want any of the above to happen. Not yet
891 * used, but this allows for easy and efficient race-condition
892 * prevention later on.
894 static void kbd_bh(void)
896 unsigned char leds
= getleds();
898 if (leds
!= ledstate
) {
904 __initfunc(int kbd_init(void))
907 struct kbd_struct kbd0
;
908 extern struct tty_driver console_driver
;
910 kbd0
.ledflagstate
= kbd0
.default_ledflagstate
= KBD_DEFLEDS
;
911 kbd0
.ledmode
= LED_SHOW_FLAGS
;
912 kbd0
.lockstate
= KBD_DEFLOCK
;
914 kbd0
.modeflags
= KBD_DEFMODE
;
915 kbd0
.kbdmode
= VC_XLATE
;
917 for (i
= 0 ; i
< MAX_NR_CONSOLES
; i
++)
920 ttytab
= console_driver
.table
;
923 init_bh(KEYBOARD_BH
, kbd_bh
);
924 mark_bh(KEYBOARD_BH
);