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/slab.h>
20 MODULE_AUTHOR("Dmitry Torokhov <dtor@mail.ru>");
21 MODULE_DESCRIPTION("Generic support for sparse keymaps");
22 MODULE_LICENSE("GPL v2");
23 MODULE_VERSION("0.1");
26 * sparse_keymap_entry_from_scancode - perform sparse keymap lookup
27 * @dev: Input device using sparse keymap
30 * This function is used to perform &struct key_entry lookup in an
31 * input device using sparse keymap.
33 struct key_entry *sparse_keymap_entry_from_scancode(struct input_dev *dev,
36 struct key_entry *key;
38 for (key = dev->keycode; key->type != KE_END; key++)
39 if (code == key->code)
44 EXPORT_SYMBOL(sparse_keymap_entry_from_scancode);
47 * sparse_keymap_entry_from_keycode - perform sparse keymap lookup
48 * @dev: Input device using sparse keymap
51 * This function is used to perform &struct key_entry lookup in an
52 * input device using sparse keymap.
54 struct key_entry *sparse_keymap_entry_from_keycode(struct input_dev *dev,
57 struct key_entry *key;
59 for (key = dev->keycode; key->type != KE_END; key++)
60 if (key->type == KE_KEY && keycode == key->keycode)
65 EXPORT_SYMBOL(sparse_keymap_entry_from_keycode);
67 static int sparse_keymap_getkeycode(struct input_dev *dev,
68 unsigned int scancode,
69 unsigned int *keycode)
71 const struct key_entry *key;
74 key = sparse_keymap_entry_from_scancode(dev, scancode);
75 if (key && key->type == KE_KEY) {
76 *keycode = key->keycode;
84 static int sparse_keymap_setkeycode(struct input_dev *dev,
85 unsigned int scancode,
88 struct key_entry *key;
92 key = sparse_keymap_entry_from_scancode(dev, scancode);
93 if (key && key->type == KE_KEY) {
94 old_keycode = key->keycode;
95 key->keycode = keycode;
96 set_bit(keycode, dev->keybit);
97 if (!sparse_keymap_entry_from_keycode(dev, old_keycode))
98 clear_bit(old_keycode, dev->keybit);
107 * sparse_keymap_setup - set up sparse keymap for an input device
109 * @keymap: Keymap in form of array of &key_entry structures ending
110 * with %KE_END type entry
111 * @setup: Function that can be used to adjust keymap entries
112 * depending on device's deeds, may be %NULL
114 * The function calculates size and allocates copy of the original
115 * keymap after which sets up input device event bits appropriately.
116 * Before destroying input device allocated keymap should be freed
117 * with a call to sparse_keymap_free().
119 int sparse_keymap_setup(struct input_dev *dev,
120 const struct key_entry *keymap,
121 int (*setup)(struct input_dev *, struct key_entry *))
123 size_t map_size = 1; /* to account for the last KE_END entry */
124 const struct key_entry *e;
125 struct key_entry *map, *entry;
129 for (e = keymap; e->type != KE_END; e++)
132 map = kcalloc(map_size, sizeof (struct key_entry), GFP_KERNEL);
136 memcpy(map, keymap, map_size * sizeof (struct key_entry));
138 for (i = 0; i < map_size; i++) {
142 error = setup(dev, entry);
147 switch (entry->type) {
149 __set_bit(EV_KEY, dev->evbit);
150 __set_bit(entry->keycode, dev->keybit);
154 __set_bit(EV_SW, dev->evbit);
155 __set_bit(entry->sw.code, dev->swbit);
161 dev->keycodemax = map_size;
162 dev->getkeycode = sparse_keymap_getkeycode;
163 dev->setkeycode = sparse_keymap_setkeycode;
172 EXPORT_SYMBOL(sparse_keymap_setup);
175 * sparse_keymap_free - free memory allocated for sparse keymap
176 * @dev: Input device using sparse keymap
178 * This function is used to free memory allocated by sparse keymap
179 * in an input device that was set up by sparse_keymap_setup().
180 * NOTE: It is safe to cal this function while input device is
181 * still registered (however the drivers should care not to try to
182 * use freed keymap and thus have to shut off interrups/polling
183 * before freeing the keymap).
185 void sparse_keymap_free(struct input_dev *dev)
190 * Take event lock to prevent racing with input_get_keycode()
191 * and input_set_keycode() if we are called while input device
192 * is still registered.
194 spin_lock_irqsave(&dev->event_lock, flags);
200 spin_unlock_irqrestore(&dev->event_lock, flags);
202 EXPORT_SYMBOL(sparse_keymap_free);
205 * sparse_keymap_report_entry - report event corresponding to given key entry
206 * @dev: Input device for which event should be reported
207 * @ke: key entry describing event
208 * @value: Value that should be reported (ignored by %KE_SW entries)
209 * @autorelease: Signals whether release event should be emitted for %KE_KEY
210 * entries right after reporting press event, ignored by all other
213 * This function is used to report input event described by given
216 void sparse_keymap_report_entry(struct input_dev *dev, const struct key_entry *ke,
217 unsigned int value, bool autorelease)
221 input_report_key(dev, ke->keycode, value);
223 if (value && autorelease) {
224 input_report_key(dev, ke->keycode, 0);
230 value = ke->sw.value;
234 input_report_switch(dev, ke->sw.code, value);
238 EXPORT_SYMBOL(sparse_keymap_report_entry);
241 * sparse_keymap_report_event - report event corresponding to given scancode
242 * @dev: Input device using sparse keymap
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
249 * This function is used to perform lookup in an input device using sparse
250 * keymap and report corresponding event. Returns %true if lookup was
251 * successful and %false otherwise.
253 bool sparse_keymap_report_event(struct input_dev *dev, unsigned int code,
254 unsigned int value, bool autorelease)
256 const struct key_entry *ke =
257 sparse_keymap_entry_from_scancode(dev, code);
260 sparse_keymap_report_entry(dev, ke, value, autorelease);
266 EXPORT_SYMBOL(sparse_keymap_report_event);