1 // SPDX-License-Identifier: GPL-2.0
3 * Written for linux by Johan Myreen as a translation from
4 * the assembly version by Linus (with diacriticals added)
6 * Some additional features added by Christoph Niemann (ChN), March 1993
8 * Loadable keymaps by Risto Kankkunen, May 1993
10 * Diacriticals redone & other small changes, aeb@cwi.nl, June 1993
11 * Added decr/incr_console, dynamic keymaps, Unicode support,
12 * dynamic function/string keys, led setting, Sept 1994
13 * `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)
22 * 30-07-98: Dead keys redone, aeb@cwi.nl.
23 * 21-08-02: Converted to input API, major cleanup. (Vojtech Pavlik)
26 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
28 #include <linux/consolemap.h>
29 #include <linux/init.h>
30 #include <linux/input.h>
31 #include <linux/jiffies.h>
32 #include <linux/kbd_diacr.h>
33 #include <linux/kbd_kern.h>
34 #include <linux/leds.h>
36 #include <linux/module.h>
37 #include <linux/nospec.h>
38 #include <linux/notifier.h>
39 #include <linux/reboot.h>
40 #include <linux/sched/debug.h>
41 #include <linux/sched/signal.h>
42 #include <linux/slab.h>
43 #include <linux/spinlock.h>
44 #include <linux/string.h>
45 #include <linux/tty_flip.h>
46 #include <linux/tty.h>
47 #include <linux/uaccess.h>
48 #include <linux/vt_kern.h>
50 #include <asm/irq_regs.h>
53 * Exported functions/variables
56 #define KBD_DEFMODE (BIT(VC_REPEAT) | BIT(VC_META))
58 #if defined(CONFIG_X86) || defined(CONFIG_PARISC)
59 #include <asm/kbdleds.h>
61 static inline int kbd_defleds(void)
74 k_self, k_fn, k_spec, k_pad,\
75 k_dead, k_cons, k_cur, k_shift,\
76 k_meta, k_ascii, k_lock, k_lowercase,\
77 k_slock, k_dead2, k_brl, k_ignore
79 typedef void (k_handler_fn
)(struct vc_data
*vc
, unsigned char value
,
81 static k_handler_fn K_HANDLERS
;
82 static k_handler_fn
*k_handler
[16] = { K_HANDLERS
};
85 fn_null, fn_enter, fn_show_ptregs, fn_show_mem,\
86 fn_show_state, fn_send_intr, fn_lastcons, fn_caps_toggle,\
87 fn_num, fn_hold, fn_scroll_forw, fn_scroll_back,\
88 fn_boot_it, fn_caps_on, fn_compose, fn_SAK,\
89 fn_dec_console, fn_inc_console, fn_spawn_con, fn_bare_num
91 typedef void (fn_handler_fn
)(struct vc_data
*vc
);
92 static fn_handler_fn FN_HANDLERS
;
93 static fn_handler_fn
*fn_handler
[] = { FN_HANDLERS
};
96 * Variables exported for vt_ioctl.c
99 struct vt_spawn_console vt_spawn_con
= {
100 .lock
= __SPIN_LOCK_UNLOCKED(vt_spawn_con
.lock
),
110 static struct kbd_struct kbd_table
[MAX_NR_CONSOLES
];
111 static struct kbd_struct
*kbd
= kbd_table
;
113 /* maximum values each key_handler can handle */
114 static const unsigned char max_vals
[] = {
116 [ KT_FN
] = ARRAY_SIZE(func_table
) - 1,
117 [ KT_SPEC
] = ARRAY_SIZE(fn_handler
) - 1,
118 [ KT_PAD
] = NR_PAD
- 1,
119 [ KT_DEAD
] = NR_DEAD
- 1,
122 [ KT_SHIFT
] = NR_SHIFT
- 1,
124 [ KT_ASCII
] = NR_ASCII
- 1,
125 [ KT_LOCK
] = NR_LOCK
- 1,
127 [ KT_SLOCK
] = NR_LOCK
- 1,
129 [ KT_BRL
] = NR_BRL
- 1,
132 static const int NR_TYPES
= ARRAY_SIZE(max_vals
);
134 static struct input_handler kbd_handler
;
135 static DEFINE_SPINLOCK(kbd_event_lock
);
136 static DEFINE_SPINLOCK(led_lock
);
137 static DEFINE_SPINLOCK(func_buf_lock
); /* guard 'func_buf' and friends */
138 static DECLARE_BITMAP(key_down
, KEY_CNT
); /* keyboard key bitmap */
139 static unsigned char shift_down
[NR_SHIFT
]; /* shift state counters.. */
140 static bool dead_key_next
;
142 /* Handles a number being assembled on the number pad */
143 static bool npadch_active
;
144 static unsigned int npadch_value
;
146 static unsigned int diacr
;
147 static bool rep
; /* flag telling character repeat */
149 static int shift_state
= 0;
151 static unsigned int ledstate
= -1U; /* undefined */
152 static unsigned char ledioctl
;
155 * Notifier list for console keyboard events
157 static ATOMIC_NOTIFIER_HEAD(keyboard_notifier_list
);
159 int register_keyboard_notifier(struct notifier_block
*nb
)
161 return atomic_notifier_chain_register(&keyboard_notifier_list
, nb
);
163 EXPORT_SYMBOL_GPL(register_keyboard_notifier
);
165 int unregister_keyboard_notifier(struct notifier_block
*nb
)
167 return atomic_notifier_chain_unregister(&keyboard_notifier_list
, nb
);
169 EXPORT_SYMBOL_GPL(unregister_keyboard_notifier
);
172 * Translation of scancodes to keycodes. We set them on only the first
173 * keyboard in the list that accepts the scancode and keycode.
174 * Explanation for not choosing the first attached keyboard anymore:
175 * USB keyboards for example have two event devices: one for all "normal"
176 * keys and one for extra function keys (like "volume up", "make coffee",
177 * etc.). So this means that scancodes for the extra function keys won't
178 * be valid for the first event device, but will be for the second.
181 struct getset_keycode_data
{
182 struct input_keymap_entry ke
;
186 static int getkeycode_helper(struct input_handle
*handle
, void *data
)
188 struct getset_keycode_data
*d
= data
;
190 d
->error
= input_get_keycode(handle
->dev
, &d
->ke
);
192 return d
->error
== 0; /* stop as soon as we successfully get one */
195 static int getkeycode(unsigned int scancode
)
197 struct getset_keycode_data d
= {
200 .len
= sizeof(scancode
),
206 memcpy(d
.ke
.scancode
, &scancode
, sizeof(scancode
));
208 input_handler_for_each_handle(&kbd_handler
, &d
, getkeycode_helper
);
210 return d
.error
?: d
.ke
.keycode
;
213 static int setkeycode_helper(struct input_handle
*handle
, void *data
)
215 struct getset_keycode_data
*d
= data
;
217 d
->error
= input_set_keycode(handle
->dev
, &d
->ke
);
219 return d
->error
== 0; /* stop as soon as we successfully set one */
222 static int setkeycode(unsigned int scancode
, unsigned int keycode
)
224 struct getset_keycode_data d
= {
227 .len
= sizeof(scancode
),
233 memcpy(d
.ke
.scancode
, &scancode
, sizeof(scancode
));
235 input_handler_for_each_handle(&kbd_handler
, &d
, setkeycode_helper
);
241 * Making beeps and bells. Note that we prefer beeps to bells, but when
242 * shutting the sound off we do both.
245 static int kd_sound_helper(struct input_handle
*handle
, void *data
)
247 unsigned int *hz
= data
;
248 struct input_dev
*dev
= handle
->dev
;
250 if (test_bit(EV_SND
, dev
->evbit
)) {
251 if (test_bit(SND_TONE
, dev
->sndbit
)) {
252 input_inject_event(handle
, EV_SND
, SND_TONE
, *hz
);
256 if (test_bit(SND_BELL
, dev
->sndbit
))
257 input_inject_event(handle
, EV_SND
, SND_BELL
, *hz
? 1 : 0);
263 static void kd_nosound(struct timer_list
*unused
)
265 static unsigned int zero
;
267 input_handler_for_each_handle(&kbd_handler
, &zero
, kd_sound_helper
);
270 static DEFINE_TIMER(kd_mksound_timer
, kd_nosound
);
272 void kd_mksound(unsigned int hz
, unsigned int ticks
)
274 del_timer_sync(&kd_mksound_timer
);
276 input_handler_for_each_handle(&kbd_handler
, &hz
, kd_sound_helper
);
279 mod_timer(&kd_mksound_timer
, jiffies
+ ticks
);
281 EXPORT_SYMBOL(kd_mksound
);
284 * Setting the keyboard rate.
287 static int kbd_rate_helper(struct input_handle
*handle
, void *data
)
289 struct input_dev
*dev
= handle
->dev
;
290 struct kbd_repeat
*rpt
= data
;
292 if (test_bit(EV_REP
, dev
->evbit
)) {
294 if (rpt
[0].delay
> 0)
295 input_inject_event(handle
,
296 EV_REP
, REP_DELAY
, rpt
[0].delay
);
297 if (rpt
[0].period
> 0)
298 input_inject_event(handle
,
299 EV_REP
, REP_PERIOD
, rpt
[0].period
);
301 rpt
[1].delay
= dev
->rep
[REP_DELAY
];
302 rpt
[1].period
= dev
->rep
[REP_PERIOD
];
308 int kbd_rate(struct kbd_repeat
*rpt
)
310 struct kbd_repeat data
[2] = { *rpt
};
312 input_handler_for_each_handle(&kbd_handler
, data
, kbd_rate_helper
);
313 *rpt
= data
[1]; /* Copy currently used settings */
321 static void put_queue(struct vc_data
*vc
, int ch
)
323 tty_insert_flip_char(&vc
->port
, ch
, 0);
324 tty_schedule_flip(&vc
->port
);
327 static void puts_queue(struct vc_data
*vc
, const char *cp
)
329 tty_insert_flip_string(&vc
->port
, cp
, strlen(cp
));
330 tty_schedule_flip(&vc
->port
);
333 static void applkey(struct vc_data
*vc
, int key
, char mode
)
335 static char buf
[] = { 0x1b, 'O', 0x00, 0x00 };
337 buf
[1] = (mode
? 'O' : '[');
343 * Many other routines do put_queue, but I think either
344 * they produce ASCII, or they produce some user-assigned
345 * string, and in both cases we might assume that it is
348 static void to_utf8(struct vc_data
*vc
, uint c
)
353 else if (c
< 0x800) {
354 /* 110***** 10****** */
355 put_queue(vc
, 0xc0 | (c
>> 6));
356 put_queue(vc
, 0x80 | (c
& 0x3f));
357 } else if (c
< 0x10000) {
358 if (c
>= 0xD800 && c
< 0xE000)
362 /* 1110**** 10****** 10****** */
363 put_queue(vc
, 0xe0 | (c
>> 12));
364 put_queue(vc
, 0x80 | ((c
>> 6) & 0x3f));
365 put_queue(vc
, 0x80 | (c
& 0x3f));
366 } else if (c
< 0x110000) {
367 /* 11110*** 10****** 10****** 10****** */
368 put_queue(vc
, 0xf0 | (c
>> 18));
369 put_queue(vc
, 0x80 | ((c
>> 12) & 0x3f));
370 put_queue(vc
, 0x80 | ((c
>> 6) & 0x3f));
371 put_queue(vc
, 0x80 | (c
& 0x3f));
376 * Called after returning from RAW mode or when changing consoles - recompute
377 * shift_down[] and shift_state from key_down[] maybe called when keymap is
378 * undefined, so that shiftkey release is seen. The caller must hold the
382 static void do_compute_shiftstate(void)
384 unsigned int k
, sym
, val
;
387 memset(shift_down
, 0, sizeof(shift_down
));
389 for_each_set_bit(k
, key_down
, min(NR_KEYS
, KEY_CNT
)) {
390 sym
= U(key_maps
[0][k
]);
391 if (KTYP(sym
) != KT_SHIFT
&& KTYP(sym
) != KT_SLOCK
)
395 if (val
== KVAL(K_CAPSSHIFT
))
399 shift_state
|= BIT(val
);
403 /* We still have to export this method to vt.c */
404 void compute_shiftstate(void)
407 spin_lock_irqsave(&kbd_event_lock
, flags
);
408 do_compute_shiftstate();
409 spin_unlock_irqrestore(&kbd_event_lock
, flags
);
413 * We have a combining character DIACR here, followed by the character CH.
414 * If the combination occurs in the table, return the corresponding value.
415 * Otherwise, if CH is a space or equals DIACR, return DIACR.
416 * Otherwise, conclude that DIACR was not combining after all,
417 * queue it and return CH.
419 static unsigned int handle_diacr(struct vc_data
*vc
, unsigned int ch
)
421 unsigned int d
= diacr
;
426 if ((d
& ~0xff) == BRL_UC_ROW
) {
427 if ((ch
& ~0xff) == BRL_UC_ROW
)
430 for (i
= 0; i
< accent_table_size
; i
++)
431 if (accent_table
[i
].diacr
== d
&& accent_table
[i
].base
== ch
)
432 return accent_table
[i
].result
;
435 if (ch
== ' ' || ch
== (BRL_UC_ROW
|0) || ch
== d
)
438 if (kbd
->kbdmode
== VC_UNICODE
)
441 int c
= conv_uni_to_8bit(d
);
450 * Special function handlers
452 static void fn_enter(struct vc_data
*vc
)
455 if (kbd
->kbdmode
== VC_UNICODE
)
458 int c
= conv_uni_to_8bit(diacr
);
466 if (vc_kbd_mode(kbd
, VC_CRLF
))
470 static void fn_caps_toggle(struct vc_data
*vc
)
475 chg_vc_kbd_led(kbd
, VC_CAPSLOCK
);
478 static void fn_caps_on(struct vc_data
*vc
)
483 set_vc_kbd_led(kbd
, VC_CAPSLOCK
);
486 static void fn_show_ptregs(struct vc_data
*vc
)
488 struct pt_regs
*regs
= get_irq_regs();
494 static void fn_hold(struct vc_data
*vc
)
496 struct tty_struct
*tty
= vc
->port
.tty
;
502 * Note: SCROLLOCK will be set (cleared) by stop_tty (start_tty);
503 * these routines are also activated by ^S/^Q.
504 * (And SCROLLOCK can also be set by the ioctl KDSKBLED.)
512 static void fn_num(struct vc_data
*vc
)
514 if (vc_kbd_mode(kbd
, VC_APPLIC
))
521 * Bind this to Shift-NumLock if you work in application keypad mode
522 * but want to be able to change the NumLock flag.
523 * Bind this to NumLock if you prefer that the NumLock key always
524 * changes the NumLock flag.
526 static void fn_bare_num(struct vc_data
*vc
)
529 chg_vc_kbd_led(kbd
, VC_NUMLOCK
);
532 static void fn_lastcons(struct vc_data
*vc
)
534 /* switch to the last used console, ChN */
535 set_console(last_console
);
538 static void fn_dec_console(struct vc_data
*vc
)
540 int i
, cur
= fg_console
;
542 /* Currently switching? Queue this next switch relative to that. */
543 if (want_console
!= -1)
546 for (i
= cur
- 1; i
!= cur
; i
--) {
548 i
= MAX_NR_CONSOLES
- 1;
549 if (vc_cons_allocated(i
))
555 static void fn_inc_console(struct vc_data
*vc
)
557 int i
, cur
= fg_console
;
559 /* Currently switching? Queue this next switch relative to that. */
560 if (want_console
!= -1)
563 for (i
= cur
+1; i
!= cur
; i
++) {
564 if (i
== MAX_NR_CONSOLES
)
566 if (vc_cons_allocated(i
))
572 static void fn_send_intr(struct vc_data
*vc
)
574 tty_insert_flip_char(&vc
->port
, 0, TTY_BREAK
);
575 tty_schedule_flip(&vc
->port
);
578 static void fn_scroll_forw(struct vc_data
*vc
)
583 static void fn_scroll_back(struct vc_data
*vc
)
588 static void fn_show_mem(struct vc_data
*vc
)
593 static void fn_show_state(struct vc_data
*vc
)
598 static void fn_boot_it(struct vc_data
*vc
)
603 static void fn_compose(struct vc_data
*vc
)
605 dead_key_next
= true;
608 static void fn_spawn_con(struct vc_data
*vc
)
610 spin_lock(&vt_spawn_con
.lock
);
611 if (vt_spawn_con
.pid
)
612 if (kill_pid(vt_spawn_con
.pid
, vt_spawn_con
.sig
, 1)) {
613 put_pid(vt_spawn_con
.pid
);
614 vt_spawn_con
.pid
= NULL
;
616 spin_unlock(&vt_spawn_con
.lock
);
619 static void fn_SAK(struct vc_data
*vc
)
621 struct work_struct
*SAK_work
= &vc_cons
[fg_console
].SAK_work
;
622 schedule_work(SAK_work
);
625 static void fn_null(struct vc_data
*vc
)
627 do_compute_shiftstate();
631 * Special key handlers
633 static void k_ignore(struct vc_data
*vc
, unsigned char value
, char up_flag
)
637 static void k_spec(struct vc_data
*vc
, unsigned char value
, char up_flag
)
641 if (value
>= ARRAY_SIZE(fn_handler
))
643 if ((kbd
->kbdmode
== VC_RAW
||
644 kbd
->kbdmode
== VC_MEDIUMRAW
||
645 kbd
->kbdmode
== VC_OFF
) &&
646 value
!= KVAL(K_SAK
))
647 return; /* SAK is allowed even in raw mode */
648 fn_handler
[value
](vc
);
651 static void k_lowercase(struct vc_data
*vc
, unsigned char value
, char up_flag
)
653 pr_err("k_lowercase was called - impossible\n");
656 static void k_unicode(struct vc_data
*vc
, unsigned int value
, char up_flag
)
659 return; /* no action, if this is a key release */
662 value
= handle_diacr(vc
, value
);
665 dead_key_next
= false;
669 if (kbd
->kbdmode
== VC_UNICODE
)
672 int c
= conv_uni_to_8bit(value
);
679 * Handle dead key. Note that we now may have several
680 * dead keys modifying the same character. Very useful
683 static void k_deadunicode(struct vc_data
*vc
, unsigned int value
, char up_flag
)
688 diacr
= (diacr
? handle_diacr(vc
, value
) : value
);
691 static void k_self(struct vc_data
*vc
, unsigned char value
, char up_flag
)
693 k_unicode(vc
, conv_8bit_to_uni(value
), up_flag
);
696 static void k_dead2(struct vc_data
*vc
, unsigned char value
, char up_flag
)
698 k_deadunicode(vc
, value
, up_flag
);
702 * Obsolete - for backwards compatibility only
704 static void k_dead(struct vc_data
*vc
, unsigned char value
, char up_flag
)
706 static const unsigned char ret_diacr
[NR_DEAD
] = {
707 '`', /* dead_grave */
708 '\'', /* dead_acute */
709 '^', /* dead_circumflex */
710 '~', /* dead_tilda */
711 '"', /* dead_diaeresis */
712 ',', /* dead_cedilla */
713 '_', /* dead_macron */
714 'U', /* dead_breve */
715 '.', /* dead_abovedot */
716 '*', /* dead_abovering */
717 '=', /* dead_doubleacute */
718 'c', /* dead_caron */
719 'k', /* dead_ogonek */
721 '#', /* dead_voiced_sound */
722 'o', /* dead_semivoiced_sound */
723 '!', /* dead_belowdot */
726 '-', /* dead_stroke */
727 ')', /* dead_abovecomma */
728 '(', /* dead_abovereversedcomma */
729 ':', /* dead_doublegrave */
730 'n', /* dead_invertedbreve */
731 ';', /* dead_belowcomma */
732 '$', /* dead_currency */
733 '@', /* dead_greek */
736 k_deadunicode(vc
, ret_diacr
[value
], up_flag
);
739 static void k_cons(struct vc_data
*vc
, unsigned char value
, char up_flag
)
747 static void k_fn(struct vc_data
*vc
, unsigned char value
, char up_flag
)
752 if ((unsigned)value
< ARRAY_SIZE(func_table
)) {
755 spin_lock_irqsave(&func_buf_lock
, flags
);
756 if (func_table
[value
])
757 puts_queue(vc
, func_table
[value
]);
758 spin_unlock_irqrestore(&func_buf_lock
, flags
);
761 pr_err("k_fn called with value=%d\n", value
);
764 static void k_cur(struct vc_data
*vc
, unsigned char value
, char up_flag
)
766 static const char cur_chars
[] = "BDCA";
771 applkey(vc
, cur_chars
[value
], vc_kbd_mode(kbd
, VC_CKMODE
));
774 static void k_pad(struct vc_data
*vc
, unsigned char value
, char up_flag
)
776 static const char pad_chars
[] = "0123456789+-*/\015,.?()#";
777 static const char app_map
[] = "pqrstuvwxylSRQMnnmPQS";
780 return; /* no action, if this is a key release */
782 /* kludge... shift forces cursor/number keys */
783 if (vc_kbd_mode(kbd
, VC_APPLIC
) && !shift_down
[KG_SHIFT
]) {
784 applkey(vc
, app_map
[value
], 1);
788 if (!vc_kbd_led(kbd
, VC_NUMLOCK
)) {
793 k_fn(vc
, KVAL(K_REMOVE
), 0);
796 k_fn(vc
, KVAL(K_INSERT
), 0);
799 k_fn(vc
, KVAL(K_SELECT
), 0);
802 k_cur(vc
, KVAL(K_DOWN
), 0);
805 k_fn(vc
, KVAL(K_PGDN
), 0);
808 k_cur(vc
, KVAL(K_LEFT
), 0);
811 k_cur(vc
, KVAL(K_RIGHT
), 0);
814 k_fn(vc
, KVAL(K_FIND
), 0);
817 k_cur(vc
, KVAL(K_UP
), 0);
820 k_fn(vc
, KVAL(K_PGUP
), 0);
823 applkey(vc
, 'G', vc_kbd_mode(kbd
, VC_APPLIC
));
828 put_queue(vc
, pad_chars
[value
]);
829 if (value
== KVAL(K_PENTER
) && vc_kbd_mode(kbd
, VC_CRLF
))
833 static void k_shift(struct vc_data
*vc
, unsigned char value
, char up_flag
)
835 int old_state
= shift_state
;
841 * a CapsShift key acts like Shift but undoes CapsLock
843 if (value
== KVAL(K_CAPSSHIFT
)) {
844 value
= KVAL(K_SHIFT
);
846 clr_vc_kbd_led(kbd
, VC_CAPSLOCK
);
851 * handle the case that two shift or control
852 * keys are depressed simultaneously
854 if (shift_down
[value
])
859 if (shift_down
[value
])
860 shift_state
|= BIT(value
);
862 shift_state
&= ~BIT(value
);
865 if (up_flag
&& shift_state
!= old_state
&& npadch_active
) {
866 if (kbd
->kbdmode
== VC_UNICODE
)
867 to_utf8(vc
, npadch_value
);
869 put_queue(vc
, npadch_value
& 0xff);
870 npadch_active
= false;
874 static void k_meta(struct vc_data
*vc
, unsigned char value
, char up_flag
)
879 if (vc_kbd_mode(kbd
, VC_META
)) {
880 put_queue(vc
, '\033');
881 put_queue(vc
, value
);
883 put_queue(vc
, value
| BIT(7));
886 static void k_ascii(struct vc_data
*vc
, unsigned char value
, char up_flag
)
894 /* decimal input of code, while Alt depressed */
897 /* hexadecimal input of code, while AltGr depressed */
902 if (!npadch_active
) {
904 npadch_active
= true;
907 npadch_value
= npadch_value
* base
+ value
;
910 static void k_lock(struct vc_data
*vc
, unsigned char value
, char up_flag
)
915 chg_vc_kbd_lock(kbd
, value
);
918 static void k_slock(struct vc_data
*vc
, unsigned char value
, char up_flag
)
920 k_shift(vc
, value
, up_flag
);
924 chg_vc_kbd_slock(kbd
, value
);
925 /* try to make Alt, oops, AltGr and such work */
926 if (!key_maps
[kbd
->lockstate
^ kbd
->slockstate
]) {
928 chg_vc_kbd_slock(kbd
, value
);
932 /* by default, 300ms interval for combination release */
933 static unsigned brl_timeout
= 300;
934 MODULE_PARM_DESC(brl_timeout
, "Braille keys release delay in ms (0 for commit on first key release)");
935 module_param(brl_timeout
, uint
, 0644);
937 static unsigned brl_nbchords
= 1;
938 MODULE_PARM_DESC(brl_nbchords
, "Number of chords that produce a braille pattern (0 for dead chords)");
939 module_param(brl_nbchords
, uint
, 0644);
941 static void k_brlcommit(struct vc_data
*vc
, unsigned int pattern
, char up_flag
)
943 static unsigned long chords
;
944 static unsigned committed
;
947 k_deadunicode(vc
, BRL_UC_ROW
| pattern
, up_flag
);
949 committed
|= pattern
;
951 if (chords
== brl_nbchords
) {
952 k_unicode(vc
, BRL_UC_ROW
| committed
, up_flag
);
959 static void k_brl(struct vc_data
*vc
, unsigned char value
, char up_flag
)
961 static unsigned pressed
, committing
;
962 static unsigned long releasestart
;
964 if (kbd
->kbdmode
!= VC_UNICODE
) {
966 pr_warn("keyboard mode must be unicode for braille patterns\n");
971 k_unicode(vc
, BRL_UC_ROW
, up_flag
);
979 pressed
|= BIT(value
- 1);
981 committing
= pressed
;
982 } else if (brl_timeout
) {
985 releasestart
+ msecs_to_jiffies(brl_timeout
))) {
986 committing
= pressed
;
987 releasestart
= jiffies
;
989 pressed
&= ~BIT(value
- 1);
990 if (!pressed
&& committing
) {
991 k_brlcommit(vc
, committing
, 0);
996 k_brlcommit(vc
, committing
, 0);
999 pressed
&= ~BIT(value
- 1);
1003 #if IS_ENABLED(CONFIG_INPUT_LEDS) && IS_ENABLED(CONFIG_LEDS_TRIGGERS)
1005 struct kbd_led_trigger
{
1006 struct led_trigger trigger
;
1010 static int kbd_led_trigger_activate(struct led_classdev
*cdev
)
1012 struct kbd_led_trigger
*trigger
=
1013 container_of(cdev
->trigger
, struct kbd_led_trigger
, trigger
);
1015 tasklet_disable(&keyboard_tasklet
);
1016 if (ledstate
!= -1U)
1017 led_trigger_event(&trigger
->trigger
,
1018 ledstate
& trigger
->mask
?
1019 LED_FULL
: LED_OFF
);
1020 tasklet_enable(&keyboard_tasklet
);
1025 #define KBD_LED_TRIGGER(_led_bit, _name) { \
1028 .activate = kbd_led_trigger_activate, \
1030 .mask = BIT(_led_bit), \
1033 #define KBD_LOCKSTATE_TRIGGER(_led_bit, _name) \
1034 KBD_LED_TRIGGER((_led_bit) + 8, _name)
1036 static struct kbd_led_trigger kbd_led_triggers
[] = {
1037 KBD_LED_TRIGGER(VC_SCROLLOCK
, "kbd-scrolllock"),
1038 KBD_LED_TRIGGER(VC_NUMLOCK
, "kbd-numlock"),
1039 KBD_LED_TRIGGER(VC_CAPSLOCK
, "kbd-capslock"),
1040 KBD_LED_TRIGGER(VC_KANALOCK
, "kbd-kanalock"),
1042 KBD_LOCKSTATE_TRIGGER(VC_SHIFTLOCK
, "kbd-shiftlock"),
1043 KBD_LOCKSTATE_TRIGGER(VC_ALTGRLOCK
, "kbd-altgrlock"),
1044 KBD_LOCKSTATE_TRIGGER(VC_CTRLLOCK
, "kbd-ctrllock"),
1045 KBD_LOCKSTATE_TRIGGER(VC_ALTLOCK
, "kbd-altlock"),
1046 KBD_LOCKSTATE_TRIGGER(VC_SHIFTLLOCK
, "kbd-shiftllock"),
1047 KBD_LOCKSTATE_TRIGGER(VC_SHIFTRLOCK
, "kbd-shiftrlock"),
1048 KBD_LOCKSTATE_TRIGGER(VC_CTRLLLOCK
, "kbd-ctrlllock"),
1049 KBD_LOCKSTATE_TRIGGER(VC_CTRLRLOCK
, "kbd-ctrlrlock"),
1052 static void kbd_propagate_led_state(unsigned int old_state
,
1053 unsigned int new_state
)
1055 struct kbd_led_trigger
*trigger
;
1056 unsigned int changed
= old_state
^ new_state
;
1059 for (i
= 0; i
< ARRAY_SIZE(kbd_led_triggers
); i
++) {
1060 trigger
= &kbd_led_triggers
[i
];
1062 if (changed
& trigger
->mask
)
1063 led_trigger_event(&trigger
->trigger
,
1064 new_state
& trigger
->mask
?
1065 LED_FULL
: LED_OFF
);
1069 static int kbd_update_leds_helper(struct input_handle
*handle
, void *data
)
1071 unsigned int led_state
= *(unsigned int *)data
;
1073 if (test_bit(EV_LED
, handle
->dev
->evbit
))
1074 kbd_propagate_led_state(~led_state
, led_state
);
1079 static void kbd_init_leds(void)
1084 for (i
= 0; i
< ARRAY_SIZE(kbd_led_triggers
); i
++) {
1085 error
= led_trigger_register(&kbd_led_triggers
[i
].trigger
);
1087 pr_err("error %d while registering trigger %s\n",
1088 error
, kbd_led_triggers
[i
].trigger
.name
);
1094 static int kbd_update_leds_helper(struct input_handle
*handle
, void *data
)
1096 unsigned int leds
= *(unsigned int *)data
;
1098 if (test_bit(EV_LED
, handle
->dev
->evbit
)) {
1099 input_inject_event(handle
, EV_LED
, LED_SCROLLL
, !!(leds
& BIT(0)));
1100 input_inject_event(handle
, EV_LED
, LED_NUML
, !!(leds
& BIT(1)));
1101 input_inject_event(handle
, EV_LED
, LED_CAPSL
, !!(leds
& BIT(2)));
1102 input_inject_event(handle
, EV_SYN
, SYN_REPORT
, 0);
1108 static void kbd_propagate_led_state(unsigned int old_state
,
1109 unsigned int new_state
)
1111 input_handler_for_each_handle(&kbd_handler
, &new_state
,
1112 kbd_update_leds_helper
);
1115 static void kbd_init_leds(void)
1122 * The leds display either (i) the status of NumLock, CapsLock, ScrollLock,
1123 * or (ii) whatever pattern of lights people want to show using KDSETLED,
1124 * or (iii) specified bits of specified words in kernel memory.
1126 static unsigned char getledstate(void)
1128 return ledstate
& 0xff;
1131 void setledstate(struct kbd_struct
*kb
, unsigned int led
)
1133 unsigned long flags
;
1134 spin_lock_irqsave(&led_lock
, flags
);
1137 kb
->ledmode
= LED_SHOW_IOCTL
;
1139 kb
->ledmode
= LED_SHOW_FLAGS
;
1142 spin_unlock_irqrestore(&led_lock
, flags
);
1145 static inline unsigned char getleds(void)
1147 struct kbd_struct
*kb
= kbd_table
+ fg_console
;
1149 if (kb
->ledmode
== LED_SHOW_IOCTL
)
1152 return kb
->ledflagstate
;
1156 * vt_get_leds - helper for braille console
1157 * @console: console to read
1158 * @flag: flag we want to check
1160 * Check the status of a keyboard led flag and report it back
1162 int vt_get_leds(int console
, int flag
)
1164 struct kbd_struct
*kb
= kbd_table
+ console
;
1166 unsigned long flags
;
1168 spin_lock_irqsave(&led_lock
, flags
);
1169 ret
= vc_kbd_led(kb
, flag
);
1170 spin_unlock_irqrestore(&led_lock
, flags
);
1174 EXPORT_SYMBOL_GPL(vt_get_leds
);
1177 * vt_set_led_state - set LED state of a console
1178 * @console: console to set
1181 * Set the LEDs on a console. This is a wrapper for the VT layer
1182 * so that we can keep kbd knowledge internal
1184 void vt_set_led_state(int console
, int leds
)
1186 struct kbd_struct
*kb
= kbd_table
+ console
;
1187 setledstate(kb
, leds
);
1191 * vt_kbd_con_start - Keyboard side of console start
1194 * Handle console start. This is a wrapper for the VT layer
1195 * so that we can keep kbd knowledge internal
1197 * FIXME: We eventually need to hold the kbd lock here to protect
1198 * the LED updating. We can't do it yet because fn_hold calls stop_tty
1199 * and start_tty under the kbd_event_lock, while normal tty paths
1200 * don't hold the lock. We probably need to split out an LED lock
1201 * but not during an -rc release!
1203 void vt_kbd_con_start(int console
)
1205 struct kbd_struct
*kb
= kbd_table
+ console
;
1206 unsigned long flags
;
1207 spin_lock_irqsave(&led_lock
, flags
);
1208 clr_vc_kbd_led(kb
, VC_SCROLLOCK
);
1210 spin_unlock_irqrestore(&led_lock
, flags
);
1214 * vt_kbd_con_stop - Keyboard side of console stop
1217 * Handle console stop. This is a wrapper for the VT layer
1218 * so that we can keep kbd knowledge internal
1220 void vt_kbd_con_stop(int console
)
1222 struct kbd_struct
*kb
= kbd_table
+ console
;
1223 unsigned long flags
;
1224 spin_lock_irqsave(&led_lock
, flags
);
1225 set_vc_kbd_led(kb
, VC_SCROLLOCK
);
1227 spin_unlock_irqrestore(&led_lock
, flags
);
1231 * This is the tasklet that updates LED state of LEDs using standard
1232 * keyboard triggers. The reason we use tasklet is that we need to
1233 * handle the scenario when keyboard handler is not registered yet
1234 * but we already getting updates from the VT to update led state.
1236 static void kbd_bh(unsigned long dummy
)
1239 unsigned long flags
;
1241 spin_lock_irqsave(&led_lock
, flags
);
1243 leds
|= (unsigned int)kbd
->lockstate
<< 8;
1244 spin_unlock_irqrestore(&led_lock
, flags
);
1246 if (leds
!= ledstate
) {
1247 kbd_propagate_led_state(ledstate
, leds
);
1252 DECLARE_TASKLET_DISABLED_OLD(keyboard_tasklet
, kbd_bh
);
1254 #if defined(CONFIG_X86) || defined(CONFIG_IA64) || defined(CONFIG_ALPHA) ||\
1255 defined(CONFIG_MIPS) || defined(CONFIG_PPC) || defined(CONFIG_SPARC) ||\
1256 defined(CONFIG_PARISC) || defined(CONFIG_SUPERH) ||\
1257 (defined(CONFIG_ARM) && defined(CONFIG_KEYBOARD_ATKBD) && !defined(CONFIG_ARCH_RPC))
1259 static inline bool kbd_is_hw_raw(const struct input_dev
*dev
)
1261 if (!test_bit(EV_MSC
, dev
->evbit
) || !test_bit(MSC_RAW
, dev
->mscbit
))
1264 return dev
->id
.bustype
== BUS_I8042
&&
1265 dev
->id
.vendor
== 0x0001 && dev
->id
.product
== 0x0001;
1268 static const unsigned short x86_keycodes
[256] =
1269 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
1270 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
1271 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
1272 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
1273 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
1274 80, 81, 82, 83, 84,118, 86, 87, 88,115,120,119,121,112,123, 92,
1275 284,285,309, 0,312, 91,327,328,329,331,333,335,336,337,338,339,
1276 367,288,302,304,350, 89,334,326,267,126,268,269,125,347,348,349,
1277 360,261,262,263,268,376,100,101,321,316,373,286,289,102,351,355,
1278 103,104,105,275,287,279,258,106,274,107,294,364,358,363,362,361,
1279 291,108,381,281,290,272,292,305,280, 99,112,257,306,359,113,114,
1280 264,117,271,374,379,265,266, 93, 94, 95, 85,259,375,260, 90,116,
1281 377,109,111,277,278,282,283,295,296,297,299,300,301,293,303,307,
1282 308,310,313,314,315,317,318,319,320,357,322,323,324,325,276,330,
1283 332,340,365,342,343,344,345,346,356,270,341,368,369,370,371,372 };
1286 static int sparc_l1_a_state
;
1287 extern void sun_do_break(void);
1290 static int emulate_raw(struct vc_data
*vc
, unsigned int keycode
,
1291 unsigned char up_flag
)
1298 put_queue(vc
, 0xe1);
1299 put_queue(vc
, 0x1d | up_flag
);
1300 put_queue(vc
, 0x45 | up_flag
);
1305 put_queue(vc
, 0xf2);
1310 put_queue(vc
, 0xf1);
1315 * Real AT keyboards (that's what we're trying
1316 * to emulate here) emit 0xe0 0x2a 0xe0 0x37 when
1317 * pressing PrtSc/SysRq alone, but simply 0x54
1318 * when pressing Alt+PrtSc/SysRq.
1320 if (test_bit(KEY_LEFTALT
, key_down
) ||
1321 test_bit(KEY_RIGHTALT
, key_down
)) {
1322 put_queue(vc
, 0x54 | up_flag
);
1324 put_queue(vc
, 0xe0);
1325 put_queue(vc
, 0x2a | up_flag
);
1326 put_queue(vc
, 0xe0);
1327 put_queue(vc
, 0x37 | up_flag
);
1335 code
= x86_keycodes
[keycode
];
1340 put_queue(vc
, 0xe0);
1341 put_queue(vc
, (code
& 0x7f) | up_flag
);
1351 static inline bool kbd_is_hw_raw(const struct input_dev
*dev
)
1356 static int emulate_raw(struct vc_data
*vc
, unsigned int keycode
, unsigned char up_flag
)
1361 put_queue(vc
, keycode
| up_flag
);
1366 static void kbd_rawcode(unsigned char data
)
1368 struct vc_data
*vc
= vc_cons
[fg_console
].d
;
1370 kbd
= kbd_table
+ vc
->vc_num
;
1371 if (kbd
->kbdmode
== VC_RAW
)
1372 put_queue(vc
, data
);
1375 static void kbd_keycode(unsigned int keycode
, int down
, bool hw_raw
)
1377 struct vc_data
*vc
= vc_cons
[fg_console
].d
;
1378 unsigned short keysym
, *key_map
;
1381 struct tty_struct
*tty
;
1383 struct keyboard_notifier_param param
= { .vc
= vc
, .value
= keycode
, .down
= down
};
1388 if (tty
&& (!tty
->driver_data
)) {
1389 /* No driver data? Strange. Okay we fix it then. */
1390 tty
->driver_data
= vc
;
1393 kbd
= kbd_table
+ vc
->vc_num
;
1396 if (keycode
== KEY_STOP
)
1397 sparc_l1_a_state
= down
;
1402 raw_mode
= (kbd
->kbdmode
== VC_RAW
);
1403 if (raw_mode
&& !hw_raw
)
1404 if (emulate_raw(vc
, keycode
, !down
<< 7))
1405 if (keycode
< BTN_MISC
&& printk_ratelimit())
1406 pr_warn("can't emulate rawmode for keycode %d\n",
1410 if (keycode
== KEY_A
&& sparc_l1_a_state
) {
1411 sparc_l1_a_state
= false;
1416 if (kbd
->kbdmode
== VC_MEDIUMRAW
) {
1418 * This is extended medium raw mode, with keys above 127
1419 * encoded as 0, high 7 bits, low 7 bits, with the 0 bearing
1420 * the 'up' flag if needed. 0 is reserved, so this shouldn't
1421 * interfere with anything else. The two bytes after 0 will
1422 * always have the up flag set not to interfere with older
1423 * applications. This allows for 16384 different keycodes,
1424 * which should be enough.
1426 if (keycode
< 128) {
1427 put_queue(vc
, keycode
| (!down
<< 7));
1429 put_queue(vc
, !down
<< 7);
1430 put_queue(vc
, (keycode
>> 7) | BIT(7));
1431 put_queue(vc
, keycode
| BIT(7));
1436 assign_bit(keycode
, key_down
, down
);
1439 (!vc_kbd_mode(kbd
, VC_REPEAT
) ||
1440 (tty
&& !L_ECHO(tty
) && tty_chars_in_buffer(tty
)))) {
1442 * Don't repeat a key if the input buffers are not empty and the
1443 * characters get aren't echoed locally. This makes key repeat
1444 * usable with slow applications and under heavy loads.
1449 param
.shift
= shift_final
= (shift_state
| kbd
->slockstate
) ^ kbd
->lockstate
;
1450 param
.ledstate
= kbd
->ledflagstate
;
1451 key_map
= key_maps
[shift_final
];
1453 rc
= atomic_notifier_call_chain(&keyboard_notifier_list
,
1454 KBD_KEYCODE
, ¶m
);
1455 if (rc
== NOTIFY_STOP
|| !key_map
) {
1456 atomic_notifier_call_chain(&keyboard_notifier_list
,
1457 KBD_UNBOUND_KEYCODE
, ¶m
);
1458 do_compute_shiftstate();
1459 kbd
->slockstate
= 0;
1463 if (keycode
< NR_KEYS
)
1464 keysym
= key_map
[keycode
];
1465 else if (keycode
>= KEY_BRL_DOT1
&& keycode
<= KEY_BRL_DOT8
)
1466 keysym
= U(K(KT_BRL
, keycode
- KEY_BRL_DOT1
+ 1));
1470 type
= KTYP(keysym
);
1473 param
.value
= keysym
;
1474 rc
= atomic_notifier_call_chain(&keyboard_notifier_list
,
1475 KBD_UNICODE
, ¶m
);
1476 if (rc
!= NOTIFY_STOP
)
1477 if (down
&& !raw_mode
)
1478 k_unicode(vc
, keysym
, !down
);
1484 if (type
== KT_LETTER
) {
1486 if (vc_kbd_led(kbd
, VC_CAPSLOCK
)) {
1487 key_map
= key_maps
[shift_final
^ BIT(KG_SHIFT
)];
1489 keysym
= key_map
[keycode
];
1493 param
.value
= keysym
;
1494 rc
= atomic_notifier_call_chain(&keyboard_notifier_list
,
1495 KBD_KEYSYM
, ¶m
);
1496 if (rc
== NOTIFY_STOP
)
1499 if ((raw_mode
|| kbd
->kbdmode
== VC_OFF
) && type
!= KT_SPEC
&& type
!= KT_SHIFT
)
1502 (*k_handler
[type
])(vc
, keysym
& 0xff, !down
);
1504 param
.ledstate
= kbd
->ledflagstate
;
1505 atomic_notifier_call_chain(&keyboard_notifier_list
, KBD_POST_KEYSYM
, ¶m
);
1507 if (type
!= KT_SLOCK
)
1508 kbd
->slockstate
= 0;
1511 static void kbd_event(struct input_handle
*handle
, unsigned int event_type
,
1512 unsigned int event_code
, int value
)
1514 /* We are called with interrupts disabled, just take the lock */
1515 spin_lock(&kbd_event_lock
);
1517 if (event_type
== EV_MSC
&& event_code
== MSC_RAW
&&
1518 kbd_is_hw_raw(handle
->dev
))
1520 if (event_type
== EV_KEY
&& event_code
<= KEY_MAX
)
1521 kbd_keycode(event_code
, value
, kbd_is_hw_raw(handle
->dev
));
1523 spin_unlock(&kbd_event_lock
);
1525 tasklet_schedule(&keyboard_tasklet
);
1526 do_poke_blanked_console
= 1;
1527 schedule_console_callback();
1530 static bool kbd_match(struct input_handler
*handler
, struct input_dev
*dev
)
1532 if (test_bit(EV_SND
, dev
->evbit
))
1535 if (test_bit(EV_KEY
, dev
->evbit
)) {
1536 if (find_next_bit(dev
->keybit
, BTN_MISC
, KEY_RESERVED
) <
1539 if (find_next_bit(dev
->keybit
, KEY_BRL_DOT10
+ 1,
1540 KEY_BRL_DOT1
) <= KEY_BRL_DOT10
)
1548 * When a keyboard (or other input device) is found, the kbd_connect
1549 * function is called. The function then looks at the device, and if it
1550 * likes it, it can open it and get events from it. In this (kbd_connect)
1551 * function, we should decide which VT to bind that keyboard to initially.
1553 static int kbd_connect(struct input_handler
*handler
, struct input_dev
*dev
,
1554 const struct input_device_id
*id
)
1556 struct input_handle
*handle
;
1559 handle
= kzalloc(sizeof(struct input_handle
), GFP_KERNEL
);
1564 handle
->handler
= handler
;
1565 handle
->name
= "kbd";
1567 error
= input_register_handle(handle
);
1569 goto err_free_handle
;
1571 error
= input_open_device(handle
);
1573 goto err_unregister_handle
;
1577 err_unregister_handle
:
1578 input_unregister_handle(handle
);
1584 static void kbd_disconnect(struct input_handle
*handle
)
1586 input_close_device(handle
);
1587 input_unregister_handle(handle
);
1592 * Start keyboard handler on the new keyboard by refreshing LED state to
1593 * match the rest of the system.
1595 static void kbd_start(struct input_handle
*handle
)
1597 tasklet_disable(&keyboard_tasklet
);
1599 if (ledstate
!= -1U)
1600 kbd_update_leds_helper(handle
, &ledstate
);
1602 tasklet_enable(&keyboard_tasklet
);
1605 static const struct input_device_id kbd_ids
[] = {
1607 .flags
= INPUT_DEVICE_ID_MATCH_EVBIT
,
1608 .evbit
= { BIT_MASK(EV_KEY
) },
1612 .flags
= INPUT_DEVICE_ID_MATCH_EVBIT
,
1613 .evbit
= { BIT_MASK(EV_SND
) },
1616 { }, /* Terminating entry */
1619 MODULE_DEVICE_TABLE(input
, kbd_ids
);
1621 static struct input_handler kbd_handler
= {
1624 .connect
= kbd_connect
,
1625 .disconnect
= kbd_disconnect
,
1628 .id_table
= kbd_ids
,
1631 int __init
kbd_init(void)
1636 for (i
= 0; i
< MAX_NR_CONSOLES
; i
++) {
1637 kbd_table
[i
].ledflagstate
= kbd_defleds();
1638 kbd_table
[i
].default_ledflagstate
= kbd_defleds();
1639 kbd_table
[i
].ledmode
= LED_SHOW_FLAGS
;
1640 kbd_table
[i
].lockstate
= KBD_DEFLOCK
;
1641 kbd_table
[i
].slockstate
= 0;
1642 kbd_table
[i
].modeflags
= KBD_DEFMODE
;
1643 kbd_table
[i
].kbdmode
= default_utf8
? VC_UNICODE
: VC_XLATE
;
1648 error
= input_register_handler(&kbd_handler
);
1652 tasklet_enable(&keyboard_tasklet
);
1653 tasklet_schedule(&keyboard_tasklet
);
1658 /* Ioctl support code */
1661 * vt_do_diacrit - diacritical table updates
1662 * @cmd: ioctl request
1663 * @udp: pointer to user data for ioctl
1664 * @perm: permissions check computed by caller
1666 * Update the diacritical tables atomically and safely. Lock them
1667 * against simultaneous keypresses
1669 int vt_do_diacrit(unsigned int cmd
, void __user
*udp
, int perm
)
1671 unsigned long flags
;
1678 struct kbdiacrs __user
*a
= udp
;
1679 struct kbdiacr
*dia
;
1682 dia
= kmalloc_array(MAX_DIACR
, sizeof(struct kbdiacr
),
1687 /* Lock the diacriticals table, make a copy and then
1688 copy it after we unlock */
1689 spin_lock_irqsave(&kbd_event_lock
, flags
);
1691 asize
= accent_table_size
;
1692 for (i
= 0; i
< asize
; i
++) {
1693 dia
[i
].diacr
= conv_uni_to_8bit(
1694 accent_table
[i
].diacr
);
1695 dia
[i
].base
= conv_uni_to_8bit(
1696 accent_table
[i
].base
);
1697 dia
[i
].result
= conv_uni_to_8bit(
1698 accent_table
[i
].result
);
1700 spin_unlock_irqrestore(&kbd_event_lock
, flags
);
1702 if (put_user(asize
, &a
->kb_cnt
))
1704 else if (copy_to_user(a
->kbdiacr
, dia
,
1705 asize
* sizeof(struct kbdiacr
)))
1712 struct kbdiacrsuc __user
*a
= udp
;
1715 buf
= kmalloc_array(MAX_DIACR
, sizeof(struct kbdiacruc
),
1720 /* Lock the diacriticals table, make a copy and then
1721 copy it after we unlock */
1722 spin_lock_irqsave(&kbd_event_lock
, flags
);
1724 asize
= accent_table_size
;
1725 memcpy(buf
, accent_table
, asize
* sizeof(struct kbdiacruc
));
1727 spin_unlock_irqrestore(&kbd_event_lock
, flags
);
1729 if (put_user(asize
, &a
->kb_cnt
))
1731 else if (copy_to_user(a
->kbdiacruc
, buf
,
1732 asize
*sizeof(struct kbdiacruc
)))
1740 struct kbdiacrs __user
*a
= udp
;
1741 struct kbdiacr
*dia
= NULL
;
1747 if (get_user(ct
, &a
->kb_cnt
))
1749 if (ct
>= MAX_DIACR
)
1754 dia
= memdup_user(a
->kbdiacr
,
1755 sizeof(struct kbdiacr
) * ct
);
1757 return PTR_ERR(dia
);
1761 spin_lock_irqsave(&kbd_event_lock
, flags
);
1762 accent_table_size
= ct
;
1763 for (i
= 0; i
< ct
; i
++) {
1764 accent_table
[i
].diacr
=
1765 conv_8bit_to_uni(dia
[i
].diacr
);
1766 accent_table
[i
].base
=
1767 conv_8bit_to_uni(dia
[i
].base
);
1768 accent_table
[i
].result
=
1769 conv_8bit_to_uni(dia
[i
].result
);
1771 spin_unlock_irqrestore(&kbd_event_lock
, flags
);
1778 struct kbdiacrsuc __user
*a
= udp
;
1785 if (get_user(ct
, &a
->kb_cnt
))
1788 if (ct
>= MAX_DIACR
)
1792 buf
= memdup_user(a
->kbdiacruc
,
1793 ct
* sizeof(struct kbdiacruc
));
1795 return PTR_ERR(buf
);
1797 spin_lock_irqsave(&kbd_event_lock
, flags
);
1799 memcpy(accent_table
, buf
,
1800 ct
* sizeof(struct kbdiacruc
));
1801 accent_table_size
= ct
;
1802 spin_unlock_irqrestore(&kbd_event_lock
, flags
);
1811 * vt_do_kdskbmode - set keyboard mode ioctl
1812 * @console: the console to use
1813 * @arg: the requested mode
1815 * Update the keyboard mode bits while holding the correct locks.
1816 * Return 0 for success or an error code.
1818 int vt_do_kdskbmode(int console
, unsigned int arg
)
1820 struct kbd_struct
*kb
= kbd_table
+ console
;
1822 unsigned long flags
;
1824 spin_lock_irqsave(&kbd_event_lock
, flags
);
1827 kb
->kbdmode
= VC_RAW
;
1830 kb
->kbdmode
= VC_MEDIUMRAW
;
1833 kb
->kbdmode
= VC_XLATE
;
1834 do_compute_shiftstate();
1837 kb
->kbdmode
= VC_UNICODE
;
1838 do_compute_shiftstate();
1841 kb
->kbdmode
= VC_OFF
;
1846 spin_unlock_irqrestore(&kbd_event_lock
, flags
);
1851 * vt_do_kdskbmeta - set keyboard meta state
1852 * @console: the console to use
1853 * @arg: the requested meta state
1855 * Update the keyboard meta bits while holding the correct locks.
1856 * Return 0 for success or an error code.
1858 int vt_do_kdskbmeta(int console
, unsigned int arg
)
1860 struct kbd_struct
*kb
= kbd_table
+ console
;
1862 unsigned long flags
;
1864 spin_lock_irqsave(&kbd_event_lock
, flags
);
1867 clr_vc_kbd_mode(kb
, VC_META
);
1870 set_vc_kbd_mode(kb
, VC_META
);
1875 spin_unlock_irqrestore(&kbd_event_lock
, flags
);
1879 int vt_do_kbkeycode_ioctl(int cmd
, struct kbkeycode __user
*user_kbkc
,
1882 struct kbkeycode tmp
;
1885 if (copy_from_user(&tmp
, user_kbkc
, sizeof(struct kbkeycode
)))
1889 kc
= getkeycode(tmp
.scancode
);
1891 kc
= put_user(kc
, &user_kbkc
->keycode
);
1896 kc
= setkeycode(tmp
.scancode
, tmp
.keycode
);
1902 static unsigned short vt_kdgkbent(unsigned char kbdmode
, unsigned char idx
,
1905 unsigned short *key_map
, val
;
1906 unsigned long flags
;
1908 /* Ensure another thread doesn't free it under us */
1909 spin_lock_irqsave(&kbd_event_lock
, flags
);
1910 key_map
= key_maps
[map
];
1912 val
= U(key_map
[idx
]);
1913 if (kbdmode
!= VC_UNICODE
&& KTYP(val
) >= NR_TYPES
)
1916 val
= idx
? K_HOLE
: K_NOSUCHMAP
;
1917 spin_unlock_irqrestore(&kbd_event_lock
, flags
);
1922 static int vt_kdskbent(unsigned char kbdmode
, unsigned char idx
,
1923 unsigned char map
, unsigned short val
)
1925 unsigned long flags
;
1926 unsigned short *key_map
, *new_map
, oldval
;
1928 if (!idx
&& val
== K_NOSUCHMAP
) {
1929 spin_lock_irqsave(&kbd_event_lock
, flags
);
1930 /* deallocate map */
1931 key_map
= key_maps
[map
];
1932 if (map
&& key_map
) {
1933 key_maps
[map
] = NULL
;
1934 if (key_map
[0] == U(K_ALLOCATED
)) {
1939 spin_unlock_irqrestore(&kbd_event_lock
, flags
);
1944 if (KTYP(val
) < NR_TYPES
) {
1945 if (KVAL(val
) > max_vals
[KTYP(val
)])
1947 } else if (kbdmode
!= VC_UNICODE
)
1950 /* ++Geert: non-PC keyboards may generate keycode zero */
1951 #if !defined(__mc68000__) && !defined(__powerpc__)
1952 /* assignment to entry 0 only tests validity of args */
1957 new_map
= kmalloc(sizeof(plain_map
), GFP_KERNEL
);
1961 spin_lock_irqsave(&kbd_event_lock
, flags
);
1962 key_map
= key_maps
[map
];
1963 if (key_map
== NULL
) {
1966 if (keymap_count
>= MAX_NR_OF_USER_KEYMAPS
&&
1967 !capable(CAP_SYS_RESOURCE
)) {
1968 spin_unlock_irqrestore(&kbd_event_lock
, flags
);
1972 key_maps
[map
] = new_map
;
1974 key_map
[0] = U(K_ALLOCATED
);
1975 for (j
= 1; j
< NR_KEYS
; j
++)
1976 key_map
[j
] = U(K_HOLE
);
1981 oldval
= U(key_map
[idx
]);
1986 if ((oldval
== K_SAK
|| val
== K_SAK
) && !capable(CAP_SYS_ADMIN
)) {
1987 spin_unlock_irqrestore(&kbd_event_lock
, flags
);
1991 key_map
[idx
] = U(val
);
1992 if (!map
&& (KTYP(oldval
) == KT_SHIFT
|| KTYP(val
) == KT_SHIFT
))
1993 do_compute_shiftstate();
1995 spin_unlock_irqrestore(&kbd_event_lock
, flags
);
2000 int vt_do_kdsk_ioctl(int cmd
, struct kbentry __user
*user_kbe
, int perm
,
2003 struct kbd_struct
*kb
= kbd_table
+ console
;
2006 if (copy_from_user(&kbe
, user_kbe
, sizeof(struct kbentry
)))
2011 return put_user(vt_kdgkbent(kb
->kbdmode
, kbe
.kb_index
,
2013 &user_kbe
->kb_value
);
2015 if (!perm
|| !capable(CAP_SYS_TTY_CONFIG
))
2017 return vt_kdskbent(kb
->kbdmode
, kbe
.kb_index
, kbe
.kb_table
,
2023 static char *vt_kdskbsent(char *kbs
, unsigned char cur
)
2025 static DECLARE_BITMAP(is_kmalloc
, MAX_NR_FUNC
);
2026 char *cur_f
= func_table
[cur
];
2028 if (cur_f
&& strlen(cur_f
) >= strlen(kbs
)) {
2033 func_table
[cur
] = kbs
;
2035 return __test_and_set_bit(cur
, is_kmalloc
) ? cur_f
: NULL
;
2038 int vt_do_kdgkb_ioctl(int cmd
, struct kbsentry __user
*user_kdgkb
, int perm
)
2040 unsigned char kb_func
;
2041 unsigned long flags
;
2045 if (get_user(kb_func
, &user_kdgkb
->kb_func
))
2048 kb_func
= array_index_nospec(kb_func
, MAX_NR_FUNC
);
2052 /* size should have been a struct member */
2053 ssize_t len
= sizeof(user_kdgkb
->kb_string
);
2055 kbs
= kmalloc(len
, GFP_KERNEL
);
2059 spin_lock_irqsave(&func_buf_lock
, flags
);
2060 len
= strlcpy(kbs
, func_table
[kb_func
] ? : "", len
);
2061 spin_unlock_irqrestore(&func_buf_lock
, flags
);
2063 ret
= copy_to_user(user_kdgkb
->kb_string
, kbs
, len
+ 1) ?
2069 if (!perm
|| !capable(CAP_SYS_TTY_CONFIG
))
2072 kbs
= strndup_user(user_kdgkb
->kb_string
,
2073 sizeof(user_kdgkb
->kb_string
));
2075 return PTR_ERR(kbs
);
2077 spin_lock_irqsave(&func_buf_lock
, flags
);
2078 kbs
= vt_kdskbsent(kbs
, kb_func
);
2079 spin_unlock_irqrestore(&func_buf_lock
, flags
);
2090 int vt_do_kdskled(int console
, int cmd
, unsigned long arg
, int perm
)
2092 struct kbd_struct
*kb
= kbd_table
+ console
;
2093 unsigned long flags
;
2094 unsigned char ucval
;
2097 /* the ioctls below read/set the flags usually shown in the leds */
2098 /* don't use them - they will go away without warning */
2100 spin_lock_irqsave(&kbd_event_lock
, flags
);
2101 ucval
= kb
->ledflagstate
| (kb
->default_ledflagstate
<< 4);
2102 spin_unlock_irqrestore(&kbd_event_lock
, flags
);
2103 return put_user(ucval
, (char __user
*)arg
);
2110 spin_lock_irqsave(&led_lock
, flags
);
2111 kb
->ledflagstate
= (arg
& 7);
2112 kb
->default_ledflagstate
= ((arg
>> 4) & 7);
2114 spin_unlock_irqrestore(&led_lock
, flags
);
2117 /* the ioctls below only set the lights, not the functions */
2118 /* for those, see KDGKBLED and KDSKBLED above */
2120 ucval
= getledstate();
2121 return put_user(ucval
, (char __user
*)arg
);
2126 setledstate(kb
, arg
);
2129 return -ENOIOCTLCMD
;
2132 int vt_do_kdgkbmode(int console
)
2134 struct kbd_struct
*kb
= kbd_table
+ console
;
2135 /* This is a spot read so needs no locking */
2136 switch (kb
->kbdmode
) {
2151 * vt_do_kdgkbmeta - report meta status
2152 * @console: console to report
2154 * Report the meta flag status of this console
2156 int vt_do_kdgkbmeta(int console
)
2158 struct kbd_struct
*kb
= kbd_table
+ console
;
2159 /* Again a spot read so no locking */
2160 return vc_kbd_mode(kb
, VC_META
) ? K_ESCPREFIX
: K_METABIT
;
2164 * vt_reset_unicode - reset the unicode status
2165 * @console: console being reset
2167 * Restore the unicode console state to its default
2169 void vt_reset_unicode(int console
)
2171 unsigned long flags
;
2173 spin_lock_irqsave(&kbd_event_lock
, flags
);
2174 kbd_table
[console
].kbdmode
= default_utf8
? VC_UNICODE
: VC_XLATE
;
2175 spin_unlock_irqrestore(&kbd_event_lock
, flags
);
2179 * vt_get_shiftstate - shift bit state
2181 * Report the shift bits from the keyboard state. We have to export
2182 * this to support some oddities in the vt layer.
2184 int vt_get_shift_state(void)
2186 /* Don't lock as this is a transient report */
2191 * vt_reset_keyboard - reset keyboard state
2192 * @console: console to reset
2194 * Reset the keyboard bits for a console as part of a general console
2197 void vt_reset_keyboard(int console
)
2199 struct kbd_struct
*kb
= kbd_table
+ console
;
2200 unsigned long flags
;
2202 spin_lock_irqsave(&kbd_event_lock
, flags
);
2203 set_vc_kbd_mode(kb
, VC_REPEAT
);
2204 clr_vc_kbd_mode(kb
, VC_CKMODE
);
2205 clr_vc_kbd_mode(kb
, VC_APPLIC
);
2206 clr_vc_kbd_mode(kb
, VC_CRLF
);
2209 spin_lock(&led_lock
);
2210 kb
->ledmode
= LED_SHOW_FLAGS
;
2211 kb
->ledflagstate
= kb
->default_ledflagstate
;
2212 spin_unlock(&led_lock
);
2213 /* do not do set_leds here because this causes an endless tasklet loop
2214 when the keyboard hasn't been initialized yet */
2215 spin_unlock_irqrestore(&kbd_event_lock
, flags
);
2219 * vt_get_kbd_mode_bit - read keyboard status bits
2220 * @console: console to read from
2221 * @bit: mode bit to read
2223 * Report back a vt mode bit. We do this without locking so the
2224 * caller must be sure that there are no synchronization needs
2227 int vt_get_kbd_mode_bit(int console
, int bit
)
2229 struct kbd_struct
*kb
= kbd_table
+ console
;
2230 return vc_kbd_mode(kb
, bit
);
2234 * vt_set_kbd_mode_bit - read keyboard status bits
2235 * @console: console to read from
2236 * @bit: mode bit to read
2238 * Set a vt mode bit. We do this without locking so the
2239 * caller must be sure that there are no synchronization needs
2242 void vt_set_kbd_mode_bit(int console
, int bit
)
2244 struct kbd_struct
*kb
= kbd_table
+ console
;
2245 unsigned long flags
;
2247 spin_lock_irqsave(&kbd_event_lock
, flags
);
2248 set_vc_kbd_mode(kb
, bit
);
2249 spin_unlock_irqrestore(&kbd_event_lock
, flags
);
2253 * vt_clr_kbd_mode_bit - read keyboard status bits
2254 * @console: console to read from
2255 * @bit: mode bit to read
2257 * Report back a vt mode bit. We do this without locking so the
2258 * caller must be sure that there are no synchronization needs
2261 void vt_clr_kbd_mode_bit(int console
, int bit
)
2263 struct kbd_struct
*kb
= kbd_table
+ console
;
2264 unsigned long flags
;
2266 spin_lock_irqsave(&kbd_event_lock
, flags
);
2267 clr_vc_kbd_mode(kb
, bit
);
2268 spin_unlock_irqrestore(&kbd_event_lock
, flags
);