blob: 12a3ad83296da259306a3bd6c2d934c0e862695d [file] [log] [blame]
Dmitry Torokhov36203c42009-12-04 10:22:23 -08001/*
2 * Generic support for sparse keymaps
3 *
4 * Copyright (c) 2009 Dmitry Torokhov
5 *
6 * Derived from wistron button driver:
7 * Copyright (C) 2005 Miloslav Trmac <mitr@volny.cz>
8 * Copyright (C) 2005 Bernhard Rosenkraenzer <bero@arklinux.org>
9 * Copyright (C) 2005 Dmitry Torokhov <dtor@mail.ru>
10 *
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License version 2 as published by
13 * the Free Software Foundation.
14 */
15
16#include <linux/input.h>
17#include <linux/input/sparse-keymap.h>
Paul Gortmakerd2d84422011-07-03 13:53:48 -040018#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090019#include <linux/slab.h>
Dmitry Torokhov36203c42009-12-04 10:22:23 -080020
21MODULE_AUTHOR("Dmitry Torokhov <dtor@mail.ru>");
22MODULE_DESCRIPTION("Generic support for sparse keymaps");
23MODULE_LICENSE("GPL v2");
24MODULE_VERSION("0.1");
25
Dmitry Torokhov67127f32010-09-09 21:54:22 -070026static unsigned int sparse_keymap_get_key_index(struct input_dev *dev,
27 const struct key_entry *k)
28{
29 struct key_entry *key;
30 unsigned int idx = 0;
31
32 for (key = dev->keycode; key->type != KE_END; key++) {
33 if (key->type == KE_KEY) {
34 if (key == k)
35 break;
36 idx++;
37 }
38 }
39
40 return idx;
41}
42
43static struct key_entry *sparse_keymap_entry_by_index(struct input_dev *dev,
44 unsigned int index)
45{
46 struct key_entry *key;
47 unsigned int key_cnt = 0;
48
49 for (key = dev->keycode; key->type != KE_END; key++)
50 if (key->type == KE_KEY)
51 if (key_cnt++ == index)
52 return key;
53
54 return NULL;
55}
56
Dmitry Torokhov36203c42009-12-04 10:22:23 -080057/**
58 * sparse_keymap_entry_from_scancode - perform sparse keymap lookup
59 * @dev: Input device using sparse keymap
60 * @code: Scan code
61 *
62 * This function is used to perform &struct key_entry lookup in an
63 * input device using sparse keymap.
64 */
65struct key_entry *sparse_keymap_entry_from_scancode(struct input_dev *dev,
66 unsigned int code)
67{
68 struct key_entry *key;
69
70 for (key = dev->keycode; key->type != KE_END; key++)
71 if (code == key->code)
72 return key;
73
74 return NULL;
75}
76EXPORT_SYMBOL(sparse_keymap_entry_from_scancode);
77
78/**
79 * sparse_keymap_entry_from_keycode - perform sparse keymap lookup
80 * @dev: Input device using sparse keymap
81 * @keycode: Key code
82 *
83 * This function is used to perform &struct key_entry lookup in an
84 * input device using sparse keymap.
85 */
86struct key_entry *sparse_keymap_entry_from_keycode(struct input_dev *dev,
87 unsigned int keycode)
88{
89 struct key_entry *key;
90
91 for (key = dev->keycode; key->type != KE_END; key++)
92 if (key->type == KE_KEY && keycode == key->keycode)
93 return key;
94
95 return NULL;
96}
97EXPORT_SYMBOL(sparse_keymap_entry_from_keycode);
98
Dmitry Torokhov67127f32010-09-09 21:54:22 -070099static struct key_entry *sparse_keymap_locate(struct input_dev *dev,
100 const struct input_keymap_entry *ke)
101{
102 struct key_entry *key;
103 unsigned int scancode;
104
105 if (ke->flags & INPUT_KEYMAP_BY_INDEX)
106 key = sparse_keymap_entry_by_index(dev, ke->index);
107 else if (input_scancode_to_scalar(ke, &scancode) == 0)
108 key = sparse_keymap_entry_from_scancode(dev, scancode);
109 else
110 key = NULL;
111
112 return key;
113}
114
Dmitry Torokhov36203c42009-12-04 10:22:23 -0800115static int sparse_keymap_getkeycode(struct input_dev *dev,
Dmitry Torokhov67127f32010-09-09 21:54:22 -0700116 struct input_keymap_entry *ke)
Dmitry Torokhov36203c42009-12-04 10:22:23 -0800117{
Dmitry Torokhov2e2e3b92010-03-21 22:56:15 -0700118 const struct key_entry *key;
Dmitry Torokhov36203c42009-12-04 10:22:23 -0800119
Dmitry Torokhov2e2e3b92010-03-21 22:56:15 -0700120 if (dev->keycode) {
Dmitry Torokhov67127f32010-09-09 21:54:22 -0700121 key = sparse_keymap_locate(dev, ke);
Dmitry Torokhov2e2e3b92010-03-21 22:56:15 -0700122 if (key && key->type == KE_KEY) {
Dmitry Torokhov67127f32010-09-09 21:54:22 -0700123 ke->keycode = key->keycode;
124 if (!(ke->flags & INPUT_KEYMAP_BY_INDEX))
125 ke->index =
126 sparse_keymap_get_key_index(dev, key);
127 ke->len = sizeof(key->code);
128 memcpy(ke->scancode, &key->code, sizeof(key->code));
Dmitry Torokhov2e2e3b92010-03-21 22:56:15 -0700129 return 0;
130 }
Dmitry Torokhov36203c42009-12-04 10:22:23 -0800131 }
132
133 return -EINVAL;
134}
135
136static int sparse_keymap_setkeycode(struct input_dev *dev,
Dmitry Torokhov67127f32010-09-09 21:54:22 -0700137 const struct input_keymap_entry *ke,
138 unsigned int *old_keycode)
Dmitry Torokhov36203c42009-12-04 10:22:23 -0800139{
140 struct key_entry *key;
Dmitry Torokhov36203c42009-12-04 10:22:23 -0800141
Dmitry Torokhov2e2e3b92010-03-21 22:56:15 -0700142 if (dev->keycode) {
Dmitry Torokhov67127f32010-09-09 21:54:22 -0700143 key = sparse_keymap_locate(dev, ke);
Dmitry Torokhov2e2e3b92010-03-21 22:56:15 -0700144 if (key && key->type == KE_KEY) {
Dmitry Torokhov67127f32010-09-09 21:54:22 -0700145 *old_keycode = key->keycode;
146 key->keycode = ke->keycode;
147 set_bit(ke->keycode, dev->keybit);
148 if (!sparse_keymap_entry_from_keycode(dev, *old_keycode))
149 clear_bit(*old_keycode, dev->keybit);
Dmitry Torokhov2e2e3b92010-03-21 22:56:15 -0700150 return 0;
151 }
Dmitry Torokhov36203c42009-12-04 10:22:23 -0800152 }
153
154 return -EINVAL;
155}
156
157/**
158 * sparse_keymap_setup - set up sparse keymap for an input device
159 * @dev: Input device
160 * @keymap: Keymap in form of array of &key_entry structures ending
161 * with %KE_END type entry
162 * @setup: Function that can be used to adjust keymap entries
Michał Kępieńfabeb162017-03-08 09:20:10 -0800163 * depending on device's needs, may be %NULL
Dmitry Torokhov36203c42009-12-04 10:22:23 -0800164 *
165 * The function calculates size and allocates copy of the original
166 * keymap after which sets up input device event bits appropriately.
Michał Kępieńfabeb162017-03-08 09:20:10 -0800167 * The allocated copy of the keymap is automatically freed when it
168 * is no longer needed.
Dmitry Torokhov36203c42009-12-04 10:22:23 -0800169 */
170int sparse_keymap_setup(struct input_dev *dev,
171 const struct key_entry *keymap,
172 int (*setup)(struct input_dev *, struct key_entry *))
173{
174 size_t map_size = 1; /* to account for the last KE_END entry */
175 const struct key_entry *e;
176 struct key_entry *map, *entry;
177 int i;
178 int error;
179
180 for (e = keymap; e->type != KE_END; e++)
181 map_size++;
182
Michał Kępieńfabeb162017-03-08 09:20:10 -0800183 map = devm_kmemdup(&dev->dev, keymap, map_size * sizeof(*map),
184 GFP_KERNEL);
Dmitry Torokhov36203c42009-12-04 10:22:23 -0800185 if (!map)
186 return -ENOMEM;
187
Dmitry Torokhov36203c42009-12-04 10:22:23 -0800188 for (i = 0; i < map_size; i++) {
189 entry = &map[i];
190
191 if (setup) {
192 error = setup(dev, entry);
193 if (error)
Michał Kępieńfabeb162017-03-08 09:20:10 -0800194 return error;
Dmitry Torokhov36203c42009-12-04 10:22:23 -0800195 }
196
197 switch (entry->type) {
198 case KE_KEY:
199 __set_bit(EV_KEY, dev->evbit);
200 __set_bit(entry->keycode, dev->keybit);
201 break;
202
203 case KE_SW:
Dmitry Torokhovcb1b1452011-01-20 23:05:17 -0800204 case KE_VSW:
Dmitry Torokhov36203c42009-12-04 10:22:23 -0800205 __set_bit(EV_SW, dev->evbit);
206 __set_bit(entry->sw.code, dev->swbit);
207 break;
208 }
209 }
210
Seth Forsheef3cf5c42011-03-26 22:53:34 -0700211 if (test_bit(EV_KEY, dev->evbit)) {
Seth Forshee170531b2011-03-26 22:53:34 -0700212 __set_bit(KEY_UNKNOWN, dev->keybit);
Seth Forsheef3cf5c42011-03-26 22:53:34 -0700213 __set_bit(EV_MSC, dev->evbit);
214 __set_bit(MSC_SCAN, dev->mscbit);
215 }
216
Dmitry Torokhov36203c42009-12-04 10:22:23 -0800217 dev->keycode = map;
218 dev->keycodemax = map_size;
Dmitry Torokhovaebd6362011-01-31 21:06:39 -0800219 dev->getkeycode = sparse_keymap_getkeycode;
220 dev->setkeycode = sparse_keymap_setkeycode;
Dmitry Torokhov36203c42009-12-04 10:22:23 -0800221
222 return 0;
Dmitry Torokhov36203c42009-12-04 10:22:23 -0800223}
224EXPORT_SYMBOL(sparse_keymap_setup);
225
226/**
227 * sparse_keymap_free - free memory allocated for sparse keymap
228 * @dev: Input device using sparse keymap
229 *
Michał Kępieńfabeb162017-03-08 09:20:10 -0800230 * This function used to free memory allocated by sparse keymap
Dmitry Torokhov36203c42009-12-04 10:22:23 -0800231 * in an input device that was set up by sparse_keymap_setup().
Michał Kępieńfabeb162017-03-08 09:20:10 -0800232 * Since sparse_keymap_setup() now uses a managed allocation for the
233 * keymap copy, use of this function is deprecated.
Dmitry Torokhov36203c42009-12-04 10:22:23 -0800234 */
235void sparse_keymap_free(struct input_dev *dev)
236{
Dmitry Torokhov36203c42009-12-04 10:22:23 -0800237}
238EXPORT_SYMBOL(sparse_keymap_free);
239
240/**
241 * sparse_keymap_report_entry - report event corresponding to given key entry
242 * @dev: Input device for which event should be reported
243 * @ke: key entry describing event
244 * @value: Value that should be reported (ignored by %KE_SW entries)
245 * @autorelease: Signals whether release event should be emitted for %KE_KEY
246 * entries right after reporting press event, ignored by all other
247 * entries
248 *
249 * This function is used to report input event described by given
250 * &struct key_entry.
251 */
252void sparse_keymap_report_entry(struct input_dev *dev, const struct key_entry *ke,
253 unsigned int value, bool autorelease)
254{
255 switch (ke->type) {
256 case KE_KEY:
Seth Forsheef3cf5c42011-03-26 22:53:34 -0700257 input_event(dev, EV_MSC, MSC_SCAN, ke->code);
Dmitry Torokhov36203c42009-12-04 10:22:23 -0800258 input_report_key(dev, ke->keycode, value);
259 input_sync(dev);
260 if (value && autorelease) {
261 input_report_key(dev, ke->keycode, 0);
262 input_sync(dev);
263 }
264 break;
265
266 case KE_SW:
267 value = ke->sw.value;
268 /* fall through */
269
270 case KE_VSW:
271 input_report_switch(dev, ke->sw.code, value);
272 break;
273 }
274}
275EXPORT_SYMBOL(sparse_keymap_report_entry);
276
277/**
278 * sparse_keymap_report_event - report event corresponding to given scancode
279 * @dev: Input device using sparse keymap
280 * @code: Scan code
281 * @value: Value that should be reported (ignored by %KE_SW entries)
282 * @autorelease: Signals whether release event should be emitted for %KE_KEY
283 * entries right after reporting press event, ignored by all other
284 * entries
285 *
286 * This function is used to perform lookup in an input device using sparse
287 * keymap and report corresponding event. Returns %true if lookup was
288 * successful and %false otherwise.
289 */
290bool sparse_keymap_report_event(struct input_dev *dev, unsigned int code,
291 unsigned int value, bool autorelease)
292{
293 const struct key_entry *ke =
294 sparse_keymap_entry_from_scancode(dev, code);
Seth Forshee170531b2011-03-26 22:53:34 -0700295 struct key_entry unknown_ke;
Dmitry Torokhov36203c42009-12-04 10:22:23 -0800296
297 if (ke) {
298 sparse_keymap_report_entry(dev, ke, value, autorelease);
299 return true;
300 }
301
Seth Forshee170531b2011-03-26 22:53:34 -0700302 /* Report an unknown key event as a debugging aid */
303 unknown_ke.type = KE_KEY;
304 unknown_ke.code = code;
305 unknown_ke.keycode = KEY_UNKNOWN;
306 sparse_keymap_report_entry(dev, &unknown_ke, value, true);
307
Dmitry Torokhov36203c42009-12-04 10:22:23 -0800308 return false;
309}
310EXPORT_SYMBOL(sparse_keymap_report_event);
311