blob: da264f4166ea4a468bf48a133ef6c6b48d3fdbea [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass9bc590e2012-04-17 09:01:30 +00002/*
3 * Translate key codes into ASCII
4 *
5 * Copyright (c) 2011 The Chromium OS Authors.
6 * (C) Copyright 2004 DENX Software Engineering, Wolfgang Denk, wd@denx.de
Simon Glass9bc590e2012-04-17 09:01:30 +00007 */
8
9#include <common.h>
Simon Glass24b852a2015-11-08 23:47:45 -070010#include <console.h>
Bin Mengcd810912015-11-12 05:33:02 -080011#include <dm.h>
Simon Glass7b51b572019-08-01 09:46:52 -060012#include <env.h>
Simon Glass92778b22015-10-18 21:17:12 -060013#include <errno.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060014#include <log.h>
Simon Glass9bc590e2012-04-17 09:01:30 +000015#include <stdio_dev.h>
16#include <input.h>
Bin Mengcd810912015-11-12 05:33:02 -080017#ifdef CONFIG_DM_KEYBOARD
18#include <keyboard.h>
19#endif
Simon Glass9bc590e2012-04-17 09:01:30 +000020#include <linux/input.h>
21
22enum {
23 /* These correspond to the lights on the keyboard */
Bin Meng377a0692015-11-12 05:33:03 -080024 FLAG_SCROLL_LOCK = 1 << 0,
25 FLAG_NUM_LOCK = 1 << 1,
26 FLAG_CAPS_LOCK = 1 << 2,
Simon Glass9bc590e2012-04-17 09:01:30 +000027
28 /* Special flag ORed with key code to indicate release */
29 KEY_RELEASE = 1 << 15,
30 KEY_MASK = 0xfff,
31};
32
33/*
34 * These takes map key codes to ASCII. 0xff means no key, or special key.
35 * Three tables are provided - one for plain keys, one for when the shift
36 * 'modifier' key is pressed and one for when the ctrl modifier key is
37 * pressed.
38 */
39static const uchar kbd_plain_xlate[] = {
40 0xff, 0x1b, '1', '2', '3', '4', '5', '6',
41 '7', '8', '9', '0', '-', '=', '\b', '\t', /* 0x00 - 0x0f */
42 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i',
43 'o', 'p', '[', ']', '\r', 0xff, 'a', 's', /* 0x10 - 0x1f */
44 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';',
45 '\'', '`', 0xff, '\\', 'z', 'x', 'c', 'v', /* 0x20 - 0x2f */
46 'b', 'n', 'm', ',' , '.', '/', 0xff, 0xff, 0xff,
47 ' ', 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 0x30 - 0x3f */
48 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, '7',
49 '8', '9', '-', '4', '5', '6', '+', '1', /* 0x40 - 0x4f */
50 '2', '3', '0', '.', 0xff, 0xff, 0xff, 0xff,
51 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 0x50 - 0x5F */
Simon Glass77c7f042015-10-18 21:17:23 -060052 '\r', 0xff, '/', '*',
Simon Glass9bc590e2012-04-17 09:01:30 +000053};
54
55static unsigned char kbd_shift_xlate[] = {
56 0xff, 0x1b, '!', '@', '#', '$', '%', '^',
57 '&', '*', '(', ')', '_', '+', '\b', '\t', /* 0x00 - 0x0f */
58 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I',
59 'O', 'P', '{', '}', '\r', 0xff, 'A', 'S', /* 0x10 - 0x1f */
60 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':',
61 '"', '~', 0xff, '|', 'Z', 'X', 'C', 'V', /* 0x20 - 0x2f */
62 'B', 'N', 'M', '<', '>', '?', 0xff, 0xff, 0xff,
63 ' ', 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 0x30 - 0x3f */
64 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, '7',
65 '8', '9', '-', '4', '5', '6', '+', '1', /* 0x40 - 0x4f */
66 '2', '3', '0', '.', 0xff, 0xff, 0xff, 0xff, 0xff,
67 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 0x50 - 0x5F */
Simon Glass77c7f042015-10-18 21:17:23 -060068 '\r', 0xff, '/', '*',
Simon Glass9bc590e2012-04-17 09:01:30 +000069};
70
71static unsigned char kbd_ctrl_xlate[] = {
72 0xff, 0x1b, '1', 0x00, '3', '4', '5', 0x1E,
73 '7', '8', '9', '0', 0x1F, '=', '\b', '\t', /* 0x00 - 0x0f */
Simon Glass2e5513b2015-10-18 21:17:22 -060074 0x11, 0x17, 0x05, 0x12, 0x14, 0x19, 0x15, 0x09,
Simon Glass9bc590e2012-04-17 09:01:30 +000075 0x0f, 0x10, 0x1b, 0x1d, '\n', 0xff, 0x01, 0x13, /* 0x10 - 0x1f */
76 0x04, 0x06, 0x08, 0x09, 0x0a, 0x0b, 0x0c, ';',
77 '\'', '~', 0x00, 0x1c, 0x1a, 0x18, 0x03, 0x16, /* 0x20 - 0x2f */
78 0x02, 0x0e, 0x0d, '<', '>', '?', 0xff, 0xff,
79 0xff, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 0x30 - 0x3f */
80 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, '7',
81 '8', '9', '-', '4', '5', '6', '+', '1', /* 0x40 - 0x4f */
82 '2', '3', '0', '.', 0xff, 0xff, 0xff, 0xff,
83 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 0x50 - 0x5F */
Simon Glass77c7f042015-10-18 21:17:23 -060084 '\r', 0xff, '/', '*',
Simon Glass9bc590e2012-04-17 09:01:30 +000085};
86
Heinrich Schuchardta84f5592018-03-02 21:11:35 +010087/*
88 * German keymap. Special letters are mapped according to code page 437.
89 */
Simon Glassb1d7a182015-11-11 10:05:37 -070090static const uchar kbd_plain_xlate_german[] = {
91 0xff, 0x1b, '1', '2', '3', '4', '5', '6', /* scan 00-07 */
92 '7', '8', '9', '0', 0xe1, '\'', 0x08, '\t', /* scan 08-0F */
93 'q', 'w', 'e', 'r', 't', 'z', 'u', 'i', /* scan 10-17 */
94 'o', 'p', 0x81, '+', '\r', 0xff, 'a', 's', /* scan 18-1F */
95 'd', 'f', 'g', 'h', 'j', 'k', 'l', 0x94, /* scan 20-27 */
96 0x84, '^', 0xff, '#', 'y', 'x', 'c', 'v', /* scan 28-2F */
97 'b', 'n', 'm', ',', '.', '-', 0xff, '*', /* scan 30-37 */
98 ' ', ' ', 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 38-3F */
99 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, '7', /* scan 40-47 */
100 '8', '9', '-', '4', '5', '6', '+', '1', /* scan 48-4F */
101 '2', '3', '0', ',', 0xff, 0xff, '<', 0xff, /* scan 50-57 */
102 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 58-5F */
103 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 60-67 */
104 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 68-6F */
105 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 70-77 */
106 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 78-7F */
107 '\r', 0xff, '/', '*',
108};
109
110static unsigned char kbd_shift_xlate_german[] = {
111 0xff, 0x1b, '!', '"', 0x15, '$', '%', '&', /* scan 00-07 */
112 '/', '(', ')', '=', '?', '`', 0x08, '\t', /* scan 08-0F */
113 'Q', 'W', 'E', 'R', 'T', 'Z', 'U', 'I', /* scan 10-17 */
114 'O', 'P', 0x9a, '*', '\r', 0xff, 'A', 'S', /* scan 18-1F */
115 'D', 'F', 'G', 'H', 'J', 'K', 'L', 0x99, /* scan 20-27 */
116 0x8e, 0xf8, 0xff, '\'', 'Y', 'X', 'C', 'V', /* scan 28-2F */
117 'B', 'N', 'M', ';', ':', '_', 0xff, '*', /* scan 30-37 */
118 ' ', ' ', 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 38-3F */
119 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, '7', /* scan 40-47 */
120 '8', '9', '-', '4', '5', '6', '+', '1', /* scan 48-4F */
121 '2', '3', '0', ',', 0xff, 0xff, '>', 0xff, /* scan 50-57 */
122 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 58-5F */
123 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 60-67 */
124 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 68-6F */
125 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 70-77 */
126 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 78-7F */
127 '\r', 0xff, '/', '*',
128};
129
130static unsigned char kbd_right_alt_xlate_german[] = {
Heinrich Schuchardta84f5592018-03-02 21:11:35 +0100131 0xff, 0xff, 0xff, 0xfd, 0xff, 0xff, 0xff, 0xff, /* scan 00-07 */
Simon Glassb1d7a182015-11-11 10:05:37 -0700132 '{', '[', ']', '}', '\\', 0xff, 0xff, 0xff, /* scan 08-0F */
133 '@', 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 10-17 */
134 0xff, 0xff, 0xff, '~', 0xff, 0xff, 0xff, 0xff, /* scan 18-1F */
135 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 20-27 */
136 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 28-2F */
137 0xff, 0xff, 0xe6, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 30-37 */
138 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 38-3F */
139 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 40-47 */
140 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 48-4F */
141 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, '|', 0xff, /* scan 50-57 */
142};
143
144enum kbd_mask {
145 KBD_ENGLISH = 1 << 0,
146 KBD_GERMAN = 1 << 1,
147};
148
149static struct kbd_entry {
150 int kbd_mask; /* Which languages this is for */
151 int left_keycode; /* Left keycode to select this map */
152 int right_keycode; /* Right keycode to select this map */
153 const uchar *xlate; /* Ascii code for each keycode */
154 int num_entries; /* Number of entries in xlate */
155} kbd_entry[] = {
156 { KBD_ENGLISH, -1, -1,
157 kbd_plain_xlate, ARRAY_SIZE(kbd_plain_xlate) },
158 { KBD_GERMAN, -1, -1,
159 kbd_plain_xlate_german, ARRAY_SIZE(kbd_plain_xlate_german) },
160 { KBD_ENGLISH, KEY_LEFTSHIFT, KEY_RIGHTSHIFT,
161 kbd_shift_xlate, ARRAY_SIZE(kbd_shift_xlate) },
162 { KBD_GERMAN, KEY_LEFTSHIFT, KEY_RIGHTSHIFT,
163 kbd_shift_xlate_german, ARRAY_SIZE(kbd_shift_xlate_german) },
164 { KBD_ENGLISH | KBD_GERMAN, KEY_LEFTCTRL, KEY_RIGHTCTRL,
165 kbd_ctrl_xlate, ARRAY_SIZE(kbd_ctrl_xlate) },
166 { KBD_GERMAN, -1, KEY_RIGHTALT,
167 kbd_right_alt_xlate_german,
168 ARRAY_SIZE(kbd_right_alt_xlate_german) },
169 {},
170};
171
Hung-Te Lin44abe472012-10-11 15:15:53 +0000172/*
173 * Scan key code to ANSI 3.64 escape sequence table. This table is
174 * incomplete in that it does not include all possible extra keys.
175 */
176static struct {
177 int kbd_scan_code;
178 char *escape;
179} kbd_to_ansi364[] = {
180 { KEY_UP, "\033[A"},
181 { KEY_DOWN, "\033[B"},
182 { KEY_RIGHT, "\033[C"},
183 { KEY_LEFT, "\033[D"},
184};
185
186/* Maximum number of output characters that an ANSI sequence expands to */
187#define ANSI_CHAR_MAX 3
Simon Glass9bc590e2012-04-17 09:01:30 +0000188
Kim Phillipsc14e94e2012-10-29 13:34:42 +0000189static int input_queue_ascii(struct input_config *config, int ch)
Simon Glass9bc590e2012-04-17 09:01:30 +0000190{
191 if (config->fifo_in + 1 == INPUT_BUFFER_LEN) {
192 if (!config->fifo_out)
193 return -1; /* buffer full */
194 else
195 config->fifo_in = 0;
196 } else {
197 if (config->fifo_in + 1 == config->fifo_out)
198 return -1; /* buffer full */
199 config->fifo_in++;
200 }
Simon Glass3a85e432015-10-18 21:17:24 -0600201 debug(" {%02x} ", ch);
Simon Glass9bc590e2012-04-17 09:01:30 +0000202 config->fifo[config->fifo_in] = (uchar)ch;
203
204 return 0;
205}
206
207int input_tstc(struct input_config *config)
208{
209 if (config->fifo_in == config->fifo_out && config->read_keys) {
210 if (!(*config->read_keys)(config))
211 return 0;
212 }
213 return config->fifo_in != config->fifo_out;
214}
215
216int input_getc(struct input_config *config)
217{
218 int err = 0;
219
220 while (config->fifo_in == config->fifo_out) {
221 if (config->read_keys)
222 err = (*config->read_keys)(config);
223 if (err)
224 return -1;
225 }
226
227 if (++config->fifo_out == INPUT_BUFFER_LEN)
228 config->fifo_out = 0;
229
230 return config->fifo[config->fifo_out];
231}
232
233/**
234 * Process a modifier/special key press or release and decide which key
235 * translation array should be used as a result.
236 *
237 * TODO: Should keep track of modifier press/release
238 *
239 * @param config Input state
240 * @param key Key code to process
241 * @param release 0 if a press, 1 if a release
242 * @return pointer to keycode->ascii translation table that should be used
243 */
244static struct input_key_xlate *process_modifier(struct input_config *config,
245 int key, int release)
246{
Bin Mengcd810912015-11-12 05:33:02 -0800247#ifdef CONFIG_DM_KEYBOARD
248 struct udevice *dev = config->dev;
249 struct keyboard_ops *ops = keyboard_get_ops(dev);
250#endif
Simon Glass9bc590e2012-04-17 09:01:30 +0000251 struct input_key_xlate *table;
Simon Glass9bc590e2012-04-17 09:01:30 +0000252 int i;
253
254 /* Start with the main table, and see what modifiers change it */
255 assert(config->num_tables > 0);
256 table = &config->table[0];
257 for (i = 1; i < config->num_tables; i++) {
258 struct input_key_xlate *tab = &config->table[i];
259
260 if (key == tab->left_keycode || key == tab->right_keycode)
261 table = tab;
262 }
263
264 /* Handle the lighted keys */
265 if (!release) {
Simon Glassa683d0d2015-11-11 10:05:38 -0700266 int flip = -1;
267
Simon Glass9bc590e2012-04-17 09:01:30 +0000268 switch (key) {
269 case KEY_SCROLLLOCK:
270 flip = FLAG_SCROLL_LOCK;
271 break;
272 case KEY_NUMLOCK:
273 flip = FLAG_NUM_LOCK;
274 break;
275 case KEY_CAPSLOCK:
276 flip = FLAG_CAPS_LOCK;
277 break;
278 }
Simon Glass9bc590e2012-04-17 09:01:30 +0000279
Simon Glassa683d0d2015-11-11 10:05:38 -0700280 if (flip != -1) {
281 int leds = 0;
Simon Glass9bc590e2012-04-17 09:01:30 +0000282
Bin Meng533c81a2015-11-12 05:33:01 -0800283 config->flags ^= flip;
Simon Glassa683d0d2015-11-11 10:05:38 -0700284 if (config->flags & FLAG_NUM_LOCK)
285 leds |= INPUT_LED_NUM;
286 if (config->flags & FLAG_CAPS_LOCK)
287 leds |= INPUT_LED_CAPS;
288 if (config->flags & FLAG_SCROLL_LOCK)
289 leds |= INPUT_LED_SCROLL;
290 config->leds = leds;
Simon Glass3b5f6f52015-11-11 10:05:40 -0700291 config->leds_changed = flip;
Bin Mengcd810912015-11-12 05:33:02 -0800292
293#ifdef CONFIG_DM_KEYBOARD
294 if (ops->update_leds) {
295 if (ops->update_leds(dev, config->leds))
296 debug("Update keyboard's LED failed\n");
297 }
298#endif
Simon Glassa683d0d2015-11-11 10:05:38 -0700299 }
Simon Glass9bc590e2012-04-17 09:01:30 +0000300 }
301
302 return table;
303}
304
305/**
306 * Search an int array for a key value
307 *
308 * @param array Array to search
309 * @param count Number of elements in array
310 * @param key Key value to find
311 * @return element where value was first found, -1 if none
312 */
313static int array_search(int *array, int count, int key)
314{
315 int i;
316
317 for (i = 0; i < count; i++) {
318 if (array[i] == key)
319 return i;
320 }
321
322 return -1;
323}
324
325/**
326 * Sort an array so that those elements that exist in the ordering are
327 * first in the array, and in the same order as the ordering. The algorithm
328 * is O(count * ocount) and designed for small arrays.
329 *
330 * TODO: Move this to common / lib?
331 *
332 * @param dest Array with elements to sort, also destination array
333 * @param count Number of elements to sort
334 * @param order Array containing ordering elements
335 * @param ocount Number of ordering elements
336 * @return number of elements in dest that are in order (these will be at the
337 * start of dest).
338 */
339static int sort_array_by_ordering(int *dest, int count, int *order,
340 int ocount)
341{
342 int temp[count];
343 int dest_count;
344 int same; /* number of elements which are the same */
345 int i;
346
347 /* setup output items, copy items to be sorted into our temp area */
348 memcpy(temp, dest, count * sizeof(*dest));
349 dest_count = 0;
350
351 /* work through the ordering, move over the elements we agree on */
352 for (i = 0; i < ocount; i++) {
353 if (array_search(temp, count, order[i]) != -1)
354 dest[dest_count++] = order[i];
355 }
356 same = dest_count;
357
358 /* now move over the elements that are not in the ordering */
359 for (i = 0; i < count; i++) {
360 if (array_search(order, ocount, temp[i]) == -1)
361 dest[dest_count++] = temp[i];
362 }
363 assert(dest_count == count);
364 return same;
365}
366
367/**
368 * Check a list of key codes against the previous key scan
369 *
370 * Given a list of new key codes, we check how many of these are the same
371 * as last time.
372 *
373 * @param config Input state
374 * @param keycode List of key codes to examine
375 * @param num_keycodes Number of key codes
376 * @param same Returns number of key codes which are the same
377 */
378static int input_check_keycodes(struct input_config *config,
379 int keycode[], int num_keycodes, int *same)
380{
381 /* Select the 'plain' xlate table to start with */
382 if (!config->num_tables) {
383 debug("%s: No xlate tables: cannot decode keys\n", __func__);
384 return -1;
385 }
386
387 /* sort the keycodes into the same order as the previous ones */
388 *same = sort_array_by_ordering(keycode, num_keycodes,
389 config->prev_keycodes, config->num_prev_keycodes);
390
391 memcpy(config->prev_keycodes, keycode, num_keycodes * sizeof(int));
392 config->num_prev_keycodes = num_keycodes;
393
394 return *same != num_keycodes;
395}
396
397/**
Hung-Te Lin44abe472012-10-11 15:15:53 +0000398 * Checks and converts a special key code into ANSI 3.64 escape sequence.
399 *
400 * @param config Input state
401 * @param keycode Key code to examine
402 * @param output_ch Buffer to place output characters into. It should
403 * be at least ANSI_CHAR_MAX bytes long, to allow for
404 * an ANSI sequence.
405 * @param max_chars Maximum number of characters to add to output_ch
406 * @return number of characters output, if the key was converted, otherwise 0.
407 * This may be larger than max_chars, in which case the overflow
408 * characters are not output.
409 */
410static int input_keycode_to_ansi364(struct input_config *config,
411 int keycode, char output_ch[], int max_chars)
412{
413 const char *escape;
414 int ch_count;
415 int i;
416
417 for (i = ch_count = 0; i < ARRAY_SIZE(kbd_to_ansi364); i++) {
418 if (keycode != kbd_to_ansi364[i].kbd_scan_code)
419 continue;
420 for (escape = kbd_to_ansi364[i].escape; *escape; escape++) {
421 if (ch_count < max_chars)
422 output_ch[ch_count] = *escape;
423 ch_count++;
424 }
425 return ch_count;
426 }
427
428 return 0;
429}
430
431/**
432 * Converts and queues a list of key codes in escaped ASCII string form
Simon Glass9bc590e2012-04-17 09:01:30 +0000433 * Convert a list of key codes into ASCII
434 *
435 * You must call input_check_keycodes() before this. It turns the keycode
Hung-Te Lin44abe472012-10-11 15:15:53 +0000436 * list into a list of ASCII characters and sends them to the input layer.
Simon Glass9bc590e2012-04-17 09:01:30 +0000437 *
438 * Characters which were seen last time do not generate fresh ASCII output.
Hung-Te Lin44abe472012-10-11 15:15:53 +0000439 * The output (calls to queue_ascii) may be longer than num_keycodes, if the
440 * keycode contains special keys that was encoded to longer escaped sequence.
Simon Glass9bc590e2012-04-17 09:01:30 +0000441 *
442 * @param config Input state
443 * @param keycode List of key codes to examine
444 * @param num_keycodes Number of key codes
Hung-Te Lin44abe472012-10-11 15:15:53 +0000445 * @param output_ch Buffer to place output characters into. It should
446 * be at last ANSI_CHAR_MAX * num_keycodes, to allow for
447 * ANSI sequences.
448 * @param max_chars Maximum number of characters to add to output_ch
Simon Glass9bc590e2012-04-17 09:01:30 +0000449 * @param same Number of key codes which are the same
Hung-Te Lin44abe472012-10-11 15:15:53 +0000450 * @return number of characters written into output_ch, or -1 if we would
451 * exceed max_chars chars.
Simon Glass9bc590e2012-04-17 09:01:30 +0000452 */
453static int input_keycodes_to_ascii(struct input_config *config,
Hung-Te Lin44abe472012-10-11 15:15:53 +0000454 int keycode[], int num_keycodes, char output_ch[],
455 int max_chars, int same)
Simon Glass9bc590e2012-04-17 09:01:30 +0000456{
457 struct input_key_xlate *table;
Hung-Te Lin44abe472012-10-11 15:15:53 +0000458 int ch_count = 0;
Simon Glass9bc590e2012-04-17 09:01:30 +0000459 int i;
460
461 table = &config->table[0];
462
463 /* deal with modifiers first */
464 for (i = 0; i < num_keycodes; i++) {
465 int key = keycode[i] & KEY_MASK;
466
467 if (key >= table->num_entries || table->xlate[key] == 0xff) {
468 table = process_modifier(config, key,
469 keycode[i] & KEY_RELEASE);
470 }
471 }
472
Hung-Te Lin44abe472012-10-11 15:15:53 +0000473 /* Start conversion by looking for the first new keycode (by same). */
474 for (i = same; i < num_keycodes; i++) {
Simon Glass9bc590e2012-04-17 09:01:30 +0000475 int key = keycode[i];
Simon Glassba420342015-11-11 10:05:39 -0700476 int ch;
Simon Glass9bc590e2012-04-17 09:01:30 +0000477
Hung-Te Lin44abe472012-10-11 15:15:53 +0000478 /*
479 * For a normal key (with an ASCII value), add it; otherwise
480 * translate special key to escape sequence if possible.
481 */
Simon Glassba420342015-11-11 10:05:39 -0700482 if (key < table->num_entries) {
483 ch = table->xlate[key];
484 if ((config->flags & FLAG_CAPS_LOCK) &&
485 ch >= 'a' && ch <= 'z')
486 ch -= 'a' - 'A';
Bin Menge5f330c2015-11-12 05:33:04 -0800487 /* ban digit numbers if 'Num Lock' is not on */
488 if (!(config->flags & FLAG_NUM_LOCK)) {
489 if (key >= KEY_KP7 && key <= KEY_KPDOT &&
490 key != KEY_KPMINUS && key != KEY_KPPLUS)
491 ch = 0xff;
492 }
Simon Glassba420342015-11-11 10:05:39 -0700493 if (ch_count < max_chars && ch != 0xff)
494 output_ch[ch_count++] = (uchar)ch;
Hung-Te Lin44abe472012-10-11 15:15:53 +0000495 } else {
496 ch_count += input_keycode_to_ansi364(config, key,
497 output_ch, max_chars);
Simon Glass9bc590e2012-04-17 09:01:30 +0000498 }
499 }
500
Hung-Te Lin44abe472012-10-11 15:15:53 +0000501 if (ch_count > max_chars) {
502 debug("%s: Output char buffer overflow size=%d, need=%d\n",
503 __func__, max_chars, ch_count);
504 return -1;
505 }
506
Simon Glass9bc590e2012-04-17 09:01:30 +0000507 /* ok, so return keys */
508 return ch_count;
509}
510
Simon Glass3a85e432015-10-18 21:17:24 -0600511static int _input_send_keycodes(struct input_config *config, int keycode[],
512 int num_keycodes, bool do_send)
Simon Glass9bc590e2012-04-17 09:01:30 +0000513{
Hung-Te Lin44abe472012-10-11 15:15:53 +0000514 char ch[num_keycodes * ANSI_CHAR_MAX];
Simon Glass9bc590e2012-04-17 09:01:30 +0000515 int count, i, same = 0;
516 int is_repeat = 0;
517 unsigned delay_ms;
518
519 config->modifiers = 0;
520 if (!input_check_keycodes(config, keycode, num_keycodes, &same)) {
521 /*
522 * Same as last time - is it time for another repeat?
523 * TODO(sjg@chromium.org) We drop repeats here and since
524 * the caller may not call in again for a while, our
525 * auto-repeat speed is not quite correct. We should
526 * insert another character if we later realise that we
527 * have missed a repeat slot.
528 */
Simon Glass0b186c02015-10-18 21:17:25 -0600529 is_repeat = config->allow_repeats || (config->repeat_rate_ms &&
530 (int)get_timer(config->next_repeat_ms) >= 0);
Simon Glass9bc590e2012-04-17 09:01:30 +0000531 if (!is_repeat)
532 return 0;
533 }
534
535 count = input_keycodes_to_ascii(config, keycode, num_keycodes,
Hung-Te Lin44abe472012-10-11 15:15:53 +0000536 ch, sizeof(ch), is_repeat ? 0 : same);
Simon Glass3a85e432015-10-18 21:17:24 -0600537 if (do_send) {
538 for (i = 0; i < count; i++)
539 input_queue_ascii(config, ch[i]);
540 }
Simon Glass9bc590e2012-04-17 09:01:30 +0000541 delay_ms = is_repeat ?
542 config->repeat_rate_ms :
543 config->repeat_delay_ms;
544
545 config->next_repeat_ms = get_timer(0) + delay_ms;
Hung-Te Lin44abe472012-10-11 15:15:53 +0000546
547 return count;
Simon Glass9bc590e2012-04-17 09:01:30 +0000548}
549
Simon Glass3a85e432015-10-18 21:17:24 -0600550int input_send_keycodes(struct input_config *config, int keycode[],
551 int num_keycodes)
552{
553 return _input_send_keycodes(config, keycode, num_keycodes, true);
554}
555
556int input_add_keycode(struct input_config *config, int new_keycode,
557 bool release)
558{
559 int keycode[INPUT_MAX_MODIFIERS + 1];
560 int count, i;
561
562 /* Add the old keycodes which are not removed by this new one */
563 for (i = 0, count = 0; i < config->num_prev_keycodes; i++) {
564 int code = config->prev_keycodes[i];
565
566 if (new_keycode == code) {
567 if (release)
568 continue;
569 new_keycode = -1;
570 }
571 keycode[count++] = code;
572 }
573
574 if (!release && new_keycode != -1)
575 keycode[count++] = new_keycode;
576 debug("\ncodes for %02x/%d: ", new_keycode, release);
577 for (i = 0; i < count; i++)
578 debug("%02x ", keycode[i]);
579 debug("\n");
580
581 /* Don't output any ASCII characters if this is a key release */
582 return _input_send_keycodes(config, keycode, count, !release);
583}
584
Simon Glass9bc590e2012-04-17 09:01:30 +0000585int input_add_table(struct input_config *config, int left_keycode,
586 int right_keycode, const uchar *xlate, int num_entries)
587{
588 struct input_key_xlate *table;
589
590 if (config->num_tables == INPUT_MAX_MODIFIERS) {
591 debug("%s: Too many modifier tables\n", __func__);
592 return -1;
593 }
594
595 table = &config->table[config->num_tables++];
596 table->left_keycode = left_keycode;
597 table->right_keycode = right_keycode;
598 table->xlate = xlate;
599 table->num_entries = num_entries;
600
601 return 0;
602}
603
Simon Glass1b1d3e62012-09-27 15:18:41 +0000604void input_set_delays(struct input_config *config, int repeat_delay_ms,
Simon Glass9bc590e2012-04-17 09:01:30 +0000605 int repeat_rate_ms)
606{
Simon Glass1b1d3e62012-09-27 15:18:41 +0000607 config->repeat_delay_ms = repeat_delay_ms;
608 config->repeat_rate_ms = repeat_rate_ms;
609}
610
Simon Glass0b186c02015-10-18 21:17:25 -0600611void input_allow_repeats(struct input_config *config, bool allow_repeats)
612{
613 config->allow_repeats = allow_repeats;
614}
615
Simon Glass3b5f6f52015-11-11 10:05:40 -0700616int input_leds_changed(struct input_config *config)
617{
618 if (config->leds_changed)
619 return config->leds;
620
621 return -1;
622}
623
Simon Glassb1d7a182015-11-11 10:05:37 -0700624int input_add_tables(struct input_config *config, bool german)
Simon Glass66877b02015-10-18 21:17:13 -0600625{
Simon Glassb1d7a182015-11-11 10:05:37 -0700626 struct kbd_entry *entry;
627 int mask;
Simon Glass66877b02015-10-18 21:17:13 -0600628 int ret;
629
Simon Glassb1d7a182015-11-11 10:05:37 -0700630 mask = german ? KBD_GERMAN : KBD_ENGLISH;
631 for (entry = kbd_entry; entry->kbd_mask; entry++) {
632 if (!(mask & entry->kbd_mask))
633 continue;
634 ret = input_add_table(config, entry->left_keycode,
635 entry->right_keycode, entry->xlate,
636 entry->num_entries);
637 if (ret)
638 return ret;
639 }
Simon Glass66877b02015-10-18 21:17:13 -0600640
Simon Glassb1d7a182015-11-11 10:05:37 -0700641 return 0;
Simon Glass66877b02015-10-18 21:17:13 -0600642}
643
Simon Glass1b1d3e62012-09-27 15:18:41 +0000644int input_init(struct input_config *config, int leds)
645{
Simon Glass9bc590e2012-04-17 09:01:30 +0000646 memset(config, '\0', sizeof(*config));
647 config->leds = leds;
Simon Glass9bc590e2012-04-17 09:01:30 +0000648
649 return 0;
650}
651
652int input_stdio_register(struct stdio_dev *dev)
653{
654 int error;
655
656 error = stdio_register(dev);
Simon Glass985ca392018-10-01 12:22:10 -0600657#if !defined(CONFIG_SPL_BUILD) || CONFIG_IS_ENABLED(ENV_SUPPORT)
Simon Glass9bc590e2012-04-17 09:01:30 +0000658 /* check if this is the standard input device */
Simon Glass00caae62017-08-03 12:22:12 -0600659 if (!error && strcmp(env_get("stdin"), dev->name) == 0) {
Simon Glass9bc590e2012-04-17 09:01:30 +0000660 /* reassign the console */
661 if (OVERWRITE_CONSOLE ||
662 console_assign(stdin, dev->name))
663 return -1;
664 }
Simon Glass985ca392018-10-01 12:22:10 -0600665#else
666 error = error;
667#endif
Simon Glass9bc590e2012-04-17 09:01:30 +0000668
669 return 0;
670}