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/consolemap.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/init.h>
35 #include <linux/slab.h>
36 #include <linux/irq.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>
43 #include <linux/reboot.h>
44 #include <linux/notifier.h>
45 #include <linux/jiffies.h>
47 #include <linux/kdb.h>
48 #endif /* CONFIG_KDB */
50 extern void ctrl_alt_del(void);
53 * Exported functions/variables
56 #define KBD_DEFMODE ((1 << VC_REPEAT) | (1 << VC_META))
59 * Some laptops take the 789uiojklm,. keys as number pad when NumLock is on.
60 * This seems a good reason to start with NumLock off. On HIL keyboards
61 * of PARISC machines however there is no NumLock key and everyone expects the keypad
62 * to be used for numbers.
65 #if defined(CONFIG_PARISC) && (defined(CONFIG_KEYBOARD_HIL) || defined(CONFIG_KEYBOARD_HIL_OLD))
66 #define KBD_DEFLEDS (1 << VC_NUMLOCK)
73 void compute_shiftstate(void);
80 k_self, k_fn, k_spec, k_pad,\
81 k_dead, k_cons, k_cur, k_shift,\
82 k_meta, k_ascii, k_lock, k_lowercase,\
83 k_slock, k_dead2, k_brl, k_ignore
85 typedef void (k_handler_fn)(struct vc_data *vc, unsigned char value,
87 static k_handler_fn K_HANDLERS;
88 k_handler_fn *k_handler[16] = { K_HANDLERS };
89 EXPORT_SYMBOL_GPL(k_handler);
92 fn_null, fn_enter, fn_show_ptregs, fn_show_mem,\
93 fn_show_state, fn_send_intr, fn_lastcons, fn_caps_toggle,\
94 fn_num, fn_hold, fn_scroll_forw, fn_scroll_back,\
95 fn_boot_it, fn_caps_on, fn_compose, fn_SAK,\
96 fn_dec_console, fn_inc_console, fn_spawn_con, fn_bare_num
98 typedef void (fn_handler_fn)(struct vc_data *vc);
99 static fn_handler_fn FN_HANDLERS;
100 static fn_handler_fn *fn_handler[] = { FN_HANDLERS };
103 * Variables exported for vt_ioctl.c
106 /* maximum values each key_handler can handle */
107 const int max_vals[] = {
108 255, ARRAY_SIZE(func_table) - 1, ARRAY_SIZE(fn_handler) - 1, NR_PAD - 1,
109 NR_DEAD - 1, 255, 3, NR_SHIFT - 1, 255, NR_ASCII - 1, NR_LOCK - 1,
110 255, NR_LOCK - 1, 255, NR_BRL - 1
113 const int NR_TYPES = ARRAY_SIZE(max_vals);
115 struct kbd_struct kbd_table[MAX_NR_CONSOLES];
116 EXPORT_SYMBOL_GPL(kbd_table);
117 static struct kbd_struct *kbd = kbd_table;
119 struct vt_spawn_console vt_spawn_con = {
120 .lock = __SPIN_LOCK_UNLOCKED(vt_spawn_con.lock),
126 * Variables exported for vt.c
135 static struct input_handler kbd_handler;
136 static DEFINE_SPINLOCK(kbd_event_lock);
137 static unsigned long key_down[BITS_TO_LONGS(KEY_CNT)]; /* keyboard key bitmap */
138 static unsigned char shift_down[NR_SHIFT]; /* shift state counters.. */
139 static int dead_key_next;
140 static int npadch = -1; /* -1 or number assembled on pad */
141 static unsigned int diacr;
142 static char rep; /* flag telling character repeat */
144 static unsigned char ledstate = 0xff; /* undefined */
145 static unsigned char ledioctl;
147 static struct ledptr {
150 unsigned char valid:1;
153 /* Simple translation table for the SysRq keys */
155 #ifdef CONFIG_MAGIC_SYSRQ
156 unsigned char kbd_sysrq_xlate[KEY_MAX + 1] =
157 "\000\0331234567890-=\177\t" /* 0x00 - 0x0f */
158 "qwertyuiop[]\r\000as" /* 0x10 - 0x1f */
159 "dfghjkl;'`\000\\zxcv" /* 0x20 - 0x2f */
160 "bnm,./\000*\000 \000\201\202\203\204\205" /* 0x30 - 0x3f */
161 "\206\207\210\211\212\000\000789-456+1" /* 0x40 - 0x4f */
162 "230\177\000\000\213\214\000\000\000\000\000\000\000\000\000\000" /* 0x50 - 0x5f */
163 "\r\000/"; /* 0x60 - 0x6f */
164 static int sysrq_down;
165 static int sysrq_alt_use;
167 static int sysrq_alt;
170 * Notifier list for console keyboard events
172 static ATOMIC_NOTIFIER_HEAD(keyboard_notifier_list);
174 int register_keyboard_notifier(struct notifier_block *nb)
176 return atomic_notifier_chain_register(&keyboard_notifier_list, nb);
178 EXPORT_SYMBOL_GPL(register_keyboard_notifier);
180 int unregister_keyboard_notifier(struct notifier_block *nb)
182 return atomic_notifier_chain_unregister(&keyboard_notifier_list, nb);
184 EXPORT_SYMBOL_GPL(unregister_keyboard_notifier);
187 * Translation of scancodes to keycodes. We set them on only the first
188 * keyboard in the list that accepts the scancode and keycode.
189 * Explanation for not choosing the first attached keyboard anymore:
190 * USB keyboards for example have two event devices: one for all "normal"
191 * keys and one for extra function keys (like "volume up", "make coffee",
192 * etc.). So this means that scancodes for the extra function keys won't
193 * be valid for the first event device, but will be for the second.
196 struct getset_keycode_data {
197 unsigned int scancode;
198 unsigned int keycode;
202 static int getkeycode_helper(struct input_handle *handle, void *data)
204 struct getset_keycode_data *d = data;
206 d->error = input_get_keycode(handle->dev, d->scancode, &d->keycode);
208 return d->error == 0; /* stop as soon as we successfully get one */
211 int getkeycode(unsigned int scancode)
213 struct getset_keycode_data d = { scancode, 0, -ENODEV };
215 input_handler_for_each_handle(&kbd_handler, &d, getkeycode_helper);
217 return d.error ?: d.keycode;
220 static int setkeycode_helper(struct input_handle *handle, void *data)
222 struct getset_keycode_data *d = data;
224 d->error = input_set_keycode(handle->dev, d->scancode, d->keycode);
226 return d->error == 0; /* stop as soon as we successfully set one */
229 int setkeycode(unsigned int scancode, unsigned int keycode)
231 struct getset_keycode_data d = { scancode, keycode, -ENODEV };
233 input_handler_for_each_handle(&kbd_handler, &d, setkeycode_helper);
239 * Making beeps and bells. Note that we prefer beeps to bells, but when
240 * shutting the sound off we do both.
243 static int kd_sound_helper(struct input_handle *handle, void *data)
245 unsigned int *hz = data;
246 struct input_dev *dev = handle->dev;
248 if (test_bit(EV_SND, dev->evbit)) {
249 if (test_bit(SND_TONE, dev->sndbit)) {
250 input_inject_event(handle, EV_SND, SND_TONE, *hz);
254 if (test_bit(SND_BELL, dev->sndbit))
255 input_inject_event(handle, EV_SND, SND_BELL, *hz ? 1 : 0);
261 static void kd_nosound(unsigned long ignored)
263 static unsigned int zero;
265 input_handler_for_each_handle(&kbd_handler, &zero, kd_sound_helper);
268 static DEFINE_TIMER(kd_mksound_timer, kd_nosound, 0, 0);
270 void kd_mksound(unsigned int hz, unsigned int ticks)
272 del_timer_sync(&kd_mksound_timer);
274 input_handler_for_each_handle(&kbd_handler, &hz, kd_sound_helper);
277 mod_timer(&kd_mksound_timer, jiffies + ticks);
279 EXPORT_SYMBOL(kd_mksound);
282 * Setting the keyboard rate.
285 static int kbd_rate_helper(struct input_handle *handle, void *data)
287 struct input_dev *dev = handle->dev;
288 struct kbd_repeat *rep = data;
290 if (test_bit(EV_REP, dev->evbit)) {
292 if (rep[0].delay > 0)
293 input_inject_event(handle,
294 EV_REP, REP_DELAY, rep[0].delay);
295 if (rep[0].period > 0)
296 input_inject_event(handle,
297 EV_REP, REP_PERIOD, rep[0].period);
299 rep[1].delay = dev->rep[REP_DELAY];
300 rep[1].period = dev->rep[REP_PERIOD];
306 int kbd_rate(struct kbd_repeat *rep)
308 struct kbd_repeat data[2] = { *rep };
310 input_handler_for_each_handle(&kbd_handler, data, kbd_rate_helper);
311 *rep = data[1]; /* Copy currently used settings */
319 static void put_queue(struct vc_data *vc, int ch)
321 struct tty_struct *tty = vc->vc_tty;
324 tty_insert_flip_char(tty, ch, 0);
325 con_schedule_flip(tty);
329 static void puts_queue(struct vc_data *vc, char *cp)
331 struct tty_struct *tty = vc->vc_tty;
337 tty_insert_flip_char(tty, *cp, 0);
340 con_schedule_flip(tty);
343 static void applkey(struct vc_data *vc, int key, char mode)
345 static char buf[] = { 0x1b, 'O', 0x00, 0x00 };
347 buf[1] = (mode ? 'O' : '[');
353 * Many other routines do put_queue, but I think either
354 * they produce ASCII, or they produce some user-assigned
355 * string, and in both cases we might assume that it is
358 static void to_utf8(struct vc_data *vc, uint c)
363 else if (c < 0x800) {
364 /* 110***** 10****** */
365 put_queue(vc, 0xc0 | (c >> 6));
366 put_queue(vc, 0x80 | (c & 0x3f));
367 } else if (c < 0x10000) {
368 if (c >= 0xD800 && c < 0xE000)
372 /* 1110**** 10****** 10****** */
373 put_queue(vc, 0xe0 | (c >> 12));
374 put_queue(vc, 0x80 | ((c >> 6) & 0x3f));
375 put_queue(vc, 0x80 | (c & 0x3f));
376 } else if (c < 0x110000) {
377 /* 11110*** 10****** 10****** 10****** */
378 put_queue(vc, 0xf0 | (c >> 18));
379 put_queue(vc, 0x80 | ((c >> 12) & 0x3f));
380 put_queue(vc, 0x80 | ((c >> 6) & 0x3f));
381 put_queue(vc, 0x80 | (c & 0x3f));
386 * Called after returning from RAW mode or when changing consoles - recompute
387 * shift_down[] and shift_state from key_down[] maybe called when keymap is
388 * undefined, so that shiftkey release is seen
390 void compute_shiftstate(void)
392 unsigned int i, j, k, sym, val;
395 memset(shift_down, 0, sizeof(shift_down));
397 for (i = 0; i < ARRAY_SIZE(key_down); i++) {
402 k = i * BITS_PER_LONG;
404 for (j = 0; j < BITS_PER_LONG; j++, k++) {
406 if (!test_bit(k, key_down))
409 sym = U(key_maps[0][k]);
410 if (KTYP(sym) != KT_SHIFT && KTYP(sym) != KT_SLOCK)
414 if (val == KVAL(K_CAPSSHIFT))
418 shift_state |= (1 << val);
424 * We have a combining character DIACR here, followed by the character CH.
425 * If the combination occurs in the table, return the corresponding value.
426 * Otherwise, if CH is a space or equals DIACR, return DIACR.
427 * Otherwise, conclude that DIACR was not combining after all,
428 * queue it and return CH.
430 static unsigned int handle_diacr(struct vc_data *vc, unsigned int ch)
432 unsigned int d = diacr;
437 if ((d & ~0xff) == BRL_UC_ROW) {
438 if ((ch & ~0xff) == BRL_UC_ROW)
441 for (i = 0; i < accent_table_size; i++)
442 if (accent_table[i].diacr == d && accent_table[i].base == ch)
443 return accent_table[i].result;
446 if (ch == ' ' || ch == (BRL_UC_ROW|0) || ch == d)
449 if (kbd->kbdmode == VC_UNICODE)
452 int c = conv_uni_to_8bit(d);
461 * Special function handlers
463 static void fn_enter(struct vc_data *vc)
466 if (kbd->kbdmode == VC_UNICODE)
469 int c = conv_uni_to_8bit(diacr);
476 if (vc_kbd_mode(kbd, VC_CRLF))
480 static void fn_caps_toggle(struct vc_data *vc)
484 chg_vc_kbd_led(kbd, VC_CAPSLOCK);
487 static void fn_caps_on(struct vc_data *vc)
491 set_vc_kbd_led(kbd, VC_CAPSLOCK);
494 static void fn_show_ptregs(struct vc_data *vc)
496 struct pt_regs *regs = get_irq_regs();
501 static void fn_hold(struct vc_data *vc)
503 struct tty_struct *tty = vc->vc_tty;
509 * Note: SCROLLOCK will be set (cleared) by stop_tty (start_tty);
510 * these routines are also activated by ^S/^Q.
511 * (And SCROLLOCK can also be set by the ioctl KDSKBLED.)
519 static void fn_num(struct vc_data *vc)
521 if (vc_kbd_mode(kbd,VC_APPLIC))
528 * Bind this to Shift-NumLock if you work in application keypad mode
529 * but want to be able to change the NumLock flag.
530 * Bind this to NumLock if you prefer that the NumLock key always
531 * changes the NumLock flag.
533 static void fn_bare_num(struct vc_data *vc)
536 chg_vc_kbd_led(kbd, VC_NUMLOCK);
539 static void fn_lastcons(struct vc_data *vc)
541 /* switch to the last used console, ChN */
542 set_console(last_console);
545 static void fn_dec_console(struct vc_data *vc)
547 int i, cur = fg_console;
549 /* Currently switching? Queue this next switch relative to that. */
550 if (want_console != -1)
553 for (i = cur - 1; i != cur; i--) {
555 i = MAX_NR_CONSOLES - 1;
556 if (vc_cons_allocated(i))
562 static void fn_inc_console(struct vc_data *vc)
564 int i, cur = fg_console;
566 /* Currently switching? Queue this next switch relative to that. */
567 if (want_console != -1)
570 for (i = cur+1; i != cur; i++) {
571 if (i == MAX_NR_CONSOLES)
573 if (vc_cons_allocated(i))
579 static void fn_send_intr(struct vc_data *vc)
581 struct tty_struct *tty = vc->vc_tty;
585 tty_insert_flip_char(tty, 0, TTY_BREAK);
586 con_schedule_flip(tty);
589 static void fn_scroll_forw(struct vc_data *vc)
594 static void fn_scroll_back(struct vc_data *vc)
599 static void fn_show_mem(struct vc_data *vc)
604 static void fn_show_state(struct vc_data *vc)
609 static void fn_boot_it(struct vc_data *vc)
614 static void fn_compose(struct vc_data *vc)
619 static void fn_spawn_con(struct vc_data *vc)
621 spin_lock(&vt_spawn_con.lock);
622 if (vt_spawn_con.pid)
623 if (kill_pid(vt_spawn_con.pid, vt_spawn_con.sig, 1)) {
624 put_pid(vt_spawn_con.pid);
625 vt_spawn_con.pid = NULL;
627 spin_unlock(&vt_spawn_con.lock);
630 static void fn_SAK(struct vc_data *vc)
632 struct work_struct *SAK_work = &vc_cons[fg_console].SAK_work;
633 schedule_work(SAK_work);
636 static void fn_null(struct vc_data *vc)
638 compute_shiftstate();
642 * Special key handlers
644 static void k_ignore(struct vc_data *vc, unsigned char value, char up_flag)
648 static void k_spec(struct vc_data *vc, unsigned char value, char up_flag)
652 if (value >= ARRAY_SIZE(fn_handler))
654 if ((kbd->kbdmode == VC_RAW ||
655 kbd->kbdmode == VC_MEDIUMRAW) &&
656 value != KVAL(K_SAK))
657 return; /* SAK is allowed even in raw mode */
658 fn_handler[value](vc);
661 static void k_lowercase(struct vc_data *vc, unsigned char value, char up_flag)
663 printk(KERN_ERR "keyboard.c: k_lowercase was called - impossible\n");
666 static void k_unicode(struct vc_data *vc, unsigned int value, char up_flag)
669 return; /* no action, if this is a key release */
672 value = handle_diacr(vc, value);
679 if (kbd->kbdmode == VC_UNICODE)
682 int c = conv_uni_to_8bit(value);
689 * Handle dead key. Note that we now may have several
690 * dead keys modifying the same character. Very useful
693 static void k_deadunicode(struct vc_data *vc, unsigned int value, char up_flag)
697 diacr = (diacr ? handle_diacr(vc, value) : value);
700 static void k_self(struct vc_data *vc, unsigned char value, char up_flag)
702 k_unicode(vc, conv_8bit_to_uni(value), up_flag);
705 static void k_dead2(struct vc_data *vc, unsigned char value, char up_flag)
707 k_deadunicode(vc, value, up_flag);
711 * Obsolete - for backwards compatibility only
713 static void k_dead(struct vc_data *vc, unsigned char value, char up_flag)
715 static const unsigned char ret_diacr[NR_DEAD] = {'`', '\'', '^', '~', '"', ',' };
716 value = ret_diacr[value];
717 k_deadunicode(vc, value, up_flag);
720 static void k_cons(struct vc_data *vc, unsigned char value, char up_flag)
727 static void k_fn(struct vc_data *vc, unsigned char value, char up_flag)
734 if (v < ARRAY_SIZE(func_table)) {
735 if (func_table[value])
736 puts_queue(vc, func_table[value]);
738 printk(KERN_ERR "k_fn called with value=%d\n", value);
741 static void k_cur(struct vc_data *vc, unsigned char value, char up_flag)
743 static const char cur_chars[] = "BDCA";
747 applkey(vc, cur_chars[value], vc_kbd_mode(kbd, VC_CKMODE));
750 static void k_pad(struct vc_data *vc, unsigned char value, char up_flag)
752 static const char pad_chars[] = "0123456789+-*/\015,.?()#";
753 static const char app_map[] = "pqrstuvwxylSRQMnnmPQS";
756 return; /* no action, if this is a key release */
758 /* kludge... shift forces cursor/number keys */
759 if (vc_kbd_mode(kbd, VC_APPLIC) && !shift_down[KG_SHIFT]) {
760 applkey(vc, app_map[value], 1);
764 if (!vc_kbd_led(kbd, VC_NUMLOCK))
768 k_fn(vc, KVAL(K_REMOVE), 0);
771 k_fn(vc, KVAL(K_INSERT), 0);
774 k_fn(vc, KVAL(K_SELECT), 0);
777 k_cur(vc, KVAL(K_DOWN), 0);
780 k_fn(vc, KVAL(K_PGDN), 0);
783 k_cur(vc, KVAL(K_LEFT), 0);
786 k_cur(vc, KVAL(K_RIGHT), 0);
789 k_fn(vc, KVAL(K_FIND), 0);
792 k_cur(vc, KVAL(K_UP), 0);
795 k_fn(vc, KVAL(K_PGUP), 0);
798 applkey(vc, 'G', vc_kbd_mode(kbd, VC_APPLIC));
802 put_queue(vc, pad_chars[value]);
803 if (value == KVAL(K_PENTER) && vc_kbd_mode(kbd, VC_CRLF))
807 static void k_shift(struct vc_data *vc, unsigned char value, char up_flag)
809 int old_state = shift_state;
815 * a CapsShift key acts like Shift but undoes CapsLock
817 if (value == KVAL(K_CAPSSHIFT)) {
818 value = KVAL(K_SHIFT);
820 clr_vc_kbd_led(kbd, VC_CAPSLOCK);
825 * handle the case that two shift or control
826 * keys are depressed simultaneously
828 if (shift_down[value])
833 if (shift_down[value])
834 shift_state |= (1 << value);
836 shift_state &= ~(1 << value);
839 if (up_flag && shift_state != old_state && npadch != -1) {
840 if (kbd->kbdmode == VC_UNICODE)
843 put_queue(vc, npadch & 0xff);
848 static void k_meta(struct vc_data *vc, unsigned char value, char up_flag)
853 if (vc_kbd_mode(kbd, VC_META)) {
854 put_queue(vc, '\033');
855 put_queue(vc, value);
857 put_queue(vc, value | 0x80);
860 static void k_ascii(struct vc_data *vc, unsigned char value, char up_flag)
868 /* decimal input of code, while Alt depressed */
871 /* hexadecimal input of code, while AltGr depressed */
879 npadch = npadch * base + value;
882 static void k_lock(struct vc_data *vc, unsigned char value, char up_flag)
886 chg_vc_kbd_lock(kbd, value);
889 static void k_slock(struct vc_data *vc, unsigned char value, char up_flag)
891 k_shift(vc, value, up_flag);
894 chg_vc_kbd_slock(kbd, value);
895 /* try to make Alt, oops, AltGr and such work */
896 if (!key_maps[kbd->lockstate ^ kbd->slockstate]) {
898 chg_vc_kbd_slock(kbd, value);
902 /* by default, 300ms interval for combination release */
903 static unsigned brl_timeout = 300;
904 MODULE_PARM_DESC(brl_timeout, "Braille keys release delay in ms (0 for commit on first key release)");
905 module_param(brl_timeout, uint, 0644);
907 static unsigned brl_nbchords = 1;
908 MODULE_PARM_DESC(brl_nbchords, "Number of chords that produce a braille pattern (0 for dead chords)");
909 module_param(brl_nbchords, uint, 0644);
911 static void k_brlcommit(struct vc_data *vc, unsigned int pattern, char up_flag)
913 static unsigned long chords;
914 static unsigned committed;
917 k_deadunicode(vc, BRL_UC_ROW | pattern, up_flag);
919 committed |= pattern;
921 if (chords == brl_nbchords) {
922 k_unicode(vc, BRL_UC_ROW | committed, up_flag);
929 static void k_brl(struct vc_data *vc, unsigned char value, char up_flag)
931 static unsigned pressed,committing;
932 static unsigned long releasestart;
934 if (kbd->kbdmode != VC_UNICODE) {
936 printk("keyboard mode must be unicode for braille patterns\n");
941 k_unicode(vc, BRL_UC_ROW, up_flag);
952 releasestart + msecs_to_jiffies(brl_timeout))) {
953 committing = pressed;
954 releasestart = jiffies;
956 pressed &= ~(1 << (value - 1));
959 k_brlcommit(vc, committing, 0);
965 k_brlcommit(vc, committing, 0);
968 pressed &= ~(1 << (value - 1));
971 pressed |= 1 << (value - 1);
973 committing = pressed;
978 * The leds display either (i) the status of NumLock, CapsLock, ScrollLock,
979 * or (ii) whatever pattern of lights people want to show using KDSETLED,
980 * or (iii) specified bits of specified words in kernel memory.
982 unsigned char getledstate(void)
987 void setledstate(struct kbd_struct *kbd, unsigned int led)
991 kbd->ledmode = LED_SHOW_IOCTL;
993 kbd->ledmode = LED_SHOW_FLAGS;
997 static inline unsigned char getleds(void)
999 struct kbd_struct *kbd = kbd_table + fg_console;
1003 if (kbd->ledmode == LED_SHOW_IOCTL)
1006 leds = kbd->ledflagstate;
1008 if (kbd->ledmode == LED_SHOW_MEM) {
1009 for (i = 0; i < 3; i++)
1010 if (ledptrs[i].valid) {
1011 if (*ledptrs[i].addr & ledptrs[i].mask)
1020 static int kbd_update_leds_helper(struct input_handle *handle, void *data)
1022 unsigned char leds = *(unsigned char *)data;
1024 if (test_bit(EV_LED, handle->dev->evbit)) {
1025 input_inject_event(handle, EV_LED, LED_SCROLLL, !!(leds & 0x01));
1026 input_inject_event(handle, EV_LED, LED_NUML, !!(leds & 0x02));
1027 input_inject_event(handle, EV_LED, LED_CAPSL, !!(leds & 0x04));
1028 input_inject_event(handle, EV_SYN, SYN_REPORT, 0);
1035 * This is the tasklet that updates LED state on all keyboards
1036 * attached to the box. The reason we use tasklet is that we
1037 * need to handle the scenario when keyboard handler is not
1038 * registered yet but we already getting updates form VT to
1041 static void kbd_bh(unsigned long dummy)
1043 unsigned char leds = getleds();
1045 if (leds != ledstate) {
1046 input_handler_for_each_handle(&kbd_handler, &leds,
1047 kbd_update_leds_helper);
1052 DECLARE_TASKLET_DISABLED(keyboard_tasklet, kbd_bh, 0);
1054 #if defined(CONFIG_X86) || defined(CONFIG_IA64) || defined(CONFIG_ALPHA) ||\
1055 defined(CONFIG_MIPS) || defined(CONFIG_PPC) || defined(CONFIG_SPARC) ||\
1056 defined(CONFIG_PARISC) || defined(CONFIG_SUPERH) ||\
1057 (defined(CONFIG_ARM) && defined(CONFIG_KEYBOARD_ATKBD) && !defined(CONFIG_ARCH_RPC)) ||\
1058 defined(CONFIG_AVR32)
1060 #define HW_RAW(dev) (test_bit(EV_MSC, dev->evbit) && test_bit(MSC_RAW, dev->mscbit) &&\
1061 ((dev)->id.bustype == BUS_I8042) && ((dev)->id.vendor == 0x0001) && ((dev)->id.product == 0x0001))
1063 static const unsigned short x86_keycodes[256] =
1064 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
1065 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
1066 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
1067 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
1068 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
1069 80, 81, 82, 83, 84,118, 86, 87, 88,115,120,119,121,112,123, 92,
1070 284,285,309, 0,312, 91,327,328,329,331,333,335,336,337,338,339,
1071 367,288,302,304,350, 89,334,326,267,126,268,269,125,347,348,349,
1072 360,261,262,263,268,376,100,101,321,316,373,286,289,102,351,355,
1073 103,104,105,275,287,279,258,106,274,107,294,364,358,363,362,361,
1074 291,108,381,281,290,272,292,305,280, 99,112,257,306,359,113,114,
1075 264,117,271,374,379,265,266, 93, 94, 95, 85,259,375,260, 90,116,
1076 377,109,111,277,278,282,283,295,296,297,299,300,301,293,303,307,
1077 308,310,313,314,315,317,318,319,320,357,322,323,324,325,276,330,
1078 332,340,365,342,343,344,345,346,356,270,341,368,369,370,371,372 };
1081 static int sparc_l1_a_state = 0;
1082 extern void sun_do_break(void);
1085 static int emulate_raw(struct vc_data *vc, unsigned int keycode,
1086 unsigned char up_flag)
1092 put_queue(vc, 0xe1);
1093 put_queue(vc, 0x1d | up_flag);
1094 put_queue(vc, 0x45 | up_flag);
1099 put_queue(vc, 0xf2);
1104 put_queue(vc, 0xf1);
1109 * Real AT keyboards (that's what we're trying
1110 * to emulate here emit 0xe0 0x2a 0xe0 0x37 when
1111 * pressing PrtSc/SysRq alone, but simply 0x54
1112 * when pressing Alt+PrtSc/SysRq.
1115 put_queue(vc, 0x54 | up_flag);
1117 put_queue(vc, 0xe0);
1118 put_queue(vc, 0x2a | up_flag);
1119 put_queue(vc, 0xe0);
1120 put_queue(vc, 0x37 | up_flag);
1128 code = x86_keycodes[keycode];
1133 put_queue(vc, 0xe0);
1134 put_queue(vc, (code & 0x7f) | up_flag);
1144 #define HW_RAW(dev) 0
1146 static int emulate_raw(struct vc_data *vc, unsigned int keycode, unsigned char up_flag)
1151 put_queue(vc, keycode | up_flag);
1156 static void kbd_rawcode(unsigned char data)
1158 struct vc_data *vc = vc_cons[fg_console].d;
1159 kbd = kbd_table + vc->vc_num;
1160 if (kbd->kbdmode == VC_RAW)
1161 put_queue(vc, data);
1164 static void kbd_keycode(unsigned int keycode, int down, int hw_raw)
1166 struct vc_data *vc = vc_cons[fg_console].d;
1167 unsigned short keysym, *key_map;
1168 unsigned char type, raw_mode;
1169 struct tty_struct *tty;
1171 struct keyboard_notifier_param param = { .vc = vc, .value = keycode, .down = down };
1175 if (tty && (!tty->driver_data)) {
1176 /* No driver data? Strange. Okay we fix it then. */
1177 tty->driver_data = vc;
1180 kbd = kbd_table + vc->vc_num;
1182 if (keycode == KEY_LEFTALT || keycode == KEY_RIGHTALT)
1183 sysrq_alt = down ? keycode : 0;
1185 if (keycode == KEY_STOP)
1186 sparc_l1_a_state = down;
1191 #ifdef CONFIG_MAC_EMUMOUSEBTN
1192 if (mac_hid_mouse_emulate_buttons(1, keycode, down))
1194 #endif /* CONFIG_MAC_EMUMOUSEBTN */
1196 if ((raw_mode = (kbd->kbdmode == VC_RAW)) && !hw_raw)
1197 if (emulate_raw(vc, keycode, !down << 7))
1198 if (keycode < BTN_MISC && printk_ratelimit())
1199 printk(KERN_WARNING "keyboard.c: can't emulate rawmode for keycode %d\n", keycode);
1201 #ifdef CONFIG_BOOTSPLASH
1202 /* This code has to be redone for some non-x86 platforms */
1203 if (down == 1 && (keycode == 0x3c || keycode == 0x01)) { /* F2 and ESC on PC keyboard */
1204 extern int splash_verbose(void);
1205 if (splash_verbose())
1211 if (down && !rep && keycode == KEY_PAUSE && kdb_on == 1) {
1212 kdb(KDB_REASON_KEYBOARD, 0, get_irq_regs());
1215 #endif /* CONFIG_KDB */
1217 #ifdef CONFIG_MAGIC_SYSRQ /* Handle the SysRq Hack */
1218 if (keycode == KEY_SYSRQ && (sysrq_down || (down == 1 && sysrq_alt))) {
1221 sysrq_alt_use = sysrq_alt;
1225 if (sysrq_down && !down && keycode == sysrq_alt_use)
1227 if (sysrq_down && down && !rep) {
1228 handle_sysrq(kbd_sysrq_xlate[keycode], tty);
1233 if (keycode == KEY_A && sparc_l1_a_state) {
1234 sparc_l1_a_state = 0;
1239 if (kbd->kbdmode == VC_MEDIUMRAW) {
1241 * This is extended medium raw mode, with keys above 127
1242 * encoded as 0, high 7 bits, low 7 bits, with the 0 bearing
1243 * the 'up' flag if needed. 0 is reserved, so this shouldn't
1244 * interfere with anything else. The two bytes after 0 will
1245 * always have the up flag set not to interfere with older
1246 * applications. This allows for 16384 different keycodes,
1247 * which should be enough.
1249 if (keycode < 128) {
1250 put_queue(vc, keycode | (!down << 7));
1252 put_queue(vc, !down << 7);
1253 put_queue(vc, (keycode >> 7) | 0x80);
1254 put_queue(vc, keycode | 0x80);
1260 set_bit(keycode, key_down);
1262 clear_bit(keycode, key_down);
1265 (!vc_kbd_mode(kbd, VC_REPEAT) ||
1266 (tty && !L_ECHO(tty) && tty_chars_in_buffer(tty)))) {
1268 * Don't repeat a key if the input buffers are not empty and the
1269 * characters get aren't echoed locally. This makes key repeat
1270 * usable with slow applications and under heavy loads.
1275 param.shift = shift_final = (shift_state | kbd->slockstate) ^ kbd->lockstate;
1276 param.ledstate = kbd->ledflagstate;
1277 key_map = key_maps[shift_final];
1279 if (atomic_notifier_call_chain(&keyboard_notifier_list, KBD_KEYCODE, ¶m) == NOTIFY_STOP || !key_map) {
1280 atomic_notifier_call_chain(&keyboard_notifier_list, KBD_UNBOUND_KEYCODE, ¶m);
1281 compute_shiftstate();
1282 kbd->slockstate = 0;
1286 if (keycode >= NR_KEYS)
1287 if (keycode >= KEY_BRL_DOT1 && keycode <= KEY_BRL_DOT8)
1288 keysym = U(K(KT_BRL, keycode - KEY_BRL_DOT1 + 1));
1292 keysym = key_map[keycode];
1294 type = KTYP(keysym);
1297 param.value = keysym;
1298 if (atomic_notifier_call_chain(&keyboard_notifier_list, KBD_UNICODE, ¶m) == NOTIFY_STOP)
1300 if (down && !raw_mode)
1301 to_utf8(vc, keysym);
1307 if (type == KT_LETTER) {
1309 if (vc_kbd_led(kbd, VC_CAPSLOCK)) {
1310 key_map = key_maps[shift_final ^ (1 << KG_SHIFT)];
1312 keysym = key_map[keycode];
1315 param.value = keysym;
1317 if (atomic_notifier_call_chain(&keyboard_notifier_list, KBD_KEYSYM, ¶m) == NOTIFY_STOP)
1320 if (raw_mode && type != KT_SPEC && type != KT_SHIFT)
1323 (*k_handler[type])(vc, keysym & 0xff, !down);
1325 param.ledstate = kbd->ledflagstate;
1326 atomic_notifier_call_chain(&keyboard_notifier_list, KBD_POST_KEYSYM, ¶m);
1328 if (type != KT_SLOCK)
1329 kbd->slockstate = 0;
1332 static void kbd_event(struct input_handle *handle, unsigned int event_type,
1333 unsigned int event_code, int value)
1335 /* We are called with interrupts disabled, just take the lock */
1336 spin_lock(&kbd_event_lock);
1338 if (event_type == EV_MSC && event_code == MSC_RAW && HW_RAW(handle->dev))
1340 if (event_type == EV_KEY)
1341 kbd_keycode(event_code, value, HW_RAW(handle->dev));
1343 spin_unlock(&kbd_event_lock);
1345 tasklet_schedule(&keyboard_tasklet);
1346 do_poke_blanked_console = 1;
1347 schedule_console_callback();
1351 * When a keyboard (or other input device) is found, the kbd_connect
1352 * function is called. The function then looks at the device, and if it
1353 * likes it, it can open it and get events from it. In this (kbd_connect)
1354 * function, we should decide which VT to bind that keyboard to initially.
1356 static int kbd_connect(struct input_handler *handler, struct input_dev *dev,
1357 const struct input_device_id *id)
1359 struct input_handle *handle;
1363 for (i = KEY_RESERVED; i < BTN_MISC; i++)
1364 if (test_bit(i, dev->keybit))
1367 if (i == BTN_MISC && !test_bit(EV_SND, dev->evbit))
1370 handle = kzalloc(sizeof(struct input_handle), GFP_KERNEL);
1375 handle->handler = handler;
1376 handle->name = "kbd";
1378 error = input_register_handle(handle);
1380 goto err_free_handle;
1382 error = input_open_device(handle);
1384 goto err_unregister_handle;
1388 err_unregister_handle:
1389 input_unregister_handle(handle);
1395 static void kbd_disconnect(struct input_handle *handle)
1397 input_close_device(handle);
1398 input_unregister_handle(handle);
1403 * Start keyboard handler on the new keyboard by refreshing LED state to
1404 * match the rest of the system.
1406 static void kbd_start(struct input_handle *handle)
1408 tasklet_disable(&keyboard_tasklet);
1410 if (ledstate != 0xff)
1411 kbd_update_leds_helper(handle, &ledstate);
1413 tasklet_enable(&keyboard_tasklet);
1416 static const struct input_device_id kbd_ids[] = {
1418 .flags = INPUT_DEVICE_ID_MATCH_EVBIT,
1419 .evbit = { BIT_MASK(EV_KEY) },
1423 .flags = INPUT_DEVICE_ID_MATCH_EVBIT,
1424 .evbit = { BIT_MASK(EV_SND) },
1427 { }, /* Terminating entry */
1430 MODULE_DEVICE_TABLE(input, kbd_ids);
1432 static struct input_handler kbd_handler = {
1434 .connect = kbd_connect,
1435 .disconnect = kbd_disconnect,
1438 .id_table = kbd_ids,
1441 int __init kbd_init(void)
1446 for (i = 0; i < MAX_NR_CONSOLES; i++) {
1447 kbd_table[i].ledflagstate = KBD_DEFLEDS;
1448 kbd_table[i].default_ledflagstate = KBD_DEFLEDS;
1449 kbd_table[i].ledmode = LED_SHOW_FLAGS;
1450 kbd_table[i].lockstate = KBD_DEFLOCK;
1451 kbd_table[i].slockstate = 0;
1452 kbd_table[i].modeflags = KBD_DEFMODE;
1453 kbd_table[i].kbdmode = default_utf8 ? VC_UNICODE : VC_XLATE;
1456 error = input_register_handler(&kbd_handler);
1460 tasklet_enable(&keyboard_tasklet);
1461 tasklet_schedule(&keyboard_tasklet);