FRV: Use generic show_interrupts()
[cris-mirror.git] / drivers / input / sparse-keymap.c
blob337bf51bc984253abf9a77b558eb36c8c10deb53
1 /*
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");
25 static unsigned int sparse_keymap_get_key_index(struct input_dev *dev,
26 const struct key_entry *k)
28 struct key_entry *key;
29 unsigned int idx = 0;
31 for (key = dev->keycode; key->type != KE_END; key++) {
32 if (key->type == KE_KEY) {
33 if (key == k)
34 break;
35 idx++;
39 return idx;
42 static struct key_entry *sparse_keymap_entry_by_index(struct input_dev *dev,
43 unsigned int index)
45 struct key_entry *key;
46 unsigned int key_cnt = 0;
48 for (key = dev->keycode; key->type != KE_END; key++)
49 if (key->type == KE_KEY)
50 if (key_cnt++ == index)
51 return key;
53 return NULL;
56 /**
57 * sparse_keymap_entry_from_scancode - perform sparse keymap lookup
58 * @dev: Input device using sparse keymap
59 * @code: Scan code
61 * This function is used to perform &struct key_entry lookup in an
62 * input device using sparse keymap.
64 struct key_entry *sparse_keymap_entry_from_scancode(struct input_dev *dev,
65 unsigned int code)
67 struct key_entry *key;
69 for (key = dev->keycode; key->type != KE_END; key++)
70 if (code == key->code)
71 return key;
73 return NULL;
75 EXPORT_SYMBOL(sparse_keymap_entry_from_scancode);
77 /**
78 * sparse_keymap_entry_from_keycode - perform sparse keymap lookup
79 * @dev: Input device using sparse keymap
80 * @keycode: Key code
82 * This function is used to perform &struct key_entry lookup in an
83 * input device using sparse keymap.
85 struct key_entry *sparse_keymap_entry_from_keycode(struct input_dev *dev,
86 unsigned int keycode)
88 struct key_entry *key;
90 for (key = dev->keycode; key->type != KE_END; key++)
91 if (key->type == KE_KEY && keycode == key->keycode)
92 return key;
94 return NULL;
96 EXPORT_SYMBOL(sparse_keymap_entry_from_keycode);
98 static struct key_entry *sparse_keymap_locate(struct input_dev *dev,
99 const struct input_keymap_entry *ke)
101 struct key_entry *key;
102 unsigned int scancode;
104 if (ke->flags & INPUT_KEYMAP_BY_INDEX)
105 key = sparse_keymap_entry_by_index(dev, ke->index);
106 else if (input_scancode_to_scalar(ke, &scancode) == 0)
107 key = sparse_keymap_entry_from_scancode(dev, scancode);
108 else
109 key = NULL;
111 return key;
114 static int sparse_keymap_getkeycode(struct input_dev *dev,
115 struct input_keymap_entry *ke)
117 const struct key_entry *key;
119 if (dev->keycode) {
120 key = sparse_keymap_locate(dev, ke);
121 if (key && key->type == KE_KEY) {
122 ke->keycode = key->keycode;
123 if (!(ke->flags & INPUT_KEYMAP_BY_INDEX))
124 ke->index =
125 sparse_keymap_get_key_index(dev, key);
126 ke->len = sizeof(key->code);
127 memcpy(ke->scancode, &key->code, sizeof(key->code));
128 return 0;
132 return -EINVAL;
135 static int sparse_keymap_setkeycode(struct input_dev *dev,
136 const struct input_keymap_entry *ke,
137 unsigned int *old_keycode)
139 struct key_entry *key;
141 if (dev->keycode) {
142 key = sparse_keymap_locate(dev, ke);
143 if (key && key->type == KE_KEY) {
144 *old_keycode = key->keycode;
145 key->keycode = ke->keycode;
146 set_bit(ke->keycode, dev->keybit);
147 if (!sparse_keymap_entry_from_keycode(dev, *old_keycode))
148 clear_bit(*old_keycode, dev->keybit);
149 return 0;
153 return -EINVAL;
157 * sparse_keymap_setup - set up sparse keymap for an input device
158 * @dev: Input device
159 * @keymap: Keymap in form of array of &key_entry structures ending
160 * with %KE_END type entry
161 * @setup: Function that can be used to adjust keymap entries
162 * depending on device's deeds, may be %NULL
164 * The function calculates size and allocates copy of the original
165 * keymap after which sets up input device event bits appropriately.
166 * Before destroying input device allocated keymap should be freed
167 * with a call to sparse_keymap_free().
169 int sparse_keymap_setup(struct input_dev *dev,
170 const struct key_entry *keymap,
171 int (*setup)(struct input_dev *, struct key_entry *))
173 size_t map_size = 1; /* to account for the last KE_END entry */
174 const struct key_entry *e;
175 struct key_entry *map, *entry;
176 int i;
177 int error;
179 for (e = keymap; e->type != KE_END; e++)
180 map_size++;
182 map = kcalloc(map_size, sizeof (struct key_entry), GFP_KERNEL);
183 if (!map)
184 return -ENOMEM;
186 memcpy(map, keymap, map_size * sizeof (struct key_entry));
188 for (i = 0; i < map_size; i++) {
189 entry = &map[i];
191 if (setup) {
192 error = setup(dev, entry);
193 if (error)
194 goto err_out;
197 switch (entry->type) {
198 case KE_KEY:
199 __set_bit(EV_KEY, dev->evbit);
200 __set_bit(entry->keycode, dev->keybit);
201 break;
203 case KE_SW:
204 case KE_VSW:
205 __set_bit(EV_SW, dev->evbit);
206 __set_bit(entry->sw.code, dev->swbit);
207 break;
211 dev->keycode = map;
212 dev->keycodemax = map_size;
213 dev->getkeycode = sparse_keymap_getkeycode;
214 dev->setkeycode = sparse_keymap_setkeycode;
216 return 0;
218 err_out:
219 kfree(map);
220 return error;
222 EXPORT_SYMBOL(sparse_keymap_setup);
225 * sparse_keymap_free - free memory allocated for sparse keymap
226 * @dev: Input device using sparse keymap
228 * This function is used to free memory allocated by sparse keymap
229 * in an input device that was set up by sparse_keymap_setup().
230 * NOTE: It is safe to cal this function while input device is
231 * still registered (however the drivers should care not to try to
232 * use freed keymap and thus have to shut off interrups/polling
233 * before freeing the keymap).
235 void sparse_keymap_free(struct input_dev *dev)
237 unsigned long flags;
240 * Take event lock to prevent racing with input_get_keycode()
241 * and input_set_keycode() if we are called while input device
242 * is still registered.
244 spin_lock_irqsave(&dev->event_lock, flags);
246 kfree(dev->keycode);
247 dev->keycode = NULL;
248 dev->keycodemax = 0;
250 spin_unlock_irqrestore(&dev->event_lock, flags);
252 EXPORT_SYMBOL(sparse_keymap_free);
255 * sparse_keymap_report_entry - report event corresponding to given key entry
256 * @dev: Input device for which event should be reported
257 * @ke: key entry describing event
258 * @value: Value that should be reported (ignored by %KE_SW entries)
259 * @autorelease: Signals whether release event should be emitted for %KE_KEY
260 * entries right after reporting press event, ignored by all other
261 * entries
263 * This function is used to report input event described by given
264 * &struct key_entry.
266 void sparse_keymap_report_entry(struct input_dev *dev, const struct key_entry *ke,
267 unsigned int value, bool autorelease)
269 switch (ke->type) {
270 case KE_KEY:
271 input_report_key(dev, ke->keycode, value);
272 input_sync(dev);
273 if (value && autorelease) {
274 input_report_key(dev, ke->keycode, 0);
275 input_sync(dev);
277 break;
279 case KE_SW:
280 value = ke->sw.value;
281 /* fall through */
283 case KE_VSW:
284 input_report_switch(dev, ke->sw.code, value);
285 break;
288 EXPORT_SYMBOL(sparse_keymap_report_entry);
291 * sparse_keymap_report_event - report event corresponding to given scancode
292 * @dev: Input device using sparse keymap
293 * @code: Scan code
294 * @value: Value that should be reported (ignored by %KE_SW entries)
295 * @autorelease: Signals whether release event should be emitted for %KE_KEY
296 * entries right after reporting press event, ignored by all other
297 * entries
299 * This function is used to perform lookup in an input device using sparse
300 * keymap and report corresponding event. Returns %true if lookup was
301 * successful and %false otherwise.
303 bool sparse_keymap_report_event(struct input_dev *dev, unsigned int code,
304 unsigned int value, bool autorelease)
306 const struct key_entry *ke =
307 sparse_keymap_entry_from_scancode(dev, code);
309 if (ke) {
310 sparse_keymap_report_entry(dev, ke, value, autorelease);
311 return true;
314 return false;
316 EXPORT_SYMBOL(sparse_keymap_report_event);