19bc590e5SSimon Glass /* 29bc590e5SSimon Glass * Translate key codes into ASCII 39bc590e5SSimon Glass * 49bc590e5SSimon Glass * Copyright (c) 2011 The Chromium OS Authors. 59bc590e5SSimon Glass * (C) Copyright 2004 DENX Software Engineering, Wolfgang Denk, wd@denx.de 69bc590e5SSimon Glass * 71a459660SWolfgang Denk * SPDX-License-Identifier: GPL-2.0+ 89bc590e5SSimon Glass */ 99bc590e5SSimon Glass 109bc590e5SSimon Glass #include <common.h> 1192778b27SSimon Glass #include <errno.h> 129bc590e5SSimon Glass #include <stdio_dev.h> 139bc590e5SSimon Glass #include <input.h> 149bc590e5SSimon Glass #include <linux/input.h> 159bc590e5SSimon Glass 169bc590e5SSimon Glass enum { 179bc590e5SSimon Glass /* These correspond to the lights on the keyboard */ 189bc590e5SSimon Glass FLAG_NUM_LOCK = 1 << 0, 199bc590e5SSimon Glass FLAG_CAPS_LOCK = 1 << 1, 209bc590e5SSimon Glass FLAG_SCROLL_LOCK = 1 << 2, 219bc590e5SSimon Glass 229bc590e5SSimon Glass /* Special flag ORed with key code to indicate release */ 239bc590e5SSimon Glass KEY_RELEASE = 1 << 15, 249bc590e5SSimon Glass KEY_MASK = 0xfff, 259bc590e5SSimon Glass }; 269bc590e5SSimon Glass 279bc590e5SSimon Glass /* 289bc590e5SSimon Glass * These takes map key codes to ASCII. 0xff means no key, or special key. 299bc590e5SSimon Glass * Three tables are provided - one for plain keys, one for when the shift 309bc590e5SSimon Glass * 'modifier' key is pressed and one for when the ctrl modifier key is 319bc590e5SSimon Glass * pressed. 329bc590e5SSimon Glass */ 339bc590e5SSimon Glass static const uchar kbd_plain_xlate[] = { 349bc590e5SSimon Glass 0xff, 0x1b, '1', '2', '3', '4', '5', '6', 359bc590e5SSimon Glass '7', '8', '9', '0', '-', '=', '\b', '\t', /* 0x00 - 0x0f */ 369bc590e5SSimon Glass 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 379bc590e5SSimon Glass 'o', 'p', '[', ']', '\r', 0xff, 'a', 's', /* 0x10 - 0x1f */ 389bc590e5SSimon Glass 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', 399bc590e5SSimon Glass '\'', '`', 0xff, '\\', 'z', 'x', 'c', 'v', /* 0x20 - 0x2f */ 409bc590e5SSimon Glass 'b', 'n', 'm', ',' , '.', '/', 0xff, 0xff, 0xff, 419bc590e5SSimon Glass ' ', 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 0x30 - 0x3f */ 429bc590e5SSimon Glass 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, '7', 439bc590e5SSimon Glass '8', '9', '-', '4', '5', '6', '+', '1', /* 0x40 - 0x4f */ 449bc590e5SSimon Glass '2', '3', '0', '.', 0xff, 0xff, 0xff, 0xff, 459bc590e5SSimon Glass 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 0x50 - 0x5F */ 4677c7f045SSimon Glass '\r', 0xff, '/', '*', 479bc590e5SSimon Glass }; 489bc590e5SSimon Glass 499bc590e5SSimon Glass static unsigned char kbd_shift_xlate[] = { 509bc590e5SSimon Glass 0xff, 0x1b, '!', '@', '#', '$', '%', '^', 519bc590e5SSimon Glass '&', '*', '(', ')', '_', '+', '\b', '\t', /* 0x00 - 0x0f */ 529bc590e5SSimon Glass 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 539bc590e5SSimon Glass 'O', 'P', '{', '}', '\r', 0xff, 'A', 'S', /* 0x10 - 0x1f */ 549bc590e5SSimon Glass 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':', 559bc590e5SSimon Glass '"', '~', 0xff, '|', 'Z', 'X', 'C', 'V', /* 0x20 - 0x2f */ 569bc590e5SSimon Glass 'B', 'N', 'M', '<', '>', '?', 0xff, 0xff, 0xff, 579bc590e5SSimon Glass ' ', 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 0x30 - 0x3f */ 589bc590e5SSimon Glass 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, '7', 599bc590e5SSimon Glass '8', '9', '-', '4', '5', '6', '+', '1', /* 0x40 - 0x4f */ 609bc590e5SSimon Glass '2', '3', '0', '.', 0xff, 0xff, 0xff, 0xff, 0xff, 619bc590e5SSimon Glass 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 0x50 - 0x5F */ 6277c7f045SSimon Glass '\r', 0xff, '/', '*', 639bc590e5SSimon Glass }; 649bc590e5SSimon Glass 659bc590e5SSimon Glass static unsigned char kbd_ctrl_xlate[] = { 669bc590e5SSimon Glass 0xff, 0x1b, '1', 0x00, '3', '4', '5', 0x1E, 679bc590e5SSimon Glass '7', '8', '9', '0', 0x1F, '=', '\b', '\t', /* 0x00 - 0x0f */ 682e5513bdSSimon Glass 0x11, 0x17, 0x05, 0x12, 0x14, 0x19, 0x15, 0x09, 699bc590e5SSimon Glass 0x0f, 0x10, 0x1b, 0x1d, '\n', 0xff, 0x01, 0x13, /* 0x10 - 0x1f */ 709bc590e5SSimon Glass 0x04, 0x06, 0x08, 0x09, 0x0a, 0x0b, 0x0c, ';', 719bc590e5SSimon Glass '\'', '~', 0x00, 0x1c, 0x1a, 0x18, 0x03, 0x16, /* 0x20 - 0x2f */ 729bc590e5SSimon Glass 0x02, 0x0e, 0x0d, '<', '>', '?', 0xff, 0xff, 739bc590e5SSimon Glass 0xff, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 0x30 - 0x3f */ 749bc590e5SSimon Glass 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, '7', 759bc590e5SSimon Glass '8', '9', '-', '4', '5', '6', '+', '1', /* 0x40 - 0x4f */ 769bc590e5SSimon Glass '2', '3', '0', '.', 0xff, 0xff, 0xff, 0xff, 779bc590e5SSimon Glass 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 0x50 - 0x5F */ 7877c7f045SSimon Glass '\r', 0xff, '/', '*', 799bc590e5SSimon Glass }; 809bc590e5SSimon Glass 8144abe47dSHung-Te Lin /* 8244abe47dSHung-Te Lin * Scan key code to ANSI 3.64 escape sequence table. This table is 8344abe47dSHung-Te Lin * incomplete in that it does not include all possible extra keys. 8444abe47dSHung-Te Lin */ 8544abe47dSHung-Te Lin static struct { 8644abe47dSHung-Te Lin int kbd_scan_code; 8744abe47dSHung-Te Lin char *escape; 8844abe47dSHung-Te Lin } kbd_to_ansi364[] = { 8944abe47dSHung-Te Lin { KEY_UP, "\033[A"}, 9044abe47dSHung-Te Lin { KEY_DOWN, "\033[B"}, 9144abe47dSHung-Te Lin { KEY_RIGHT, "\033[C"}, 9244abe47dSHung-Te Lin { KEY_LEFT, "\033[D"}, 9344abe47dSHung-Te Lin }; 9444abe47dSHung-Te Lin 9544abe47dSHung-Te Lin /* Maximum number of output characters that an ANSI sequence expands to */ 9644abe47dSHung-Te Lin #define ANSI_CHAR_MAX 3 979bc590e5SSimon Glass 98c14e94e5SKim Phillips static int input_queue_ascii(struct input_config *config, int ch) 999bc590e5SSimon Glass { 1009bc590e5SSimon Glass if (config->fifo_in + 1 == INPUT_BUFFER_LEN) { 1019bc590e5SSimon Glass if (!config->fifo_out) 1029bc590e5SSimon Glass return -1; /* buffer full */ 1039bc590e5SSimon Glass else 1049bc590e5SSimon Glass config->fifo_in = 0; 1059bc590e5SSimon Glass } else { 1069bc590e5SSimon Glass if (config->fifo_in + 1 == config->fifo_out) 1079bc590e5SSimon Glass return -1; /* buffer full */ 1089bc590e5SSimon Glass config->fifo_in++; 1099bc590e5SSimon Glass } 1103a85e436SSimon Glass debug(" {%02x} ", ch); 1119bc590e5SSimon Glass config->fifo[config->fifo_in] = (uchar)ch; 1129bc590e5SSimon Glass 1139bc590e5SSimon Glass return 0; 1149bc590e5SSimon Glass } 1159bc590e5SSimon Glass 1169bc590e5SSimon Glass int input_tstc(struct input_config *config) 1179bc590e5SSimon Glass { 1189bc590e5SSimon Glass if (config->fifo_in == config->fifo_out && config->read_keys) { 1199bc590e5SSimon Glass if (!(*config->read_keys)(config)) 1209bc590e5SSimon Glass return 0; 1219bc590e5SSimon Glass } 1229bc590e5SSimon Glass return config->fifo_in != config->fifo_out; 1239bc590e5SSimon Glass } 1249bc590e5SSimon Glass 1259bc590e5SSimon Glass int input_getc(struct input_config *config) 1269bc590e5SSimon Glass { 1279bc590e5SSimon Glass int err = 0; 1289bc590e5SSimon Glass 1299bc590e5SSimon Glass while (config->fifo_in == config->fifo_out) { 1309bc590e5SSimon Glass if (config->read_keys) 1319bc590e5SSimon Glass err = (*config->read_keys)(config); 1329bc590e5SSimon Glass if (err) 1339bc590e5SSimon Glass return -1; 1349bc590e5SSimon Glass } 1359bc590e5SSimon Glass 1369bc590e5SSimon Glass if (++config->fifo_out == INPUT_BUFFER_LEN) 1379bc590e5SSimon Glass config->fifo_out = 0; 1389bc590e5SSimon Glass 1399bc590e5SSimon Glass return config->fifo[config->fifo_out]; 1409bc590e5SSimon Glass } 1419bc590e5SSimon Glass 1429bc590e5SSimon Glass /** 1439bc590e5SSimon Glass * Process a modifier/special key press or release and decide which key 1449bc590e5SSimon Glass * translation array should be used as a result. 1459bc590e5SSimon Glass * 1469bc590e5SSimon Glass * TODO: Should keep track of modifier press/release 1479bc590e5SSimon Glass * 1489bc590e5SSimon Glass * @param config Input state 1499bc590e5SSimon Glass * @param key Key code to process 1509bc590e5SSimon Glass * @param release 0 if a press, 1 if a release 1519bc590e5SSimon Glass * @return pointer to keycode->ascii translation table that should be used 1529bc590e5SSimon Glass */ 1539bc590e5SSimon Glass static struct input_key_xlate *process_modifier(struct input_config *config, 1549bc590e5SSimon Glass int key, int release) 1559bc590e5SSimon Glass { 1569bc590e5SSimon Glass struct input_key_xlate *table; 1579bc590e5SSimon Glass int flip = -1; 1589bc590e5SSimon Glass int i; 1599bc590e5SSimon Glass 1609bc590e5SSimon Glass /* Start with the main table, and see what modifiers change it */ 1619bc590e5SSimon Glass assert(config->num_tables > 0); 1629bc590e5SSimon Glass table = &config->table[0]; 1639bc590e5SSimon Glass for (i = 1; i < config->num_tables; i++) { 1649bc590e5SSimon Glass struct input_key_xlate *tab = &config->table[i]; 1659bc590e5SSimon Glass 1669bc590e5SSimon Glass if (key == tab->left_keycode || key == tab->right_keycode) 1679bc590e5SSimon Glass table = tab; 1689bc590e5SSimon Glass } 1699bc590e5SSimon Glass 1709bc590e5SSimon Glass /* Handle the lighted keys */ 1719bc590e5SSimon Glass if (!release) { 1729bc590e5SSimon Glass switch (key) { 1739bc590e5SSimon Glass case KEY_SCROLLLOCK: 1749bc590e5SSimon Glass flip = FLAG_SCROLL_LOCK; 1759bc590e5SSimon Glass break; 1769bc590e5SSimon Glass case KEY_NUMLOCK: 1779bc590e5SSimon Glass flip = FLAG_NUM_LOCK; 1789bc590e5SSimon Glass break; 1799bc590e5SSimon Glass case KEY_CAPSLOCK: 1809bc590e5SSimon Glass flip = FLAG_CAPS_LOCK; 1819bc590e5SSimon Glass break; 1829bc590e5SSimon Glass } 1839bc590e5SSimon Glass } 1849bc590e5SSimon Glass 1859bc590e5SSimon Glass if (flip != -1) { 1869bc590e5SSimon Glass int leds = 0; 1879bc590e5SSimon Glass 1889bc590e5SSimon Glass config->leds ^= flip; 1899bc590e5SSimon Glass if (config->flags & FLAG_NUM_LOCK) 1909bc590e5SSimon Glass leds |= INPUT_LED_NUM; 1919bc590e5SSimon Glass if (config->flags & FLAG_CAPS_LOCK) 1929bc590e5SSimon Glass leds |= INPUT_LED_CAPS; 1939bc590e5SSimon Glass if (config->flags & FLAG_SCROLL_LOCK) 1949bc590e5SSimon Glass leds |= INPUT_LED_SCROLL; 1959bc590e5SSimon Glass config->leds = leds; 1969bc590e5SSimon Glass } 1979bc590e5SSimon Glass 1989bc590e5SSimon Glass return table; 1999bc590e5SSimon Glass } 2009bc590e5SSimon Glass 2019bc590e5SSimon Glass /** 2029bc590e5SSimon Glass * Search an int array for a key value 2039bc590e5SSimon Glass * 2049bc590e5SSimon Glass * @param array Array to search 2059bc590e5SSimon Glass * @param count Number of elements in array 2069bc590e5SSimon Glass * @param key Key value to find 2079bc590e5SSimon Glass * @return element where value was first found, -1 if none 2089bc590e5SSimon Glass */ 2099bc590e5SSimon Glass static int array_search(int *array, int count, int key) 2109bc590e5SSimon Glass { 2119bc590e5SSimon Glass int i; 2129bc590e5SSimon Glass 2139bc590e5SSimon Glass for (i = 0; i < count; i++) { 2149bc590e5SSimon Glass if (array[i] == key) 2159bc590e5SSimon Glass return i; 2169bc590e5SSimon Glass } 2179bc590e5SSimon Glass 2189bc590e5SSimon Glass return -1; 2199bc590e5SSimon Glass } 2209bc590e5SSimon Glass 2219bc590e5SSimon Glass /** 2229bc590e5SSimon Glass * Sort an array so that those elements that exist in the ordering are 2239bc590e5SSimon Glass * first in the array, and in the same order as the ordering. The algorithm 2249bc590e5SSimon Glass * is O(count * ocount) and designed for small arrays. 2259bc590e5SSimon Glass * 2269bc590e5SSimon Glass * TODO: Move this to common / lib? 2279bc590e5SSimon Glass * 2289bc590e5SSimon Glass * @param dest Array with elements to sort, also destination array 2299bc590e5SSimon Glass * @param count Number of elements to sort 2309bc590e5SSimon Glass * @param order Array containing ordering elements 2319bc590e5SSimon Glass * @param ocount Number of ordering elements 2329bc590e5SSimon Glass * @return number of elements in dest that are in order (these will be at the 2339bc590e5SSimon Glass * start of dest). 2349bc590e5SSimon Glass */ 2359bc590e5SSimon Glass static int sort_array_by_ordering(int *dest, int count, int *order, 2369bc590e5SSimon Glass int ocount) 2379bc590e5SSimon Glass { 2389bc590e5SSimon Glass int temp[count]; 2399bc590e5SSimon Glass int dest_count; 2409bc590e5SSimon Glass int same; /* number of elements which are the same */ 2419bc590e5SSimon Glass int i; 2429bc590e5SSimon Glass 2439bc590e5SSimon Glass /* setup output items, copy items to be sorted into our temp area */ 2449bc590e5SSimon Glass memcpy(temp, dest, count * sizeof(*dest)); 2459bc590e5SSimon Glass dest_count = 0; 2469bc590e5SSimon Glass 2479bc590e5SSimon Glass /* work through the ordering, move over the elements we agree on */ 2489bc590e5SSimon Glass for (i = 0; i < ocount; i++) { 2499bc590e5SSimon Glass if (array_search(temp, count, order[i]) != -1) 2509bc590e5SSimon Glass dest[dest_count++] = order[i]; 2519bc590e5SSimon Glass } 2529bc590e5SSimon Glass same = dest_count; 2539bc590e5SSimon Glass 2549bc590e5SSimon Glass /* now move over the elements that are not in the ordering */ 2559bc590e5SSimon Glass for (i = 0; i < count; i++) { 2569bc590e5SSimon Glass if (array_search(order, ocount, temp[i]) == -1) 2579bc590e5SSimon Glass dest[dest_count++] = temp[i]; 2589bc590e5SSimon Glass } 2599bc590e5SSimon Glass assert(dest_count == count); 2609bc590e5SSimon Glass return same; 2619bc590e5SSimon Glass } 2629bc590e5SSimon Glass 2639bc590e5SSimon Glass /** 2649bc590e5SSimon Glass * Check a list of key codes against the previous key scan 2659bc590e5SSimon Glass * 2669bc590e5SSimon Glass * Given a list of new key codes, we check how many of these are the same 2679bc590e5SSimon Glass * as last time. 2689bc590e5SSimon Glass * 2699bc590e5SSimon Glass * @param config Input state 2709bc590e5SSimon Glass * @param keycode List of key codes to examine 2719bc590e5SSimon Glass * @param num_keycodes Number of key codes 2729bc590e5SSimon Glass * @param same Returns number of key codes which are the same 2739bc590e5SSimon Glass */ 2749bc590e5SSimon Glass static int input_check_keycodes(struct input_config *config, 2759bc590e5SSimon Glass int keycode[], int num_keycodes, int *same) 2769bc590e5SSimon Glass { 2779bc590e5SSimon Glass /* Select the 'plain' xlate table to start with */ 2789bc590e5SSimon Glass if (!config->num_tables) { 2799bc590e5SSimon Glass debug("%s: No xlate tables: cannot decode keys\n", __func__); 2809bc590e5SSimon Glass return -1; 2819bc590e5SSimon Glass } 2829bc590e5SSimon Glass 2839bc590e5SSimon Glass /* sort the keycodes into the same order as the previous ones */ 2849bc590e5SSimon Glass *same = sort_array_by_ordering(keycode, num_keycodes, 2859bc590e5SSimon Glass config->prev_keycodes, config->num_prev_keycodes); 2869bc590e5SSimon Glass 2879bc590e5SSimon Glass memcpy(config->prev_keycodes, keycode, num_keycodes * sizeof(int)); 2889bc590e5SSimon Glass config->num_prev_keycodes = num_keycodes; 2899bc590e5SSimon Glass 2909bc590e5SSimon Glass return *same != num_keycodes; 2919bc590e5SSimon Glass } 2929bc590e5SSimon Glass 2939bc590e5SSimon Glass /** 29444abe47dSHung-Te Lin * Checks and converts a special key code into ANSI 3.64 escape sequence. 29544abe47dSHung-Te Lin * 29644abe47dSHung-Te Lin * @param config Input state 29744abe47dSHung-Te Lin * @param keycode Key code to examine 29844abe47dSHung-Te Lin * @param output_ch Buffer to place output characters into. It should 29944abe47dSHung-Te Lin * be at least ANSI_CHAR_MAX bytes long, to allow for 30044abe47dSHung-Te Lin * an ANSI sequence. 30144abe47dSHung-Te Lin * @param max_chars Maximum number of characters to add to output_ch 30244abe47dSHung-Te Lin * @return number of characters output, if the key was converted, otherwise 0. 30344abe47dSHung-Te Lin * This may be larger than max_chars, in which case the overflow 30444abe47dSHung-Te Lin * characters are not output. 30544abe47dSHung-Te Lin */ 30644abe47dSHung-Te Lin static int input_keycode_to_ansi364(struct input_config *config, 30744abe47dSHung-Te Lin int keycode, char output_ch[], int max_chars) 30844abe47dSHung-Te Lin { 30944abe47dSHung-Te Lin const char *escape; 31044abe47dSHung-Te Lin int ch_count; 31144abe47dSHung-Te Lin int i; 31244abe47dSHung-Te Lin 31344abe47dSHung-Te Lin for (i = ch_count = 0; i < ARRAY_SIZE(kbd_to_ansi364); i++) { 31444abe47dSHung-Te Lin if (keycode != kbd_to_ansi364[i].kbd_scan_code) 31544abe47dSHung-Te Lin continue; 31644abe47dSHung-Te Lin for (escape = kbd_to_ansi364[i].escape; *escape; escape++) { 31744abe47dSHung-Te Lin if (ch_count < max_chars) 31844abe47dSHung-Te Lin output_ch[ch_count] = *escape; 31944abe47dSHung-Te Lin ch_count++; 32044abe47dSHung-Te Lin } 32144abe47dSHung-Te Lin return ch_count; 32244abe47dSHung-Te Lin } 32344abe47dSHung-Te Lin 32444abe47dSHung-Te Lin return 0; 32544abe47dSHung-Te Lin } 32644abe47dSHung-Te Lin 32744abe47dSHung-Te Lin /** 32844abe47dSHung-Te Lin * Converts and queues a list of key codes in escaped ASCII string form 3299bc590e5SSimon Glass * Convert a list of key codes into ASCII 3309bc590e5SSimon Glass * 3319bc590e5SSimon Glass * You must call input_check_keycodes() before this. It turns the keycode 33244abe47dSHung-Te Lin * list into a list of ASCII characters and sends them to the input layer. 3339bc590e5SSimon Glass * 3349bc590e5SSimon Glass * Characters which were seen last time do not generate fresh ASCII output. 33544abe47dSHung-Te Lin * The output (calls to queue_ascii) may be longer than num_keycodes, if the 33644abe47dSHung-Te Lin * keycode contains special keys that was encoded to longer escaped sequence. 3379bc590e5SSimon Glass * 3389bc590e5SSimon Glass * @param config Input state 3399bc590e5SSimon Glass * @param keycode List of key codes to examine 3409bc590e5SSimon Glass * @param num_keycodes Number of key codes 34144abe47dSHung-Te Lin * @param output_ch Buffer to place output characters into. It should 34244abe47dSHung-Te Lin * be at last ANSI_CHAR_MAX * num_keycodes, to allow for 34344abe47dSHung-Te Lin * ANSI sequences. 34444abe47dSHung-Te Lin * @param max_chars Maximum number of characters to add to output_ch 3459bc590e5SSimon Glass * @param same Number of key codes which are the same 34644abe47dSHung-Te Lin * @return number of characters written into output_ch, or -1 if we would 34744abe47dSHung-Te Lin * exceed max_chars chars. 3489bc590e5SSimon Glass */ 3499bc590e5SSimon Glass static int input_keycodes_to_ascii(struct input_config *config, 35044abe47dSHung-Te Lin int keycode[], int num_keycodes, char output_ch[], 35144abe47dSHung-Te Lin int max_chars, int same) 3529bc590e5SSimon Glass { 3539bc590e5SSimon Glass struct input_key_xlate *table; 35444abe47dSHung-Te Lin int ch_count = 0; 3559bc590e5SSimon Glass int i; 3569bc590e5SSimon Glass 3579bc590e5SSimon Glass table = &config->table[0]; 3589bc590e5SSimon Glass 3599bc590e5SSimon Glass /* deal with modifiers first */ 3609bc590e5SSimon Glass for (i = 0; i < num_keycodes; i++) { 3619bc590e5SSimon Glass int key = keycode[i] & KEY_MASK; 3629bc590e5SSimon Glass 3639bc590e5SSimon Glass if (key >= table->num_entries || table->xlate[key] == 0xff) { 3649bc590e5SSimon Glass table = process_modifier(config, key, 3659bc590e5SSimon Glass keycode[i] & KEY_RELEASE); 3669bc590e5SSimon Glass } 3679bc590e5SSimon Glass } 3689bc590e5SSimon Glass 36944abe47dSHung-Te Lin /* Start conversion by looking for the first new keycode (by same). */ 37044abe47dSHung-Te Lin for (i = same; i < num_keycodes; i++) { 3719bc590e5SSimon Glass int key = keycode[i]; 37244abe47dSHung-Te Lin int ch = (key < table->num_entries) ? table->xlate[key] : 0xff; 3739bc590e5SSimon Glass 37444abe47dSHung-Te Lin /* 37544abe47dSHung-Te Lin * For a normal key (with an ASCII value), add it; otherwise 37644abe47dSHung-Te Lin * translate special key to escape sequence if possible. 37744abe47dSHung-Te Lin */ 37844abe47dSHung-Te Lin if (ch != 0xff) { 37944abe47dSHung-Te Lin if (ch_count < max_chars) 38044abe47dSHung-Te Lin output_ch[ch_count] = (uchar)ch; 38144abe47dSHung-Te Lin ch_count++; 38244abe47dSHung-Te Lin } else { 38344abe47dSHung-Te Lin ch_count += input_keycode_to_ansi364(config, key, 38444abe47dSHung-Te Lin output_ch, max_chars); 3859bc590e5SSimon Glass } 3869bc590e5SSimon Glass } 3879bc590e5SSimon Glass 38844abe47dSHung-Te Lin if (ch_count > max_chars) { 38944abe47dSHung-Te Lin debug("%s: Output char buffer overflow size=%d, need=%d\n", 39044abe47dSHung-Te Lin __func__, max_chars, ch_count); 39144abe47dSHung-Te Lin return -1; 39244abe47dSHung-Te Lin } 39344abe47dSHung-Te Lin 3949bc590e5SSimon Glass /* ok, so return keys */ 3959bc590e5SSimon Glass return ch_count; 3969bc590e5SSimon Glass } 3979bc590e5SSimon Glass 3983a85e436SSimon Glass static int _input_send_keycodes(struct input_config *config, int keycode[], 3993a85e436SSimon Glass int num_keycodes, bool do_send) 4009bc590e5SSimon Glass { 40144abe47dSHung-Te Lin char ch[num_keycodes * ANSI_CHAR_MAX]; 4029bc590e5SSimon Glass int count, i, same = 0; 4039bc590e5SSimon Glass int is_repeat = 0; 4049bc590e5SSimon Glass unsigned delay_ms; 4059bc590e5SSimon Glass 4069bc590e5SSimon Glass config->modifiers = 0; 4079bc590e5SSimon Glass if (!input_check_keycodes(config, keycode, num_keycodes, &same)) { 4089bc590e5SSimon Glass /* 4099bc590e5SSimon Glass * Same as last time - is it time for another repeat? 4109bc590e5SSimon Glass * TODO(sjg@chromium.org) We drop repeats here and since 4119bc590e5SSimon Glass * the caller may not call in again for a while, our 4129bc590e5SSimon Glass * auto-repeat speed is not quite correct. We should 4139bc590e5SSimon Glass * insert another character if we later realise that we 4149bc590e5SSimon Glass * have missed a repeat slot. 4159bc590e5SSimon Glass */ 416*0b186c08SSimon Glass is_repeat = config->allow_repeats || (config->repeat_rate_ms && 417*0b186c08SSimon Glass (int)get_timer(config->next_repeat_ms) >= 0); 4189bc590e5SSimon Glass if (!is_repeat) 4199bc590e5SSimon Glass return 0; 4209bc590e5SSimon Glass } 4219bc590e5SSimon Glass 4229bc590e5SSimon Glass count = input_keycodes_to_ascii(config, keycode, num_keycodes, 42344abe47dSHung-Te Lin ch, sizeof(ch), is_repeat ? 0 : same); 4243a85e436SSimon Glass if (do_send) { 4259bc590e5SSimon Glass for (i = 0; i < count; i++) 4269bc590e5SSimon Glass input_queue_ascii(config, ch[i]); 4273a85e436SSimon Glass } 4289bc590e5SSimon Glass delay_ms = is_repeat ? 4299bc590e5SSimon Glass config->repeat_rate_ms : 4309bc590e5SSimon Glass config->repeat_delay_ms; 4319bc590e5SSimon Glass 4329bc590e5SSimon Glass config->next_repeat_ms = get_timer(0) + delay_ms; 43344abe47dSHung-Te Lin 43444abe47dSHung-Te Lin return count; 4359bc590e5SSimon Glass } 4369bc590e5SSimon Glass 4373a85e436SSimon Glass int input_send_keycodes(struct input_config *config, int keycode[], 4383a85e436SSimon Glass int num_keycodes) 4393a85e436SSimon Glass { 4403a85e436SSimon Glass return _input_send_keycodes(config, keycode, num_keycodes, true); 4413a85e436SSimon Glass } 4423a85e436SSimon Glass 4433a85e436SSimon Glass int input_add_keycode(struct input_config *config, int new_keycode, 4443a85e436SSimon Glass bool release) 4453a85e436SSimon Glass { 4463a85e436SSimon Glass int keycode[INPUT_MAX_MODIFIERS + 1]; 4473a85e436SSimon Glass int count, i; 4483a85e436SSimon Glass 4493a85e436SSimon Glass /* Add the old keycodes which are not removed by this new one */ 4503a85e436SSimon Glass for (i = 0, count = 0; i < config->num_prev_keycodes; i++) { 4513a85e436SSimon Glass int code = config->prev_keycodes[i]; 4523a85e436SSimon Glass 4533a85e436SSimon Glass if (new_keycode == code) { 4543a85e436SSimon Glass if (release) 4553a85e436SSimon Glass continue; 4563a85e436SSimon Glass new_keycode = -1; 4573a85e436SSimon Glass } 4583a85e436SSimon Glass keycode[count++] = code; 4593a85e436SSimon Glass } 4603a85e436SSimon Glass 4613a85e436SSimon Glass if (!release && new_keycode != -1) 4623a85e436SSimon Glass keycode[count++] = new_keycode; 4633a85e436SSimon Glass debug("\ncodes for %02x/%d: ", new_keycode, release); 4643a85e436SSimon Glass for (i = 0; i < count; i++) 4653a85e436SSimon Glass debug("%02x ", keycode[i]); 4663a85e436SSimon Glass debug("\n"); 4673a85e436SSimon Glass 4683a85e436SSimon Glass /* Don't output any ASCII characters if this is a key release */ 4693a85e436SSimon Glass return _input_send_keycodes(config, keycode, count, !release); 4703a85e436SSimon Glass } 4713a85e436SSimon Glass 4729bc590e5SSimon Glass int input_add_table(struct input_config *config, int left_keycode, 4739bc590e5SSimon Glass int right_keycode, const uchar *xlate, int num_entries) 4749bc590e5SSimon Glass { 4759bc590e5SSimon Glass struct input_key_xlate *table; 4769bc590e5SSimon Glass 4779bc590e5SSimon Glass if (config->num_tables == INPUT_MAX_MODIFIERS) { 4789bc590e5SSimon Glass debug("%s: Too many modifier tables\n", __func__); 4799bc590e5SSimon Glass return -1; 4809bc590e5SSimon Glass } 4819bc590e5SSimon Glass 4829bc590e5SSimon Glass table = &config->table[config->num_tables++]; 4839bc590e5SSimon Glass table->left_keycode = left_keycode; 4849bc590e5SSimon Glass table->right_keycode = right_keycode; 4859bc590e5SSimon Glass table->xlate = xlate; 4869bc590e5SSimon Glass table->num_entries = num_entries; 4879bc590e5SSimon Glass 4889bc590e5SSimon Glass return 0; 4899bc590e5SSimon Glass } 4909bc590e5SSimon Glass 4911b1d3e64SSimon Glass void input_set_delays(struct input_config *config, int repeat_delay_ms, 4929bc590e5SSimon Glass int repeat_rate_ms) 4939bc590e5SSimon Glass { 4941b1d3e64SSimon Glass config->repeat_delay_ms = repeat_delay_ms; 4951b1d3e64SSimon Glass config->repeat_rate_ms = repeat_rate_ms; 4961b1d3e64SSimon Glass } 4971b1d3e64SSimon Glass 498*0b186c08SSimon Glass void input_allow_repeats(struct input_config *config, bool allow_repeats) 499*0b186c08SSimon Glass { 500*0b186c08SSimon Glass config->allow_repeats = allow_repeats; 501*0b186c08SSimon Glass } 502*0b186c08SSimon Glass 50366877b0fSSimon Glass int input_add_tables(struct input_config *config) 50466877b0fSSimon Glass { 50566877b0fSSimon Glass int ret; 50666877b0fSSimon Glass 50766877b0fSSimon Glass ret = input_add_table(config, -1, -1, 50866877b0fSSimon Glass kbd_plain_xlate, ARRAY_SIZE(kbd_plain_xlate)); 50966877b0fSSimon Glass if (ret) 51066877b0fSSimon Glass return ret; 51166877b0fSSimon Glass ret = input_add_table(config, KEY_LEFTSHIFT, KEY_RIGHTSHIFT, 51266877b0fSSimon Glass kbd_shift_xlate, ARRAY_SIZE(kbd_shift_xlate)); 51366877b0fSSimon Glass if (ret) 51466877b0fSSimon Glass return ret; 51566877b0fSSimon Glass 51666877b0fSSimon Glass return input_add_table(config, KEY_LEFTCTRL, KEY_RIGHTCTRL, 51766877b0fSSimon Glass kbd_ctrl_xlate, ARRAY_SIZE(kbd_ctrl_xlate)); 51866877b0fSSimon Glass } 51966877b0fSSimon Glass 5201b1d3e64SSimon Glass int input_init(struct input_config *config, int leds) 5211b1d3e64SSimon Glass { 5229bc590e5SSimon Glass memset(config, '\0', sizeof(*config)); 5239bc590e5SSimon Glass config->leds = leds; 5249bc590e5SSimon Glass 5259bc590e5SSimon Glass return 0; 5269bc590e5SSimon Glass } 5279bc590e5SSimon Glass 5289bc590e5SSimon Glass int input_stdio_register(struct stdio_dev *dev) 5299bc590e5SSimon Glass { 5309bc590e5SSimon Glass int error; 5319bc590e5SSimon Glass 5329bc590e5SSimon Glass error = stdio_register(dev); 5339bc590e5SSimon Glass 5349bc590e5SSimon Glass /* check if this is the standard input device */ 5359bc590e5SSimon Glass if (!error && strcmp(getenv("stdin"), dev->name) == 0) { 5369bc590e5SSimon Glass /* reassign the console */ 5379bc590e5SSimon Glass if (OVERWRITE_CONSOLE || 5389bc590e5SSimon Glass console_assign(stdin, dev->name)) 5399bc590e5SSimon Glass return -1; 5409bc590e5SSimon Glass } 5419bc590e5SSimon Glass 5429bc590e5SSimon Glass return 0; 5439bc590e5SSimon Glass } 544