1// SPDX-License-Identifier: GPL-2.0+
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
7 */
8
9#include <common.h>
10#include <dm.h>
11#include <key_matrix.h>
12#include <log.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
33	if (!config->ghost_filter || valid < 3)
34		return 0;
35
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
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. */
82	if (has_ghosting(config, keys, valid)) {
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
106 * Return: map  Pointer to allocated map
107 */
108static uchar *create_keymap(struct key_matrix *config, const 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;
132		debug("   map %d, %d: pos=%d, keycode=%d\n", row, col,
133		      entry, key_code);
134		if (pos && map_keycode == key_code)
135			*pos = entry;
136	}
137
138	return map;
139}
140
141int key_matrix_decode_fdt(struct udevice *dev, struct key_matrix *config)
142{
143	const u32 *prop;
144	int proplen;
145	uchar *plain_keycode;
146
147	prop = dev_read_prop(dev, "linux,keymap", &proplen);
148	/* Basic keymap is required */
149	if (!prop) {
150		debug("%s: cannot find keycode-plain map\n", __func__);
151		return -1;
152	}
153
154	plain_keycode = create_keymap(config, prop, proplen, KEY_FN,
155				      &config->fn_pos);
156	config->plain_keycode = plain_keycode;
157	/* Conversion error -> fail */
158	if (!config->plain_keycode)
159		return -1;
160
161	prop = dev_read_prop(dev, "linux,fn-keymap", &proplen);
162	/* fn keymap is optional */
163	if (!prop)
164		goto done;
165
166	config->fn_keycode = create_keymap(config, prop, proplen, -1, NULL);
167	/* Conversion error -> fail */
168	if (!config->fn_keycode) {
169		free(plain_keycode);
170		return -1;
171	}
172
173done:
174	debug("%s: Decoded key maps %p, %p from fdt\n", __func__,
175	      config->plain_keycode, config->fn_keycode);
176	return 0;
177}
178
179int key_matrix_init(struct key_matrix *config, int rows, int cols,
180		    int ghost_filter)
181{
182	memset(config, '\0', sizeof(*config));
183	config->num_rows = rows;
184	config->num_cols = cols;
185	config->key_count = rows * cols;
186	config->ghost_filter = ghost_filter;
187	assert(config->key_count > 0);
188
189	return 0;
190}
191