blob: e9c9bd34af656d3b2481055b70dc2ec7c8ad20d9 [file] [log] [blame]
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -03001/* ir-register.c - handle IR scancode->keycode tables
2 *
3 * Copyright (C) 2009 by Mauro Carvalho Chehab <mchehab@redhat.com>
Mauro Carvalho Chehab446e4a62009-12-11 08:34:07 -03004 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation version 2 of the License.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -030013 */
14
15#include <linux/usb/input.h>
16
17#include <media/ir-common.h>
18
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -030019#define IR_TAB_MIN_SIZE 32
Mauro Carvalho Chehabe97f4672009-12-04 17:17:47 -030020#define IR_TAB_MAX_SIZE 1024
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -030021
22/**
Mauro Carvalho Chehab7fee03e2009-12-02 15:56:47 -030023 * ir_seek_table() - returns the element order on the table
24 * @rc_tab: the ir_scancode_table with the keymap to be used
25 * @scancode: the scancode that we're seeking
26 *
27 * This routine is used by the input routines when a key is pressed at the
28 * IR. The scancode is received and needs to be converted into a keycode.
29 * If the key is not found, it returns KEY_UNKNOWN. Otherwise, returns the
30 * corresponding keycode from the table.
31 */
32static int ir_seek_table(struct ir_scancode_table *rc_tab, u32 scancode)
33{
34 int rc;
35 unsigned long flags;
36 struct ir_scancode *keymap = rc_tab->scan;
37
38 spin_lock_irqsave(&rc_tab->lock, flags);
39
40 /* FIXME: replace it by a binary search */
41
42 for (rc = 0; rc < rc_tab->size; rc++)
43 if (keymap[rc].scancode == scancode)
44 goto exit;
45
46 /* Not found */
47 rc = -EINVAL;
48
49exit:
50 spin_unlock_irqrestore(&rc_tab->lock, flags);
51 return rc;
52}
53
54/**
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -030055 * ir_roundup_tablesize() - gets an optimum value for the table size
56 * @n_elems: minimum number of entries to store keycodes
57 *
58 * This routine is used to choose the keycode table size.
59 *
60 * In order to have some empty space for new keycodes,
61 * and knowing in advance that kmalloc allocates only power of two
62 * segments, it optimizes the allocated space to have some spare space
63 * for those new keycodes by using the maximum number of entries that
64 * will be effectively be allocated by kmalloc.
65 * In order to reduce the quantity of table resizes, it has a minimum
66 * table size of IR_TAB_MIN_SIZE.
67 */
68int ir_roundup_tablesize(int n_elems)
69{
70 size_t size;
71
72 if (n_elems < IR_TAB_MIN_SIZE)
73 n_elems = IR_TAB_MIN_SIZE;
74
75 /*
76 * As kmalloc only allocates sizes of power of two, get as
77 * much entries as possible for the allocated memory segment
78 */
79 size = roundup_pow_of_two(n_elems * sizeof(struct ir_scancode));
80 n_elems = size / sizeof(struct ir_scancode);
81
82 return n_elems;
83}
Mauro Carvalho Chehab446e4a62009-12-11 08:34:07 -030084EXPORT_SYMBOL_GPL(ir_roundup_tablesize);
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -030085
86/**
87 * ir_copy_table() - copies a keytable, discarding the unused entries
88 * @destin: destin table
89 * @origin: origin table
90 *
91 * Copies all entries where the keycode is not KEY_UNKNOWN/KEY_RESERVED
92 */
93
94int ir_copy_table(struct ir_scancode_table *destin,
95 const struct ir_scancode_table *origin)
96{
97 int i, j = 0;
98
99 for (i = 0; i < origin->size; i++) {
Mauro Carvalho Chehab7fee03e2009-12-02 15:56:47 -0300100 if (origin->scan[i].keycode == KEY_UNKNOWN ||
101 origin->scan[i].keycode == KEY_RESERVED)
102 continue;
103
104 memcpy(&destin->scan[j], &origin->scan[i], sizeof(struct ir_scancode));
105 j++;
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300106 }
107 destin->size = j;
108
Mauro Carvalho Chehab7fee03e2009-12-02 15:56:47 -0300109 IR_dprintk(1, "Copied %d scancodes to the new keycode table\n", destin->size);
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300110
111 return 0;
112}
Mauro Carvalho Chehab446e4a62009-12-11 08:34:07 -0300113EXPORT_SYMBOL_GPL(ir_copy_table);
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300114
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300115/**
116 * ir_getkeycode() - get a keycode at the evdev scancode ->keycode table
117 * @dev: the struct input_dev device descriptor
118 * @scancode: the desired scancode
119 * @keycode: the keycode to be retorned.
120 *
121 * This routine is used to handle evdev EVIOCGKEY ioctl.
122 * If the key is not found, returns -EINVAL, otherwise, returns 0.
123 */
124static int ir_getkeycode(struct input_dev *dev,
125 int scancode, int *keycode)
126{
Mauro Carvalho Chehab7fee03e2009-12-02 15:56:47 -0300127 int elem;
Mauro Carvalho Chehab75543cc2009-12-11 09:44:23 -0300128 struct ir_input_dev *ir_dev = input_get_drvdata(dev);
129 struct ir_scancode_table *rc_tab = &ir_dev->rc_tab;
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300130
Mauro Carvalho Chehab7fee03e2009-12-02 15:56:47 -0300131 elem = ir_seek_table(rc_tab, scancode);
132 if (elem >= 0) {
133 *keycode = rc_tab->scan[elem].keycode;
134 return 0;
135 }
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300136
Mauro Carvalho Chehabe97f4672009-12-04 17:17:47 -0300137 /*
138 * Scancode not found and table can't be expanded
139 */
140 if (elem < 0 && rc_tab->size == IR_TAB_MAX_SIZE)
141 return -EINVAL;
142
143 /*
144 * If is there extra space, returns KEY_RESERVED,
145 * otherwise, input core won't let ir_setkeycode to work
146 */
147 *keycode = KEY_RESERVED;
148 return 0;
149}
150
Mauro Carvalho Chehabe97f4672009-12-04 17:17:47 -0300151/**
152 * ir_is_resize_needed() - Check if the table needs rezise
153 * @table: keycode table that may need to resize
154 * @n_elems: minimum number of entries to store keycodes
155 *
156 * Considering that kmalloc uses power of two storage areas, this
157 * routine detects if the real alloced size will change. If not, it
158 * just returns without doing nothing. Otherwise, it will extend or
159 * reduce the table size to meet the new needs.
160 *
161 * It returns 0 if no resize is needed, 1 otherwise.
162 */
163static int ir_is_resize_needed(struct ir_scancode_table *table, int n_elems)
164{
165 int cur_size = ir_roundup_tablesize(table->size);
166 int new_size = ir_roundup_tablesize(n_elems);
167
168 if (cur_size == new_size)
169 return 0;
170
171 /* Resize is needed */
172 return 1;
173}
174
175/**
176 * ir_delete_key() - remove a keycode from the table
177 * @rc_tab: keycode table
178 * @elem: element to be removed
179 *
180 */
181static void ir_delete_key(struct ir_scancode_table *rc_tab, int elem)
182{
183 unsigned long flags = 0;
184 int newsize = rc_tab->size - 1;
185 int resize = ir_is_resize_needed(rc_tab, newsize);
186 struct ir_scancode *oldkeymap = rc_tab->scan;
187 struct ir_scancode *newkeymap;
188
189 if (resize) {
190 newkeymap = kzalloc(ir_roundup_tablesize(newsize) *
191 sizeof(*newkeymap), GFP_ATOMIC);
192
193 /* There's no memory for resize. Keep the old table */
194 if (!newkeymap)
195 resize = 0;
196 }
197
198 if (!resize) {
199 newkeymap = oldkeymap;
200
201 /* We'll modify the live table. Lock it */
202 spin_lock_irqsave(&rc_tab->lock, flags);
203 }
204
205 /*
206 * Copy the elements before the one that will be deleted
207 * if (!resize), both oldkeymap and newkeymap points
208 * to the same place, so, there's no need to copy
209 */
210 if (resize && elem > 0)
211 memcpy(newkeymap, oldkeymap,
212 elem * sizeof(*newkeymap));
213
214 /*
215 * Copy the other elements overwriting the element to be removed
216 * This operation applies to both resize and non-resize case
217 */
218 if (elem < newsize)
219 memcpy(&newkeymap[elem], &oldkeymap[elem + 1],
220 (newsize - elem) * sizeof(*newkeymap));
221
222 if (resize) {
223 /*
224 * As the copy happened to a temporary table, only here
225 * it needs to lock while replacing the table pointers
226 * to use the new table
227 */
228 spin_lock_irqsave(&rc_tab->lock, flags);
229 rc_tab->size = newsize;
230 rc_tab->scan = newkeymap;
231 spin_unlock_irqrestore(&rc_tab->lock, flags);
232
233 /* Frees the old keytable */
234 kfree(oldkeymap);
235 } else {
236 rc_tab->size = newsize;
237 spin_unlock_irqrestore(&rc_tab->lock, flags);
238 }
239}
240
241/**
242 * ir_insert_key() - insert a keycode at the table
243 * @rc_tab: keycode table
244 * @scancode: the desired scancode
245 * @keycode: the keycode to be retorned.
246 *
247 */
248static int ir_insert_key(struct ir_scancode_table *rc_tab,
249 int scancode, int keycode)
250{
251 unsigned long flags;
252 int elem = rc_tab->size;
253 int newsize = rc_tab->size + 1;
254 int resize = ir_is_resize_needed(rc_tab, newsize);
255 struct ir_scancode *oldkeymap = rc_tab->scan;
256 struct ir_scancode *newkeymap;
257
258 if (resize) {
259 newkeymap = kzalloc(ir_roundup_tablesize(newsize) *
260 sizeof(*newkeymap), GFP_ATOMIC);
261 if (!newkeymap)
262 return -ENOMEM;
263
264 memcpy(newkeymap, oldkeymap,
265 rc_tab->size * sizeof(*newkeymap));
266 } else
267 newkeymap = oldkeymap;
268
269 /* Stores the new code at the table */
270 IR_dprintk(1, "#%d: New scan 0x%04x with key 0x%04x\n",
271 rc_tab->size, scancode, keycode);
272
273 spin_lock_irqsave(&rc_tab->lock, flags);
274 rc_tab->size = newsize;
275 if (resize) {
276 rc_tab->scan = newkeymap;
277 kfree(oldkeymap);
278 }
279 newkeymap[elem].scancode = scancode;
280 newkeymap[elem].keycode = keycode;
281 spin_unlock_irqrestore(&rc_tab->lock, flags);
282
283 return 0;
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300284}
285
286/**
287 * ir_setkeycode() - set a keycode at the evdev scancode ->keycode table
288 * @dev: the struct input_dev device descriptor
289 * @scancode: the desired scancode
290 * @keycode: the keycode to be retorned.
291 *
292 * This routine is used to handle evdev EVIOCSKEY ioctl.
293 * There's one caveat here: how can we increase the size of the table?
294 * If the key is not found, returns -EINVAL, otherwise, returns 0.
295 */
296static int ir_setkeycode(struct input_dev *dev,
297 int scancode, int keycode)
298{
Mauro Carvalho Chehab7fee03e2009-12-02 15:56:47 -0300299 int rc = 0;
Mauro Carvalho Chehab75543cc2009-12-11 09:44:23 -0300300 struct ir_input_dev *ir_dev = input_get_drvdata(dev);
301 struct ir_scancode_table *rc_tab = &ir_dev->rc_tab;
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300302 struct ir_scancode *keymap = rc_tab->scan;
Mauro Carvalho Chehab7fee03e2009-12-02 15:56:47 -0300303 unsigned long flags;
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300304
Mauro Carvalho Chehabe97f4672009-12-04 17:17:47 -0300305 /*
306 * Handle keycode table deletions
307 *
308 * If userspace is adding a KEY_UNKNOWN or KEY_RESERVED,
309 * deal as a trial to remove an existing scancode attribution
310 * if table become too big, reduce it to save space
311 */
312 if (keycode == KEY_UNKNOWN || keycode == KEY_RESERVED) {
313 rc = ir_seek_table(rc_tab, scancode);
314 if (rc < 0)
315 return 0;
316
317 IR_dprintk(1, "#%d: Deleting scan 0x%04x\n", rc, scancode);
318 clear_bit(keymap[rc].keycode, dev->keybit);
319 ir_delete_key(rc_tab, rc);
320
321 return 0;
322 }
323
324 /*
325 * Handle keycode replacements
326 *
327 * If the scancode exists, just replace by the new value
328 */
Mauro Carvalho Chehab7fee03e2009-12-02 15:56:47 -0300329 rc = ir_seek_table(rc_tab, scancode);
Mauro Carvalho Chehabe97f4672009-12-04 17:17:47 -0300330 if (rc >= 0) {
331 IR_dprintk(1, "#%d: Replacing scan 0x%04x with key 0x%04x\n",
332 rc, scancode, keycode);
333
334 clear_bit(keymap[rc].keycode, dev->keybit);
335
336 spin_lock_irqsave(&rc_tab->lock, flags);
337 keymap[rc].keycode = keycode;
338 spin_unlock_irqrestore(&rc_tab->lock, flags);
339
340 set_bit(keycode, dev->keybit);
341
342 return 0;
343 }
344
345 /*
346 * Handle new scancode inserts
347 *
348 * reallocate table if needed and insert a new keycode
349 */
350
351 /* Avoid growing the table indefinitely */
352 if (rc_tab->size + 1 > IR_TAB_MAX_SIZE)
353 return -EINVAL;
354
355 rc = ir_insert_key(rc_tab, scancode, keycode);
356 if (rc < 0)
Mauro Carvalho Chehab7fee03e2009-12-02 15:56:47 -0300357 return rc;
Mauro Carvalho Chehab7fee03e2009-12-02 15:56:47 -0300358 set_bit(keycode, dev->keybit);
359
360 return 0;
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300361}
362
363/**
364 * ir_g_keycode_from_table() - gets the keycode that corresponds to a scancode
Mauro Carvalho Chehab7fee03e2009-12-02 15:56:47 -0300365 * @input_dev: the struct input_dev descriptor of the device
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300366 * @scancode: the scancode that we're seeking
367 *
368 * This routine is used by the input routines when a key is pressed at the
369 * IR. The scancode is received and needs to be converted into a keycode.
370 * If the key is not found, it returns KEY_UNKNOWN. Otherwise, returns the
371 * corresponding keycode from the table.
372 */
373u32 ir_g_keycode_from_table(struct input_dev *dev, u32 scancode)
374{
Mauro Carvalho Chehab75543cc2009-12-11 09:44:23 -0300375 struct ir_input_dev *ir_dev = input_get_drvdata(dev);
376 struct ir_scancode_table *rc_tab = &ir_dev->rc_tab;
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300377 struct ir_scancode *keymap = rc_tab->scan;
Mauro Carvalho Chehab7fee03e2009-12-02 15:56:47 -0300378 int elem;
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300379
Mauro Carvalho Chehab7fee03e2009-12-02 15:56:47 -0300380 elem = ir_seek_table(rc_tab, scancode);
381 if (elem >= 0) {
382 IR_dprintk(1, "%s: scancode 0x%04x keycode 0x%02x\n",
383 dev->name, scancode, keymap[elem].keycode);
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300384
Mauro Carvalho Chehab7fee03e2009-12-02 15:56:47 -0300385 return rc_tab->scan[elem].keycode;
386 }
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300387
388 printk(KERN_INFO "%s: unknown key for scancode 0x%04x\n",
389 dev->name, scancode);
390
Mauro Carvalho Chehab7fee03e2009-12-02 15:56:47 -0300391 /* Reports userspace that an unknown keycode were got */
392 return KEY_RESERVED;
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300393}
Mauro Carvalho Chehab446e4a62009-12-11 08:34:07 -0300394EXPORT_SYMBOL_GPL(ir_g_keycode_from_table);
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300395
396/**
Mauro Carvalho Chehab75543cc2009-12-11 09:44:23 -0300397 * ir_input_register() - sets the IR keycode table and add the handlers
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300398 * for keymap table get/set
399 * @input_dev: the struct input_dev descriptor of the device
400 * @rc_tab: the struct ir_scancode_table table of scancode/keymap
401 *
402 * This routine is used to initialize the input infrastructure to work with
Mauro Carvalho Chehab7fee03e2009-12-02 15:56:47 -0300403 * an IR.
404 * It should be called before registering the IR device.
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300405 */
Mauro Carvalho Chehab75543cc2009-12-11 09:44:23 -0300406int ir_input_register(struct input_dev *input_dev,
407 struct ir_scancode_table *rc_tab)
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300408{
Mauro Carvalho Chehab75543cc2009-12-11 09:44:23 -0300409 struct ir_input_dev *ir_dev;
410 struct ir_scancode *keymap = rc_tab->scan;
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300411 int i;
412
413 if (rc_tab->scan == NULL || !rc_tab->size)
414 return -EINVAL;
415
Mauro Carvalho Chehab75543cc2009-12-11 09:44:23 -0300416 ir_dev = kzalloc(sizeof(*ir_dev), GFP_KERNEL);
417 if (!ir_dev)
418 return -ENOMEM;
419
420 spin_lock_init(&rc_tab->lock);
421
422 ir_dev->rc_tab.size = ir_roundup_tablesize(rc_tab->size);
423 ir_dev->rc_tab.scan = kzalloc(ir_dev->rc_tab.size *
424 sizeof(struct ir_scancode), GFP_KERNEL);
425 if (!ir_dev->rc_tab.scan)
426 return -ENOMEM;
427
428 IR_dprintk(1, "Allocated space for %d keycode entries (%zd bytes)\n",
429 ir_dev->rc_tab.size,
430 ir_dev->rc_tab.size * sizeof(ir_dev->rc_tab.scan));
431
432 ir_copy_table(&ir_dev->rc_tab, rc_tab);
433
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300434 /* set the bits for the keys */
435 IR_dprintk(1, "key map size: %d\n", rc_tab->size);
436 for (i = 0; i < rc_tab->size; i++) {
437 IR_dprintk(1, "#%d: setting bit for keycode 0x%04x\n",
438 i, keymap[i].keycode);
439 set_bit(keymap[i].keycode, input_dev->keybit);
440 }
Mauro Carvalho Chehab75543cc2009-12-11 09:44:23 -0300441 clear_bit(0, input_dev->keybit);
442
443 set_bit(EV_KEY, input_dev->evbit);
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300444
445 input_dev->getkeycode = ir_getkeycode;
446 input_dev->setkeycode = ir_setkeycode;
Mauro Carvalho Chehab75543cc2009-12-11 09:44:23 -0300447 input_set_drvdata(input_dev, ir_dev);
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300448
449 return 0;
450}
Mauro Carvalho Chehab75543cc2009-12-11 09:44:23 -0300451EXPORT_SYMBOL_GPL(ir_input_register);
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300452
Mauro Carvalho Chehab38ef6aa2009-12-11 09:47:42 -0300453void ir_input_unregister(struct input_dev *dev)
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300454{
Mauro Carvalho Chehab75543cc2009-12-11 09:44:23 -0300455 struct ir_input_dev *ir_dev = input_get_drvdata(dev);
456 struct ir_scancode_table *rc_tab = &ir_dev->rc_tab;
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300457
Mauro Carvalho Chehab05395a32009-12-06 08:32:49 -0300458 if (!rc_tab)
459 return;
460
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300461 IR_dprintk(1, "Freed keycode table\n");
462
463 rc_tab->size = 0;
464 kfree(rc_tab->scan);
465 rc_tab->scan = NULL;
Mauro Carvalho Chehab75543cc2009-12-11 09:44:23 -0300466
467 kfree(ir_dev);
468 input_set_drvdata(dev, NULL);
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300469}
Mauro Carvalho Chehab38ef6aa2009-12-11 09:47:42 -0300470EXPORT_SYMBOL_GPL(ir_input_unregister);
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300471
Mauro Carvalho Chehab446e4a62009-12-11 08:34:07 -0300472int ir_core_debug; /* ir_debug level (0,1,2) */
473EXPORT_SYMBOL_GPL(ir_core_debug);
474module_param_named(debug, ir_core_debug, int, 0644);
475
476MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
477MODULE_LICENSE("GPL");