blob: 6ab378b979bd94814eb1d06da2d7524e393809e0 [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 Glass9bc590e2012-04-17 09:01:30 +000014#include <stdio_dev.h>
15#include <input.h>
Bin Mengcd810912015-11-12 05:33:02 -080016#ifdef CONFIG_DM_KEYBOARD
17#include <keyboard.h>
18#endif
Simon Glass9bc590e2012-04-17 09:01:30 +000019#include <linux/input.h>
20
21enum {
22 /* These correspond to the lights on the keyboard */
Bin Meng377a0692015-11-12 05:33:03 -080023 FLAG_SCROLL_LOCK = 1 << 0,
24 FLAG_NUM_LOCK = 1 << 1,
25 FLAG_CAPS_LOCK = 1 << 2,
Simon Glass9bc590e2012-04-17 09:01:30 +000026
27 /* Special flag ORed with key code to indicate release */
28 KEY_RELEASE = 1 << 15,
29 KEY_MASK = 0xfff,
30};
31
32/*
33 * These takes map key codes to ASCII. 0xff means no key, or special key.
34 * Three tables are provided - one for plain keys, one for when the shift
35 * 'modifier' key is pressed and one for when the ctrl modifier key is
36 * pressed.
37 */
38static const uchar kbd_plain_xlate[] = {
39 0xff, 0x1b, '1', '2', '3', '4', '5', '6',
40 '7', '8', '9', '0', '-', '=', '\b', '\t', /* 0x00 - 0x0f */
41 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i',
42 'o', 'p', '[', ']', '\r', 0xff, 'a', 's', /* 0x10 - 0x1f */
43 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';',
44 '\'', '`', 0xff, '\\', 'z', 'x', 'c', 'v', /* 0x20 - 0x2f */
45 'b', 'n', 'm', ',' , '.', '/', 0xff, 0xff, 0xff,
46 ' ', 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 0x30 - 0x3f */
47 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, '7',
48 '8', '9', '-', '4', '5', '6', '+', '1', /* 0x40 - 0x4f */
49 '2', '3', '0', '.', 0xff, 0xff, 0xff, 0xff,
50 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 0x50 - 0x5F */
Simon Glass77c7f042015-10-18 21:17:23 -060051 '\r', 0xff, '/', '*',
Simon Glass9bc590e2012-04-17 09:01:30 +000052};
53
54static unsigned char kbd_shift_xlate[] = {
55 0xff, 0x1b, '!', '@', '#', '$', '%', '^',
56 '&', '*', '(', ')', '_', '+', '\b', '\t', /* 0x00 - 0x0f */
57 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I',
58 'O', 'P', '{', '}', '\r', 0xff, 'A', 'S', /* 0x10 - 0x1f */
59 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':',
60 '"', '~', 0xff, '|', 'Z', 'X', 'C', 'V', /* 0x20 - 0x2f */
61 'B', 'N', 'M', '<', '>', '?', 0xff, 0xff, 0xff,
62 ' ', 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 0x30 - 0x3f */
63 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, '7',
64 '8', '9', '-', '4', '5', '6', '+', '1', /* 0x40 - 0x4f */
65 '2', '3', '0', '.', 0xff, 0xff, 0xff, 0xff, 0xff,
66 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 0x50 - 0x5F */
Simon Glass77c7f042015-10-18 21:17:23 -060067 '\r', 0xff, '/', '*',
Simon Glass9bc590e2012-04-17 09:01:30 +000068};
69
70static unsigned char kbd_ctrl_xlate[] = {
71 0xff, 0x1b, '1', 0x00, '3', '4', '5', 0x1E,
72 '7', '8', '9', '0', 0x1F, '=', '\b', '\t', /* 0x00 - 0x0f */
Simon Glass2e5513b2015-10-18 21:17:22 -060073 0x11, 0x17, 0x05, 0x12, 0x14, 0x19, 0x15, 0x09,
Simon Glass9bc590e2012-04-17 09:01:30 +000074 0x0f, 0x10, 0x1b, 0x1d, '\n', 0xff, 0x01, 0x13, /* 0x10 - 0x1f */
75 0x04, 0x06, 0x08, 0x09, 0x0a, 0x0b, 0x0c, ';',
76 '\'', '~', 0x00, 0x1c, 0x1a, 0x18, 0x03, 0x16, /* 0x20 - 0x2f */
77 0x02, 0x0e, 0x0d, '<', '>', '?', 0xff, 0xff,
78 0xff, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 0x30 - 0x3f */
79 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, '7',
80 '8', '9', '-', '4', '5', '6', '+', '1', /* 0x40 - 0x4f */
81 '2', '3', '0', '.', 0xff, 0xff, 0xff, 0xff,
82 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 0x50 - 0x5F */
Simon Glass77c7f042015-10-18 21:17:23 -060083 '\r', 0xff, '/', '*',
Simon Glass9bc590e2012-04-17 09:01:30 +000084};
85
Heinrich Schuchardta84f5592018-03-02 21:11:35 +010086/*
87 * German keymap. Special letters are mapped according to code page 437.
88 */
Simon Glassb1d7a182015-11-11 10:05:37 -070089static const uchar kbd_plain_xlate_german[] = {
90 0xff, 0x1b, '1', '2', '3', '4', '5', '6', /* scan 00-07 */
91 '7', '8', '9', '0', 0xe1, '\'', 0x08, '\t', /* scan 08-0F */
92 'q', 'w', 'e', 'r', 't', 'z', 'u', 'i', /* scan 10-17 */
93 'o', 'p', 0x81, '+', '\r', 0xff, 'a', 's', /* scan 18-1F */
94 'd', 'f', 'g', 'h', 'j', 'k', 'l', 0x94, /* scan 20-27 */
95 0x84, '^', 0xff, '#', 'y', 'x', 'c', 'v', /* scan 28-2F */
96 'b', 'n', 'm', ',', '.', '-', 0xff, '*', /* scan 30-37 */
97 ' ', ' ', 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 38-3F */
98 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, '7', /* scan 40-47 */
99 '8', '9', '-', '4', '5', '6', '+', '1', /* scan 48-4F */
100 '2', '3', '0', ',', 0xff, 0xff, '<', 0xff, /* scan 50-57 */
101 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 58-5F */
102 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 60-67 */
103 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 68-6F */
104 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 70-77 */
105 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 78-7F */
106 '\r', 0xff, '/', '*',
107};
108
109static unsigned char kbd_shift_xlate_german[] = {
110 0xff, 0x1b, '!', '"', 0x15, '$', '%', '&', /* scan 00-07 */
111 '/', '(', ')', '=', '?', '`', 0x08, '\t', /* scan 08-0F */
112 'Q', 'W', 'E', 'R', 'T', 'Z', 'U', 'I', /* scan 10-17 */
113 'O', 'P', 0x9a, '*', '\r', 0xff, 'A', 'S', /* scan 18-1F */
114 'D', 'F', 'G', 'H', 'J', 'K', 'L', 0x99, /* scan 20-27 */
115 0x8e, 0xf8, 0xff, '\'', 'Y', 'X', 'C', 'V', /* scan 28-2F */
116 'B', 'N', 'M', ';', ':', '_', 0xff, '*', /* scan 30-37 */
117 ' ', ' ', 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 38-3F */
118 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, '7', /* scan 40-47 */
119 '8', '9', '-', '4', '5', '6', '+', '1', /* scan 48-4F */
120 '2', '3', '0', ',', 0xff, 0xff, '>', 0xff, /* scan 50-57 */
121 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 58-5F */
122 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 60-67 */
123 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 68-6F */
124 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 70-77 */
125 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 78-7F */
126 '\r', 0xff, '/', '*',
127};
128
129static unsigned char kbd_right_alt_xlate_german[] = {
Heinrich Schuchardta84f5592018-03-02 21:11:35 +0100130 0xff, 0xff, 0xff, 0xfd, 0xff, 0xff, 0xff, 0xff, /* scan 00-07 */
Simon Glassb1d7a182015-11-11 10:05:37 -0700131 '{', '[', ']', '}', '\\', 0xff, 0xff, 0xff, /* scan 08-0F */
132 '@', 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 10-17 */
133 0xff, 0xff, 0xff, '~', 0xff, 0xff, 0xff, 0xff, /* scan 18-1F */
134 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 20-27 */
135 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 28-2F */
136 0xff, 0xff, 0xe6, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 30-37 */
137 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 38-3F */
138 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 40-47 */
139 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 48-4F */
140 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, '|', 0xff, /* scan 50-57 */
141};
142
143enum kbd_mask {
144 KBD_ENGLISH = 1 << 0,
145 KBD_GERMAN = 1 << 1,
146};
147
148static struct kbd_entry {
149 int kbd_mask; /* Which languages this is for */
150 int left_keycode; /* Left keycode to select this map */
151 int right_keycode; /* Right keycode to select this map */
152 const uchar *xlate; /* Ascii code for each keycode */
153 int num_entries; /* Number of entries in xlate */
154} kbd_entry[] = {
155 { KBD_ENGLISH, -1, -1,
156 kbd_plain_xlate, ARRAY_SIZE(kbd_plain_xlate) },
157 { KBD_GERMAN, -1, -1,
158 kbd_plain_xlate_german, ARRAY_SIZE(kbd_plain_xlate_german) },
159 { KBD_ENGLISH, KEY_LEFTSHIFT, KEY_RIGHTSHIFT,
160 kbd_shift_xlate, ARRAY_SIZE(kbd_shift_xlate) },
161 { KBD_GERMAN, KEY_LEFTSHIFT, KEY_RIGHTSHIFT,
162 kbd_shift_xlate_german, ARRAY_SIZE(kbd_shift_xlate_german) },
163 { KBD_ENGLISH | KBD_GERMAN, KEY_LEFTCTRL, KEY_RIGHTCTRL,
164 kbd_ctrl_xlate, ARRAY_SIZE(kbd_ctrl_xlate) },
165 { KBD_GERMAN, -1, KEY_RIGHTALT,
166 kbd_right_alt_xlate_german,
167 ARRAY_SIZE(kbd_right_alt_xlate_german) },
168 {},
169};
170
Hung-Te Lin44abe472012-10-11 15:15:53 +0000171/*
172 * Scan key code to ANSI 3.64 escape sequence table. This table is
173 * incomplete in that it does not include all possible extra keys.
174 */
175static struct {
176 int kbd_scan_code;
177 char *escape;
178} kbd_to_ansi364[] = {
179 { KEY_UP, "\033[A"},
180 { KEY_DOWN, "\033[B"},
181 { KEY_RIGHT, "\033[C"},
182 { KEY_LEFT, "\033[D"},
183};
184
185/* Maximum number of output characters that an ANSI sequence expands to */
186#define ANSI_CHAR_MAX 3
Simon Glass9bc590e2012-04-17 09:01:30 +0000187
Kim Phillipsc14e94e2012-10-29 13:34:42 +0000188static int input_queue_ascii(struct input_config *config, int ch)
Simon Glass9bc590e2012-04-17 09:01:30 +0000189{
190 if (config->fifo_in + 1 == INPUT_BUFFER_LEN) {
191 if (!config->fifo_out)
192 return -1; /* buffer full */
193 else
194 config->fifo_in = 0;
195 } else {
196 if (config->fifo_in + 1 == config->fifo_out)
197 return -1; /* buffer full */
198 config->fifo_in++;
199 }
Simon Glass3a85e432015-10-18 21:17:24 -0600200 debug(" {%02x} ", ch);
Simon Glass9bc590e2012-04-17 09:01:30 +0000201 config->fifo[config->fifo_in] = (uchar)ch;
202
203 return 0;
204}
205
206int input_tstc(struct input_config *config)
207{
208 if (config->fifo_in == config->fifo_out && config->read_keys) {
209 if (!(*config->read_keys)(config))
210 return 0;
211 }
212 return config->fifo_in != config->fifo_out;
213}
214
215int input_getc(struct input_config *config)
216{
217 int err = 0;
218
219 while (config->fifo_in == config->fifo_out) {
220 if (config->read_keys)
221 err = (*config->read_keys)(config);
222 if (err)
223 return -1;
224 }
225
226 if (++config->fifo_out == INPUT_BUFFER_LEN)
227 config->fifo_out = 0;
228
229 return config->fifo[config->fifo_out];
230}
231
232/**
233 * Process a modifier/special key press or release and decide which key
234 * translation array should be used as a result.
235 *
236 * TODO: Should keep track of modifier press/release
237 *
238 * @param config Input state
239 * @param key Key code to process
240 * @param release 0 if a press, 1 if a release
241 * @return pointer to keycode->ascii translation table that should be used
242 */
243static struct input_key_xlate *process_modifier(struct input_config *config,
244 int key, int release)
245{
Bin Mengcd810912015-11-12 05:33:02 -0800246#ifdef CONFIG_DM_KEYBOARD
247 struct udevice *dev = config->dev;
248 struct keyboard_ops *ops = keyboard_get_ops(dev);
249#endif
Simon Glass9bc590e2012-04-17 09:01:30 +0000250 struct input_key_xlate *table;
Simon Glass9bc590e2012-04-17 09:01:30 +0000251 int i;
252
253 /* Start with the main table, and see what modifiers change it */
254 assert(config->num_tables > 0);
255 table = &config->table[0];
256 for (i = 1; i < config->num_tables; i++) {
257 struct input_key_xlate *tab = &config->table[i];
258
259 if (key == tab->left_keycode || key == tab->right_keycode)
260 table = tab;
261 }
262
263 /* Handle the lighted keys */
264 if (!release) {
Simon Glassa683d0d2015-11-11 10:05:38 -0700265 int flip = -1;
266
Simon Glass9bc590e2012-04-17 09:01:30 +0000267 switch (key) {
268 case KEY_SCROLLLOCK:
269 flip = FLAG_SCROLL_LOCK;
270 break;
271 case KEY_NUMLOCK:
272 flip = FLAG_NUM_LOCK;
273 break;
274 case KEY_CAPSLOCK:
275 flip = FLAG_CAPS_LOCK;
276 break;
277 }
Simon Glass9bc590e2012-04-17 09:01:30 +0000278
Simon Glassa683d0d2015-11-11 10:05:38 -0700279 if (flip != -1) {
280 int leds = 0;
Simon Glass9bc590e2012-04-17 09:01:30 +0000281
Bin Meng533c81a2015-11-12 05:33:01 -0800282 config->flags ^= flip;
Simon Glassa683d0d2015-11-11 10:05:38 -0700283 if (config->flags & FLAG_NUM_LOCK)
284 leds |= INPUT_LED_NUM;
285 if (config->flags & FLAG_CAPS_LOCK)
286 leds |= INPUT_LED_CAPS;
287 if (config->flags & FLAG_SCROLL_LOCK)
288 leds |= INPUT_LED_SCROLL;
289 config->leds = leds;
Simon Glass3b5f6f52015-11-11 10:05:40 -0700290 config->leds_changed = flip;
Bin Mengcd810912015-11-12 05:33:02 -0800291
292#ifdef CONFIG_DM_KEYBOARD
293 if (ops->update_leds) {
294 if (ops->update_leds(dev, config->leds))
295 debug("Update keyboard's LED failed\n");
296 }
297#endif
Simon Glassa683d0d2015-11-11 10:05:38 -0700298 }
Simon Glass9bc590e2012-04-17 09:01:30 +0000299 }
300
301 return table;
302}
303
304/**
305 * Search an int array for a key value
306 *
307 * @param array Array to search
308 * @param count Number of elements in array
309 * @param key Key value to find
310 * @return element where value was first found, -1 if none
311 */
312static int array_search(int *array, int count, int key)
313{
314 int i;
315
316 for (i = 0; i < count; i++) {
317 if (array[i] == key)
318 return i;
319 }
320
321 return -1;
322}
323
324/**
325 * Sort an array so that those elements that exist in the ordering are
326 * first in the array, and in the same order as the ordering. The algorithm
327 * is O(count * ocount) and designed for small arrays.
328 *
329 * TODO: Move this to common / lib?
330 *
331 * @param dest Array with elements to sort, also destination array
332 * @param count Number of elements to sort
333 * @param order Array containing ordering elements
334 * @param ocount Number of ordering elements
335 * @return number of elements in dest that are in order (these will be at the
336 * start of dest).
337 */
338static int sort_array_by_ordering(int *dest, int count, int *order,
339 int ocount)
340{
341 int temp[count];
342 int dest_count;
343 int same; /* number of elements which are the same */
344 int i;
345
346 /* setup output items, copy items to be sorted into our temp area */
347 memcpy(temp, dest, count * sizeof(*dest));
348 dest_count = 0;
349
350 /* work through the ordering, move over the elements we agree on */
351 for (i = 0; i < ocount; i++) {
352 if (array_search(temp, count, order[i]) != -1)
353 dest[dest_count++] = order[i];
354 }
355 same = dest_count;
356
357 /* now move over the elements that are not in the ordering */
358 for (i = 0; i < count; i++) {
359 if (array_search(order, ocount, temp[i]) == -1)
360 dest[dest_count++] = temp[i];
361 }
362 assert(dest_count == count);
363 return same;
364}
365
366/**
367 * Check a list of key codes against the previous key scan
368 *
369 * Given a list of new key codes, we check how many of these are the same
370 * as last time.
371 *
372 * @param config Input state
373 * @param keycode List of key codes to examine
374 * @param num_keycodes Number of key codes
375 * @param same Returns number of key codes which are the same
376 */
377static int input_check_keycodes(struct input_config *config,
378 int keycode[], int num_keycodes, int *same)
379{
380 /* Select the 'plain' xlate table to start with */
381 if (!config->num_tables) {
382 debug("%s: No xlate tables: cannot decode keys\n", __func__);
383 return -1;
384 }
385
386 /* sort the keycodes into the same order as the previous ones */
387 *same = sort_array_by_ordering(keycode, num_keycodes,
388 config->prev_keycodes, config->num_prev_keycodes);
389
390 memcpy(config->prev_keycodes, keycode, num_keycodes * sizeof(int));
391 config->num_prev_keycodes = num_keycodes;
392
393 return *same != num_keycodes;
394}
395
396/**
Hung-Te Lin44abe472012-10-11 15:15:53 +0000397 * Checks and converts a special key code into ANSI 3.64 escape sequence.
398 *
399 * @param config Input state
400 * @param keycode Key code to examine
401 * @param output_ch Buffer to place output characters into. It should
402 * be at least ANSI_CHAR_MAX bytes long, to allow for
403 * an ANSI sequence.
404 * @param max_chars Maximum number of characters to add to output_ch
405 * @return number of characters output, if the key was converted, otherwise 0.
406 * This may be larger than max_chars, in which case the overflow
407 * characters are not output.
408 */
409static int input_keycode_to_ansi364(struct input_config *config,
410 int keycode, char output_ch[], int max_chars)
411{
412 const char *escape;
413 int ch_count;
414 int i;
415
416 for (i = ch_count = 0; i < ARRAY_SIZE(kbd_to_ansi364); i++) {
417 if (keycode != kbd_to_ansi364[i].kbd_scan_code)
418 continue;
419 for (escape = kbd_to_ansi364[i].escape; *escape; escape++) {
420 if (ch_count < max_chars)
421 output_ch[ch_count] = *escape;
422 ch_count++;
423 }
424 return ch_count;
425 }
426
427 return 0;
428}
429
430/**
431 * Converts and queues a list of key codes in escaped ASCII string form
Simon Glass9bc590e2012-04-17 09:01:30 +0000432 * Convert a list of key codes into ASCII
433 *
434 * You must call input_check_keycodes() before this. It turns the keycode
Hung-Te Lin44abe472012-10-11 15:15:53 +0000435 * list into a list of ASCII characters and sends them to the input layer.
Simon Glass9bc590e2012-04-17 09:01:30 +0000436 *
437 * Characters which were seen last time do not generate fresh ASCII output.
Hung-Te Lin44abe472012-10-11 15:15:53 +0000438 * The output (calls to queue_ascii) may be longer than num_keycodes, if the
439 * keycode contains special keys that was encoded to longer escaped sequence.
Simon Glass9bc590e2012-04-17 09:01:30 +0000440 *
441 * @param config Input state
442 * @param keycode List of key codes to examine
443 * @param num_keycodes Number of key codes
Hung-Te Lin44abe472012-10-11 15:15:53 +0000444 * @param output_ch Buffer to place output characters into. It should
445 * be at last ANSI_CHAR_MAX * num_keycodes, to allow for
446 * ANSI sequences.
447 * @param max_chars Maximum number of characters to add to output_ch
Simon Glass9bc590e2012-04-17 09:01:30 +0000448 * @param same Number of key codes which are the same
Hung-Te Lin44abe472012-10-11 15:15:53 +0000449 * @return number of characters written into output_ch, or -1 if we would
450 * exceed max_chars chars.
Simon Glass9bc590e2012-04-17 09:01:30 +0000451 */
452static int input_keycodes_to_ascii(struct input_config *config,
Hung-Te Lin44abe472012-10-11 15:15:53 +0000453 int keycode[], int num_keycodes, char output_ch[],
454 int max_chars, int same)
Simon Glass9bc590e2012-04-17 09:01:30 +0000455{
456 struct input_key_xlate *table;
Hung-Te Lin44abe472012-10-11 15:15:53 +0000457 int ch_count = 0;
Simon Glass9bc590e2012-04-17 09:01:30 +0000458 int i;
459
460 table = &config->table[0];
461
462 /* deal with modifiers first */
463 for (i = 0; i < num_keycodes; i++) {
464 int key = keycode[i] & KEY_MASK;
465
466 if (key >= table->num_entries || table->xlate[key] == 0xff) {
467 table = process_modifier(config, key,
468 keycode[i] & KEY_RELEASE);
469 }
470 }
471
Hung-Te Lin44abe472012-10-11 15:15:53 +0000472 /* Start conversion by looking for the first new keycode (by same). */
473 for (i = same; i < num_keycodes; i++) {
Simon Glass9bc590e2012-04-17 09:01:30 +0000474 int key = keycode[i];
Simon Glassba420342015-11-11 10:05:39 -0700475 int ch;
Simon Glass9bc590e2012-04-17 09:01:30 +0000476
Hung-Te Lin44abe472012-10-11 15:15:53 +0000477 /*
478 * For a normal key (with an ASCII value), add it; otherwise
479 * translate special key to escape sequence if possible.
480 */
Simon Glassba420342015-11-11 10:05:39 -0700481 if (key < table->num_entries) {
482 ch = table->xlate[key];
483 if ((config->flags & FLAG_CAPS_LOCK) &&
484 ch >= 'a' && ch <= 'z')
485 ch -= 'a' - 'A';
Bin Menge5f330c2015-11-12 05:33:04 -0800486 /* ban digit numbers if 'Num Lock' is not on */
487 if (!(config->flags & FLAG_NUM_LOCK)) {
488 if (key >= KEY_KP7 && key <= KEY_KPDOT &&
489 key != KEY_KPMINUS && key != KEY_KPPLUS)
490 ch = 0xff;
491 }
Simon Glassba420342015-11-11 10:05:39 -0700492 if (ch_count < max_chars && ch != 0xff)
493 output_ch[ch_count++] = (uchar)ch;
Hung-Te Lin44abe472012-10-11 15:15:53 +0000494 } else {
495 ch_count += input_keycode_to_ansi364(config, key,
496 output_ch, max_chars);
Simon Glass9bc590e2012-04-17 09:01:30 +0000497 }
498 }
499
Hung-Te Lin44abe472012-10-11 15:15:53 +0000500 if (ch_count > max_chars) {
501 debug("%s: Output char buffer overflow size=%d, need=%d\n",
502 __func__, max_chars, ch_count);
503 return -1;
504 }
505
Simon Glass9bc590e2012-04-17 09:01:30 +0000506 /* ok, so return keys */
507 return ch_count;
508}
509
Simon Glass3a85e432015-10-18 21:17:24 -0600510static int _input_send_keycodes(struct input_config *config, int keycode[],
511 int num_keycodes, bool do_send)
Simon Glass9bc590e2012-04-17 09:01:30 +0000512{
Hung-Te Lin44abe472012-10-11 15:15:53 +0000513 char ch[num_keycodes * ANSI_CHAR_MAX];
Simon Glass9bc590e2012-04-17 09:01:30 +0000514 int count, i, same = 0;
515 int is_repeat = 0;
516 unsigned delay_ms;
517
518 config->modifiers = 0;
519 if (!input_check_keycodes(config, keycode, num_keycodes, &same)) {
520 /*
521 * Same as last time - is it time for another repeat?
522 * TODO(sjg@chromium.org) We drop repeats here and since
523 * the caller may not call in again for a while, our
524 * auto-repeat speed is not quite correct. We should
525 * insert another character if we later realise that we
526 * have missed a repeat slot.
527 */
Simon Glass0b186c02015-10-18 21:17:25 -0600528 is_repeat = config->allow_repeats || (config->repeat_rate_ms &&
529 (int)get_timer(config->next_repeat_ms) >= 0);
Simon Glass9bc590e2012-04-17 09:01:30 +0000530 if (!is_repeat)
531 return 0;
532 }
533
534 count = input_keycodes_to_ascii(config, keycode, num_keycodes,
Hung-Te Lin44abe472012-10-11 15:15:53 +0000535 ch, sizeof(ch), is_repeat ? 0 : same);
Simon Glass3a85e432015-10-18 21:17:24 -0600536 if (do_send) {
537 for (i = 0; i < count; i++)
538 input_queue_ascii(config, ch[i]);
539 }
Simon Glass9bc590e2012-04-17 09:01:30 +0000540 delay_ms = is_repeat ?
541 config->repeat_rate_ms :
542 config->repeat_delay_ms;
543
544 config->next_repeat_ms = get_timer(0) + delay_ms;
Hung-Te Lin44abe472012-10-11 15:15:53 +0000545
546 return count;
Simon Glass9bc590e2012-04-17 09:01:30 +0000547}
548
Simon Glass3a85e432015-10-18 21:17:24 -0600549int input_send_keycodes(struct input_config *config, int keycode[],
550 int num_keycodes)
551{
552 return _input_send_keycodes(config, keycode, num_keycodes, true);
553}
554
555int input_add_keycode(struct input_config *config, int new_keycode,
556 bool release)
557{
558 int keycode[INPUT_MAX_MODIFIERS + 1];
559 int count, i;
560
561 /* Add the old keycodes which are not removed by this new one */
562 for (i = 0, count = 0; i < config->num_prev_keycodes; i++) {
563 int code = config->prev_keycodes[i];
564
565 if (new_keycode == code) {
566 if (release)
567 continue;
568 new_keycode = -1;
569 }
570 keycode[count++] = code;
571 }
572
573 if (!release && new_keycode != -1)
574 keycode[count++] = new_keycode;
575 debug("\ncodes for %02x/%d: ", new_keycode, release);
576 for (i = 0; i < count; i++)
577 debug("%02x ", keycode[i]);
578 debug("\n");
579
580 /* Don't output any ASCII characters if this is a key release */
581 return _input_send_keycodes(config, keycode, count, !release);
582}
583
Simon Glass9bc590e2012-04-17 09:01:30 +0000584int input_add_table(struct input_config *config, int left_keycode,
585 int right_keycode, const uchar *xlate, int num_entries)
586{
587 struct input_key_xlate *table;
588
589 if (config->num_tables == INPUT_MAX_MODIFIERS) {
590 debug("%s: Too many modifier tables\n", __func__);
591 return -1;
592 }
593
594 table = &config->table[config->num_tables++];
595 table->left_keycode = left_keycode;
596 table->right_keycode = right_keycode;
597 table->xlate = xlate;
598 table->num_entries = num_entries;
599
600 return 0;
601}
602
Simon Glass1b1d3e62012-09-27 15:18:41 +0000603void input_set_delays(struct input_config *config, int repeat_delay_ms,
Simon Glass9bc590e2012-04-17 09:01:30 +0000604 int repeat_rate_ms)
605{
Simon Glass1b1d3e62012-09-27 15:18:41 +0000606 config->repeat_delay_ms = repeat_delay_ms;
607 config->repeat_rate_ms = repeat_rate_ms;
608}
609
Simon Glass0b186c02015-10-18 21:17:25 -0600610void input_allow_repeats(struct input_config *config, bool allow_repeats)
611{
612 config->allow_repeats = allow_repeats;
613}
614
Simon Glass3b5f6f52015-11-11 10:05:40 -0700615int input_leds_changed(struct input_config *config)
616{
617 if (config->leds_changed)
618 return config->leds;
619
620 return -1;
621}
622
Simon Glassb1d7a182015-11-11 10:05:37 -0700623int input_add_tables(struct input_config *config, bool german)
Simon Glass66877b02015-10-18 21:17:13 -0600624{
Simon Glassb1d7a182015-11-11 10:05:37 -0700625 struct kbd_entry *entry;
626 int mask;
Simon Glass66877b02015-10-18 21:17:13 -0600627 int ret;
628
Simon Glassb1d7a182015-11-11 10:05:37 -0700629 mask = german ? KBD_GERMAN : KBD_ENGLISH;
630 for (entry = kbd_entry; entry->kbd_mask; entry++) {
631 if (!(mask & entry->kbd_mask))
632 continue;
633 ret = input_add_table(config, entry->left_keycode,
634 entry->right_keycode, entry->xlate,
635 entry->num_entries);
636 if (ret)
637 return ret;
638 }
Simon Glass66877b02015-10-18 21:17:13 -0600639
Simon Glassb1d7a182015-11-11 10:05:37 -0700640 return 0;
Simon Glass66877b02015-10-18 21:17:13 -0600641}
642
Simon Glass1b1d3e62012-09-27 15:18:41 +0000643int input_init(struct input_config *config, int leds)
644{
Simon Glass9bc590e2012-04-17 09:01:30 +0000645 memset(config, '\0', sizeof(*config));
646 config->leds = leds;
Simon Glass9bc590e2012-04-17 09:01:30 +0000647
648 return 0;
649}
650
651int input_stdio_register(struct stdio_dev *dev)
652{
653 int error;
654
655 error = stdio_register(dev);
Simon Glass985ca392018-10-01 12:22:10 -0600656#if !defined(CONFIG_SPL_BUILD) || CONFIG_IS_ENABLED(ENV_SUPPORT)
Simon Glass9bc590e2012-04-17 09:01:30 +0000657 /* check if this is the standard input device */
Simon Glass00caae62017-08-03 12:22:12 -0600658 if (!error && strcmp(env_get("stdin"), dev->name) == 0) {
Simon Glass9bc590e2012-04-17 09:01:30 +0000659 /* reassign the console */
660 if (OVERWRITE_CONSOLE ||
661 console_assign(stdin, dev->name))
662 return -1;
663 }
Simon Glass985ca392018-10-01 12:22:10 -0600664#else
665 error = error;
666#endif
Simon Glass9bc590e2012-04-17 09:01:30 +0000667
668 return 0;
669}