input.c 20 KB

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