2 * Generic support for sparse keymaps
4 * Copyright (c) 2009 Dmitry Torokhov
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>
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.
16 #include <linux/input.h>
17 #include <linux/input/sparse-keymap.h>
18 #include <linux/module.h>
19 #include <linux/slab.h>
21 MODULE_AUTHOR("Dmitry Torokhov <dtor@mail.ru>");
22 MODULE_DESCRIPTION("Generic support for sparse keymaps");
23 MODULE_LICENSE("GPL v2");
24 MODULE_VERSION("0.1");
26 static unsigned int sparse_keymap_get_key_index(struct input_dev
*dev
,
27 const struct key_entry
*k
)
29 struct key_entry
*key
;
32 for (key
= dev
->keycode
; key
->type
!= KE_END
; key
++) {
33 if (key
->type
== KE_KEY
) {
43 static struct key_entry
*sparse_keymap_entry_by_index(struct input_dev
*dev
,
46 struct key_entry
*key
;
47 unsigned int key_cnt
= 0;
49 for (key
= dev
->keycode
; key
->type
!= KE_END
; key
++)
50 if (key
->type
== KE_KEY
)
51 if (key_cnt
++ == index
)
58 * sparse_keymap_entry_from_scancode - perform sparse keymap lookup
59 * @dev: Input device using sparse keymap
62 * This function is used to perform &struct key_entry lookup in an
63 * input device using sparse keymap.
65 struct key_entry
*sparse_keymap_entry_from_scancode(struct input_dev
*dev
,
68 struct key_entry
*key
;
70 for (key
= dev
->keycode
; key
->type
!= KE_END
; key
++)
71 if (code
== key
->code
)
76 EXPORT_SYMBOL(sparse_keymap_entry_from_scancode
);
79 * sparse_keymap_entry_from_keycode - perform sparse keymap lookup
80 * @dev: Input device using sparse keymap
83 * This function is used to perform &struct key_entry lookup in an
84 * input device using sparse keymap.
86 struct key_entry
*sparse_keymap_entry_from_keycode(struct input_dev
*dev
,
89 struct key_entry
*key
;
91 for (key
= dev
->keycode
; key
->type
!= KE_END
; key
++)
92 if (key
->type
== KE_KEY
&& keycode
== key
->keycode
)
97 EXPORT_SYMBOL(sparse_keymap_entry_from_keycode
);
99 static struct key_entry
*sparse_keymap_locate(struct input_dev
*dev
,
100 const struct input_keymap_entry
*ke
)
102 struct key_entry
*key
;
103 unsigned int scancode
;
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
);
115 static int sparse_keymap_getkeycode(struct input_dev
*dev
,
116 struct input_keymap_entry
*ke
)
118 const struct key_entry
*key
;
121 key
= sparse_keymap_locate(dev
, ke
);
122 if (key
&& key
->type
== KE_KEY
) {
123 ke
->keycode
= key
->keycode
;
124 if (!(ke
->flags
& INPUT_KEYMAP_BY_INDEX
))
126 sparse_keymap_get_key_index(dev
, key
);
127 ke
->len
= sizeof(key
->code
);
128 memcpy(ke
->scancode
, &key
->code
, sizeof(key
->code
));
136 static int sparse_keymap_setkeycode(struct input_dev
*dev
,
137 const struct input_keymap_entry
*ke
,
138 unsigned int *old_keycode
)
140 struct key_entry
*key
;
143 key
= sparse_keymap_locate(dev
, ke
);
144 if (key
&& key
->type
== KE_KEY
) {
145 *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
);
158 * sparse_keymap_setup - set up sparse keymap for an 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
163 * depending on device's needs, may be %NULL
165 * The function calculates size and allocates copy of the original
166 * keymap after which sets up input device event bits appropriately.
167 * The allocated copy of the keymap is automatically freed when it
168 * is no longer needed.
170 int sparse_keymap_setup(struct input_dev
*dev
,
171 const struct key_entry
*keymap
,
172 int (*setup
)(struct input_dev
*, struct key_entry
*))
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
;
180 for (e
= keymap
; e
->type
!= KE_END
; e
++)
183 map
= devm_kmemdup(&dev
->dev
, keymap
, map_size
* sizeof(*map
),
188 for (i
= 0; i
< map_size
; i
++) {
192 error
= setup(dev
, entry
);
197 switch (entry
->type
) {
199 __set_bit(EV_KEY
, dev
->evbit
);
200 __set_bit(entry
->keycode
, dev
->keybit
);
205 __set_bit(EV_SW
, dev
->evbit
);
206 __set_bit(entry
->sw
.code
, dev
->swbit
);
211 if (test_bit(EV_KEY
, dev
->evbit
)) {
212 __set_bit(KEY_UNKNOWN
, dev
->keybit
);
213 __set_bit(EV_MSC
, dev
->evbit
);
214 __set_bit(MSC_SCAN
, dev
->mscbit
);
218 dev
->keycodemax
= map_size
;
219 dev
->getkeycode
= sparse_keymap_getkeycode
;
220 dev
->setkeycode
= sparse_keymap_setkeycode
;
224 EXPORT_SYMBOL(sparse_keymap_setup
);
227 * sparse_keymap_report_entry - report event corresponding to given key entry
228 * @dev: Input device for which event should be reported
229 * @ke: key entry describing event
230 * @value: Value that should be reported (ignored by %KE_SW entries)
231 * @autorelease: Signals whether release event should be emitted for %KE_KEY
232 * entries right after reporting press event, ignored by all other
235 * This function is used to report input event described by given
238 void sparse_keymap_report_entry(struct input_dev
*dev
, const struct key_entry
*ke
,
239 unsigned int value
, bool autorelease
)
243 input_event(dev
, EV_MSC
, MSC_SCAN
, ke
->code
);
244 input_report_key(dev
, ke
->keycode
, value
);
246 if (value
&& autorelease
) {
247 input_report_key(dev
, ke
->keycode
, 0);
253 value
= ke
->sw
.value
;
257 input_report_switch(dev
, ke
->sw
.code
, value
);
261 EXPORT_SYMBOL(sparse_keymap_report_entry
);
264 * sparse_keymap_report_event - report event corresponding to given scancode
265 * @dev: Input device using sparse keymap
267 * @value: Value that should be reported (ignored by %KE_SW entries)
268 * @autorelease: Signals whether release event should be emitted for %KE_KEY
269 * entries right after reporting press event, ignored by all other
272 * This function is used to perform lookup in an input device using sparse
273 * keymap and report corresponding event. Returns %true if lookup was
274 * successful and %false otherwise.
276 bool sparse_keymap_report_event(struct input_dev
*dev
, unsigned int code
,
277 unsigned int value
, bool autorelease
)
279 const struct key_entry
*ke
=
280 sparse_keymap_entry_from_scancode(dev
, code
);
281 struct key_entry unknown_ke
;
284 sparse_keymap_report_entry(dev
, ke
, value
, autorelease
);
288 /* Report an unknown key event as a debugging aid */
289 unknown_ke
.type
= KE_KEY
;
290 unknown_ke
.code
= code
;
291 unknown_ke
.keycode
= KEY_UNKNOWN
;
292 sparse_keymap_report_entry(dev
, &unknown_ke
, value
, true);
296 EXPORT_SYMBOL(sparse_keymap_report_event
);