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