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