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