blob: 82ae18d2968561f0221d3c66ee39645bcb429e2d [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
Dmitry Torokhov36203c42009-12-04 10:22:23 -080019
20MODULE_AUTHOR("Dmitry Torokhov <dtor@mail.ru>");
21MODULE_DESCRIPTION("Generic support for sparse keymaps");
22MODULE_LICENSE("GPL v2");
23MODULE_VERSION("0.1");
24
25/**
26 * sparse_keymap_entry_from_scancode - perform sparse keymap lookup
27 * @dev: Input device using sparse keymap
28 * @code: Scan code
29 *
30 * This function is used to perform &struct key_entry lookup in an
31 * input device using sparse keymap.
32 */
33struct key_entry *sparse_keymap_entry_from_scancode(struct input_dev *dev,
34 unsigned int code)
35{
36 struct key_entry *key;
37
38 for (key = dev->keycode; key->type != KE_END; key++)
39 if (code == key->code)
40 return key;
41
42 return NULL;
43}
44EXPORT_SYMBOL(sparse_keymap_entry_from_scancode);
45
46/**
47 * sparse_keymap_entry_from_keycode - perform sparse keymap lookup
48 * @dev: Input device using sparse keymap
49 * @keycode: Key code
50 *
51 * This function is used to perform &struct key_entry lookup in an
52 * input device using sparse keymap.
53 */
54struct key_entry *sparse_keymap_entry_from_keycode(struct input_dev *dev,
55 unsigned int keycode)
56{
57 struct key_entry *key;
58
59 for (key = dev->keycode; key->type != KE_END; key++)
60 if (key->type == KE_KEY && keycode == key->keycode)
61 return key;
62
63 return NULL;
64}
65EXPORT_SYMBOL(sparse_keymap_entry_from_keycode);
66
67static int sparse_keymap_getkeycode(struct input_dev *dev,
Dmitry Torokhov58b93992010-03-08 22:37:10 -080068 unsigned int scancode,
69 unsigned int *keycode)
Dmitry Torokhov36203c42009-12-04 10:22:23 -080070{
71 const struct key_entry *key =
72 sparse_keymap_entry_from_scancode(dev, scancode);
73
74 if (key && key->type == KE_KEY) {
75 *keycode = key->keycode;
76 return 0;
77 }
78
79 return -EINVAL;
80}
81
82static int sparse_keymap_setkeycode(struct input_dev *dev,
Dmitry Torokhov58b93992010-03-08 22:37:10 -080083 unsigned int scancode,
84 unsigned int keycode)
Dmitry Torokhov36203c42009-12-04 10:22:23 -080085{
86 struct key_entry *key;
87 int old_keycode;
88
89 if (keycode < 0 || keycode > KEY_MAX)
90 return -EINVAL;
91
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);
99 return 0;
100 }
101
102 return -EINVAL;
103}
104
105/**
106 * sparse_keymap_setup - set up sparse keymap for an input device
107 * @dev: Input device
108 * @keymap: Keymap in form of array of &key_entry structures ending
109 * with %KE_END type entry
110 * @setup: Function that can be used to adjust keymap entries
111 * depending on device's deeds, may be %NULL
112 *
113 * The function calculates size and allocates copy of the original
114 * keymap after which sets up input device event bits appropriately.
115 * Before destroying input device allocated keymap should be freed
116 * with a call to sparse_keymap_free().
117 */
118int sparse_keymap_setup(struct input_dev *dev,
119 const struct key_entry *keymap,
120 int (*setup)(struct input_dev *, struct key_entry *))
121{
122 size_t map_size = 1; /* to account for the last KE_END entry */
123 const struct key_entry *e;
124 struct key_entry *map, *entry;
125 int i;
126 int error;
127
128 for (e = keymap; e->type != KE_END; e++)
129 map_size++;
130
131 map = kcalloc(map_size, sizeof (struct key_entry), GFP_KERNEL);
132 if (!map)
133 return -ENOMEM;
134
135 memcpy(map, keymap, map_size * sizeof (struct key_entry));
136
137 for (i = 0; i < map_size; i++) {
138 entry = &map[i];
139
140 if (setup) {
141 error = setup(dev, entry);
142 if (error)
143 goto err_out;
144 }
145
146 switch (entry->type) {
147 case KE_KEY:
148 __set_bit(EV_KEY, dev->evbit);
149 __set_bit(entry->keycode, dev->keybit);
150 break;
151
152 case KE_SW:
153 __set_bit(EV_SW, dev->evbit);
154 __set_bit(entry->sw.code, dev->swbit);
155 break;
156 }
157 }
158
159 dev->keycode = map;
160 dev->keycodemax = map_size;
161 dev->getkeycode = sparse_keymap_getkeycode;
162 dev->setkeycode = sparse_keymap_setkeycode;
163
164 return 0;
165
166 err_out:
167 kfree(keymap);
168 return error;
169
170}
171EXPORT_SYMBOL(sparse_keymap_setup);
172
173/**
174 * sparse_keymap_free - free memory allocated for sparse keymap
175 * @dev: Input device using sparse keymap
176 *
177 * This function is used to free memory allocated by sparse keymap
178 * in an input device that was set up by sparse_keymap_setup().
179 */
180void sparse_keymap_free(struct input_dev *dev)
181{
182 kfree(dev->keycode);
183 dev->keycode = NULL;
184 dev->keycodemax = 0;
185 dev->getkeycode = NULL;
186 dev->setkeycode = NULL;
187}
188EXPORT_SYMBOL(sparse_keymap_free);
189
190/**
191 * sparse_keymap_report_entry - report event corresponding to given key entry
192 * @dev: Input device for which event should be reported
193 * @ke: key entry describing event
194 * @value: Value that should be reported (ignored by %KE_SW entries)
195 * @autorelease: Signals whether release event should be emitted for %KE_KEY
196 * entries right after reporting press event, ignored by all other
197 * entries
198 *
199 * This function is used to report input event described by given
200 * &struct key_entry.
201 */
202void sparse_keymap_report_entry(struct input_dev *dev, const struct key_entry *ke,
203 unsigned int value, bool autorelease)
204{
205 switch (ke->type) {
206 case KE_KEY:
207 input_report_key(dev, ke->keycode, value);
208 input_sync(dev);
209 if (value && autorelease) {
210 input_report_key(dev, ke->keycode, 0);
211 input_sync(dev);
212 }
213 break;
214
215 case KE_SW:
216 value = ke->sw.value;
217 /* fall through */
218
219 case KE_VSW:
220 input_report_switch(dev, ke->sw.code, value);
221 break;
222 }
223}
224EXPORT_SYMBOL(sparse_keymap_report_entry);
225
226/**
227 * sparse_keymap_report_event - report event corresponding to given scancode
228 * @dev: Input device using sparse keymap
229 * @code: Scan code
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
233 * entries
234 *
235 * This function is used to perform lookup in an input device using sparse
236 * keymap and report corresponding event. Returns %true if lookup was
237 * successful and %false otherwise.
238 */
239bool sparse_keymap_report_event(struct input_dev *dev, unsigned int code,
240 unsigned int value, bool autorelease)
241{
242 const struct key_entry *ke =
243 sparse_keymap_entry_from_scancode(dev, code);
244
245 if (ke) {
246 sparse_keymap_report_entry(dev, ke, value, autorelease);
247 return true;
248 }
249
250 return false;
251}
252EXPORT_SYMBOL(sparse_keymap_report_event);
253