key_matrix.c 4.9 KB

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