diff options
author | Dmitry Torokhov <dmitry.torokhov@gmail.com> | 2009-12-04 13:22:23 -0500 |
---|---|---|
committer | Dmitry Torokhov <dmitry.torokhov@gmail.com> | 2009-12-07 12:26:35 -0500 |
commit | 36203c4f3d091b5f6c082663bd1f74273798043a (patch) | |
tree | bf5b6efee8f15aae253155263038ac538f7482e9 /drivers/input/sparse-keymap.c | |
parent | 6a47081c37b7dd7810ce19e156c1a3bf11987e9a (diff) |
Input: add generic support for sparse keymaps
More and more devices choose to reimplement support for sparse keymaps
first introduced by wistron driver. Move it into a library module so it
can be easily used by interested parties.
Reviewed-by: Anisse Astier <anisse@astier.eu>
Signed-off-by: Dmitry Torokhov <dtor@mail.ru>
Diffstat (limited to 'drivers/input/sparse-keymap.c')
-rw-r--r-- | drivers/input/sparse-keymap.c | 250 |
1 files changed, 250 insertions, 0 deletions
diff --git a/drivers/input/sparse-keymap.c b/drivers/input/sparse-keymap.c new file mode 100644 index 000000000000..fbd3987af57f --- /dev/null +++ b/drivers/input/sparse-keymap.c | |||
@@ -0,0 +1,250 @@ | |||
1 | /* | ||
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> | ||
18 | |||
19 | MODULE_AUTHOR("Dmitry Torokhov <dtor@mail.ru>"); | ||
20 | MODULE_DESCRIPTION("Generic support for sparse keymaps"); | ||
21 | MODULE_LICENSE("GPL v2"); | ||
22 | MODULE_VERSION("0.1"); | ||
23 | |||
24 | /** | ||
25 | * sparse_keymap_entry_from_scancode - perform sparse keymap lookup | ||
26 | * @dev: Input device using sparse keymap | ||
27 | * @code: Scan code | ||
28 | * | ||
29 | * This function is used to perform &struct key_entry lookup in an | ||
30 | * input device using sparse keymap. | ||
31 | */ | ||
32 | struct key_entry *sparse_keymap_entry_from_scancode(struct input_dev *dev, | ||
33 | unsigned int code) | ||
34 | { | ||
35 | struct key_entry *key; | ||
36 | |||
37 | for (key = dev->keycode; key->type != KE_END; key++) | ||
38 | if (code == key->code) | ||
39 | return key; | ||
40 | |||
41 | return NULL; | ||
42 | } | ||
43 | EXPORT_SYMBOL(sparse_keymap_entry_from_scancode); | ||
44 | |||
45 | /** | ||
46 | * sparse_keymap_entry_from_keycode - perform sparse keymap lookup | ||
47 | * @dev: Input device using sparse keymap | ||
48 | * @keycode: Key code | ||
49 | * | ||
50 | * This function is used to perform &struct key_entry lookup in an | ||
51 | * input device using sparse keymap. | ||
52 | */ | ||
53 | struct key_entry *sparse_keymap_entry_from_keycode(struct input_dev *dev, | ||
54 | unsigned int keycode) | ||
55 | { | ||
56 | struct key_entry *key; | ||
57 | |||
58 | for (key = dev->keycode; key->type != KE_END; key++) | ||
59 | if (key->type == KE_KEY && keycode == key->keycode) | ||
60 | return key; | ||
61 | |||
62 | return NULL; | ||
63 | } | ||
64 | EXPORT_SYMBOL(sparse_keymap_entry_from_keycode); | ||
65 | |||
66 | static int sparse_keymap_getkeycode(struct input_dev *dev, | ||
67 | int scancode, int *keycode) | ||
68 | { | ||
69 | const struct key_entry *key = | ||
70 | sparse_keymap_entry_from_scancode(dev, scancode); | ||
71 | |||
72 | if (key && key->type == KE_KEY) { | ||
73 | *keycode = key->keycode; | ||
74 | return 0; | ||
75 | } | ||
76 | |||
77 | return -EINVAL; | ||
78 | } | ||
79 | |||
80 | static int sparse_keymap_setkeycode(struct input_dev *dev, | ||
81 | int scancode, int keycode) | ||
82 | { | ||
83 | struct key_entry *key; | ||
84 | int old_keycode; | ||
85 | |||
86 | if (keycode < 0 || keycode > KEY_MAX) | ||
87 | return -EINVAL; | ||
88 | |||
89 | key = sparse_keymap_entry_from_scancode(dev, scancode); | ||
90 | if (key && key->type == KE_KEY) { | ||
91 | old_keycode = key->keycode; | ||
92 | key->keycode = keycode; | ||
93 | set_bit(keycode, dev->keybit); | ||
94 | if (!sparse_keymap_entry_from_keycode(dev, old_keycode)) | ||
95 | clear_bit(old_keycode, dev->keybit); | ||
96 | return 0; | ||
97 | } | ||
98 | |||
99 | return -EINVAL; | ||
100 | } | ||
101 | |||
102 | /** | ||
103 | * sparse_keymap_setup - set up sparse keymap for an input device | ||
104 | * @dev: Input device | ||
105 | * @keymap: Keymap in form of array of &key_entry structures ending | ||
106 | * with %KE_END type entry | ||
107 | * @setup: Function that can be used to adjust keymap entries | ||
108 | * depending on device's deeds, may be %NULL | ||
109 | * | ||
110 | * The function calculates size and allocates copy of the original | ||
111 | * keymap after which sets up input device event bits appropriately. | ||
112 | * Before destroying input device allocated keymap should be freed | ||
113 | * with a call to sparse_keymap_free(). | ||
114 | */ | ||
115 | int sparse_keymap_setup(struct input_dev *dev, | ||
116 | const struct key_entry *keymap, | ||
117 | int (*setup)(struct input_dev *, struct key_entry *)) | ||
118 | { | ||
119 | size_t map_size = 1; /* to account for the last KE_END entry */ | ||
120 | const struct key_entry *e; | ||
121 | struct key_entry *map, *entry; | ||
122 | int i; | ||
123 | int error; | ||
124 | |||
125 | for (e = keymap; e->type != KE_END; e++) | ||
126 | map_size++; | ||
127 | |||
128 | map = kcalloc(map_size, sizeof (struct key_entry), GFP_KERNEL); | ||
129 | if (!map) | ||
130 | return -ENOMEM; | ||
131 | |||
132 | memcpy(map, keymap, map_size * sizeof (struct key_entry)); | ||
133 | |||
134 | for (i = 0; i < map_size; i++) { | ||
135 | entry = &map[i]; | ||
136 | |||
137 | if (setup) { | ||
138 | error = setup(dev, entry); | ||
139 | if (error) | ||
140 | goto err_out; | ||
141 | } | ||
142 | |||
143 | switch (entry->type) { | ||
144 | case KE_KEY: | ||
145 | __set_bit(EV_KEY, dev->evbit); | ||
146 | __set_bit(entry->keycode, dev->keybit); | ||
147 | break; | ||
148 | |||
149 | case KE_SW: | ||
150 | __set_bit(EV_SW, dev->evbit); | ||
151 | __set_bit(entry->sw.code, dev->swbit); | ||
152 | break; | ||
153 | } | ||
154 | } | ||
155 | |||
156 | dev->keycode = map; | ||
157 | dev->keycodemax = map_size; | ||
158 | dev->getkeycode = sparse_keymap_getkeycode; | ||
159 | dev->setkeycode = sparse_keymap_setkeycode; | ||
160 | |||
161 | return 0; | ||
162 | |||
163 | err_out: | ||
164 | kfree(keymap); | ||
165 | return error; | ||
166 | |||
167 | } | ||
168 | EXPORT_SYMBOL(sparse_keymap_setup); | ||
169 | |||
170 | /** | ||
171 | * sparse_keymap_free - free memory allocated for sparse keymap | ||
172 | * @dev: Input device using sparse keymap | ||
173 | * | ||
174 | * This function is used to free memory allocated by sparse keymap | ||
175 | * in an input device that was set up by sparse_keymap_setup(). | ||
176 | */ | ||
177 | void sparse_keymap_free(struct input_dev *dev) | ||
178 | { | ||
179 | kfree(dev->keycode); | ||
180 | dev->keycode = NULL; | ||
181 | dev->keycodemax = 0; | ||
182 | dev->getkeycode = NULL; | ||
183 | dev->setkeycode = NULL; | ||
184 | } | ||
185 | EXPORT_SYMBOL(sparse_keymap_free); | ||
186 | |||
187 | /** | ||
188 | * sparse_keymap_report_entry - report event corresponding to given key entry | ||
189 | * @dev: Input device for which event should be reported | ||
190 | * @ke: key entry describing event | ||
191 | * @value: Value that should be reported (ignored by %KE_SW entries) | ||
192 | * @autorelease: Signals whether release event should be emitted for %KE_KEY | ||
193 | * entries right after reporting press event, ignored by all other | ||
194 | * entries | ||
195 | * | ||
196 | * This function is used to report input event described by given | ||
197 | * &struct key_entry. | ||
198 | */ | ||
199 | void sparse_keymap_report_entry(struct input_dev *dev, const struct key_entry *ke, | ||
200 | unsigned int value, bool autorelease) | ||
201 | { | ||
202 | switch (ke->type) { | ||
203 | case KE_KEY: | ||
204 | input_report_key(dev, ke->keycode, value); | ||
205 | input_sync(dev); | ||
206 | if (value && autorelease) { | ||
207 | input_report_key(dev, ke->keycode, 0); | ||
208 | input_sync(dev); | ||
209 | } | ||
210 | break; | ||
211 | |||
212 | case KE_SW: | ||
213 | value = ke->sw.value; | ||
214 | /* fall through */ | ||
215 | |||
216 | case KE_VSW: | ||
217 | input_report_switch(dev, ke->sw.code, value); | ||
218 | break; | ||
219 | } | ||
220 | } | ||
221 | EXPORT_SYMBOL(sparse_keymap_report_entry); | ||
222 | |||
223 | /** | ||
224 | * sparse_keymap_report_event - report event corresponding to given scancode | ||
225 | * @dev: Input device using sparse keymap | ||
226 | * @code: Scan code | ||
227 | * @value: Value that should be reported (ignored by %KE_SW entries) | ||
228 | * @autorelease: Signals whether release event should be emitted for %KE_KEY | ||
229 | * entries right after reporting press event, ignored by all other | ||
230 | * entries | ||
231 | * | ||
232 | * This function is used to perform lookup in an input device using sparse | ||
233 | * keymap and report corresponding event. Returns %true if lookup was | ||
234 | * successful and %false otherwise. | ||
235 | */ | ||
236 | bool sparse_keymap_report_event(struct input_dev *dev, unsigned int code, | ||
237 | unsigned int value, bool autorelease) | ||
238 | { | ||
239 | const struct key_entry *ke = | ||
240 | sparse_keymap_entry_from_scancode(dev, code); | ||
241 | |||
242 | if (ke) { | ||
243 | sparse_keymap_report_entry(dev, ke, value, autorelease); | ||
244 | return true; | ||
245 | } | ||
246 | |||
247 | return false; | ||
248 | } | ||
249 | EXPORT_SYMBOL(sparse_keymap_report_event); | ||
250 | |||