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> 1124b852a7SSimon Glass #include <console.h> 12cd810918SBin Meng #include <dm.h> 1392778b27SSimon Glass #include <errno.h> 149bc590e5SSimon Glass #include <stdio_dev.h> 159bc590e5SSimon Glass #include <input.h> 16cd810918SBin Meng #ifdef CONFIG_DM_KEYBOARD 17cd810918SBin Meng #include <keyboard.h> 18cd810918SBin Meng #endif 199bc590e5SSimon Glass #include <linux/input.h> 209bc590e5SSimon Glass 219bc590e5SSimon Glass enum { 229bc590e5SSimon Glass /* These correspond to the lights on the keyboard */ 23377a0696SBin Meng FLAG_SCROLL_LOCK = 1 << 0, 24377a0696SBin Meng FLAG_NUM_LOCK = 1 << 1, 25377a0696SBin Meng FLAG_CAPS_LOCK = 1 << 2, 269bc590e5SSimon Glass 279bc590e5SSimon Glass /* Special flag ORed with key code to indicate release */ 289bc590e5SSimon Glass KEY_RELEASE = 1 << 15, 299bc590e5SSimon Glass KEY_MASK = 0xfff, 309bc590e5SSimon Glass }; 319bc590e5SSimon Glass 329bc590e5SSimon Glass /* 339bc590e5SSimon Glass * These takes map key codes to ASCII. 0xff means no key, or special key. 349bc590e5SSimon Glass * Three tables are provided - one for plain keys, one for when the shift 359bc590e5SSimon Glass * 'modifier' key is pressed and one for when the ctrl modifier key is 369bc590e5SSimon Glass * pressed. 379bc590e5SSimon Glass */ 389bc590e5SSimon Glass static const uchar kbd_plain_xlate[] = { 399bc590e5SSimon Glass 0xff, 0x1b, '1', '2', '3', '4', '5', '6', 409bc590e5SSimon Glass '7', '8', '9', '0', '-', '=', '\b', '\t', /* 0x00 - 0x0f */ 419bc590e5SSimon Glass 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 429bc590e5SSimon Glass 'o', 'p', '[', ']', '\r', 0xff, 'a', 's', /* 0x10 - 0x1f */ 439bc590e5SSimon Glass 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', 449bc590e5SSimon Glass '\'', '`', 0xff, '\\', 'z', 'x', 'c', 'v', /* 0x20 - 0x2f */ 459bc590e5SSimon Glass 'b', 'n', 'm', ',' , '.', '/', 0xff, 0xff, 0xff, 469bc590e5SSimon Glass ' ', 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 0x30 - 0x3f */ 479bc590e5SSimon Glass 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, '7', 489bc590e5SSimon Glass '8', '9', '-', '4', '5', '6', '+', '1', /* 0x40 - 0x4f */ 499bc590e5SSimon Glass '2', '3', '0', '.', 0xff, 0xff, 0xff, 0xff, 509bc590e5SSimon Glass 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 0x50 - 0x5F */ 5177c7f045SSimon Glass '\r', 0xff, '/', '*', 529bc590e5SSimon Glass }; 539bc590e5SSimon Glass 549bc590e5SSimon Glass static unsigned char kbd_shift_xlate[] = { 559bc590e5SSimon Glass 0xff, 0x1b, '!', '@', '#', '$', '%', '^', 569bc590e5SSimon Glass '&', '*', '(', ')', '_', '+', '\b', '\t', /* 0x00 - 0x0f */ 579bc590e5SSimon Glass 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 589bc590e5SSimon Glass 'O', 'P', '{', '}', '\r', 0xff, 'A', 'S', /* 0x10 - 0x1f */ 599bc590e5SSimon Glass 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':', 609bc590e5SSimon Glass '"', '~', 0xff, '|', 'Z', 'X', 'C', 'V', /* 0x20 - 0x2f */ 619bc590e5SSimon Glass 'B', 'N', 'M', '<', '>', '?', 0xff, 0xff, 0xff, 629bc590e5SSimon Glass ' ', 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 0x30 - 0x3f */ 639bc590e5SSimon Glass 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, '7', 649bc590e5SSimon Glass '8', '9', '-', '4', '5', '6', '+', '1', /* 0x40 - 0x4f */ 659bc590e5SSimon Glass '2', '3', '0', '.', 0xff, 0xff, 0xff, 0xff, 0xff, 669bc590e5SSimon Glass 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 0x50 - 0x5F */ 6777c7f045SSimon Glass '\r', 0xff, '/', '*', 689bc590e5SSimon Glass }; 699bc590e5SSimon Glass 709bc590e5SSimon Glass static unsigned char kbd_ctrl_xlate[] = { 719bc590e5SSimon Glass 0xff, 0x1b, '1', 0x00, '3', '4', '5', 0x1E, 729bc590e5SSimon Glass '7', '8', '9', '0', 0x1F, '=', '\b', '\t', /* 0x00 - 0x0f */ 732e5513bdSSimon Glass 0x11, 0x17, 0x05, 0x12, 0x14, 0x19, 0x15, 0x09, 749bc590e5SSimon Glass 0x0f, 0x10, 0x1b, 0x1d, '\n', 0xff, 0x01, 0x13, /* 0x10 - 0x1f */ 759bc590e5SSimon Glass 0x04, 0x06, 0x08, 0x09, 0x0a, 0x0b, 0x0c, ';', 769bc590e5SSimon Glass '\'', '~', 0x00, 0x1c, 0x1a, 0x18, 0x03, 0x16, /* 0x20 - 0x2f */ 779bc590e5SSimon Glass 0x02, 0x0e, 0x0d, '<', '>', '?', 0xff, 0xff, 789bc590e5SSimon Glass 0xff, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 0x30 - 0x3f */ 799bc590e5SSimon Glass 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, '7', 809bc590e5SSimon Glass '8', '9', '-', '4', '5', '6', '+', '1', /* 0x40 - 0x4f */ 819bc590e5SSimon Glass '2', '3', '0', '.', 0xff, 0xff, 0xff, 0xff, 829bc590e5SSimon Glass 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 0x50 - 0x5F */ 8377c7f045SSimon Glass '\r', 0xff, '/', '*', 849bc590e5SSimon Glass }; 859bc590e5SSimon Glass 86b1d7a187SSimon Glass static const uchar kbd_plain_xlate_german[] = { 87b1d7a187SSimon Glass 0xff, 0x1b, '1', '2', '3', '4', '5', '6', /* scan 00-07 */ 88b1d7a187SSimon Glass '7', '8', '9', '0', 0xe1, '\'', 0x08, '\t', /* scan 08-0F */ 89b1d7a187SSimon Glass 'q', 'w', 'e', 'r', 't', 'z', 'u', 'i', /* scan 10-17 */ 90b1d7a187SSimon Glass 'o', 'p', 0x81, '+', '\r', 0xff, 'a', 's', /* scan 18-1F */ 91b1d7a187SSimon Glass 'd', 'f', 'g', 'h', 'j', 'k', 'l', 0x94, /* scan 20-27 */ 92b1d7a187SSimon Glass 0x84, '^', 0xff, '#', 'y', 'x', 'c', 'v', /* scan 28-2F */ 93b1d7a187SSimon Glass 'b', 'n', 'm', ',', '.', '-', 0xff, '*', /* scan 30-37 */ 94b1d7a187SSimon Glass ' ', ' ', 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 38-3F */ 95b1d7a187SSimon Glass 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, '7', /* scan 40-47 */ 96b1d7a187SSimon Glass '8', '9', '-', '4', '5', '6', '+', '1', /* scan 48-4F */ 97b1d7a187SSimon Glass '2', '3', '0', ',', 0xff, 0xff, '<', 0xff, /* scan 50-57 */ 98b1d7a187SSimon Glass 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 58-5F */ 99b1d7a187SSimon Glass 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 60-67 */ 100b1d7a187SSimon Glass 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 68-6F */ 101b1d7a187SSimon Glass 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 70-77 */ 102b1d7a187SSimon Glass 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 78-7F */ 103b1d7a187SSimon Glass '\r', 0xff, '/', '*', 104b1d7a187SSimon Glass }; 105b1d7a187SSimon Glass 106b1d7a187SSimon Glass static unsigned char kbd_shift_xlate_german[] = { 107b1d7a187SSimon Glass 0xff, 0x1b, '!', '"', 0x15, '$', '%', '&', /* scan 00-07 */ 108b1d7a187SSimon Glass '/', '(', ')', '=', '?', '`', 0x08, '\t', /* scan 08-0F */ 109b1d7a187SSimon Glass 'Q', 'W', 'E', 'R', 'T', 'Z', 'U', 'I', /* scan 10-17 */ 110b1d7a187SSimon Glass 'O', 'P', 0x9a, '*', '\r', 0xff, 'A', 'S', /* scan 18-1F */ 111b1d7a187SSimon Glass 'D', 'F', 'G', 'H', 'J', 'K', 'L', 0x99, /* scan 20-27 */ 112b1d7a187SSimon Glass 0x8e, 0xf8, 0xff, '\'', 'Y', 'X', 'C', 'V', /* scan 28-2F */ 113b1d7a187SSimon Glass 'B', 'N', 'M', ';', ':', '_', 0xff, '*', /* scan 30-37 */ 114b1d7a187SSimon Glass ' ', ' ', 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 38-3F */ 115b1d7a187SSimon Glass 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, '7', /* scan 40-47 */ 116b1d7a187SSimon Glass '8', '9', '-', '4', '5', '6', '+', '1', /* scan 48-4F */ 117b1d7a187SSimon Glass '2', '3', '0', ',', 0xff, 0xff, '>', 0xff, /* scan 50-57 */ 118b1d7a187SSimon Glass 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 58-5F */ 119b1d7a187SSimon Glass 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 60-67 */ 120b1d7a187SSimon Glass 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 68-6F */ 121b1d7a187SSimon Glass 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 70-77 */ 122b1d7a187SSimon Glass 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 78-7F */ 123b1d7a187SSimon Glass '\r', 0xff, '/', '*', 124b1d7a187SSimon Glass }; 125b1d7a187SSimon Glass 126b1d7a187SSimon Glass static unsigned char kbd_right_alt_xlate_german[] = { 127b1d7a187SSimon Glass 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 00-07 */ 128b1d7a187SSimon Glass '{', '[', ']', '}', '\\', 0xff, 0xff, 0xff, /* scan 08-0F */ 129b1d7a187SSimon Glass '@', 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 10-17 */ 130b1d7a187SSimon Glass 0xff, 0xff, 0xff, '~', 0xff, 0xff, 0xff, 0xff, /* scan 18-1F */ 131b1d7a187SSimon Glass 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 20-27 */ 132b1d7a187SSimon Glass 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 28-2F */ 133b1d7a187SSimon Glass 0xff, 0xff, 0xe6, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 30-37 */ 134b1d7a187SSimon Glass 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 38-3F */ 135b1d7a187SSimon Glass 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 40-47 */ 136b1d7a187SSimon Glass 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 48-4F */ 137b1d7a187SSimon Glass 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, '|', 0xff, /* scan 50-57 */ 138b1d7a187SSimon Glass }; 139b1d7a187SSimon Glass 140b1d7a187SSimon Glass enum kbd_mask { 141b1d7a187SSimon Glass KBD_ENGLISH = 1 << 0, 142b1d7a187SSimon Glass KBD_GERMAN = 1 << 1, 143b1d7a187SSimon Glass }; 144b1d7a187SSimon Glass 145b1d7a187SSimon Glass static struct kbd_entry { 146b1d7a187SSimon Glass int kbd_mask; /* Which languages this is for */ 147b1d7a187SSimon Glass int left_keycode; /* Left keycode to select this map */ 148b1d7a187SSimon Glass int right_keycode; /* Right keycode to select this map */ 149b1d7a187SSimon Glass const uchar *xlate; /* Ascii code for each keycode */ 150b1d7a187SSimon Glass int num_entries; /* Number of entries in xlate */ 151b1d7a187SSimon Glass } kbd_entry[] = { 152b1d7a187SSimon Glass { KBD_ENGLISH, -1, -1, 153b1d7a187SSimon Glass kbd_plain_xlate, ARRAY_SIZE(kbd_plain_xlate) }, 154b1d7a187SSimon Glass { KBD_GERMAN, -1, -1, 155b1d7a187SSimon Glass kbd_plain_xlate_german, ARRAY_SIZE(kbd_plain_xlate_german) }, 156b1d7a187SSimon Glass { KBD_ENGLISH, KEY_LEFTSHIFT, KEY_RIGHTSHIFT, 157b1d7a187SSimon Glass kbd_shift_xlate, ARRAY_SIZE(kbd_shift_xlate) }, 158b1d7a187SSimon Glass { KBD_GERMAN, KEY_LEFTSHIFT, KEY_RIGHTSHIFT, 159b1d7a187SSimon Glass kbd_shift_xlate_german, ARRAY_SIZE(kbd_shift_xlate_german) }, 160b1d7a187SSimon Glass { KBD_ENGLISH | KBD_GERMAN, KEY_LEFTCTRL, KEY_RIGHTCTRL, 161b1d7a187SSimon Glass kbd_ctrl_xlate, ARRAY_SIZE(kbd_ctrl_xlate) }, 162b1d7a187SSimon Glass { KBD_GERMAN, -1, KEY_RIGHTALT, 163b1d7a187SSimon Glass kbd_right_alt_xlate_german, 164b1d7a187SSimon Glass ARRAY_SIZE(kbd_right_alt_xlate_german) }, 165b1d7a187SSimon Glass {}, 166b1d7a187SSimon Glass }; 167b1d7a187SSimon Glass 16844abe47dSHung-Te Lin /* 16944abe47dSHung-Te Lin * Scan key code to ANSI 3.64 escape sequence table. This table is 17044abe47dSHung-Te Lin * incomplete in that it does not include all possible extra keys. 17144abe47dSHung-Te Lin */ 17244abe47dSHung-Te Lin static struct { 17344abe47dSHung-Te Lin int kbd_scan_code; 17444abe47dSHung-Te Lin char *escape; 17544abe47dSHung-Te Lin } kbd_to_ansi364[] = { 17644abe47dSHung-Te Lin { KEY_UP, "\033[A"}, 17744abe47dSHung-Te Lin { KEY_DOWN, "\033[B"}, 17844abe47dSHung-Te Lin { KEY_RIGHT, "\033[C"}, 17944abe47dSHung-Te Lin { KEY_LEFT, "\033[D"}, 18044abe47dSHung-Te Lin }; 18144abe47dSHung-Te Lin 18244abe47dSHung-Te Lin /* Maximum number of output characters that an ANSI sequence expands to */ 18344abe47dSHung-Te Lin #define ANSI_CHAR_MAX 3 1849bc590e5SSimon Glass 185c14e94e5SKim Phillips static int input_queue_ascii(struct input_config *config, int ch) 1869bc590e5SSimon Glass { 1879bc590e5SSimon Glass if (config->fifo_in + 1 == INPUT_BUFFER_LEN) { 1889bc590e5SSimon Glass if (!config->fifo_out) 1899bc590e5SSimon Glass return -1; /* buffer full */ 1909bc590e5SSimon Glass else 1919bc590e5SSimon Glass config->fifo_in = 0; 1929bc590e5SSimon Glass } else { 1939bc590e5SSimon Glass if (config->fifo_in + 1 == config->fifo_out) 1949bc590e5SSimon Glass return -1; /* buffer full */ 1959bc590e5SSimon Glass config->fifo_in++; 1969bc590e5SSimon Glass } 1973a85e436SSimon Glass debug(" {%02x} ", ch); 1989bc590e5SSimon Glass config->fifo[config->fifo_in] = (uchar)ch; 1999bc590e5SSimon Glass 2009bc590e5SSimon Glass return 0; 2019bc590e5SSimon Glass } 2029bc590e5SSimon Glass 2039bc590e5SSimon Glass int input_tstc(struct input_config *config) 2049bc590e5SSimon Glass { 2059bc590e5SSimon Glass if (config->fifo_in == config->fifo_out && config->read_keys) { 2069bc590e5SSimon Glass if (!(*config->read_keys)(config)) 2079bc590e5SSimon Glass return 0; 2089bc590e5SSimon Glass } 2099bc590e5SSimon Glass return config->fifo_in != config->fifo_out; 2109bc590e5SSimon Glass } 2119bc590e5SSimon Glass 2129bc590e5SSimon Glass int input_getc(struct input_config *config) 2139bc590e5SSimon Glass { 2149bc590e5SSimon Glass int err = 0; 2159bc590e5SSimon Glass 2169bc590e5SSimon Glass while (config->fifo_in == config->fifo_out) { 2179bc590e5SSimon Glass if (config->read_keys) 2189bc590e5SSimon Glass err = (*config->read_keys)(config); 2199bc590e5SSimon Glass if (err) 2209bc590e5SSimon Glass return -1; 2219bc590e5SSimon Glass } 2229bc590e5SSimon Glass 2239bc590e5SSimon Glass if (++config->fifo_out == INPUT_BUFFER_LEN) 2249bc590e5SSimon Glass config->fifo_out = 0; 2259bc590e5SSimon Glass 2269bc590e5SSimon Glass return config->fifo[config->fifo_out]; 2279bc590e5SSimon Glass } 2289bc590e5SSimon Glass 2299bc590e5SSimon Glass /** 2309bc590e5SSimon Glass * Process a modifier/special key press or release and decide which key 2319bc590e5SSimon Glass * translation array should be used as a result. 2329bc590e5SSimon Glass * 2339bc590e5SSimon Glass * TODO: Should keep track of modifier press/release 2349bc590e5SSimon Glass * 2359bc590e5SSimon Glass * @param config Input state 2369bc590e5SSimon Glass * @param key Key code to process 2379bc590e5SSimon Glass * @param release 0 if a press, 1 if a release 2389bc590e5SSimon Glass * @return pointer to keycode->ascii translation table that should be used 2399bc590e5SSimon Glass */ 2409bc590e5SSimon Glass static struct input_key_xlate *process_modifier(struct input_config *config, 2419bc590e5SSimon Glass int key, int release) 2429bc590e5SSimon Glass { 243cd810918SBin Meng #ifdef CONFIG_DM_KEYBOARD 244cd810918SBin Meng struct udevice *dev = config->dev; 245cd810918SBin Meng struct keyboard_ops *ops = keyboard_get_ops(dev); 246cd810918SBin Meng #endif 2479bc590e5SSimon Glass struct input_key_xlate *table; 2489bc590e5SSimon Glass int i; 2499bc590e5SSimon Glass 2509bc590e5SSimon Glass /* Start with the main table, and see what modifiers change it */ 2519bc590e5SSimon Glass assert(config->num_tables > 0); 2529bc590e5SSimon Glass table = &config->table[0]; 2539bc590e5SSimon Glass for (i = 1; i < config->num_tables; i++) { 2549bc590e5SSimon Glass struct input_key_xlate *tab = &config->table[i]; 2559bc590e5SSimon Glass 2569bc590e5SSimon Glass if (key == tab->left_keycode || key == tab->right_keycode) 2579bc590e5SSimon Glass table = tab; 2589bc590e5SSimon Glass } 2599bc590e5SSimon Glass 2609bc590e5SSimon Glass /* Handle the lighted keys */ 2619bc590e5SSimon Glass if (!release) { 262a683d0d3SSimon Glass int flip = -1; 263a683d0d3SSimon Glass 2649bc590e5SSimon Glass switch (key) { 2659bc590e5SSimon Glass case KEY_SCROLLLOCK: 2669bc590e5SSimon Glass flip = FLAG_SCROLL_LOCK; 2679bc590e5SSimon Glass break; 2689bc590e5SSimon Glass case KEY_NUMLOCK: 2699bc590e5SSimon Glass flip = FLAG_NUM_LOCK; 2709bc590e5SSimon Glass break; 2719bc590e5SSimon Glass case KEY_CAPSLOCK: 2729bc590e5SSimon Glass flip = FLAG_CAPS_LOCK; 2739bc590e5SSimon Glass break; 2749bc590e5SSimon Glass } 2759bc590e5SSimon Glass 2769bc590e5SSimon Glass if (flip != -1) { 2779bc590e5SSimon Glass int leds = 0; 2789bc590e5SSimon Glass 279533c81a9SBin Meng config->flags ^= flip; 2809bc590e5SSimon Glass if (config->flags & FLAG_NUM_LOCK) 2819bc590e5SSimon Glass leds |= INPUT_LED_NUM; 2829bc590e5SSimon Glass if (config->flags & FLAG_CAPS_LOCK) 2839bc590e5SSimon Glass leds |= INPUT_LED_CAPS; 2849bc590e5SSimon Glass if (config->flags & FLAG_SCROLL_LOCK) 2859bc590e5SSimon Glass leds |= INPUT_LED_SCROLL; 2869bc590e5SSimon Glass config->leds = leds; 2873b5f6f50SSimon Glass config->leds_changed = flip; 288cd810918SBin Meng 289cd810918SBin Meng #ifdef CONFIG_DM_KEYBOARD 290cd810918SBin Meng if (ops->update_leds) { 291cd810918SBin Meng if (ops->update_leds(dev, config->leds)) 292cd810918SBin Meng debug("Update keyboard's LED failed\n"); 293cd810918SBin Meng } 294cd810918SBin Meng #endif 2959bc590e5SSimon Glass } 296a683d0d3SSimon Glass } 2979bc590e5SSimon Glass 2989bc590e5SSimon Glass return table; 2999bc590e5SSimon Glass } 3009bc590e5SSimon Glass 3019bc590e5SSimon Glass /** 3029bc590e5SSimon Glass * Search an int array for a key value 3039bc590e5SSimon Glass * 3049bc590e5SSimon Glass * @param array Array to search 3059bc590e5SSimon Glass * @param count Number of elements in array 3069bc590e5SSimon Glass * @param key Key value to find 3079bc590e5SSimon Glass * @return element where value was first found, -1 if none 3089bc590e5SSimon Glass */ 3099bc590e5SSimon Glass static int array_search(int *array, int count, int key) 3109bc590e5SSimon Glass { 3119bc590e5SSimon Glass int i; 3129bc590e5SSimon Glass 3139bc590e5SSimon Glass for (i = 0; i < count; i++) { 3149bc590e5SSimon Glass if (array[i] == key) 3159bc590e5SSimon Glass return i; 3169bc590e5SSimon Glass } 3179bc590e5SSimon Glass 3189bc590e5SSimon Glass return -1; 3199bc590e5SSimon Glass } 3209bc590e5SSimon Glass 3219bc590e5SSimon Glass /** 3229bc590e5SSimon Glass * Sort an array so that those elements that exist in the ordering are 3239bc590e5SSimon Glass * first in the array, and in the same order as the ordering. The algorithm 3249bc590e5SSimon Glass * is O(count * ocount) and designed for small arrays. 3259bc590e5SSimon Glass * 3269bc590e5SSimon Glass * TODO: Move this to common / lib? 3279bc590e5SSimon Glass * 3289bc590e5SSimon Glass * @param dest Array with elements to sort, also destination array 3299bc590e5SSimon Glass * @param count Number of elements to sort 3309bc590e5SSimon Glass * @param order Array containing ordering elements 3319bc590e5SSimon Glass * @param ocount Number of ordering elements 3329bc590e5SSimon Glass * @return number of elements in dest that are in order (these will be at the 3339bc590e5SSimon Glass * start of dest). 3349bc590e5SSimon Glass */ 3359bc590e5SSimon Glass static int sort_array_by_ordering(int *dest, int count, int *order, 3369bc590e5SSimon Glass int ocount) 3379bc590e5SSimon Glass { 3389bc590e5SSimon Glass int temp[count]; 3399bc590e5SSimon Glass int dest_count; 3409bc590e5SSimon Glass int same; /* number of elements which are the same */ 3419bc590e5SSimon Glass int i; 3429bc590e5SSimon Glass 3439bc590e5SSimon Glass /* setup output items, copy items to be sorted into our temp area */ 3449bc590e5SSimon Glass memcpy(temp, dest, count * sizeof(*dest)); 3459bc590e5SSimon Glass dest_count = 0; 3469bc590e5SSimon Glass 3479bc590e5SSimon Glass /* work through the ordering, move over the elements we agree on */ 3489bc590e5SSimon Glass for (i = 0; i < ocount; i++) { 3499bc590e5SSimon Glass if (array_search(temp, count, order[i]) != -1) 3509bc590e5SSimon Glass dest[dest_count++] = order[i]; 3519bc590e5SSimon Glass } 3529bc590e5SSimon Glass same = dest_count; 3539bc590e5SSimon Glass 3549bc590e5SSimon Glass /* now move over the elements that are not in the ordering */ 3559bc590e5SSimon Glass for (i = 0; i < count; i++) { 3569bc590e5SSimon Glass if (array_search(order, ocount, temp[i]) == -1) 3579bc590e5SSimon Glass dest[dest_count++] = temp[i]; 3589bc590e5SSimon Glass } 3599bc590e5SSimon Glass assert(dest_count == count); 3609bc590e5SSimon Glass return same; 3619bc590e5SSimon Glass } 3629bc590e5SSimon Glass 3639bc590e5SSimon Glass /** 3649bc590e5SSimon Glass * Check a list of key codes against the previous key scan 3659bc590e5SSimon Glass * 3669bc590e5SSimon Glass * Given a list of new key codes, we check how many of these are the same 3679bc590e5SSimon Glass * as last time. 3689bc590e5SSimon Glass * 3699bc590e5SSimon Glass * @param config Input state 3709bc590e5SSimon Glass * @param keycode List of key codes to examine 3719bc590e5SSimon Glass * @param num_keycodes Number of key codes 3729bc590e5SSimon Glass * @param same Returns number of key codes which are the same 3739bc590e5SSimon Glass */ 3749bc590e5SSimon Glass static int input_check_keycodes(struct input_config *config, 3759bc590e5SSimon Glass int keycode[], int num_keycodes, int *same) 3769bc590e5SSimon Glass { 3779bc590e5SSimon Glass /* Select the 'plain' xlate table to start with */ 3789bc590e5SSimon Glass if (!config->num_tables) { 3799bc590e5SSimon Glass debug("%s: No xlate tables: cannot decode keys\n", __func__); 3809bc590e5SSimon Glass return -1; 3819bc590e5SSimon Glass } 3829bc590e5SSimon Glass 3839bc590e5SSimon Glass /* sort the keycodes into the same order as the previous ones */ 3849bc590e5SSimon Glass *same = sort_array_by_ordering(keycode, num_keycodes, 3859bc590e5SSimon Glass config->prev_keycodes, config->num_prev_keycodes); 3869bc590e5SSimon Glass 3879bc590e5SSimon Glass memcpy(config->prev_keycodes, keycode, num_keycodes * sizeof(int)); 3889bc590e5SSimon Glass config->num_prev_keycodes = num_keycodes; 3899bc590e5SSimon Glass 3909bc590e5SSimon Glass return *same != num_keycodes; 3919bc590e5SSimon Glass } 3929bc590e5SSimon Glass 3939bc590e5SSimon Glass /** 39444abe47dSHung-Te Lin * Checks and converts a special key code into ANSI 3.64 escape sequence. 39544abe47dSHung-Te Lin * 39644abe47dSHung-Te Lin * @param config Input state 39744abe47dSHung-Te Lin * @param keycode Key code to examine 39844abe47dSHung-Te Lin * @param output_ch Buffer to place output characters into. It should 39944abe47dSHung-Te Lin * be at least ANSI_CHAR_MAX bytes long, to allow for 40044abe47dSHung-Te Lin * an ANSI sequence. 40144abe47dSHung-Te Lin * @param max_chars Maximum number of characters to add to output_ch 40244abe47dSHung-Te Lin * @return number of characters output, if the key was converted, otherwise 0. 40344abe47dSHung-Te Lin * This may be larger than max_chars, in which case the overflow 40444abe47dSHung-Te Lin * characters are not output. 40544abe47dSHung-Te Lin */ 40644abe47dSHung-Te Lin static int input_keycode_to_ansi364(struct input_config *config, 40744abe47dSHung-Te Lin int keycode, char output_ch[], int max_chars) 40844abe47dSHung-Te Lin { 40944abe47dSHung-Te Lin const char *escape; 41044abe47dSHung-Te Lin int ch_count; 41144abe47dSHung-Te Lin int i; 41244abe47dSHung-Te Lin 41344abe47dSHung-Te Lin for (i = ch_count = 0; i < ARRAY_SIZE(kbd_to_ansi364); i++) { 41444abe47dSHung-Te Lin if (keycode != kbd_to_ansi364[i].kbd_scan_code) 41544abe47dSHung-Te Lin continue; 41644abe47dSHung-Te Lin for (escape = kbd_to_ansi364[i].escape; *escape; escape++) { 41744abe47dSHung-Te Lin if (ch_count < max_chars) 41844abe47dSHung-Te Lin output_ch[ch_count] = *escape; 41944abe47dSHung-Te Lin ch_count++; 42044abe47dSHung-Te Lin } 42144abe47dSHung-Te Lin return ch_count; 42244abe47dSHung-Te Lin } 42344abe47dSHung-Te Lin 42444abe47dSHung-Te Lin return 0; 42544abe47dSHung-Te Lin } 42644abe47dSHung-Te Lin 42744abe47dSHung-Te Lin /** 42844abe47dSHung-Te Lin * Converts and queues a list of key codes in escaped ASCII string form 4299bc590e5SSimon Glass * Convert a list of key codes into ASCII 4309bc590e5SSimon Glass * 4319bc590e5SSimon Glass * You must call input_check_keycodes() before this. It turns the keycode 43244abe47dSHung-Te Lin * list into a list of ASCII characters and sends them to the input layer. 4339bc590e5SSimon Glass * 4349bc590e5SSimon Glass * Characters which were seen last time do not generate fresh ASCII output. 43544abe47dSHung-Te Lin * The output (calls to queue_ascii) may be longer than num_keycodes, if the 43644abe47dSHung-Te Lin * keycode contains special keys that was encoded to longer escaped sequence. 4379bc590e5SSimon Glass * 4389bc590e5SSimon Glass * @param config Input state 4399bc590e5SSimon Glass * @param keycode List of key codes to examine 4409bc590e5SSimon Glass * @param num_keycodes Number of key codes 44144abe47dSHung-Te Lin * @param output_ch Buffer to place output characters into. It should 44244abe47dSHung-Te Lin * be at last ANSI_CHAR_MAX * num_keycodes, to allow for 44344abe47dSHung-Te Lin * ANSI sequences. 44444abe47dSHung-Te Lin * @param max_chars Maximum number of characters to add to output_ch 4459bc590e5SSimon Glass * @param same Number of key codes which are the same 44644abe47dSHung-Te Lin * @return number of characters written into output_ch, or -1 if we would 44744abe47dSHung-Te Lin * exceed max_chars chars. 4489bc590e5SSimon Glass */ 4499bc590e5SSimon Glass static int input_keycodes_to_ascii(struct input_config *config, 45044abe47dSHung-Te Lin int keycode[], int num_keycodes, char output_ch[], 45144abe47dSHung-Te Lin int max_chars, int same) 4529bc590e5SSimon Glass { 4539bc590e5SSimon Glass struct input_key_xlate *table; 45444abe47dSHung-Te Lin int ch_count = 0; 4559bc590e5SSimon Glass int i; 4569bc590e5SSimon Glass 4579bc590e5SSimon Glass table = &config->table[0]; 4589bc590e5SSimon Glass 4599bc590e5SSimon Glass /* deal with modifiers first */ 4609bc590e5SSimon Glass for (i = 0; i < num_keycodes; i++) { 4619bc590e5SSimon Glass int key = keycode[i] & KEY_MASK; 4629bc590e5SSimon Glass 4639bc590e5SSimon Glass if (key >= table->num_entries || table->xlate[key] == 0xff) { 4649bc590e5SSimon Glass table = process_modifier(config, key, 4659bc590e5SSimon Glass keycode[i] & KEY_RELEASE); 4669bc590e5SSimon Glass } 4679bc590e5SSimon Glass } 4689bc590e5SSimon Glass 46944abe47dSHung-Te Lin /* Start conversion by looking for the first new keycode (by same). */ 47044abe47dSHung-Te Lin for (i = same; i < num_keycodes; i++) { 4719bc590e5SSimon Glass int key = keycode[i]; 472ba420342SSimon Glass int ch; 4739bc590e5SSimon Glass 47444abe47dSHung-Te Lin /* 47544abe47dSHung-Te Lin * For a normal key (with an ASCII value), add it; otherwise 47644abe47dSHung-Te Lin * translate special key to escape sequence if possible. 47744abe47dSHung-Te Lin */ 478ba420342SSimon Glass if (key < table->num_entries) { 479ba420342SSimon Glass ch = table->xlate[key]; 480ba420342SSimon Glass if ((config->flags & FLAG_CAPS_LOCK) && 481ba420342SSimon Glass ch >= 'a' && ch <= 'z') 482ba420342SSimon Glass ch -= 'a' - 'A'; 483e5f330c4SBin Meng /* ban digit numbers if 'Num Lock' is not on */ 484e5f330c4SBin Meng if (!(config->flags & FLAG_NUM_LOCK)) { 485e5f330c4SBin Meng if (key >= KEY_KP7 && key <= KEY_KPDOT && 486e5f330c4SBin Meng key != KEY_KPMINUS && key != KEY_KPPLUS) 487e5f330c4SBin Meng ch = 0xff; 488e5f330c4SBin Meng } 489ba420342SSimon Glass if (ch_count < max_chars && ch != 0xff) 490ba420342SSimon Glass output_ch[ch_count++] = (uchar)ch; 49144abe47dSHung-Te Lin } else { 49244abe47dSHung-Te Lin ch_count += input_keycode_to_ansi364(config, key, 49344abe47dSHung-Te Lin output_ch, max_chars); 4949bc590e5SSimon Glass } 4959bc590e5SSimon Glass } 4969bc590e5SSimon Glass 49744abe47dSHung-Te Lin if (ch_count > max_chars) { 49844abe47dSHung-Te Lin debug("%s: Output char buffer overflow size=%d, need=%d\n", 49944abe47dSHung-Te Lin __func__, max_chars, ch_count); 50044abe47dSHung-Te Lin return -1; 50144abe47dSHung-Te Lin } 50244abe47dSHung-Te Lin 5039bc590e5SSimon Glass /* ok, so return keys */ 5049bc590e5SSimon Glass return ch_count; 5059bc590e5SSimon Glass } 5069bc590e5SSimon Glass 5073a85e436SSimon Glass static int _input_send_keycodes(struct input_config *config, int keycode[], 5083a85e436SSimon Glass int num_keycodes, bool do_send) 5099bc590e5SSimon Glass { 51044abe47dSHung-Te Lin char ch[num_keycodes * ANSI_CHAR_MAX]; 5119bc590e5SSimon Glass int count, i, same = 0; 5129bc590e5SSimon Glass int is_repeat = 0; 5139bc590e5SSimon Glass unsigned delay_ms; 5149bc590e5SSimon Glass 5159bc590e5SSimon Glass config->modifiers = 0; 5169bc590e5SSimon Glass if (!input_check_keycodes(config, keycode, num_keycodes, &same)) { 5179bc590e5SSimon Glass /* 5189bc590e5SSimon Glass * Same as last time - is it time for another repeat? 5199bc590e5SSimon Glass * TODO(sjg@chromium.org) We drop repeats here and since 5209bc590e5SSimon Glass * the caller may not call in again for a while, our 5219bc590e5SSimon Glass * auto-repeat speed is not quite correct. We should 5229bc590e5SSimon Glass * insert another character if we later realise that we 5239bc590e5SSimon Glass * have missed a repeat slot. 5249bc590e5SSimon Glass */ 5250b186c08SSimon Glass is_repeat = config->allow_repeats || (config->repeat_rate_ms && 5260b186c08SSimon Glass (int)get_timer(config->next_repeat_ms) >= 0); 5279bc590e5SSimon Glass if (!is_repeat) 5289bc590e5SSimon Glass return 0; 5299bc590e5SSimon Glass } 5309bc590e5SSimon Glass 5319bc590e5SSimon Glass count = input_keycodes_to_ascii(config, keycode, num_keycodes, 53244abe47dSHung-Te Lin ch, sizeof(ch), is_repeat ? 0 : same); 5333a85e436SSimon Glass if (do_send) { 5349bc590e5SSimon Glass for (i = 0; i < count; i++) 5359bc590e5SSimon Glass input_queue_ascii(config, ch[i]); 5363a85e436SSimon Glass } 5379bc590e5SSimon Glass delay_ms = is_repeat ? 5389bc590e5SSimon Glass config->repeat_rate_ms : 5399bc590e5SSimon Glass config->repeat_delay_ms; 5409bc590e5SSimon Glass 5419bc590e5SSimon Glass config->next_repeat_ms = get_timer(0) + delay_ms; 54244abe47dSHung-Te Lin 54344abe47dSHung-Te Lin return count; 5449bc590e5SSimon Glass } 5459bc590e5SSimon Glass 5463a85e436SSimon Glass int input_send_keycodes(struct input_config *config, int keycode[], 5473a85e436SSimon Glass int num_keycodes) 5483a85e436SSimon Glass { 5493a85e436SSimon Glass return _input_send_keycodes(config, keycode, num_keycodes, true); 5503a85e436SSimon Glass } 5513a85e436SSimon Glass 5523a85e436SSimon Glass int input_add_keycode(struct input_config *config, int new_keycode, 5533a85e436SSimon Glass bool release) 5543a85e436SSimon Glass { 5553a85e436SSimon Glass int keycode[INPUT_MAX_MODIFIERS + 1]; 5563a85e436SSimon Glass int count, i; 5573a85e436SSimon Glass 5583a85e436SSimon Glass /* Add the old keycodes which are not removed by this new one */ 5593a85e436SSimon Glass for (i = 0, count = 0; i < config->num_prev_keycodes; i++) { 5603a85e436SSimon Glass int code = config->prev_keycodes[i]; 5613a85e436SSimon Glass 5623a85e436SSimon Glass if (new_keycode == code) { 5633a85e436SSimon Glass if (release) 5643a85e436SSimon Glass continue; 5653a85e436SSimon Glass new_keycode = -1; 5663a85e436SSimon Glass } 5673a85e436SSimon Glass keycode[count++] = code; 5683a85e436SSimon Glass } 5693a85e436SSimon Glass 5703a85e436SSimon Glass if (!release && new_keycode != -1) 5713a85e436SSimon Glass keycode[count++] = new_keycode; 5723a85e436SSimon Glass debug("\ncodes for %02x/%d: ", new_keycode, release); 5733a85e436SSimon Glass for (i = 0; i < count; i++) 5743a85e436SSimon Glass debug("%02x ", keycode[i]); 5753a85e436SSimon Glass debug("\n"); 5763a85e436SSimon Glass 5773a85e436SSimon Glass /* Don't output any ASCII characters if this is a key release */ 5783a85e436SSimon Glass return _input_send_keycodes(config, keycode, count, !release); 5793a85e436SSimon Glass } 5803a85e436SSimon Glass 5819bc590e5SSimon Glass int input_add_table(struct input_config *config, int left_keycode, 5829bc590e5SSimon Glass int right_keycode, const uchar *xlate, int num_entries) 5839bc590e5SSimon Glass { 5849bc590e5SSimon Glass struct input_key_xlate *table; 5859bc590e5SSimon Glass 5869bc590e5SSimon Glass if (config->num_tables == INPUT_MAX_MODIFIERS) { 5879bc590e5SSimon Glass debug("%s: Too many modifier tables\n", __func__); 5889bc590e5SSimon Glass return -1; 5899bc590e5SSimon Glass } 5909bc590e5SSimon Glass 5919bc590e5SSimon Glass table = &config->table[config->num_tables++]; 5929bc590e5SSimon Glass table->left_keycode = left_keycode; 5939bc590e5SSimon Glass table->right_keycode = right_keycode; 5949bc590e5SSimon Glass table->xlate = xlate; 5959bc590e5SSimon Glass table->num_entries = num_entries; 5969bc590e5SSimon Glass 5979bc590e5SSimon Glass return 0; 5989bc590e5SSimon Glass } 5999bc590e5SSimon Glass 6001b1d3e64SSimon Glass void input_set_delays(struct input_config *config, int repeat_delay_ms, 6019bc590e5SSimon Glass int repeat_rate_ms) 6029bc590e5SSimon Glass { 6031b1d3e64SSimon Glass config->repeat_delay_ms = repeat_delay_ms; 6041b1d3e64SSimon Glass config->repeat_rate_ms = repeat_rate_ms; 6051b1d3e64SSimon Glass } 6061b1d3e64SSimon Glass 6070b186c08SSimon Glass void input_allow_repeats(struct input_config *config, bool allow_repeats) 6080b186c08SSimon Glass { 6090b186c08SSimon Glass config->allow_repeats = allow_repeats; 6100b186c08SSimon Glass } 6110b186c08SSimon Glass 6123b5f6f50SSimon Glass int input_leds_changed(struct input_config *config) 6133b5f6f50SSimon Glass { 6143b5f6f50SSimon Glass if (config->leds_changed) 6153b5f6f50SSimon Glass return config->leds; 6163b5f6f50SSimon Glass 6173b5f6f50SSimon Glass return -1; 6183b5f6f50SSimon Glass } 6193b5f6f50SSimon Glass 620b1d7a187SSimon Glass int input_add_tables(struct input_config *config, bool german) 62166877b0fSSimon Glass { 622b1d7a187SSimon Glass struct kbd_entry *entry; 623b1d7a187SSimon Glass int mask; 62466877b0fSSimon Glass int ret; 62566877b0fSSimon Glass 626b1d7a187SSimon Glass mask = german ? KBD_GERMAN : KBD_ENGLISH; 627b1d7a187SSimon Glass for (entry = kbd_entry; entry->kbd_mask; entry++) { 628b1d7a187SSimon Glass if (!(mask & entry->kbd_mask)) 629b1d7a187SSimon Glass continue; 630b1d7a187SSimon Glass ret = input_add_table(config, entry->left_keycode, 631b1d7a187SSimon Glass entry->right_keycode, entry->xlate, 632b1d7a187SSimon Glass entry->num_entries); 63366877b0fSSimon Glass if (ret) 63466877b0fSSimon Glass return ret; 635b1d7a187SSimon Glass } 63666877b0fSSimon Glass 637b1d7a187SSimon Glass return 0; 63866877b0fSSimon Glass } 63966877b0fSSimon Glass 6401b1d3e64SSimon Glass int input_init(struct input_config *config, int leds) 6411b1d3e64SSimon Glass { 6429bc590e5SSimon Glass memset(config, '\0', sizeof(*config)); 6439bc590e5SSimon Glass config->leds = leds; 6449bc590e5SSimon Glass 6459bc590e5SSimon Glass return 0; 6469bc590e5SSimon Glass } 6479bc590e5SSimon Glass 6489bc590e5SSimon Glass int input_stdio_register(struct stdio_dev *dev) 6499bc590e5SSimon Glass { 6509bc590e5SSimon Glass int error; 6519bc590e5SSimon Glass 6529bc590e5SSimon Glass error = stdio_register(dev); 6539bc590e5SSimon Glass 6549bc590e5SSimon Glass /* check if this is the standard input device */ 655*00caae6dSSimon Glass if (!error && strcmp(env_get("stdin"), dev->name) == 0) { 6569bc590e5SSimon Glass /* reassign the console */ 6579bc590e5SSimon Glass if (OVERWRITE_CONSOLE || 6589bc590e5SSimon Glass console_assign(stdin, dev->name)) 6599bc590e5SSimon Glass return -1; 6609bc590e5SSimon Glass } 6619bc590e5SSimon Glass 6629bc590e5SSimon Glass return 0; 6639bc590e5SSimon Glass } 664