16 #include <linux/input.h>
18 #include <linux/module.h>
19 #include <linux/slab.h>
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++) {
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++)
51 if (key_cnt++ == index)
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)
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)
99 static struct key_entry *sparse_keymap_locate(
struct input_dev *dev,
102 struct key_entry *
key;
103 unsigned int scancode;
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,
118 const struct key_entry *
key;
121 key = sparse_keymap_locate(dev, ke);
122 if (key && key->type ==
KE_KEY) {
126 sparse_keymap_get_key_index(dev, key);
127 ke->
len =
sizeof(key->code);
136 static int sparse_keymap_setkeycode(
struct input_dev *dev,
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;
148 if (!sparse_keymap_entry_from_keycode(dev, *old_keycode))
170 int sparse_keymap_setup(
struct input_dev *dev,
171 const struct key_entry *keymap,
172 int (*
setup)(
struct input_dev *,
struct key_entry *))
175 const struct key_entry *
e;
180 for (e = keymap; e->type !=
KE_END; e++)
183 map = kcalloc(map_size,
sizeof(
struct key_entry),
GFP_KERNEL);
187 memcpy(map, keymap, map_size *
sizeof(
struct key_entry));
189 for (i = 0; i < map_size; i++) {
193 error =
setup(dev, entry);
198 switch (entry->type) {
219 dev->keycodemax = map_size;
220 dev->getkeycode = sparse_keymap_getkeycode;
221 dev->setkeycode = sparse_keymap_setkeycode;
242 void sparse_keymap_free(
struct input_dev *dev)
257 spin_unlock_irqrestore(&dev->event_lock, flags);
273 void sparse_keymap_report_entry(
struct input_dev *dev,
const struct key_entry *ke,
274 unsigned int value,
bool autorelease)
279 input_report_key(dev, ke->keycode, value);
281 if (value && autorelease) {
282 input_report_key(dev, ke->keycode, 0);
288 value = ke->sw.value;
292 input_report_switch(dev, ke->sw.code, value);
311 bool sparse_keymap_report_event(
struct input_dev *dev,
unsigned int code,
312 unsigned int value,
bool autorelease)
314 const struct key_entry *ke =
315 sparse_keymap_entry_from_scancode(dev, code);
316 struct key_entry unknown_ke;
319 sparse_keymap_report_entry(dev, ke, value, autorelease);
325 unknown_ke.code =
code;
327 sparse_keymap_report_entry(dev, &unknown_ke, value,
true);