25 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
28 #include <linux/module.h>
29 #include <linux/sched.h>
30 #include <linux/tty.h>
33 #include <linux/string.h>
35 #include <linux/slab.h>
40 #include <linux/input.h>
41 #include <linux/reboot.h>
46 #include <asm/irq_regs.h>
54 #define KBD_DEFMODE ((1 << VC_REPEAT) | (1 << VC_META))
56 #if defined(CONFIG_X86) || defined(CONFIG_PARISC)
57 #include <asm/kbdleds.h>
59 static inline int kbd_defleds(
void)
72 k_self, k_fn, k_spec, k_pad,\
73 k_dead, k_cons, k_cur, k_shift,\
74 k_meta, k_ascii, k_lock, k_lowercase,\
75 k_slock, k_dead2, k_brl, k_ignore
83 fn_null, fn_enter, fn_show_ptregs, fn_show_mem,\
84 fn_show_state, fn_send_intr, fn_lastcons, fn_caps_toggle,\
85 fn_num, fn_hold, fn_scroll_forw, fn_scroll_back,\
86 fn_boot_it, fn_caps_on, fn_compose, fn_SAK,\
87 fn_dec_console, fn_inc_console, fn_spawn_con, fn_bare_num
112 static const int max_vals[] = {
115 255, NR_LOCK - 1, 255,
NR_BRL - 1
118 static const int NR_TYPES =
ARRAY_SIZE(max_vals);
120 static struct input_handler kbd_handler;
124 static unsigned char shift_down[
NR_SHIFT];
125 static bool dead_key_next;
126 static int npadch = -1;
127 static unsigned int diacr;
130 static int shift_state = 0;
132 static unsigned char ledstate = 0xff;
133 static unsigned char ledioctl;
135 static struct ledptr {
138 unsigned char valid:1;
173 static int getkeycode_helper(
struct input_handle *
handle,
void *
data)
177 d->
error = input_get_keycode(handle->dev, &d->
ke);
179 return d->
error == 0;
182 static int getkeycode(
unsigned int scancode)
187 .len =
sizeof(scancode),
193 memcpy(d.
ke.scancode, &scancode,
sizeof(scancode));
195 input_handler_for_each_handle(&kbd_handler, &d, getkeycode_helper);
197 return d.
error ?: d.
ke.keycode;
200 static int setkeycode_helper(
struct input_handle *
handle,
void *
data)
204 d->
error = input_set_keycode(handle->dev, &d->
ke);
206 return d->
error == 0;
209 static int setkeycode(
unsigned int scancode,
unsigned int keycode)
214 .len =
sizeof(scancode),
220 memcpy(d.
ke.scancode, &scancode,
sizeof(scancode));
222 input_handler_for_each_handle(&kbd_handler, &d, setkeycode_helper);
232 static int kd_sound_helper(
struct input_handle *
handle,
void *
data)
234 unsigned int *hz =
data;
235 struct input_dev *
dev = handle->dev;
250 static void kd_nosound(
unsigned long ignored)
252 static unsigned int zero;
254 input_handler_for_each_handle(&kbd_handler, &zero, kd_sound_helper);
257 static DEFINE_TIMER(kd_mksound_timer, kd_nosound, 0, 0);
263 input_handler_for_each_handle(&kbd_handler, &hz, kd_sound_helper);
274 static int kbd_rate_helper(
struct input_handle *handle,
void *data)
276 struct input_dev *dev = handle->dev;
281 if (rep[0].
delay > 0)
282 input_inject_event(handle,
285 input_inject_event(handle,
299 input_handler_for_each_handle(&kbd_handler, data, kbd_rate_helper);
308 static void put_queue(
struct vc_data *vc,
int ch)
313 tty_insert_flip_char(tty, ch, 0);
318 static void puts_queue(
struct vc_data *vc,
char *
cp)
326 tty_insert_flip_char(tty, *cp, 0);
334 static char buf[] = { 0x1b,
'O', 0x00, 0x00 };
336 buf[1] = (mode ?
'O' :
'[');
352 else if (c < 0x800) {
354 put_queue(vc, 0xc0 | (c >> 6));
355 put_queue(vc, 0x80 | (c & 0x3f));
356 }
else if (c < 0x10000) {
357 if (c >= 0xD800 && c < 0xE000)
362 put_queue(vc, 0xe0 | (c >> 12));
363 put_queue(vc, 0x80 | ((c >> 6) & 0x3f));
364 put_queue(vc, 0x80 | (c & 0x3f));
365 }
else if (c < 0x110000) {
367 put_queue(vc, 0xf0 | (c >> 18));
368 put_queue(vc, 0x80 | ((c >> 12) & 0x3f));
369 put_queue(vc, 0x80 | ((c >> 6) & 0x3f));
370 put_queue(vc, 0x80 | (c & 0x3f));
381 static void do_compute_shiftstate(
void)
383 unsigned int i,
j,
k, sym,
val;
386 memset(shift_down, 0,
sizeof(shift_down));
409 shift_state |= (1 <<
val);
419 do_compute_shiftstate();
420 spin_unlock_irqrestore(&kbd_event_lock, flags);
430 static unsigned int handle_diacr(
struct vc_data *vc,
unsigned int ch)
432 unsigned int d = diacr;
446 if (ch ==
' ' || ch == (
BRL_UC_ROW|0) || ch == d)
463 static void fn_enter(
struct vc_data *vc)
481 static void fn_caps_toggle(
struct vc_data *vc)
489 static void fn_caps_on(
struct vc_data *vc)
497 static void fn_show_ptregs(
struct vc_data *vc)
505 static void fn_hold(
struct vc_data *vc)
523 static void fn_num(
struct vc_data *vc)
537 static void fn_bare_num(
struct vc_data *vc)
543 static void fn_lastcons(
struct vc_data *vc)
549 static void fn_dec_console(
struct vc_data *vc)
557 for (i = cur - 1; i !=
cur; i--) {
566 static void fn_inc_console(
struct vc_data *vc)
574 for (i = cur+1; i !=
cur; i++) {
583 static void fn_send_intr(
struct vc_data *vc)
593 static void fn_scroll_forw(
struct vc_data *vc)
598 static void fn_scroll_back(
struct vc_data *vc)
603 static void fn_show_mem(
struct vc_data *vc)
608 static void fn_show_state(
struct vc_data *vc)
613 static void fn_boot_it(
struct vc_data *vc)
618 static void fn_compose(
struct vc_data *vc)
620 dead_key_next =
true;
623 static void fn_spawn_con(
struct vc_data *vc)
625 spin_lock(&vt_spawn_con.
lock);
626 if (vt_spawn_con.
pid)
631 spin_unlock(&vt_spawn_con.
lock);
634 static void fn_SAK(
struct vc_data *vc)
640 static void fn_null(
struct vc_data *vc)
642 do_compute_shiftstate();
648 static void k_ignore(
struct vc_data *vc,
unsigned char value,
char up_flag)
652 static void k_spec(
struct vc_data *vc,
unsigned char value,
char up_flag)
663 fn_handler[
value](vc);
666 static void k_lowercase(
struct vc_data *vc,
unsigned char value,
char up_flag)
668 pr_err(
"k_lowercase was called - impossible\n");
671 static void k_unicode(
struct vc_data *vc,
unsigned int value,
char up_flag)
677 value = handle_diacr(vc, value);
680 dead_key_next =
false;
698 static void k_deadunicode(
struct vc_data *vc,
unsigned int value,
char up_flag)
703 diacr = (diacr ? handle_diacr(vc, value) : value);
706 static void k_self(
struct vc_data *vc,
unsigned char value,
char up_flag)
711 static void k_dead2(
struct vc_data *vc,
unsigned char value,
char up_flag)
713 k_deadunicode(vc, value, up_flag);
719 static void k_dead(
struct vc_data *vc,
unsigned char value,
char up_flag)
721 static const unsigned char ret_diacr[
NR_DEAD] = {
'`',
'\'',
'^',
'~',
'"',
',' };
723 k_deadunicode(vc, ret_diacr[value], up_flag);
726 static void k_cons(
struct vc_data *vc,
unsigned char value,
char up_flag)
734 static void k_fn(
struct vc_data *vc,
unsigned char value,
char up_flag)
743 pr_err(
"k_fn called with value=%d\n", value);
746 static void k_cur(
struct vc_data *vc,
unsigned char value,
char up_flag)
748 static const char cur_chars[] =
"BDCA";
753 applkey(vc, cur_chars[value], vc_kbd_mode(kbd,
VC_CKMODE));
756 static void k_pad(
struct vc_data *vc,
unsigned char value,
char up_flag)
758 static const char pad_chars[] =
"0123456789+-*/\015,.?()#";
759 static const char app_map[] =
"pqrstuvwxylSRQMnnmPQS";
766 applkey(vc, app_map[value], 1);
805 applkey(vc,
'G', vc_kbd_mode(kbd,
VC_APPLIC));
810 put_queue(vc, pad_chars[value]);
815 static void k_shift(
struct vc_data *vc,
unsigned char value,
char up_flag)
817 int old_state = shift_state;
836 if (shift_down[value])
841 if (shift_down[value])
842 shift_state |= (1 <<
value);
844 shift_state &= ~(1 <<
value);
847 if (up_flag && shift_state != old_state && npadch != -1) {
851 put_queue(vc, npadch & 0xff);
856 static void k_meta(
struct vc_data *vc,
unsigned char value,
char up_flag)
861 if (vc_kbd_mode(kbd,
VC_META)) {
862 put_queue(vc,
'\033');
863 put_queue(vc, value);
865 put_queue(vc, value | 0x80);
868 static void k_ascii(
struct vc_data *vc,
unsigned char value,
char up_flag)
887 npadch = npadch * base +
value;
890 static void k_lock(
struct vc_data *vc,
unsigned char value,
char up_flag)
895 chg_vc_kbd_lock(kbd, value);
898 static void k_slock(
struct vc_data *vc,
unsigned char value,
char up_flag)
900 k_shift(vc, value, up_flag);
904 chg_vc_kbd_slock(kbd, value);
908 chg_vc_kbd_slock(kbd, value);
913 static unsigned brl_timeout = 300;
914 MODULE_PARM_DESC(brl_timeout,
"Braille keys release delay in ms (0 for commit on first key release)");
917 static unsigned brl_nbchords = 1;
918 MODULE_PARM_DESC(brl_nbchords,
"Number of chords that produce a braille pattern (0 for dead chords)");
921 static void k_brlcommit(
struct vc_data *vc,
unsigned int pattern,
char up_flag)
923 static unsigned long chords;
924 static unsigned committed;
927 k_deadunicode(vc,
BRL_UC_ROW | pattern, up_flag);
931 if (chords == brl_nbchords) {
932 k_unicode(vc,
BRL_UC_ROW | committed, up_flag);
939 static void k_brl(
struct vc_data *vc,
unsigned char value,
char up_flag)
941 static unsigned pressed, committing;
942 static unsigned long releasestart;
946 pr_warning(
"keyboard mode must be unicode for braille patterns\n");
959 pressed |= 1 << (value - 1);
961 committing = pressed;
962 }
else if (brl_timeout) {
966 committing = pressed;
969 pressed &= ~(1 << (value - 1));
970 if (!pressed && committing) {
971 k_brlcommit(vc, committing, 0);
976 k_brlcommit(vc, committing, 0);
979 pressed &= ~(1 << (value - 1));
988 static unsigned char getledstate(
void)
1004 spin_unlock_irqrestore(&led_lock, flags);
1007 static inline unsigned char getleds(
void)
1019 for (i = 0; i < 3; i++)
1020 if (ledptrs[i].
valid) {
1021 if (*ledptrs[i].
addr & ledptrs[i].
mask)
1030 static int kbd_update_leds_helper(
struct input_handle *handle,
void *data)
1032 unsigned char leds = *(
unsigned char *)data;
1055 unsigned long flags;
1058 ret = vc_kbd_led(kbd, flag);
1059 spin_unlock_irqrestore(&led_lock, flags);
1095 unsigned long flags;
1099 spin_unlock_irqrestore(&led_lock, flags);
1112 unsigned long flags;
1116 spin_unlock_irqrestore(&led_lock, flags);
1126 static void kbd_bh(
unsigned long dummy)
1129 unsigned long flags;
1133 spin_unlock_irqrestore(&led_lock, flags);
1135 if (leds != ledstate) {
1136 input_handler_for_each_handle(&kbd_handler, &leds,
1137 kbd_update_leds_helper);
1144 #if defined(CONFIG_X86) || defined(CONFIG_IA64) || defined(CONFIG_ALPHA) ||\
1145 defined(CONFIG_MIPS) || defined(CONFIG_PPC) || defined(CONFIG_SPARC) ||\
1146 defined(CONFIG_PARISC) || defined(CONFIG_SUPERH) ||\
1147 (defined(CONFIG_ARM) && defined(CONFIG_KEYBOARD_ATKBD) && !defined(CONFIG_ARCH_RPC)) ||\
1148 defined(CONFIG_AVR32)
1150 #define HW_RAW(dev) (test_bit(EV_MSC, dev->evbit) && test_bit(MSC_RAW, dev->mscbit) &&\
1151 ((dev)->id.bustype == BUS_I8042) && ((dev)->id.vendor == 0x0001) && ((dev)->id.product == 0x0001))
1153 static const unsigned short x86_keycodes[256] =
1154 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
1155 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
1156 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
1157 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
1158 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
1159 80, 81, 82, 83, 84,118, 86, 87, 88,115,120,119,121,112,123, 92,
1160 284,285,309, 0,312, 91,327,328,329,331,333,335,336,337,338,339,
1161 367,288,302,304,350, 89,334,326,267,126,268,269,125,347,348,349,
1162 360,261,262,263,268,376,100,101,321,316,373,286,289,102,351,355,
1163 103,104,105,275,287,279,258,106,274,107,294,364,358,363,362,361,
1164 291,108,381,281,290,272,292,305,280, 99,112,257,306,359,113,114,
1165 264,117,271,374,379,265,266, 93, 94, 95, 85,259,375,260, 90,116,
1166 377,109,111,277,278,282,283,295,296,297,299,300,301,293,303,307,
1167 308,310,313,314,315,317,318,319,320,357,322,323,324,325,276,330,
1168 332,340,365,342,343,344,345,346,356,270,341,368,369,370,371,372 };
1171 static int sparc_l1_a_state;
1175 static int emulate_raw(
struct vc_data *vc,
unsigned int keycode,
1176 unsigned char up_flag)
1183 put_queue(vc, 0xe1);
1184 put_queue(vc, 0x1d | up_flag);
1185 put_queue(vc, 0x45 | up_flag);
1190 put_queue(vc, 0xf2);
1195 put_queue(vc, 0xf1);
1207 put_queue(vc, 0x54 | up_flag);
1209 put_queue(vc, 0xe0);
1210 put_queue(vc, 0x2a | up_flag);
1211 put_queue(vc, 0xe0);
1212 put_queue(vc, 0x37 | up_flag);
1225 put_queue(vc, 0xe0);
1226 put_queue(vc, (code & 0x7f) | up_flag);
1236 #define HW_RAW(dev) 0
1238 static int emulate_raw(
struct vc_data *vc,
unsigned int keycode,
unsigned char up_flag)
1243 put_queue(vc, keycode | up_flag);
1248 static void kbd_rawcode(
unsigned char data)
1252 kbd = kbd_table + vc->
vc_num;
1254 put_queue(vc, data);
1260 unsigned short keysym, *key_map;
1275 kbd = kbd_table + vc->
vc_num;
1279 sparc_l1_a_state =
down;
1285 if (raw_mode && !hw_raw)
1286 if (emulate_raw(vc, keycode, !down << 7))
1287 if (keycode <
BTN_MISC && printk_ratelimit())
1288 pr_warning(
"can't emulate rawmode for keycode %d\n",
1292 if (keycode ==
KEY_A && sparc_l1_a_state) {
1293 sparc_l1_a_state =
false;
1308 if (keycode < 128) {
1309 put_queue(vc, keycode | (!down << 7));
1311 put_queue(vc, !down << 7);
1312 put_queue(vc, (keycode >> 7) | 0x80);
1313 put_queue(vc, keycode | 0x80);
1339 KBD_KEYCODE, ¶m);
1340 if (rc == NOTIFY_STOP || !key_map) {
1342 KBD_UNBOUND_KEYCODE, ¶m);
1343 do_compute_shiftstate();
1355 type =
KTYP(keysym);
1358 param.
value = keysym;
1360 KBD_UNICODE, ¶m);
1361 if (rc != NOTIFY_STOP)
1362 if (down && !raw_mode)
1363 to_utf8(vc, keysym);
1378 param.
value = keysym;
1380 KBD_KEYSYM, ¶m);
1381 if (rc == NOTIFY_STOP)
1387 (*k_handler[
type])(vc, keysym & 0xff, !down);
1396 static void kbd_event(
struct input_handle *handle,
unsigned int event_type,
1400 spin_lock(&kbd_event_lock);
1404 if (event_type ==
EV_KEY)
1407 spin_unlock(&kbd_event_lock);
1409 tasklet_schedule(&keyboard_tasklet);
1414 static bool kbd_match(
struct input_handler *handler,
struct input_dev *dev)
1439 static int kbd_connect(
struct input_handler *handler,
struct input_dev *dev,
1442 struct input_handle *
handle;
1445 handle = kzalloc(
sizeof(
struct input_handle),
GFP_KERNEL);
1450 handle->handler = handler;
1451 handle->name =
"kbd";
1453 error = input_register_handle(handle);
1455 goto err_free_handle;
1457 error = input_open_device(handle);
1459 goto err_unregister_handle;
1463 err_unregister_handle:
1464 input_unregister_handle(handle);
1470 static void kbd_disconnect(
struct input_handle *handle)
1472 input_close_device(handle);
1473 input_unregister_handle(handle);
1481 static void kbd_start(
struct input_handle *handle)
1483 tasklet_disable(&keyboard_tasklet);
1485 if (ledstate != 0xff)
1486 kbd_update_leds_helper(handle, &ledstate);
1488 tasklet_enable(&keyboard_tasklet);
1507 static struct input_handler kbd_handler = {
1510 .connect = kbd_connect,
1511 .disconnect = kbd_disconnect,
1514 .id_table = kbd_ids,
1523 kbd_table[
i].ledflagstate = kbd_defleds();
1524 kbd_table[
i].default_ledflagstate = kbd_defleds();
1527 kbd_table[
i].slockstate = 0;
1532 error = input_register_handler(&kbd_handler);
1556 unsigned long flags;
1576 for (i = 0; i < asize; i++) {
1584 spin_unlock_irqrestore(&kbd_event_lock, flags);
1589 asize *
sizeof(
struct kbdiacr)))
1611 spin_unlock_irqrestore(&kbd_event_lock, flags);
1643 sizeof(
struct kbdiacr) * ct)) {
1650 accent_table_size =
ct;
1651 for (i = 0; i <
ct; i++) {
1659 spin_unlock_irqrestore(&kbd_event_lock, flags);
1695 accent_table_size =
ct;
1696 spin_unlock_irqrestore(&kbd_event_lock, flags);
1716 unsigned long flags;
1728 do_compute_shiftstate();
1732 do_compute_shiftstate();
1740 spin_unlock_irqrestore(&kbd_event_lock, flags);
1756 unsigned long flags;
1761 clr_vc_kbd_mode(kbd,
VC_META);
1764 set_vc_kbd_mode(kbd,
VC_META);
1769 spin_unlock_irqrestore(&kbd_event_lock, flags);
1785 kc =
put_user(kc, &user_kbkc->keycode);
1796 #define i (tmp.kb_index)
1797 #define s (tmp.kb_table)
1798 #define v (tmp.kb_value)
1806 unsigned long flags;
1820 val =
U(key_map[i]);
1825 spin_unlock_irqrestore(&kbd_event_lock, flags);
1826 return put_user(val, &user_kbe->kb_value);
1841 spin_unlock_irqrestore(&kbd_event_lock, flags);
1845 if (
KTYP(
v) < NR_TYPES) {
1853 #if !defined(__mc68000__) && !defined(__powerpc__)
1864 if (key_map ==
NULL) {
1869 spin_unlock_irqrestore(&kbd_event_lock, flags);
1889 spin_unlock_irqrestore(&kbd_event_lock, flags);
1894 do_compute_shiftstate();
1896 spin_unlock_irqrestore(&kbd_event_lock, flags);
1914 char *first_free, *fj, *fnw;
1939 up = user_kdgkb->kb_string;
1942 for ( ; *p && sz; p++, sz--)
1971 memmove(fj + delta, fj, first_free - fj);
1993 for (k = 0; k <
j; k++)
1997 if (first_free > fj) {
2021 unsigned long flags;
2022 unsigned char ucval;
2030 spin_unlock_irqrestore(&kbd_event_lock, flags);
2042 spin_unlock_irqrestore(&led_lock, flags);
2048 ucval = getledstate();
2099 unsigned long flags;
2103 spin_unlock_irqrestore(&kbd_event_lock, flags);
2128 unsigned long flags;
2134 clr_vc_kbd_mode(kbd,
VC_CRLF);
2137 spin_lock(&led_lock);
2140 spin_unlock(&led_lock);
2143 spin_unlock_irqrestore(&kbd_event_lock, flags);
2158 return vc_kbd_mode(kbd, bit);
2173 unsigned long flags;
2176 set_vc_kbd_mode(kbd, bit);
2177 spin_unlock_irqrestore(&kbd_event_lock, flags);
2192 unsigned long flags;
2195 clr_vc_kbd_mode(kbd, bit);
2196 spin_unlock_irqrestore(&kbd_event_lock, flags);