key_matrix.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. * Manage Keyboard Matrices
  3. *
  4. * Copyright (c) 2012 The Chromium OS Authors.
  5. * (C) Copyright 2004 DENX Software Engineering, Wolfgang Denk, wd@denx.de
  6. *
  7. * See file CREDITS for list of people who contributed to this
  8. * project.
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License as
  12. * published by the Free Software Foundation; either version 2 of
  13. * the License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  23. * MA 02111-1307 USA
  24. */
  25. #include <common.h>
  26. #include <fdtdec.h>
  27. #include <key_matrix.h>
  28. #include <malloc.h>
  29. #include <linux/input.h>
  30. /**
  31. * Determine if the current keypress configuration can cause key ghosting
  32. *
  33. * We figure this out by seeing if we have two or more keys in the same
  34. * column, as well as two or more keys in the same row.
  35. *
  36. * @param config Keyboard matrix config
  37. * @param keys List of keys to check
  38. * @param valid Number of valid keypresses to check
  39. * @return 0 if no ghosting is possible, 1 if it is
  40. */
  41. static int has_ghosting(struct key_matrix *config, struct key_matrix_key *keys,
  42. int valid)
  43. {
  44. int key_in_same_col = 0, key_in_same_row = 0;
  45. int i, j;
  46. if (!config->ghost_filter || valid < 3)
  47. return 0;
  48. for (i = 0; i < valid; i++) {
  49. /*
  50. * Find 2 keys such that one key is in the same row
  51. * and the other is in the same column as the i-th key.
  52. */
  53. for (j = i + 1; j < valid; j++) {
  54. if (keys[j].col == keys[i].col)
  55. key_in_same_col = 1;
  56. if (keys[j].row == keys[i].row)
  57. key_in_same_row = 1;
  58. }
  59. }
  60. if (key_in_same_col && key_in_same_row)
  61. return 1;
  62. else
  63. return 0;
  64. }
  65. int key_matrix_decode(struct key_matrix *config, struct key_matrix_key keys[],
  66. int num_keys, int keycode[], int max_keycodes)
  67. {
  68. const u8 *keymap;
  69. int valid, upto;
  70. int pos;
  71. debug("%s: num_keys = %d\n", __func__, num_keys);
  72. keymap = config->plain_keycode;
  73. for (valid = upto = 0; upto < num_keys; upto++) {
  74. struct key_matrix_key *key = &keys[upto];
  75. debug(" valid=%d, row=%d, col=%d\n", key->valid, key->row,
  76. key->col);
  77. if (!key->valid)
  78. continue;
  79. pos = key->row * config->num_cols + key->col;
  80. if (config->fn_keycode && pos == config->fn_pos)
  81. keymap = config->fn_keycode;
  82. /* Convert the (row, col) values into a keycode */
  83. if (valid < max_keycodes)
  84. keycode[valid++] = keymap[pos];
  85. debug(" keycode=%d\n", keymap[pos]);
  86. }
  87. /* For a ghost key config, ignore the keypresses for this iteration. */
  88. if (has_ghosting(config, keys, valid)) {
  89. valid = 0;
  90. debug(" ghosting detected!\n");
  91. }
  92. debug(" %d valid keycodes found\n", valid);
  93. return valid;
  94. }
  95. /**
  96. * Create a new keycode map from some provided data
  97. *
  98. * This decodes a keycode map in the format used by the fdt, which is one
  99. * word per entry, with the row, col and keycode encoded in that word.
  100. *
  101. * We create a (row x col) size byte array with each entry containing the
  102. * keycode for that (row, col). We also search for map_keycode and return
  103. * its position if found (this is used for finding the Fn key).
  104. *
  105. * @param config Key matrix dimensions structure
  106. * @param data Keycode data
  107. * @param len Number of entries in keycode table
  108. * @param map_keycode Key code to find in the map
  109. * @param pos Returns position of map_keycode, if found, else -1
  110. * @return map Pointer to allocated map
  111. */
  112. static uchar *create_keymap(struct key_matrix *config, u32 *data, int len,
  113. int map_keycode, int *pos)
  114. {
  115. uchar *map;
  116. if (pos)
  117. *pos = -1;
  118. map = (uchar *)calloc(1, config->key_count);
  119. if (!map) {
  120. debug("%s: failed to malloc %d bytes\n", __func__,
  121. config->key_count);
  122. return NULL;
  123. }
  124. for (; len >= sizeof(u32); data++, len -= 4) {
  125. u32 tmp = fdt32_to_cpu(*data);
  126. int key_code, row, col;
  127. int entry;
  128. row = (tmp >> 24) & 0xff;
  129. col = (tmp >> 16) & 0xff;
  130. key_code = tmp & 0xffff;
  131. entry = row * config->num_cols + col;
  132. map[entry] = key_code;
  133. debug(" map %d, %d: pos=%d, keycode=%d\n", row, col,
  134. entry, key_code);
  135. if (pos && map_keycode == key_code)
  136. *pos = entry;
  137. }
  138. return map;
  139. }
  140. int key_matrix_decode_fdt(struct key_matrix *config, const void *blob,
  141. int node)
  142. {
  143. const struct fdt_property *prop;
  144. const char prefix[] = "linux,";
  145. int plen = sizeof(prefix) - 1;
  146. int offset;
  147. /* Check each property name for ones that we understand */
  148. for (offset = fdt_first_property_offset(blob, node);
  149. offset > 0;
  150. offset = fdt_next_property_offset(blob, offset)) {
  151. const char *name;
  152. int len;
  153. prop = fdt_get_property_by_offset(blob, offset, NULL);
  154. name = fdt_string(blob, fdt32_to_cpu(prop->nameoff));
  155. len = strlen(name);
  156. /* Name needs to match "1,<type>keymap" */
  157. debug("%s: property '%s'\n", __func__, name);
  158. if (strncmp(name, prefix, plen) ||
  159. len < plen + 6 ||
  160. strcmp(name + len - 6, "keymap"))
  161. continue;
  162. len -= plen + 6;
  163. if (len == 0) {
  164. config->plain_keycode = create_keymap(config,
  165. (u32 *)prop->data, fdt32_to_cpu(prop->len),
  166. KEY_FN, &config->fn_pos);
  167. } else if (0 == strncmp(name + plen, "fn-", len)) {
  168. config->fn_keycode = create_keymap(config,
  169. (u32 *)prop->data, fdt32_to_cpu(prop->len),
  170. -1, NULL);
  171. } else {
  172. debug("%s: unrecognised property '%s'\n", __func__,
  173. name);
  174. }
  175. }
  176. debug("%s: Decoded key maps %p, %p from fdt\n", __func__,
  177. config->plain_keycode, config->fn_keycode);
  178. if (!config->plain_keycode) {
  179. debug("%s: cannot find keycode-plain map\n", __func__);
  180. return -1;
  181. }
  182. return 0;
  183. }
  184. int key_matrix_init(struct key_matrix *config, int rows, int cols,
  185. int ghost_filter)
  186. {
  187. memset(config, '\0', sizeof(*config));
  188. config->num_rows = rows;
  189. config->num_cols = cols;
  190. config->key_count = rows * cols;
  191. config->ghost_filter = ghost_filter;
  192. assert(config->key_count > 0);
  193. return 0;
  194. }