blob: 8867e4964e6cb5b7a43c50aacd531e89208dad14 [file] [log] [blame]
Bernie Thompson92c27c52012-04-17 09:01:31 +00001/*
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 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02007 * SPDX-License-Identifier: GPL-2.0+
Bernie Thompson92c27c52012-04-17 09:01:31 +00008 */
9
Stephan Linzc20dbf62012-08-02 10:49:24 +000010#include <common.h>
Bernie Thompson92c27c52012-04-17 09:01:31 +000011#include <fdtdec.h>
12#include <key_matrix.h>
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
25 * @return 0 if no ghosting is possible, 1 if it is
26 */
27static 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 Glass71dc6bc2012-09-27 15:18:42 +000033 if (!config->ghost_filter || valid < 3)
34 return 0;
35
Bernie Thompson92c27c52012-04-17 09:01:31 +000036 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
55int 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 Glass71dc6bc2012-09-27 15:18:42 +000082 if (has_ghosting(config, keys, valid)) {
Bernie Thompson92c27c52012-04-17 09:01:31 +000083 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
106 * @return map Pointer to allocated map
107 */
108static uchar *create_keymap(struct key_matrix *config, u32 *data, int len,
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 Glass14813f12012-09-27 15:18:43 +0000132 debug(" map %d, %d: pos=%d, keycode=%d\n", row, col,
133 entry, key_code);
Bernie Thompson92c27c52012-04-17 09:01:31 +0000134 if (pos && map_keycode == key_code)
135 *pos = entry;
136 }
137
138 return map;
139}
140
Stephen Warrendf637fa2013-05-23 12:09:57 +0000141int key_matrix_decode_fdt(struct key_matrix *config, const void *blob, int node)
Bernie Thompson92c27c52012-04-17 09:01:31 +0000142{
143 const struct fdt_property *prop;
Stephen Warrendf637fa2013-05-23 12:09:57 +0000144 int proplen;
145 uchar *plain_keycode;
Bernie Thompson92c27c52012-04-17 09:01:31 +0000146
Stephen Warrendf637fa2013-05-23 12:09:57 +0000147 prop = fdt_get_property(blob, node, "linux,keymap", &proplen);
148 /* Basic keymap is required */
Stephen Warren374e8372013-06-06 10:48:30 -0400149 if (!prop) {
150 debug("%s: cannot find keycode-plain map\n", __func__);
Stephen Warrendf637fa2013-05-23 12:09:57 +0000151 return -1;
Stephen Warren374e8372013-06-06 10:48:30 -0400152 }
Bernie Thompson92c27c52012-04-17 09:01:31 +0000153
Stephen Warrendf637fa2013-05-23 12:09:57 +0000154 plain_keycode = create_keymap(config, (u32 *)prop->data,
155 proplen, KEY_FN, &config->fn_pos);
156 config->plain_keycode = plain_keycode;
157 /* Conversion error -> fail */
158 if (!config->plain_keycode)
159 return -1;
Bernie Thompson92c27c52012-04-17 09:01:31 +0000160
Stephen Warrendf637fa2013-05-23 12:09:57 +0000161 prop = fdt_get_property(blob, node, "linux,fn-keymap", &proplen);
162 /* fn keymap is optional */
163 if (!prop)
164 goto done;
Bernie Thompson92c27c52012-04-17 09:01:31 +0000165
Stephen Warrendf637fa2013-05-23 12:09:57 +0000166 config->fn_keycode = create_keymap(config, (u32 *)prop->data,
167 proplen, -1, NULL);
168 /* Conversion error -> fail */
Stephen Warren374e8372013-06-06 10:48:30 -0400169 if (!config->fn_keycode) {
Stephen Warrendf637fa2013-05-23 12:09:57 +0000170 free(plain_keycode);
Bernie Thompson92c27c52012-04-17 09:01:31 +0000171 return -1;
172 }
173
Stephen Warrendf637fa2013-05-23 12:09:57 +0000174done:
175 debug("%s: Decoded key maps %p, %p from fdt\n", __func__,
176 config->plain_keycode, config->fn_keycode);
Bernie Thompson92c27c52012-04-17 09:01:31 +0000177 return 0;
178}
179
Simon Glass71dc6bc2012-09-27 15:18:42 +0000180int key_matrix_init(struct key_matrix *config, int rows, int cols,
181 int ghost_filter)
Bernie Thompson92c27c52012-04-17 09:01:31 +0000182{
183 memset(config, '\0', sizeof(*config));
184 config->num_rows = rows;
185 config->num_cols = cols;
186 config->key_count = rows * cols;
Simon Glass71dc6bc2012-09-27 15:18:42 +0000187 config->ghost_filter = ghost_filter;
Bernie Thompson92c27c52012-04-17 09:01:31 +0000188 assert(config->key_count > 0);
189
190 return 0;
191}