key_matrix.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Manage Keyboard Matrices
  4. *
  5. * Copyright (c) 2012 The Chromium OS Authors.
  6. * (C) Copyright 2004 DENX Software Engineering, Wolfgang Denk, wd@denx.de
  7. */
  8. #include <common.h>
  9. #include <dm.h>
  10. #include <key_matrix.h>
  11. #include <log.h>
  12. #include <malloc.h>
  13. #include <linux/input.h>
  14. /**
  15. * Determine if the current keypress configuration can cause key ghosting
  16. *
  17. * We figure this out by seeing if we have two or more keys in the same
  18. * column, as well as two or more keys in the same row.
  19. *
  20. * @param config Keyboard matrix config
  21. * @param keys List of keys to check
  22. * @param valid Number of valid keypresses to check
  23. * @return 0 if no ghosting is possible, 1 if it is
  24. */
  25. static int has_ghosting(struct key_matrix *config, struct key_matrix_key *keys,
  26. int valid)
  27. {
  28. int key_in_same_col = 0, key_in_same_row = 0;
  29. int i, j;
  30. if (!config->ghost_filter || valid < 3)
  31. return 0;
  32. for (i = 0; i < valid; i++) {
  33. /*
  34. * Find 2 keys such that one key is in the same row
  35. * and the other is in the same column as the i-th key.
  36. */
  37. for (j = i + 1; j < valid; j++) {
  38. if (keys[j].col == keys[i].col)
  39. key_in_same_col = 1;
  40. if (keys[j].row == keys[i].row)
  41. key_in_same_row = 1;
  42. }
  43. }
  44. if (key_in_same_col && key_in_same_row)
  45. return 1;
  46. else
  47. return 0;
  48. }
  49. int key_matrix_decode(struct key_matrix *config, struct key_matrix_key keys[],
  50. int num_keys, int keycode[], int max_keycodes)
  51. {
  52. const u8 *keymap;
  53. int valid, upto;
  54. int pos;
  55. debug("%s: num_keys = %d\n", __func__, num_keys);
  56. keymap = config->plain_keycode;
  57. for (valid = upto = 0; upto < num_keys; upto++) {
  58. struct key_matrix_key *key = &keys[upto];
  59. debug(" valid=%d, row=%d, col=%d\n", key->valid, key->row,
  60. key->col);
  61. if (!key->valid)
  62. continue;
  63. pos = key->row * config->num_cols + key->col;
  64. if (config->fn_keycode && pos == config->fn_pos)
  65. keymap = config->fn_keycode;
  66. /* Convert the (row, col) values into a keycode */
  67. if (valid < max_keycodes)
  68. keycode[valid++] = keymap[pos];
  69. debug(" keycode=%d\n", keymap[pos]);
  70. }
  71. /* For a ghost key config, ignore the keypresses for this iteration. */
  72. if (has_ghosting(config, keys, valid)) {
  73. valid = 0;
  74. debug(" ghosting detected!\n");
  75. }
  76. debug(" %d valid keycodes found\n", valid);
  77. return valid;
  78. }
  79. /**
  80. * Create a new keycode map from some provided data
  81. *
  82. * This decodes a keycode map in the format used by the fdt, which is one
  83. * word per entry, with the row, col and keycode encoded in that word.
  84. *
  85. * We create a (row x col) size byte array with each entry containing the
  86. * keycode for that (row, col). We also search for map_keycode and return
  87. * its position if found (this is used for finding the Fn key).
  88. *
  89. * @param config Key matrix dimensions structure
  90. * @param data Keycode data
  91. * @param len Number of entries in keycode table
  92. * @param map_keycode Key code to find in the map
  93. * @param pos Returns position of map_keycode, if found, else -1
  94. * @return map Pointer to allocated map
  95. */
  96. static uchar *create_keymap(struct key_matrix *config, const u32 *data, int len,
  97. int map_keycode, int *pos)
  98. {
  99. uchar *map;
  100. if (pos)
  101. *pos = -1;
  102. map = (uchar *)calloc(1, config->key_count);
  103. if (!map) {
  104. debug("%s: failed to malloc %d bytes\n", __func__,
  105. config->key_count);
  106. return NULL;
  107. }
  108. for (; len >= sizeof(u32); data++, len -= 4) {
  109. u32 tmp = fdt32_to_cpu(*data);
  110. int key_code, row, col;
  111. int entry;
  112. row = (tmp >> 24) & 0xff;
  113. col = (tmp >> 16) & 0xff;
  114. key_code = tmp & 0xffff;
  115. entry = row * config->num_cols + col;
  116. map[entry] = key_code;
  117. debug(" map %d, %d: pos=%d, keycode=%d\n", row, col,
  118. entry, key_code);
  119. if (pos && map_keycode == key_code)
  120. *pos = entry;
  121. }
  122. return map;
  123. }
  124. int key_matrix_decode_fdt(struct udevice *dev, struct key_matrix *config)
  125. {
  126. const u32 *prop;
  127. int proplen;
  128. uchar *plain_keycode;
  129. prop = dev_read_prop(dev, "linux,keymap", &proplen);
  130. /* Basic keymap is required */
  131. if (!prop) {
  132. debug("%s: cannot find keycode-plain map\n", __func__);
  133. return -1;
  134. }
  135. plain_keycode = create_keymap(config, prop, proplen, KEY_FN,
  136. &config->fn_pos);
  137. config->plain_keycode = plain_keycode;
  138. /* Conversion error -> fail */
  139. if (!config->plain_keycode)
  140. return -1;
  141. prop = dev_read_prop(dev, "linux,fn-keymap", &proplen);
  142. /* fn keymap is optional */
  143. if (!prop)
  144. goto done;
  145. config->fn_keycode = create_keymap(config, prop, proplen, -1, NULL);
  146. /* Conversion error -> fail */
  147. if (!config->fn_keycode) {
  148. free(plain_keycode);
  149. return -1;
  150. }
  151. done:
  152. debug("%s: Decoded key maps %p, %p from fdt\n", __func__,
  153. config->plain_keycode, config->fn_keycode);
  154. return 0;
  155. }
  156. int key_matrix_init(struct key_matrix *config, int rows, int cols,
  157. int ghost_filter)
  158. {
  159. memset(config, '\0', sizeof(*config));
  160. config->num_rows = rows;
  161. config->num_cols = cols;
  162. config->key_count = rows * cols;
  163. config->ghost_filter = ghost_filter;
  164. assert(config->key_count > 0);
  165. return 0;
  166. }