1 // SPDX-License-Identifier: GPL-2.0+
3 * Translate key codes into ASCII
5 * Copyright (c) 2011 The Chromium OS Authors.
6 * (C) Copyright 2004 DENX Software Engineering, Wolfgang Denk, wd@denx.de
13 #include <stdio_dev.h>
15 #ifdef CONFIG_DM_KEYBOARD
18 #include <linux/input.h>
21 /* These correspond to the lights on the keyboard */
22 FLAG_SCROLL_LOCK = 1 << 0,
23 FLAG_NUM_LOCK = 1 << 1,
24 FLAG_CAPS_LOCK = 1 << 2,
26 /* Special flag ORed with key code to indicate release */
27 KEY_RELEASE = 1 << 15,
32 * These takes map key codes to ASCII. 0xff means no key, or special key.
33 * Three tables are provided - one for plain keys, one for when the shift
34 * 'modifier' key is pressed and one for when the ctrl modifier key is
37 static const uchar kbd_plain_xlate[] = {
38 0xff, 0x1b, '1', '2', '3', '4', '5', '6',
39 '7', '8', '9', '0', '-', '=', '\b', '\t', /* 0x00 - 0x0f */
40 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i',
41 'o', 'p', '[', ']', '\r', 0xff, 'a', 's', /* 0x10 - 0x1f */
42 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';',
43 '\'', '`', 0xff, '\\', 'z', 'x', 'c', 'v', /* 0x20 - 0x2f */
44 'b', 'n', 'm', ',' , '.', '/', 0xff, 0xff, 0xff,
45 ' ', 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 0x30 - 0x3f */
46 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, '7',
47 '8', '9', '-', '4', '5', '6', '+', '1', /* 0x40 - 0x4f */
48 '2', '3', '0', '.', 0xff, 0xff, 0xff, 0xff,
49 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 0x50 - 0x5F */
53 static unsigned char kbd_shift_xlate[] = {
54 0xff, 0x1b, '!', '@', '#', '$', '%', '^',
55 '&', '*', '(', ')', '_', '+', '\b', '\t', /* 0x00 - 0x0f */
56 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I',
57 'O', 'P', '{', '}', '\r', 0xff, 'A', 'S', /* 0x10 - 0x1f */
58 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':',
59 '"', '~', 0xff, '|', 'Z', 'X', 'C', 'V', /* 0x20 - 0x2f */
60 'B', 'N', 'M', '<', '>', '?', 0xff, 0xff, 0xff,
61 ' ', 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 0x30 - 0x3f */
62 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, '7',
63 '8', '9', '-', '4', '5', '6', '+', '1', /* 0x40 - 0x4f */
64 '2', '3', '0', '.', 0xff, 0xff, 0xff, 0xff, 0xff,
65 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 0x50 - 0x5F */
69 static unsigned char kbd_ctrl_xlate[] = {
70 0xff, 0x1b, '1', 0x00, '3', '4', '5', 0x1E,
71 '7', '8', '9', '0', 0x1F, '=', '\b', '\t', /* 0x00 - 0x0f */
72 0x11, 0x17, 0x05, 0x12, 0x14, 0x19, 0x15, 0x09,
73 0x0f, 0x10, 0x1b, 0x1d, '\n', 0xff, 0x01, 0x13, /* 0x10 - 0x1f */
74 0x04, 0x06, 0x08, 0x09, 0x0a, 0x0b, 0x0c, ';',
75 '\'', '~', 0x00, 0x1c, 0x1a, 0x18, 0x03, 0x16, /* 0x20 - 0x2f */
76 0x02, 0x0e, 0x0d, '<', '>', '?', 0xff, 0xff,
77 0xff, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 0x30 - 0x3f */
78 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, '7',
79 '8', '9', '-', '4', '5', '6', '+', '1', /* 0x40 - 0x4f */
80 '2', '3', '0', '.', 0xff, 0xff, 0xff, 0xff,
81 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 0x50 - 0x5F */
86 * German keymap. Special letters are mapped according to code page 437.
88 static const uchar kbd_plain_xlate_german[] = {
89 0xff, 0x1b, '1', '2', '3', '4', '5', '6', /* scan 00-07 */
90 '7', '8', '9', '0', 0xe1, '\'', 0x08, '\t', /* scan 08-0F */
91 'q', 'w', 'e', 'r', 't', 'z', 'u', 'i', /* scan 10-17 */
92 'o', 'p', 0x81, '+', '\r', 0xff, 'a', 's', /* scan 18-1F */
93 'd', 'f', 'g', 'h', 'j', 'k', 'l', 0x94, /* scan 20-27 */
94 0x84, '^', 0xff, '#', 'y', 'x', 'c', 'v', /* scan 28-2F */
95 'b', 'n', 'm', ',', '.', '-', 0xff, '*', /* scan 30-37 */
96 ' ', ' ', 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 38-3F */
97 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, '7', /* scan 40-47 */
98 '8', '9', '-', '4', '5', '6', '+', '1', /* scan 48-4F */
99 '2', '3', '0', ',', 0xff, 0xff, '<', 0xff, /* scan 50-57 */
100 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 58-5F */
101 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 60-67 */
102 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 68-6F */
103 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 70-77 */
104 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 78-7F */
105 '\r', 0xff, '/', '*',
108 static unsigned char kbd_shift_xlate_german[] = {
109 0xff, 0x1b, '!', '"', 0x15, '$', '%', '&', /* scan 00-07 */
110 '/', '(', ')', '=', '?', '`', 0x08, '\t', /* scan 08-0F */
111 'Q', 'W', 'E', 'R', 'T', 'Z', 'U', 'I', /* scan 10-17 */
112 'O', 'P', 0x9a, '*', '\r', 0xff, 'A', 'S', /* scan 18-1F */
113 'D', 'F', 'G', 'H', 'J', 'K', 'L', 0x99, /* scan 20-27 */
114 0x8e, 0xf8, 0xff, '\'', 'Y', 'X', 'C', 'V', /* scan 28-2F */
115 'B', 'N', 'M', ';', ':', '_', 0xff, '*', /* scan 30-37 */
116 ' ', ' ', 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 38-3F */
117 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, '7', /* scan 40-47 */
118 '8', '9', '-', '4', '5', '6', '+', '1', /* scan 48-4F */
119 '2', '3', '0', ',', 0xff, 0xff, '>', 0xff, /* scan 50-57 */
120 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 58-5F */
121 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 60-67 */
122 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 68-6F */
123 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 70-77 */
124 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 78-7F */
125 '\r', 0xff, '/', '*',
128 static unsigned char kbd_right_alt_xlate_german[] = {
129 0xff, 0xff, 0xff, 0xfd, 0xff, 0xff, 0xff, 0xff, /* scan 00-07 */
130 '{', '[', ']', '}', '\\', 0xff, 0xff, 0xff, /* scan 08-0F */
131 '@', 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 10-17 */
132 0xff, 0xff, 0xff, '~', 0xff, 0xff, 0xff, 0xff, /* scan 18-1F */
133 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 20-27 */
134 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 28-2F */
135 0xff, 0xff, 0xe6, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 30-37 */
136 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 38-3F */
137 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 40-47 */
138 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 48-4F */
139 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, '|', 0xff, /* scan 50-57 */
143 KBD_ENGLISH = 1 << 0,
147 static struct kbd_entry {
148 int kbd_mask; /* Which languages this is for */
149 int left_keycode; /* Left keycode to select this map */
150 int right_keycode; /* Right keycode to select this map */
151 const uchar *xlate; /* Ascii code for each keycode */
152 int num_entries; /* Number of entries in xlate */
154 { KBD_ENGLISH, -1, -1,
155 kbd_plain_xlate, ARRAY_SIZE(kbd_plain_xlate) },
156 { KBD_GERMAN, -1, -1,
157 kbd_plain_xlate_german, ARRAY_SIZE(kbd_plain_xlate_german) },
158 { KBD_ENGLISH, KEY_LEFTSHIFT, KEY_RIGHTSHIFT,
159 kbd_shift_xlate, ARRAY_SIZE(kbd_shift_xlate) },
160 { KBD_GERMAN, KEY_LEFTSHIFT, KEY_RIGHTSHIFT,
161 kbd_shift_xlate_german, ARRAY_SIZE(kbd_shift_xlate_german) },
162 { KBD_ENGLISH | KBD_GERMAN, KEY_LEFTCTRL, KEY_RIGHTCTRL,
163 kbd_ctrl_xlate, ARRAY_SIZE(kbd_ctrl_xlate) },
164 { KBD_GERMAN, -1, KEY_RIGHTALT,
165 kbd_right_alt_xlate_german,
166 ARRAY_SIZE(kbd_right_alt_xlate_german) },
171 * Scan key code to ANSI 3.64 escape sequence table. This table is
172 * incomplete in that it does not include all possible extra keys.
177 } kbd_to_ansi364[] = {
179 { KEY_DOWN, "\033[B"},
180 { KEY_RIGHT, "\033[C"},
181 { KEY_LEFT, "\033[D"},
184 /* Maximum number of output characters that an ANSI sequence expands to */
185 #define ANSI_CHAR_MAX 3
187 static int input_queue_ascii(struct input_config *config, int ch)
189 if (config->fifo_in + 1 == INPUT_BUFFER_LEN) {
190 if (!config->fifo_out)
191 return -1; /* buffer full */
195 if (config->fifo_in + 1 == config->fifo_out)
196 return -1; /* buffer full */
199 debug(" {%02x} ", ch);
200 config->fifo[config->fifo_in] = (uchar)ch;
205 int input_tstc(struct input_config *config)
207 if (config->fifo_in == config->fifo_out && config->read_keys) {
208 if (!(*config->read_keys)(config))
211 return config->fifo_in != config->fifo_out;
214 int input_getc(struct input_config *config)
218 while (config->fifo_in == config->fifo_out) {
219 if (config->read_keys)
220 err = (*config->read_keys)(config);
225 if (++config->fifo_out == INPUT_BUFFER_LEN)
226 config->fifo_out = 0;
228 return config->fifo[config->fifo_out];
232 * Process a modifier/special key press or release and decide which key
233 * translation array should be used as a result.
235 * TODO: Should keep track of modifier press/release
237 * @param config Input state
238 * @param key Key code to process
239 * @param release 0 if a press, 1 if a release
240 * @return pointer to keycode->ascii translation table that should be used
242 static struct input_key_xlate *process_modifier(struct input_config *config,
243 int key, int release)
245 #ifdef CONFIG_DM_KEYBOARD
246 struct udevice *dev = config->dev;
247 struct keyboard_ops *ops = keyboard_get_ops(dev);
249 struct input_key_xlate *table;
252 /* Start with the main table, and see what modifiers change it */
253 assert(config->num_tables > 0);
254 table = &config->table[0];
255 for (i = 1; i < config->num_tables; i++) {
256 struct input_key_xlate *tab = &config->table[i];
258 if (key == tab->left_keycode || key == tab->right_keycode)
262 /* Handle the lighted keys */
268 flip = FLAG_SCROLL_LOCK;
271 flip = FLAG_NUM_LOCK;
274 flip = FLAG_CAPS_LOCK;
281 config->flags ^= flip;
282 if (config->flags & FLAG_NUM_LOCK)
283 leds |= INPUT_LED_NUM;
284 if (config->flags & FLAG_CAPS_LOCK)
285 leds |= INPUT_LED_CAPS;
286 if (config->flags & FLAG_SCROLL_LOCK)
287 leds |= INPUT_LED_SCROLL;
289 config->leds_changed = flip;
291 #ifdef CONFIG_DM_KEYBOARD
292 if (ops->update_leds) {
293 if (ops->update_leds(dev, config->leds))
294 debug("Update keyboard's LED failed\n");
304 * Search an int array for a key value
306 * @param array Array to search
307 * @param count Number of elements in array
308 * @param key Key value to find
309 * @return element where value was first found, -1 if none
311 static int array_search(int *array, int count, int key)
315 for (i = 0; i < count; i++) {
324 * Sort an array so that those elements that exist in the ordering are
325 * first in the array, and in the same order as the ordering. The algorithm
326 * is O(count * ocount) and designed for small arrays.
328 * TODO: Move this to common / lib?
330 * @param dest Array with elements to sort, also destination array
331 * @param count Number of elements to sort
332 * @param order Array containing ordering elements
333 * @param ocount Number of ordering elements
334 * @return number of elements in dest that are in order (these will be at the
337 static int sort_array_by_ordering(int *dest, int count, int *order,
342 int same; /* number of elements which are the same */
345 /* setup output items, copy items to be sorted into our temp area */
346 memcpy(temp, dest, count * sizeof(*dest));
349 /* work through the ordering, move over the elements we agree on */
350 for (i = 0; i < ocount; i++) {
351 if (array_search(temp, count, order[i]) != -1)
352 dest[dest_count++] = order[i];
356 /* now move over the elements that are not in the ordering */
357 for (i = 0; i < count; i++) {
358 if (array_search(order, ocount, temp[i]) == -1)
359 dest[dest_count++] = temp[i];
361 assert(dest_count == count);
366 * Check a list of key codes against the previous key scan
368 * Given a list of new key codes, we check how many of these are the same
371 * @param config Input state
372 * @param keycode List of key codes to examine
373 * @param num_keycodes Number of key codes
374 * @param same Returns number of key codes which are the same
376 static int input_check_keycodes(struct input_config *config,
377 int keycode[], int num_keycodes, int *same)
379 /* Select the 'plain' xlate table to start with */
380 if (!config->num_tables) {
381 debug("%s: No xlate tables: cannot decode keys\n", __func__);
385 /* sort the keycodes into the same order as the previous ones */
386 *same = sort_array_by_ordering(keycode, num_keycodes,
387 config->prev_keycodes, config->num_prev_keycodes);
389 memcpy(config->prev_keycodes, keycode, num_keycodes * sizeof(int));
390 config->num_prev_keycodes = num_keycodes;
392 return *same != num_keycodes;
396 * Checks and converts a special key code into ANSI 3.64 escape sequence.
398 * @param config Input state
399 * @param keycode Key code to examine
400 * @param output_ch Buffer to place output characters into. It should
401 * be at least ANSI_CHAR_MAX bytes long, to allow for
403 * @param max_chars Maximum number of characters to add to output_ch
404 * @return number of characters output, if the key was converted, otherwise 0.
405 * This may be larger than max_chars, in which case the overflow
406 * characters are not output.
408 static int input_keycode_to_ansi364(struct input_config *config,
409 int keycode, char output_ch[], int max_chars)
415 for (i = ch_count = 0; i < ARRAY_SIZE(kbd_to_ansi364); i++) {
416 if (keycode != kbd_to_ansi364[i].kbd_scan_code)
418 for (escape = kbd_to_ansi364[i].escape; *escape; escape++) {
419 if (ch_count < max_chars)
420 output_ch[ch_count] = *escape;
430 * Converts and queues a list of key codes in escaped ASCII string form
431 * Convert a list of key codes into ASCII
433 * You must call input_check_keycodes() before this. It turns the keycode
434 * list into a list of ASCII characters and sends them to the input layer.
436 * Characters which were seen last time do not generate fresh ASCII output.
437 * The output (calls to queue_ascii) may be longer than num_keycodes, if the
438 * keycode contains special keys that was encoded to longer escaped sequence.
440 * @param config Input state
441 * @param keycode List of key codes to examine
442 * @param num_keycodes Number of key codes
443 * @param output_ch Buffer to place output characters into. It should
444 * be at last ANSI_CHAR_MAX * num_keycodes, to allow for
446 * @param max_chars Maximum number of characters to add to output_ch
447 * @param same Number of key codes which are the same
448 * @return number of characters written into output_ch, or -1 if we would
449 * exceed max_chars chars.
451 static int input_keycodes_to_ascii(struct input_config *config,
452 int keycode[], int num_keycodes, char output_ch[],
453 int max_chars, int same)
455 struct input_key_xlate *table;
459 table = &config->table[0];
461 /* deal with modifiers first */
462 for (i = 0; i < num_keycodes; i++) {
463 int key = keycode[i] & KEY_MASK;
465 if (key >= table->num_entries || table->xlate[key] == 0xff) {
466 table = process_modifier(config, key,
467 keycode[i] & KEY_RELEASE);
471 /* Start conversion by looking for the first new keycode (by same). */
472 for (i = same; i < num_keycodes; i++) {
473 int key = keycode[i];
477 * For a normal key (with an ASCII value), add it; otherwise
478 * translate special key to escape sequence if possible.
480 if (key < table->num_entries) {
481 ch = table->xlate[key];
482 if ((config->flags & FLAG_CAPS_LOCK) &&
483 ch >= 'a' && ch <= 'z')
485 /* ban digit numbers if 'Num Lock' is not on */
486 if (!(config->flags & FLAG_NUM_LOCK)) {
487 if (key >= KEY_KP7 && key <= KEY_KPDOT &&
488 key != KEY_KPMINUS && key != KEY_KPPLUS)
491 if (ch_count < max_chars && ch != 0xff)
492 output_ch[ch_count++] = (uchar)ch;
494 ch_count += input_keycode_to_ansi364(config, key,
495 output_ch, max_chars);
499 if (ch_count > max_chars) {
500 debug("%s: Output char buffer overflow size=%d, need=%d\n",
501 __func__, max_chars, ch_count);
505 /* ok, so return keys */
509 static int _input_send_keycodes(struct input_config *config, int keycode[],
510 int num_keycodes, bool do_send)
512 char ch[num_keycodes * ANSI_CHAR_MAX];
513 int count, i, same = 0;
517 config->modifiers = 0;
518 if (!input_check_keycodes(config, keycode, num_keycodes, &same)) {
520 * Same as last time - is it time for another repeat?
521 * TODO(sjg@chromium.org) We drop repeats here and since
522 * the caller may not call in again for a while, our
523 * auto-repeat speed is not quite correct. We should
524 * insert another character if we later realise that we
525 * have missed a repeat slot.
527 is_repeat = config->allow_repeats || (config->repeat_rate_ms &&
528 (int)get_timer(config->next_repeat_ms) >= 0);
533 count = input_keycodes_to_ascii(config, keycode, num_keycodes,
534 ch, sizeof(ch), is_repeat ? 0 : same);
536 for (i = 0; i < count; i++)
537 input_queue_ascii(config, ch[i]);
539 delay_ms = is_repeat ?
540 config->repeat_rate_ms :
541 config->repeat_delay_ms;
543 config->next_repeat_ms = get_timer(0) + delay_ms;
548 int input_send_keycodes(struct input_config *config, int keycode[],
551 return _input_send_keycodes(config, keycode, num_keycodes, true);
554 int input_add_keycode(struct input_config *config, int new_keycode,
557 int keycode[INPUT_MAX_MODIFIERS + 1];
560 /* Add the old keycodes which are not removed by this new one */
561 for (i = 0, count = 0; i < config->num_prev_keycodes; i++) {
562 int code = config->prev_keycodes[i];
564 if (new_keycode == code) {
569 keycode[count++] = code;
572 if (!release && new_keycode != -1)
573 keycode[count++] = new_keycode;
574 debug("\ncodes for %02x/%d: ", new_keycode, release);
575 for (i = 0; i < count; i++)
576 debug("%02x ", keycode[i]);
579 /* Don't output any ASCII characters if this is a key release */
580 return _input_send_keycodes(config, keycode, count, !release);
583 int input_add_table(struct input_config *config, int left_keycode,
584 int right_keycode, const uchar *xlate, int num_entries)
586 struct input_key_xlate *table;
588 if (config->num_tables == INPUT_MAX_MODIFIERS) {
589 debug("%s: Too many modifier tables\n", __func__);
593 table = &config->table[config->num_tables++];
594 table->left_keycode = left_keycode;
595 table->right_keycode = right_keycode;
596 table->xlate = xlate;
597 table->num_entries = num_entries;
602 void input_set_delays(struct input_config *config, int repeat_delay_ms,
605 config->repeat_delay_ms = repeat_delay_ms;
606 config->repeat_rate_ms = repeat_rate_ms;
609 void input_allow_repeats(struct input_config *config, bool allow_repeats)
611 config->allow_repeats = allow_repeats;
614 int input_leds_changed(struct input_config *config)
616 if (config->leds_changed)
622 int input_add_tables(struct input_config *config, bool german)
624 struct kbd_entry *entry;
628 mask = german ? KBD_GERMAN : KBD_ENGLISH;
629 for (entry = kbd_entry; entry->kbd_mask; entry++) {
630 if (!(mask & entry->kbd_mask))
632 ret = input_add_table(config, entry->left_keycode,
633 entry->right_keycode, entry->xlate,
642 int input_init(struct input_config *config, int leds)
644 memset(config, '\0', sizeof(*config));
650 int input_stdio_register(struct stdio_dev *dev)
654 error = stdio_register(dev);
655 #if !defined(CONFIG_SPL_BUILD) || CONFIG_IS_ENABLED(ENV_SUPPORT)
656 /* check if this is the standard input device */
657 if (!error && strcmp(env_get("stdin"), dev->name) == 0) {
658 /* reassign the console */
659 if (OVERWRITE_CONSOLE ||
660 console_assign(stdin, dev->name))