Staging: strip: delete the driver
[linux/fpc-iii.git] / drivers / media / IR / ir-keytable.c
blobbfca26d51827823fdb42c15778843bb2f5889c51
1 /* ir-register.c - handle IR scancode->keycode tables
3 * Copyright (C) 2009 by Mauro Carvalho Chehab <mchehab@redhat.com>
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.
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.
16 #include <linux/input.h>
17 #include <linux/slab.h>
18 #include <media/ir-common.h>
20 #define IR_TAB_MIN_SIZE 32
21 #define IR_TAB_MAX_SIZE 1024
23 /**
24 * ir_seek_table() - returns the element order on the table
25 * @rc_tab: the ir_scancode_table with the keymap to be used
26 * @scancode: the scancode that we're seeking
28 * This routine is used by the input routines when a key is pressed at the
29 * IR. The scancode is received and needs to be converted into a keycode.
30 * If the key is not found, it returns KEY_UNKNOWN. Otherwise, returns the
31 * corresponding keycode from the table.
33 static int ir_seek_table(struct ir_scancode_table *rc_tab, u32 scancode)
35 int rc;
36 unsigned long flags;
37 struct ir_scancode *keymap = rc_tab->scan;
39 spin_lock_irqsave(&rc_tab->lock, flags);
41 /* FIXME: replace it by a binary search */
43 for (rc = 0; rc < rc_tab->size; rc++)
44 if (keymap[rc].scancode == scancode)
45 goto exit;
47 /* Not found */
48 rc = -EINVAL;
50 exit:
51 spin_unlock_irqrestore(&rc_tab->lock, flags);
52 return rc;
55 /**
56 * ir_roundup_tablesize() - gets an optimum value for the table size
57 * @n_elems: minimum number of entries to store keycodes
59 * This routine is used to choose the keycode table size.
61 * In order to have some empty space for new keycodes,
62 * and knowing in advance that kmalloc allocates only power of two
63 * segments, it optimizes the allocated space to have some spare space
64 * for those new keycodes by using the maximum number of entries that
65 * will be effectively be allocated by kmalloc.
66 * In order to reduce the quantity of table resizes, it has a minimum
67 * table size of IR_TAB_MIN_SIZE.
69 static int ir_roundup_tablesize(int n_elems)
71 size_t size;
73 if (n_elems < IR_TAB_MIN_SIZE)
74 n_elems = IR_TAB_MIN_SIZE;
77 * As kmalloc only allocates sizes of power of two, get as
78 * much entries as possible for the allocated memory segment
80 size = roundup_pow_of_two(n_elems * sizeof(struct ir_scancode));
81 n_elems = size / sizeof(struct ir_scancode);
83 return n_elems;
86 /**
87 * ir_copy_table() - copies a keytable, discarding the unused entries
88 * @destin: destin table
89 * @origin: origin table
91 * Copies all entries where the keycode is not KEY_UNKNOWN/KEY_RESERVED
92 * Also copies table size and table protocol.
93 * NOTE: It shouldn't copy the lock field
96 static int ir_copy_table(struct ir_scancode_table *destin,
97 const struct ir_scancode_table *origin)
99 int i, j = 0;
101 for (i = 0; i < origin->size; i++) {
102 if (origin->scan[i].keycode == KEY_UNKNOWN ||
103 origin->scan[i].keycode == KEY_RESERVED)
104 continue;
106 memcpy(&destin->scan[j], &origin->scan[i], sizeof(struct ir_scancode));
107 j++;
109 destin->size = j;
110 destin->ir_type = origin->ir_type;
112 IR_dprintk(1, "Copied %d scancodes to the new keycode table\n", destin->size);
114 return 0;
118 * ir_getkeycode() - get a keycode at the evdev scancode ->keycode table
119 * @dev: the struct input_dev device descriptor
120 * @scancode: the desired scancode
121 * @keycode: the keycode to be retorned.
123 * This routine is used to handle evdev EVIOCGKEY ioctl.
124 * If the key is not found, returns -EINVAL, otherwise, returns 0.
126 static int ir_getkeycode(struct input_dev *dev,
127 unsigned int scancode, unsigned int *keycode)
129 int elem;
130 struct ir_input_dev *ir_dev = input_get_drvdata(dev);
131 struct ir_scancode_table *rc_tab = &ir_dev->rc_tab;
133 elem = ir_seek_table(rc_tab, scancode);
134 if (elem >= 0) {
135 *keycode = rc_tab->scan[elem].keycode;
136 return 0;
140 * Scancode not found and table can't be expanded
142 if (elem < 0 && rc_tab->size == IR_TAB_MAX_SIZE)
143 return -EINVAL;
146 * If is there extra space, returns KEY_RESERVED,
147 * otherwise, input core won't let ir_setkeycode to work
149 *keycode = KEY_RESERVED;
150 return 0;
154 * ir_is_resize_needed() - Check if the table needs rezise
155 * @table: keycode table that may need to resize
156 * @n_elems: minimum number of entries to store keycodes
158 * Considering that kmalloc uses power of two storage areas, this
159 * routine detects if the real alloced size will change. If not, it
160 * just returns without doing nothing. Otherwise, it will extend or
161 * reduce the table size to meet the new needs.
163 * It returns 0 if no resize is needed, 1 otherwise.
165 static int ir_is_resize_needed(struct ir_scancode_table *table, int n_elems)
167 int cur_size = ir_roundup_tablesize(table->size);
168 int new_size = ir_roundup_tablesize(n_elems);
170 if (cur_size == new_size)
171 return 0;
173 /* Resize is needed */
174 return 1;
178 * ir_delete_key() - remove a keycode from the table
179 * @rc_tab: keycode table
180 * @elem: element to be removed
183 static void ir_delete_key(struct ir_scancode_table *rc_tab, int elem)
185 unsigned long flags = 0;
186 int newsize = rc_tab->size - 1;
187 int resize = ir_is_resize_needed(rc_tab, newsize);
188 struct ir_scancode *oldkeymap = rc_tab->scan;
189 struct ir_scancode *newkeymap = NULL;
191 if (resize)
192 newkeymap = kzalloc(ir_roundup_tablesize(newsize) *
193 sizeof(*newkeymap), GFP_ATOMIC);
195 /* There's no memory for resize. Keep the old table */
196 if (!resize || !newkeymap) {
197 newkeymap = oldkeymap;
199 /* We'll modify the live table. Lock it */
200 spin_lock_irqsave(&rc_tab->lock, flags);
204 * Copy the elements before the one that will be deleted
205 * if (!resize), both oldkeymap and newkeymap points
206 * to the same place, so, there's no need to copy
208 if (resize && elem > 0)
209 memcpy(newkeymap, oldkeymap,
210 elem * sizeof(*newkeymap));
213 * Copy the other elements overwriting the element to be removed
214 * This operation applies to both resize and non-resize case
216 if (elem < newsize)
217 memcpy(&newkeymap[elem], &oldkeymap[elem + 1],
218 (newsize - elem) * sizeof(*newkeymap));
220 if (resize) {
222 * As the copy happened to a temporary table, only here
223 * it needs to lock while replacing the table pointers
224 * to use the new table
226 spin_lock_irqsave(&rc_tab->lock, flags);
227 rc_tab->size = newsize;
228 rc_tab->scan = newkeymap;
229 spin_unlock_irqrestore(&rc_tab->lock, flags);
231 /* Frees the old keytable */
232 kfree(oldkeymap);
233 } else {
234 rc_tab->size = newsize;
235 spin_unlock_irqrestore(&rc_tab->lock, flags);
240 * ir_insert_key() - insert a keycode at the table
241 * @rc_tab: keycode table
242 * @scancode: the desired scancode
243 * @keycode: the keycode to be retorned.
246 static int ir_insert_key(struct ir_scancode_table *rc_tab,
247 int scancode, int keycode)
249 unsigned long flags;
250 int elem = rc_tab->size;
251 int newsize = rc_tab->size + 1;
252 int resize = ir_is_resize_needed(rc_tab, newsize);
253 struct ir_scancode *oldkeymap = rc_tab->scan;
254 struct ir_scancode *newkeymap;
256 if (resize) {
257 newkeymap = kzalloc(ir_roundup_tablesize(newsize) *
258 sizeof(*newkeymap), GFP_ATOMIC);
259 if (!newkeymap)
260 return -ENOMEM;
262 memcpy(newkeymap, oldkeymap,
263 rc_tab->size * sizeof(*newkeymap));
264 } else
265 newkeymap = oldkeymap;
267 /* Stores the new code at the table */
268 IR_dprintk(1, "#%d: New scan 0x%04x with key 0x%04x\n",
269 rc_tab->size, scancode, keycode);
271 spin_lock_irqsave(&rc_tab->lock, flags);
272 rc_tab->size = newsize;
273 if (resize) {
274 rc_tab->scan = newkeymap;
275 kfree(oldkeymap);
277 newkeymap[elem].scancode = scancode;
278 newkeymap[elem].keycode = keycode;
279 spin_unlock_irqrestore(&rc_tab->lock, flags);
281 return 0;
285 * ir_setkeycode() - set a keycode at the evdev scancode ->keycode table
286 * @dev: the struct input_dev device descriptor
287 * @scancode: the desired scancode
288 * @keycode: the keycode to be retorned.
290 * This routine is used to handle evdev EVIOCSKEY ioctl.
291 * There's one caveat here: how can we increase the size of the table?
292 * If the key is not found, returns -EINVAL, otherwise, returns 0.
294 static int ir_setkeycode(struct input_dev *dev,
295 unsigned int scancode, unsigned int keycode)
297 int rc = 0;
298 struct ir_input_dev *ir_dev = input_get_drvdata(dev);
299 struct ir_scancode_table *rc_tab = &ir_dev->rc_tab;
300 struct ir_scancode *keymap = rc_tab->scan;
301 unsigned long flags;
304 * Handle keycode table deletions
306 * If userspace is adding a KEY_UNKNOWN or KEY_RESERVED,
307 * deal as a trial to remove an existing scancode attribution
308 * if table become too big, reduce it to save space
310 if (keycode == KEY_UNKNOWN || keycode == KEY_RESERVED) {
311 rc = ir_seek_table(rc_tab, scancode);
312 if (rc < 0)
313 return 0;
315 IR_dprintk(1, "#%d: Deleting scan 0x%04x\n", rc, scancode);
316 clear_bit(keymap[rc].keycode, dev->keybit);
317 ir_delete_key(rc_tab, rc);
319 return 0;
323 * Handle keycode replacements
325 * If the scancode exists, just replace by the new value
327 rc = ir_seek_table(rc_tab, scancode);
328 if (rc >= 0) {
329 IR_dprintk(1, "#%d: Replacing scan 0x%04x with key 0x%04x\n",
330 rc, scancode, keycode);
332 clear_bit(keymap[rc].keycode, dev->keybit);
334 spin_lock_irqsave(&rc_tab->lock, flags);
335 keymap[rc].keycode = keycode;
336 spin_unlock_irqrestore(&rc_tab->lock, flags);
338 set_bit(keycode, dev->keybit);
340 return 0;
344 * Handle new scancode inserts
346 * reallocate table if needed and insert a new keycode
349 /* Avoid growing the table indefinitely */
350 if (rc_tab->size + 1 > IR_TAB_MAX_SIZE)
351 return -EINVAL;
353 rc = ir_insert_key(rc_tab, scancode, keycode);
354 if (rc < 0)
355 return rc;
356 set_bit(keycode, dev->keybit);
358 return 0;
362 * ir_g_keycode_from_table() - gets the keycode that corresponds to a scancode
363 * @input_dev: the struct input_dev descriptor of the device
364 * @scancode: the scancode that we're seeking
366 * This routine is used by the input routines when a key is pressed at the
367 * IR. The scancode is received and needs to be converted into a keycode.
368 * If the key is not found, it returns KEY_UNKNOWN. Otherwise, returns the
369 * corresponding keycode from the table.
371 u32 ir_g_keycode_from_table(struct input_dev *dev, u32 scancode)
373 struct ir_input_dev *ir_dev = input_get_drvdata(dev);
374 struct ir_scancode_table *rc_tab = &ir_dev->rc_tab;
375 struct ir_scancode *keymap = rc_tab->scan;
376 int elem;
378 elem = ir_seek_table(rc_tab, scancode);
379 if (elem >= 0) {
380 IR_dprintk(1, "%s: scancode 0x%04x keycode 0x%02x\n",
381 dev->name, scancode, keymap[elem].keycode);
383 return rc_tab->scan[elem].keycode;
386 printk(KERN_INFO "%s: unknown key for scancode 0x%04x\n",
387 dev->name, scancode);
389 /* Reports userspace that an unknown keycode were got */
390 return KEY_RESERVED;
392 EXPORT_SYMBOL_GPL(ir_g_keycode_from_table);
395 * ir_input_register() - sets the IR keycode table and add the handlers
396 * for keymap table get/set
397 * @input_dev: the struct input_dev descriptor of the device
398 * @rc_tab: the struct ir_scancode_table table of scancode/keymap
400 * This routine is used to initialize the input infrastructure
401 * to work with an IR.
402 * It will register the input/evdev interface for the device and
403 * register the syfs code for IR class
405 int ir_input_register(struct input_dev *input_dev,
406 const struct ir_scancode_table *rc_tab,
407 const struct ir_dev_props *props)
409 struct ir_input_dev *ir_dev;
410 struct ir_scancode *keymap = rc_tab->scan;
411 int i, rc;
413 if (rc_tab->scan == NULL || !rc_tab->size)
414 return -EINVAL;
416 ir_dev = kzalloc(sizeof(*ir_dev), GFP_KERNEL);
417 if (!ir_dev)
418 return -ENOMEM;
420 spin_lock_init(&ir_dev->rc_tab.lock);
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 kfree(ir_dev);
427 return -ENOMEM;
430 IR_dprintk(1, "Allocated space for %d keycode entries (%zd bytes)\n",
431 ir_dev->rc_tab.size,
432 ir_dev->rc_tab.size * sizeof(ir_dev->rc_tab.scan));
434 ir_copy_table(&ir_dev->rc_tab, rc_tab);
435 ir_dev->props = props;
437 /* set the bits for the keys */
438 IR_dprintk(1, "key map size: %d\n", rc_tab->size);
439 for (i = 0; i < rc_tab->size; i++) {
440 IR_dprintk(1, "#%d: setting bit for keycode 0x%04x\n",
441 i, keymap[i].keycode);
442 set_bit(keymap[i].keycode, input_dev->keybit);
444 clear_bit(0, input_dev->keybit);
446 set_bit(EV_KEY, input_dev->evbit);
448 input_dev->getkeycode = ir_getkeycode;
449 input_dev->setkeycode = ir_setkeycode;
450 input_set_drvdata(input_dev, ir_dev);
452 rc = input_register_device(input_dev);
453 if (rc < 0)
454 goto err;
456 rc = ir_register_class(input_dev);
457 if (rc < 0) {
458 input_unregister_device(input_dev);
459 goto err;
462 return 0;
464 err:
465 kfree(rc_tab->scan);
466 kfree(ir_dev);
467 input_set_drvdata(input_dev, NULL);
468 return rc;
470 EXPORT_SYMBOL_GPL(ir_input_register);
473 * ir_input_unregister() - unregisters IR and frees resources
474 * @input_dev: the struct input_dev descriptor of the device
476 * This routine is used to free memory and de-register interfaces.
478 void ir_input_unregister(struct input_dev *dev)
480 struct ir_input_dev *ir_dev = input_get_drvdata(dev);
481 struct ir_scancode_table *rc_tab;
483 if (!ir_dev)
484 return;
486 IR_dprintk(1, "Freed keycode table\n");
488 rc_tab = &ir_dev->rc_tab;
489 rc_tab->size = 0;
490 kfree(rc_tab->scan);
491 rc_tab->scan = NULL;
493 ir_unregister_class(dev);
495 kfree(ir_dev);
496 input_unregister_device(dev);
498 EXPORT_SYMBOL_GPL(ir_input_unregister);
500 int ir_core_debug; /* ir_debug level (0,1,2) */
501 EXPORT_SYMBOL_GPL(ir_core_debug);
502 module_param_named(debug, ir_core_debug, int, 0644);
504 MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
505 MODULE_LICENSE("GPL");