]>
git.ipfire.org Git - people/ms/u-boot.git/blob - drivers/input/key_matrix.c
2 * Manage Keyboard Matrices
4 * Copyright (c) 2012 The Chromium OS Authors.
5 * (C) Copyright 2004 DENX Software Engineering, Wolfgang Denk, wd@denx.de
7 * SPDX-License-Identifier: GPL-2.0+
12 #include <key_matrix.h>
14 #include <linux/input.h>
17 * Determine if the current keypress configuration can cause key ghosting
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.
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
27 static int has_ghosting(struct key_matrix
*config
, struct key_matrix_key
*keys
,
30 int key_in_same_col
= 0, key_in_same_row
= 0;
33 if (!config
->ghost_filter
|| valid
< 3)
36 for (i
= 0; i
< valid
; i
++) {
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.
41 for (j
= i
+ 1; j
< valid
; j
++) {
42 if (keys
[j
].col
== keys
[i
].col
)
44 if (keys
[j
].row
== keys
[i
].row
)
49 if (key_in_same_col
&& key_in_same_row
)
55 int key_matrix_decode(struct key_matrix
*config
, struct key_matrix_key keys
[],
56 int num_keys
, int keycode
[], int max_keycodes
)
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
];
67 debug(" valid=%d, row=%d, col=%d\n", key
->valid
, key
->row
,
71 pos
= key
->row
* config
->num_cols
+ key
->col
;
72 if (config
->fn_keycode
&& pos
== config
->fn_pos
)
73 keymap
= config
->fn_keycode
;
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
]);
81 /* For a ghost key config, ignore the keypresses for this iteration. */
82 if (has_ghosting(config
, keys
, valid
)) {
84 debug(" ghosting detected!\n");
86 debug(" %d valid keycodes found\n", valid
);
92 * Create a new keycode map from some provided data
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.
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).
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
108 static uchar
*create_keymap(struct key_matrix
*config
, u32
*data
, int len
,
109 int map_keycode
, int *pos
)
115 map
= (uchar
*)calloc(1, config
->key_count
);
117 debug("%s: failed to malloc %d bytes\n", __func__
,
122 for (; len
>= sizeof(u32
); data
++, len
-= 4) {
123 u32 tmp
= fdt32_to_cpu(*data
);
124 int key_code
, row
, col
;
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
,
134 if (pos
&& map_keycode
== key_code
)
141 int key_matrix_decode_fdt(struct key_matrix
*config
, const void *blob
, int node
)
143 const struct fdt_property
*prop
;
145 uchar
*plain_keycode
;
147 prop
= fdt_get_property(blob
, node
, "linux,keymap", &proplen
);
148 /* Basic keymap is required */
150 debug("%s: cannot find keycode-plain map\n", __func__
);
154 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
)
161 prop
= fdt_get_property(blob
, node
, "linux,fn-keymap", &proplen
);
162 /* fn keymap is optional */
166 config
->fn_keycode
= create_keymap(config
, (u32
*)prop
->data
,
168 /* Conversion error -> fail */
169 if (!config
->fn_keycode
) {
175 debug("%s: Decoded key maps %p, %p from fdt\n", __func__
,
176 config
->plain_keycode
, config
->fn_keycode
);
180 int key_matrix_init(struct key_matrix
*config
, int rows
, int cols
,
183 memset(config
, '\0', sizeof(*config
));
184 config
->num_rows
= rows
;
185 config
->num_cols
= cols
;
186 config
->key_count
= rows
* cols
;
187 config
->ghost_filter
= ghost_filter
;
188 assert(config
->key_count
> 0);