xref: /rk3399_rockchip-uboot/drivers/input/input.c (revision 1b1d3e6461e9195f825d6d8aa6a2a0e1e3188f62)
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  *
79bc590e5SSimon Glass  * See file CREDITS for list of people who contributed to this
89bc590e5SSimon Glass  * project.
99bc590e5SSimon Glass  *
109bc590e5SSimon Glass  * This program is free software; you can redistribute it and/or
119bc590e5SSimon Glass  * modify it under the terms of the GNU General Public License as
129bc590e5SSimon Glass  * published by the Free Software Foundation; either version 2 of
139bc590e5SSimon Glass  * the License, or (at your option) any later version.
149bc590e5SSimon Glass  *
159bc590e5SSimon Glass  * This program is distributed in the hope that it will be useful,
169bc590e5SSimon Glass  * but WITHOUT ANY WARRANTY; without even the implied warranty of
179bc590e5SSimon Glass  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
189bc590e5SSimon Glass  * GNU General Public License for more details.
199bc590e5SSimon Glass  *
209bc590e5SSimon Glass  * You should have received a copy of the GNU General Public License
219bc590e5SSimon Glass  * along with this program; if not, write to the Free Software
229bc590e5SSimon Glass  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
239bc590e5SSimon Glass  * MA 02111-1307 USA
249bc590e5SSimon Glass  */
259bc590e5SSimon Glass 
269bc590e5SSimon Glass #include <common.h>
279bc590e5SSimon Glass #include <stdio_dev.h>
289bc590e5SSimon Glass #include <input.h>
299bc590e5SSimon Glass #include <linux/input.h>
309bc590e5SSimon Glass 
319bc590e5SSimon Glass enum {
329bc590e5SSimon Glass 	/* These correspond to the lights on the keyboard */
339bc590e5SSimon Glass 	FLAG_NUM_LOCK		= 1 << 0,
349bc590e5SSimon Glass 	FLAG_CAPS_LOCK		= 1 << 1,
359bc590e5SSimon Glass 	FLAG_SCROLL_LOCK	= 1 << 2,
369bc590e5SSimon Glass 
379bc590e5SSimon Glass 	/* Special flag ORed with key code to indicate release */
389bc590e5SSimon Glass 	KEY_RELEASE		= 1 << 15,
399bc590e5SSimon Glass 	KEY_MASK		= 0xfff,
409bc590e5SSimon Glass };
419bc590e5SSimon Glass 
429bc590e5SSimon Glass /*
439bc590e5SSimon Glass  * These takes map key codes to ASCII. 0xff means no key, or special key.
449bc590e5SSimon Glass  * Three tables are provided - one for plain keys, one for when the shift
459bc590e5SSimon Glass  * 'modifier' key is pressed and one for when the ctrl modifier key is
469bc590e5SSimon Glass  * pressed.
479bc590e5SSimon Glass  */
489bc590e5SSimon Glass static const uchar kbd_plain_xlate[] = {
499bc590e5SSimon Glass 	0xff, 0x1b, '1',  '2',  '3',  '4',  '5',  '6',
509bc590e5SSimon Glass 	'7',  '8',  '9',  '0',  '-',  '=', '\b', '\t',	/* 0x00 - 0x0f */
519bc590e5SSimon Glass 	'q',  'w',  'e',  'r',  't',  'y',  'u',  'i',
529bc590e5SSimon Glass 	'o',  'p',  '[',  ']', '\r', 0xff,  'a',  's',  /* 0x10 - 0x1f */
539bc590e5SSimon Glass 	'd',  'f',  'g',  'h',  'j',  'k',  'l',  ';',
549bc590e5SSimon Glass 	'\'',  '`', 0xff, '\\', 'z',  'x',  'c',  'v',	/* 0x20 - 0x2f */
559bc590e5SSimon Glass 	'b',  'n',  'm',  ',' ,  '.', '/', 0xff, 0xff, 0xff,
569bc590e5SSimon Glass 	' ', 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,	/* 0x30 - 0x3f */
579bc590e5SSimon Glass 	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,  '7',
589bc590e5SSimon Glass 	'8',  '9',  '-',  '4',  '5',  '6',  '+',  '1',	/* 0x40 - 0x4f */
599bc590e5SSimon Glass 	'2',  '3',  '0',  '.', 0xff, 0xff, 0xff, 0xff,
609bc590e5SSimon Glass 	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,	/* 0x50 - 0x5F */
619bc590e5SSimon Glass 	'\r', 0xff, 0xff
629bc590e5SSimon Glass };
639bc590e5SSimon Glass 
649bc590e5SSimon Glass static unsigned char kbd_shift_xlate[] = {
659bc590e5SSimon Glass 	0xff, 0x1b, '!', '@', '#', '$', '%', '^',
669bc590e5SSimon Glass 	'&', '*', '(', ')', '_', '+', '\b', '\t',	/* 0x00 - 0x0f */
679bc590e5SSimon Glass 	'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I',
689bc590e5SSimon Glass 	'O', 'P', '{', '}', '\r', 0xff, 'A', 'S',	/* 0x10 - 0x1f */
699bc590e5SSimon Glass 	'D', 'F', 'G', 'H', 'J', 'K', 'L', ':',
709bc590e5SSimon Glass 	'"', '~', 0xff, '|', 'Z', 'X', 'C', 'V',	/* 0x20 - 0x2f */
719bc590e5SSimon Glass 	'B', 'N', 'M', '<', '>', '?', 0xff, 0xff, 0xff,
729bc590e5SSimon Glass 	' ', 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,	/* 0x30 - 0x3f */
739bc590e5SSimon Glass 	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, '7',
749bc590e5SSimon Glass 	'8', '9', '-', '4', '5', '6', '+', '1',	/* 0x40 - 0x4f */
759bc590e5SSimon Glass 	'2', '3', '0', '.', 0xff, 0xff, 0xff, 0xff, 0xff,
769bc590e5SSimon Glass 	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,	/* 0x50 - 0x5F */
779bc590e5SSimon Glass 	'\r', 0xff, 0xff
789bc590e5SSimon Glass };
799bc590e5SSimon Glass 
809bc590e5SSimon Glass static unsigned char kbd_ctrl_xlate[] = {
819bc590e5SSimon Glass 	0xff, 0x1b, '1', 0x00, '3', '4', '5', 0x1E,
829bc590e5SSimon Glass 	'7', '8', '9', '0', 0x1F, '=', '\b', '\t',	/* 0x00 - 0x0f */
839bc590e5SSimon Glass 	0x11, 0x17, 0x05, 0x12, 0x14, 0x18, 0x15, 0x09,
849bc590e5SSimon Glass 	0x0f, 0x10, 0x1b, 0x1d, '\n', 0xff, 0x01, 0x13,	/* 0x10 - 0x1f */
859bc590e5SSimon Glass 	0x04, 0x06, 0x08, 0x09, 0x0a, 0x0b, 0x0c, ';',
869bc590e5SSimon Glass 	'\'', '~', 0x00, 0x1c, 0x1a, 0x18, 0x03, 0x16,	/* 0x20 - 0x2f */
879bc590e5SSimon Glass 	0x02, 0x0e, 0x0d, '<', '>', '?', 0xff, 0xff,
889bc590e5SSimon Glass 	0xff, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,	/* 0x30 - 0x3f */
899bc590e5SSimon Glass 	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, '7',
909bc590e5SSimon Glass 	'8', '9', '-', '4', '5', '6', '+', '1',		/* 0x40 - 0x4f */
919bc590e5SSimon Glass 	'2', '3', '0', '.', 0xff, 0xff, 0xff, 0xff,
929bc590e5SSimon Glass 	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,	/* 0x50 - 0x5F */
939bc590e5SSimon Glass 	'\r', 0xff, 0xff
949bc590e5SSimon Glass };
959bc590e5SSimon Glass 
969bc590e5SSimon Glass 
979bc590e5SSimon Glass int input_queue_ascii(struct input_config *config, int ch)
989bc590e5SSimon Glass {
999bc590e5SSimon Glass 	if (config->fifo_in + 1 == INPUT_BUFFER_LEN) {
1009bc590e5SSimon Glass 		if (!config->fifo_out)
1019bc590e5SSimon Glass 			return -1; /* buffer full */
1029bc590e5SSimon Glass 		else
1039bc590e5SSimon Glass 			config->fifo_in = 0;
1049bc590e5SSimon Glass 	} else {
1059bc590e5SSimon Glass 		if (config->fifo_in + 1 == config->fifo_out)
1069bc590e5SSimon Glass 			return -1; /* buffer full */
1079bc590e5SSimon Glass 		config->fifo_in++;
1089bc590e5SSimon Glass 	}
1099bc590e5SSimon Glass 	config->fifo[config->fifo_in] = (uchar)ch;
1109bc590e5SSimon Glass 
1119bc590e5SSimon Glass 	return 0;
1129bc590e5SSimon Glass }
1139bc590e5SSimon Glass 
1149bc590e5SSimon Glass int input_tstc(struct input_config *config)
1159bc590e5SSimon Glass {
1169bc590e5SSimon Glass 	if (config->fifo_in == config->fifo_out && config->read_keys) {
1179bc590e5SSimon Glass 		if (!(*config->read_keys)(config))
1189bc590e5SSimon Glass 			return 0;
1199bc590e5SSimon Glass 	}
1209bc590e5SSimon Glass 	return config->fifo_in != config->fifo_out;
1219bc590e5SSimon Glass }
1229bc590e5SSimon Glass 
1239bc590e5SSimon Glass int input_getc(struct input_config *config)
1249bc590e5SSimon Glass {
1259bc590e5SSimon Glass 	int err = 0;
1269bc590e5SSimon Glass 
1279bc590e5SSimon Glass 	while (config->fifo_in == config->fifo_out) {
1289bc590e5SSimon Glass 		if (config->read_keys)
1299bc590e5SSimon Glass 			err = (*config->read_keys)(config);
1309bc590e5SSimon Glass 		if (err)
1319bc590e5SSimon Glass 			return -1;
1329bc590e5SSimon Glass 	}
1339bc590e5SSimon Glass 
1349bc590e5SSimon Glass 	if (++config->fifo_out == INPUT_BUFFER_LEN)
1359bc590e5SSimon Glass 		config->fifo_out = 0;
1369bc590e5SSimon Glass 
1379bc590e5SSimon Glass 	return config->fifo[config->fifo_out];
1389bc590e5SSimon Glass }
1399bc590e5SSimon Glass 
1409bc590e5SSimon Glass /**
1419bc590e5SSimon Glass  * Process a modifier/special key press or release and decide which key
1429bc590e5SSimon Glass  * translation array should be used as a result.
1439bc590e5SSimon Glass  *
1449bc590e5SSimon Glass  * TODO: Should keep track of modifier press/release
1459bc590e5SSimon Glass  *
1469bc590e5SSimon Glass  * @param config	Input state
1479bc590e5SSimon Glass  * @param key		Key code to process
1489bc590e5SSimon Glass  * @param release	0 if a press, 1 if a release
1499bc590e5SSimon Glass  * @return pointer to keycode->ascii translation table that should be used
1509bc590e5SSimon Glass  */
1519bc590e5SSimon Glass static struct input_key_xlate *process_modifier(struct input_config *config,
1529bc590e5SSimon Glass 						int key, int release)
1539bc590e5SSimon Glass {
1549bc590e5SSimon Glass 	struct input_key_xlate *table;
1559bc590e5SSimon Glass 	int flip = -1;
1569bc590e5SSimon Glass 	int i;
1579bc590e5SSimon Glass 
1589bc590e5SSimon Glass 	/* Start with the main table, and see what modifiers change it */
1599bc590e5SSimon Glass 	assert(config->num_tables > 0);
1609bc590e5SSimon Glass 	table = &config->table[0];
1619bc590e5SSimon Glass 	for (i = 1; i < config->num_tables; i++) {
1629bc590e5SSimon Glass 		struct input_key_xlate *tab = &config->table[i];
1639bc590e5SSimon Glass 
1649bc590e5SSimon Glass 		if (key == tab->left_keycode || key == tab->right_keycode)
1659bc590e5SSimon Glass 			table = tab;
1669bc590e5SSimon Glass 	}
1679bc590e5SSimon Glass 
1689bc590e5SSimon Glass 	/* Handle the lighted keys */
1699bc590e5SSimon Glass 	if (!release) {
1709bc590e5SSimon Glass 		switch (key) {
1719bc590e5SSimon Glass 		case KEY_SCROLLLOCK:
1729bc590e5SSimon Glass 			flip = FLAG_SCROLL_LOCK;
1739bc590e5SSimon Glass 			break;
1749bc590e5SSimon Glass 		case KEY_NUMLOCK:
1759bc590e5SSimon Glass 			flip = FLAG_NUM_LOCK;
1769bc590e5SSimon Glass 			break;
1779bc590e5SSimon Glass 		case KEY_CAPSLOCK:
1789bc590e5SSimon Glass 			flip = FLAG_CAPS_LOCK;
1799bc590e5SSimon Glass 			break;
1809bc590e5SSimon Glass 		}
1819bc590e5SSimon Glass 	}
1829bc590e5SSimon Glass 
1839bc590e5SSimon Glass 	if (flip != -1) {
1849bc590e5SSimon Glass 		int leds = 0;
1859bc590e5SSimon Glass 
1869bc590e5SSimon Glass 		config->leds ^= flip;
1879bc590e5SSimon Glass 		if (config->flags & FLAG_NUM_LOCK)
1889bc590e5SSimon Glass 			leds |= INPUT_LED_NUM;
1899bc590e5SSimon Glass 		if (config->flags & FLAG_CAPS_LOCK)
1909bc590e5SSimon Glass 			leds |= INPUT_LED_CAPS;
1919bc590e5SSimon Glass 		if (config->flags & FLAG_SCROLL_LOCK)
1929bc590e5SSimon Glass 			leds |= INPUT_LED_SCROLL;
1939bc590e5SSimon Glass 		config->leds = leds;
1949bc590e5SSimon Glass 	}
1959bc590e5SSimon Glass 
1969bc590e5SSimon Glass 	return table;
1979bc590e5SSimon Glass }
1989bc590e5SSimon Glass 
1999bc590e5SSimon Glass /**
2009bc590e5SSimon Glass  * Search an int array for a key value
2019bc590e5SSimon Glass  *
2029bc590e5SSimon Glass  * @param array	Array to search
2039bc590e5SSimon Glass  * @param count	Number of elements in array
2049bc590e5SSimon Glass  * @param key	Key value to find
2059bc590e5SSimon Glass  * @return element where value was first found, -1 if none
2069bc590e5SSimon Glass  */
2079bc590e5SSimon Glass static int array_search(int *array, int count, int key)
2089bc590e5SSimon Glass {
2099bc590e5SSimon Glass 	int i;
2109bc590e5SSimon Glass 
2119bc590e5SSimon Glass 	for (i = 0; i < count; i++) {
2129bc590e5SSimon Glass 		if (array[i] == key)
2139bc590e5SSimon Glass 			return i;
2149bc590e5SSimon Glass 	}
2159bc590e5SSimon Glass 
2169bc590e5SSimon Glass 	return -1;
2179bc590e5SSimon Glass }
2189bc590e5SSimon Glass 
2199bc590e5SSimon Glass /**
2209bc590e5SSimon Glass  * Sort an array so that those elements that exist in the ordering are
2219bc590e5SSimon Glass  * first in the array, and in the same order as the ordering. The algorithm
2229bc590e5SSimon Glass  * is O(count * ocount) and designed for small arrays.
2239bc590e5SSimon Glass  *
2249bc590e5SSimon Glass  * TODO: Move this to common / lib?
2259bc590e5SSimon Glass  *
2269bc590e5SSimon Glass  * @param dest		Array with elements to sort, also destination array
2279bc590e5SSimon Glass  * @param count		Number of elements to sort
2289bc590e5SSimon Glass  * @param order		Array containing ordering elements
2299bc590e5SSimon Glass  * @param ocount	Number of ordering elements
2309bc590e5SSimon Glass  * @return number of elements in dest that are in order (these will be at the
2319bc590e5SSimon Glass  *	start of dest).
2329bc590e5SSimon Glass  */
2339bc590e5SSimon Glass static int sort_array_by_ordering(int *dest, int count, int *order,
2349bc590e5SSimon Glass 				   int ocount)
2359bc590e5SSimon Glass {
2369bc590e5SSimon Glass 	int temp[count];
2379bc590e5SSimon Glass 	int dest_count;
2389bc590e5SSimon Glass 	int same;	/* number of elements which are the same */
2399bc590e5SSimon Glass 	int i;
2409bc590e5SSimon Glass 
2419bc590e5SSimon Glass 	/* setup output items, copy items to be sorted into our temp area */
2429bc590e5SSimon Glass 	memcpy(temp, dest, count * sizeof(*dest));
2439bc590e5SSimon Glass 	dest_count = 0;
2449bc590e5SSimon Glass 
2459bc590e5SSimon Glass 	/* work through the ordering, move over the elements we agree on */
2469bc590e5SSimon Glass 	for (i = 0; i < ocount; i++) {
2479bc590e5SSimon Glass 		if (array_search(temp, count, order[i]) != -1)
2489bc590e5SSimon Glass 			dest[dest_count++] = order[i];
2499bc590e5SSimon Glass 	}
2509bc590e5SSimon Glass 	same = dest_count;
2519bc590e5SSimon Glass 
2529bc590e5SSimon Glass 	/* now move over the elements that are not in the ordering */
2539bc590e5SSimon Glass 	for (i = 0; i < count; i++) {
2549bc590e5SSimon Glass 		if (array_search(order, ocount, temp[i]) == -1)
2559bc590e5SSimon Glass 			dest[dest_count++] = temp[i];
2569bc590e5SSimon Glass 	}
2579bc590e5SSimon Glass 	assert(dest_count == count);
2589bc590e5SSimon Glass 	return same;
2599bc590e5SSimon Glass }
2609bc590e5SSimon Glass 
2619bc590e5SSimon Glass /**
2629bc590e5SSimon Glass  * Check a list of key codes against the previous key scan
2639bc590e5SSimon Glass  *
2649bc590e5SSimon Glass  * Given a list of new key codes, we check how many of these are the same
2659bc590e5SSimon Glass  * as last time.
2669bc590e5SSimon Glass  *
2679bc590e5SSimon Glass  * @param config	Input state
2689bc590e5SSimon Glass  * @param keycode	List of key codes to examine
2699bc590e5SSimon Glass  * @param num_keycodes	Number of key codes
2709bc590e5SSimon Glass  * @param same		Returns number of key codes which are the same
2719bc590e5SSimon Glass  */
2729bc590e5SSimon Glass static int input_check_keycodes(struct input_config *config,
2739bc590e5SSimon Glass 			   int keycode[], int num_keycodes, int *same)
2749bc590e5SSimon Glass {
2759bc590e5SSimon Glass 	/* Select the 'plain' xlate table to start with */
2769bc590e5SSimon Glass 	if (!config->num_tables) {
2779bc590e5SSimon Glass 		debug("%s: No xlate tables: cannot decode keys\n", __func__);
2789bc590e5SSimon Glass 		return -1;
2799bc590e5SSimon Glass 	}
2809bc590e5SSimon Glass 
2819bc590e5SSimon Glass 	/* sort the keycodes into the same order as the previous ones */
2829bc590e5SSimon Glass 	*same = sort_array_by_ordering(keycode, num_keycodes,
2839bc590e5SSimon Glass 			config->prev_keycodes, config->num_prev_keycodes);
2849bc590e5SSimon Glass 
2859bc590e5SSimon Glass 	memcpy(config->prev_keycodes, keycode, num_keycodes * sizeof(int));
2869bc590e5SSimon Glass 	config->num_prev_keycodes = num_keycodes;
2879bc590e5SSimon Glass 
2889bc590e5SSimon Glass 	return *same != num_keycodes;
2899bc590e5SSimon Glass }
2909bc590e5SSimon Glass 
2919bc590e5SSimon Glass /**
2929bc590e5SSimon Glass  * Convert a list of key codes into ASCII
2939bc590e5SSimon Glass  *
2949bc590e5SSimon Glass  * You must call input_check_keycodes() before this. It turns the keycode
2959bc590e5SSimon Glass  * list into a list of ASCII characters which are ready to send to the
2969bc590e5SSimon Glass  * input layer.
2979bc590e5SSimon Glass  *
2989bc590e5SSimon Glass  * Characters which were seen last time do not generate fresh ASCII output.
2999bc590e5SSimon Glass  *
3009bc590e5SSimon Glass  * @param config	Input state
3019bc590e5SSimon Glass  * @param keycode	List of key codes to examine
3029bc590e5SSimon Glass  * @param num_keycodes	Number of key codes
3039bc590e5SSimon Glass  * @param same		Number of key codes which are the same
3049bc590e5SSimon Glass  */
3059bc590e5SSimon Glass static int input_keycodes_to_ascii(struct input_config *config,
3069bc590e5SSimon Glass 		int keycode[], int num_keycodes, char output_ch[], int same)
3079bc590e5SSimon Glass {
3089bc590e5SSimon Glass 	struct input_key_xlate *table;
3099bc590e5SSimon Glass 	int ch_count;
3109bc590e5SSimon Glass 	int i;
3119bc590e5SSimon Glass 
3129bc590e5SSimon Glass 	table = &config->table[0];
3139bc590e5SSimon Glass 
3149bc590e5SSimon Glass 	/* deal with modifiers first */
3159bc590e5SSimon Glass 	for (i = 0; i < num_keycodes; i++) {
3169bc590e5SSimon Glass 		int key = keycode[i] & KEY_MASK;
3179bc590e5SSimon Glass 
3189bc590e5SSimon Glass 		if (key >= table->num_entries || table->xlate[key] == 0xff) {
3199bc590e5SSimon Glass 			table = process_modifier(config, key,
3209bc590e5SSimon Glass 					keycode[i] & KEY_RELEASE);
3219bc590e5SSimon Glass 		}
3229bc590e5SSimon Glass 	}
3239bc590e5SSimon Glass 
3249bc590e5SSimon Glass 	/* now find normal keys */
3259bc590e5SSimon Glass 	for (i = ch_count = 0; i < num_keycodes; i++) {
3269bc590e5SSimon Glass 		int key = keycode[i];
3279bc590e5SSimon Glass 
3289bc590e5SSimon Glass 		if (key < table->num_entries && i >= same) {
3299bc590e5SSimon Glass 			int ch = table->xlate[key];
3309bc590e5SSimon Glass 
3319bc590e5SSimon Glass 			/* If a normal key with an ASCII value, add it! */
3329bc590e5SSimon Glass 			if (ch != 0xff)
3339bc590e5SSimon Glass 				output_ch[ch_count++] = (uchar)ch;
3349bc590e5SSimon Glass 		}
3359bc590e5SSimon Glass 	}
3369bc590e5SSimon Glass 
3379bc590e5SSimon Glass 	/* ok, so return keys */
3389bc590e5SSimon Glass 	return ch_count;
3399bc590e5SSimon Glass }
3409bc590e5SSimon Glass 
3419bc590e5SSimon Glass int input_send_keycodes(struct input_config *config,
3429bc590e5SSimon Glass 			int keycode[], int num_keycodes)
3439bc590e5SSimon Glass {
3449bc590e5SSimon Glass 	char ch[num_keycodes];
3459bc590e5SSimon Glass 	int count, i, same = 0;
3469bc590e5SSimon Glass 	int is_repeat = 0;
3479bc590e5SSimon Glass 	unsigned delay_ms;
3489bc590e5SSimon Glass 
3499bc590e5SSimon Glass 	config->modifiers = 0;
3509bc590e5SSimon Glass 	if (!input_check_keycodes(config, keycode, num_keycodes, &same)) {
3519bc590e5SSimon Glass 		/*
3529bc590e5SSimon Glass 		 * Same as last time - is it time for another repeat?
3539bc590e5SSimon Glass 		 * TODO(sjg@chromium.org) We drop repeats here and since
3549bc590e5SSimon Glass 		 * the caller may not call in again for a while, our
3559bc590e5SSimon Glass 		 * auto-repeat speed is not quite correct. We should
3569bc590e5SSimon Glass 		 * insert another character if we later realise that we
3579bc590e5SSimon Glass 		 * have missed a repeat slot.
3589bc590e5SSimon Glass 		 */
359*1b1d3e64SSimon Glass 		is_repeat = config->repeat_rate_ms &&
360*1b1d3e64SSimon Glass 			(int)get_timer(config->next_repeat_ms) >= 0;
3619bc590e5SSimon Glass 		if (!is_repeat)
3629bc590e5SSimon Glass 			return 0;
3639bc590e5SSimon Glass 	}
3649bc590e5SSimon Glass 
3659bc590e5SSimon Glass 	count = input_keycodes_to_ascii(config, keycode, num_keycodes,
3669bc590e5SSimon Glass 					ch, is_repeat ? 0 : same);
3679bc590e5SSimon Glass 	for (i = 0; i < count; i++)
3689bc590e5SSimon Glass 		input_queue_ascii(config, ch[i]);
3699bc590e5SSimon Glass 	delay_ms = is_repeat ?
3709bc590e5SSimon Glass 			config->repeat_rate_ms :
3719bc590e5SSimon Glass 			config->repeat_delay_ms;
3729bc590e5SSimon Glass 
3739bc590e5SSimon Glass 	config->next_repeat_ms = get_timer(0) + delay_ms;
3749bc590e5SSimon Glass 	return 0;
3759bc590e5SSimon Glass }
3769bc590e5SSimon Glass 
3779bc590e5SSimon Glass int input_add_table(struct input_config *config, int left_keycode,
3789bc590e5SSimon Glass 		    int right_keycode, const uchar *xlate, int num_entries)
3799bc590e5SSimon Glass {
3809bc590e5SSimon Glass 	struct input_key_xlate *table;
3819bc590e5SSimon Glass 
3829bc590e5SSimon Glass 	if (config->num_tables == INPUT_MAX_MODIFIERS) {
3839bc590e5SSimon Glass 		debug("%s: Too many modifier tables\n", __func__);
3849bc590e5SSimon Glass 		return -1;
3859bc590e5SSimon Glass 	}
3869bc590e5SSimon Glass 
3879bc590e5SSimon Glass 	table = &config->table[config->num_tables++];
3889bc590e5SSimon Glass 	table->left_keycode = left_keycode;
3899bc590e5SSimon Glass 	table->right_keycode = right_keycode;
3909bc590e5SSimon Glass 	table->xlate = xlate;
3919bc590e5SSimon Glass 	table->num_entries = num_entries;
3929bc590e5SSimon Glass 
3939bc590e5SSimon Glass 	return 0;
3949bc590e5SSimon Glass }
3959bc590e5SSimon Glass 
396*1b1d3e64SSimon Glass void input_set_delays(struct input_config *config, int repeat_delay_ms,
3979bc590e5SSimon Glass 	       int repeat_rate_ms)
3989bc590e5SSimon Glass {
399*1b1d3e64SSimon Glass 	config->repeat_delay_ms = repeat_delay_ms;
400*1b1d3e64SSimon Glass 	config->repeat_rate_ms = repeat_rate_ms;
401*1b1d3e64SSimon Glass }
402*1b1d3e64SSimon Glass 
403*1b1d3e64SSimon Glass int input_init(struct input_config *config, int leds)
404*1b1d3e64SSimon Glass {
4059bc590e5SSimon Glass 	memset(config, '\0', sizeof(*config));
4069bc590e5SSimon Glass 	config->leds = leds;
4079bc590e5SSimon Glass 	if (input_add_table(config, -1, -1,
4089bc590e5SSimon Glass 			kbd_plain_xlate, ARRAY_SIZE(kbd_plain_xlate)) ||
4099bc590e5SSimon Glass 		input_add_table(config, KEY_LEFTSHIFT, KEY_RIGHTSHIFT,
4109bc590e5SSimon Glass 			kbd_shift_xlate, ARRAY_SIZE(kbd_shift_xlate)) ||
4119bc590e5SSimon Glass 		input_add_table(config, KEY_LEFTCTRL, KEY_RIGHTCTRL,
4129bc590e5SSimon Glass 			kbd_ctrl_xlate, ARRAY_SIZE(kbd_ctrl_xlate))) {
4139bc590e5SSimon Glass 		debug("%s: Could not add modifier tables\n", __func__);
4149bc590e5SSimon Glass 		return -1;
4159bc590e5SSimon Glass 	}
4169bc590e5SSimon Glass 
4179bc590e5SSimon Glass 	return 0;
4189bc590e5SSimon Glass }
4199bc590e5SSimon Glass 
4209bc590e5SSimon Glass int input_stdio_register(struct stdio_dev *dev)
4219bc590e5SSimon Glass {
4229bc590e5SSimon Glass 	int error;
4239bc590e5SSimon Glass 
4249bc590e5SSimon Glass 	error = stdio_register(dev);
4259bc590e5SSimon Glass 
4269bc590e5SSimon Glass 	/* check if this is the standard input device */
4279bc590e5SSimon Glass 	if (!error && strcmp(getenv("stdin"), dev->name) == 0) {
4289bc590e5SSimon Glass 		/* reassign the console */
4299bc590e5SSimon Glass 		if (OVERWRITE_CONSOLE ||
4309bc590e5SSimon Glass 				console_assign(stdin, dev->name))
4319bc590e5SSimon Glass 			return -1;
4329bc590e5SSimon Glass 	}
4339bc590e5SSimon Glass 
4349bc590e5SSimon Glass 	return 0;
4359bc590e5SSimon Glass }
436