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