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