blob: c08caa68295b8230c83f6065098535a8ac809523 [file] [log] [blame]
Bernie Thompson757342a2012-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 *
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 Linz1c7b1932012-08-02 10:49:24 +000026#include <common.h>
Bernie Thompson757342a2012-04-17 09:01:31 +000027#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 */
43static 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
49 for (i = 0; i < valid; i++) {
50 /*
51 * Find 2 keys such that one key is in the same row
52 * and the other is in the same column as the i-th key.
53 */
54 for (j = i + 1; j < valid; j++) {
55 if (keys[j].col == keys[i].col)
56 key_in_same_col = 1;
57 if (keys[j].row == keys[i].row)
58 key_in_same_row = 1;
59 }
60 }
61
62 if (key_in_same_col && key_in_same_row)
63 return 1;
64 else
65 return 0;
66}
67
68int key_matrix_decode(struct key_matrix *config, struct key_matrix_key keys[],
69 int num_keys, int keycode[], int max_keycodes)
70{
71 const u8 *keymap;
72 int valid, upto;
73 int pos;
74
75 debug("%s: num_keys = %d\n", __func__, num_keys);
76 keymap = config->plain_keycode;
77 for (valid = upto = 0; upto < num_keys; upto++) {
78 struct key_matrix_key *key = &keys[upto];
79
80 debug(" valid=%d, row=%d, col=%d\n", key->valid, key->row,
81 key->col);
82 if (!key->valid)
83 continue;
84 pos = key->row * config->num_cols + key->col;
85 if (config->fn_keycode && pos == config->fn_pos)
86 keymap = config->fn_keycode;
87
88 /* Convert the (row, col) values into a keycode */
89 if (valid < max_keycodes)
90 keycode[valid++] = keymap[pos];
91 debug(" keycode=%d\n", keymap[pos]);
92 }
93
94 /* For a ghost key config, ignore the keypresses for this iteration. */
95 if (valid >= 3 && has_ghosting(config, keys, valid)) {
96 valid = 0;
97 debug(" ghosting detected!\n");
98 }
99 debug(" %d valid keycodes found\n", valid);
100
101 return valid;
102}
103
104/**
105 * Create a new keycode map from some provided data
106 *
107 * This decodes a keycode map in the format used by the fdt, which is one
108 * word per entry, with the row, col and keycode encoded in that word.
109 *
110 * We create a (row x col) size byte array with each entry containing the
111 * keycode for that (row, col). We also search for map_keycode and return
112 * its position if found (this is used for finding the Fn key).
113 *
114 * @param config Key matrix dimensions structure
115 * @param data Keycode data
116 * @param len Number of entries in keycode table
117 * @param map_keycode Key code to find in the map
118 * @param pos Returns position of map_keycode, if found, else -1
119 * @return map Pointer to allocated map
120 */
121static uchar *create_keymap(struct key_matrix *config, u32 *data, int len,
122 int map_keycode, int *pos)
123{
124 uchar *map;
125
126 if (pos)
127 *pos = -1;
128 map = (uchar *)calloc(1, config->key_count);
129 if (!map) {
130 debug("%s: failed to malloc %d bytes\n", __func__,
131 config->key_count);
132 return NULL;
133 }
134
135 for (; len >= sizeof(u32); data++, len -= 4) {
136 u32 tmp = fdt32_to_cpu(*data);
137 int key_code, row, col;
138 int entry;
139
140 row = (tmp >> 24) & 0xff;
141 col = (tmp >> 16) & 0xff;
142 key_code = tmp & 0xffff;
143 entry = row * config->num_cols + col;
144 map[entry] = key_code;
145 if (pos && map_keycode == key_code)
146 *pos = entry;
147 }
148
149 return map;
150}
151
152int key_matrix_decode_fdt(struct key_matrix *config, const void *blob,
153 int node)
154{
155 const struct fdt_property *prop;
Simon Glassb0e051d2012-09-27 15:18:40 +0000156 const char prefix[] = "linux,";
157 int plen = sizeof(prefix) - 1;
Bernie Thompson757342a2012-04-17 09:01:31 +0000158 int offset;
159
160 /* Check each property name for ones that we understand */
161 for (offset = fdt_first_property_offset(blob, node);
162 offset > 0;
163 offset = fdt_next_property_offset(blob, offset)) {
164 const char *name;
165 int len;
166
167 prop = fdt_get_property_by_offset(blob, offset, NULL);
168 name = fdt_string(blob, fdt32_to_cpu(prop->nameoff));
169 len = strlen(name);
170
171 /* Name needs to match "1,<type>keymap" */
172 debug("%s: property '%s'\n", __func__, name);
Simon Glassb0e051d2012-09-27 15:18:40 +0000173 if (strncmp(name, prefix, plen) ||
174 len < plen + 6 ||
175 strcmp(name + len - 6, "keymap"))
Bernie Thompson757342a2012-04-17 09:01:31 +0000176 continue;
177
Simon Glassb0e051d2012-09-27 15:18:40 +0000178 len -= plen + 6;
Bernie Thompson757342a2012-04-17 09:01:31 +0000179 if (len == 0) {
180 config->plain_keycode = create_keymap(config,
181 (u32 *)prop->data, fdt32_to_cpu(prop->len),
182 KEY_FN, &config->fn_pos);
Simon Glassb0e051d2012-09-27 15:18:40 +0000183 } else if (0 == strncmp(name + plen, "fn-", len)) {
Bernie Thompson757342a2012-04-17 09:01:31 +0000184 config->fn_keycode = create_keymap(config,
185 (u32 *)prop->data, fdt32_to_cpu(prop->len),
186 -1, NULL);
187 } else {
188 debug("%s: unrecognised property '%s'\n", __func__,
189 name);
190 }
191 }
192 debug("%s: Decoded key maps %p, %p from fdt\n", __func__,
193 config->plain_keycode, config->fn_keycode);
194
195 if (!config->plain_keycode) {
196 debug("%s: cannot find keycode-plain map\n", __func__);
197 return -1;
198 }
199
200 return 0;
201}
202
203int key_matrix_init(struct key_matrix *config, int rows, int cols)
204{
205 memset(config, '\0', sizeof(*config));
206 config->num_rows = rows;
207 config->num_cols = cols;
208 config->key_count = rows * cols;
209 assert(config->key_count > 0);
210
211 return 0;
212}