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 <media/ir-common.h>
19 #define IR_TAB_MIN_SIZE 32
20 #define IR_TAB_MAX_SIZE 1024
23 * 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
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.
32 static int ir_seek_table(struct ir_scancode_table *rc_tab, u32 scancode)
36 struct ir_scancode *keymap = rc_tab->scan;
38 spin_lock_irqsave(&rc_tab->lock, flags);
40 /* FIXME: replace it by a binary search */
42 for (rc = 0; rc < rc_tab->size; rc++)
43 if (keymap[rc].scancode == scancode)
50 spin_unlock_irqrestore(&rc_tab->lock, flags);
55 * ir_roundup_tablesize() - gets an optimum value for the table size
56 * @n_elems: minimum number of entries to store keycodes
58 * This routine is used to choose the keycode table size.
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.
68 static int ir_roundup_tablesize(int n_elems)
72 if (n_elems < IR_TAB_MIN_SIZE)
73 n_elems = IR_TAB_MIN_SIZE;
76 * As kmalloc only allocates sizes of power of two, get as
77 * much entries as possible for the allocated memory segment
79 size = roundup_pow_of_two(n_elems * sizeof(struct ir_scancode));
80 n_elems = size / sizeof(struct ir_scancode);
86 * ir_copy_table() - copies a keytable, discarding the unused entries
87 * @destin: destin table
88 * @origin: origin table
90 * Copies all entries where the keycode is not KEY_UNKNOWN/KEY_RESERVED
91 * Also copies table size and table protocol.
92 * NOTE: It shouldn't copy the lock field
95 static int ir_copy_table(struct ir_scancode_table *destin,
96 const struct ir_scancode_table *origin)
100 for (i = 0; i < origin->size; i++) {
101 if (origin->scan[i].keycode == KEY_UNKNOWN ||
102 origin->scan[i].keycode == KEY_RESERVED)
105 memcpy(&destin->scan[j], &origin->scan[i], sizeof(struct ir_scancode));
109 destin->ir_type = origin->ir_type;
111 IR_dprintk(1, "Copied %d scancodes to the new keycode table\n", destin->size);
117 * ir_getkeycode() - get a keycode at the evdev scancode ->keycode table
118 * @dev: the struct input_dev device descriptor
119 * @scancode: the desired scancode
120 * @keycode: the keycode to be retorned.
122 * This routine is used to handle evdev EVIOCGKEY ioctl.
123 * If the key is not found, returns -EINVAL, otherwise, returns 0.
125 static int ir_getkeycode(struct input_dev *dev,
126 int scancode, int *keycode)
129 struct ir_input_dev *ir_dev = input_get_drvdata(dev);
130 struct ir_scancode_table *rc_tab = &ir_dev->rc_tab;
132 elem = ir_seek_table(rc_tab, scancode);
134 *keycode = rc_tab->scan[elem].keycode;
139 * Scancode not found and table can't be expanded
141 if (elem < 0 && rc_tab->size == IR_TAB_MAX_SIZE)
145 * If is there extra space, returns KEY_RESERVED,
146 * otherwise, input core won't let ir_setkeycode to work
148 *keycode = KEY_RESERVED;
153 * ir_is_resize_needed() - Check if the table needs rezise
154 * @table: keycode table that may need to resize
155 * @n_elems: minimum number of entries to store keycodes
157 * Considering that kmalloc uses power of two storage areas, this
158 * routine detects if the real alloced size will change. If not, it
159 * just returns without doing nothing. Otherwise, it will extend or
160 * reduce the table size to meet the new needs.
162 * It returns 0 if no resize is needed, 1 otherwise.
164 static int ir_is_resize_needed(struct ir_scancode_table *table, int n_elems)
166 int cur_size = ir_roundup_tablesize(table->size);
167 int new_size = ir_roundup_tablesize(n_elems);
169 if (cur_size == new_size)
172 /* Resize is needed */
177 * ir_delete_key() - remove a keycode from the table
178 * @rc_tab: keycode table
179 * @elem: element to be removed
182 static void ir_delete_key(struct ir_scancode_table *rc_tab, int elem)
184 unsigned long flags = 0;
185 int newsize = rc_tab->size - 1;
186 int resize = ir_is_resize_needed(rc_tab, newsize);
187 struct ir_scancode *oldkeymap = rc_tab->scan;
188 struct ir_scancode *newkeymap = NULL;
191 newkeymap = kzalloc(ir_roundup_tablesize(newsize) *
192 sizeof(*newkeymap), GFP_ATOMIC);
194 /* There's no memory for resize. Keep the old table */
195 if (!resize || !newkeymap) {
196 newkeymap = oldkeymap;
198 /* We'll modify the live table. Lock it */
199 spin_lock_irqsave(&rc_tab->lock, flags);
203 * Copy the elements before the one that will be deleted
204 * if (!resize), both oldkeymap and newkeymap points
205 * to the same place, so, there's no need to copy
207 if (resize && elem > 0)
208 memcpy(newkeymap, oldkeymap,
209 elem * sizeof(*newkeymap));
212 * Copy the other elements overwriting the element to be removed
213 * This operation applies to both resize and non-resize case
216 memcpy(&newkeymap[elem], &oldkeymap[elem + 1],
217 (newsize - elem) * sizeof(*newkeymap));
221 * As the copy happened to a temporary table, only here
222 * it needs to lock while replacing the table pointers
223 * to use the new table
225 spin_lock_irqsave(&rc_tab->lock, flags);
226 rc_tab->size = newsize;
227 rc_tab->scan = newkeymap;
228 spin_unlock_irqrestore(&rc_tab->lock, flags);
230 /* Frees the old keytable */
233 rc_tab->size = newsize;
234 spin_unlock_irqrestore(&rc_tab->lock, flags);
239 * ir_insert_key() - insert a keycode at the table
240 * @rc_tab: keycode table
241 * @scancode: the desired scancode
242 * @keycode: the keycode to be retorned.
245 static int ir_insert_key(struct ir_scancode_table *rc_tab,
246 int scancode, int keycode)
249 int elem = rc_tab->size;
250 int newsize = rc_tab->size + 1;
251 int resize = ir_is_resize_needed(rc_tab, newsize);
252 struct ir_scancode *oldkeymap = rc_tab->scan;
253 struct ir_scancode *newkeymap;
256 newkeymap = kzalloc(ir_roundup_tablesize(newsize) *
257 sizeof(*newkeymap), GFP_ATOMIC);
261 memcpy(newkeymap, oldkeymap,
262 rc_tab->size * sizeof(*newkeymap));
264 newkeymap = oldkeymap;
266 /* Stores the new code at the table */
267 IR_dprintk(1, "#%d: New scan 0x%04x with key 0x%04x\n",
268 rc_tab->size, scancode, keycode);
270 spin_lock_irqsave(&rc_tab->lock, flags);
271 rc_tab->size = newsize;
273 rc_tab->scan = newkeymap;
276 newkeymap[elem].scancode = scancode;
277 newkeymap[elem].keycode = keycode;
278 spin_unlock_irqrestore(&rc_tab->lock, flags);
284 * ir_setkeycode() - set a keycode at the evdev scancode ->keycode table
285 * @dev: the struct input_dev device descriptor
286 * @scancode: the desired scancode
287 * @keycode: the keycode to be retorned.
289 * This routine is used to handle evdev EVIOCSKEY ioctl.
290 * There's one caveat here: how can we increase the size of the table?
291 * If the key is not found, returns -EINVAL, otherwise, returns 0.
293 static int ir_setkeycode(struct input_dev *dev,
294 int scancode, int keycode)
297 struct ir_input_dev *ir_dev = input_get_drvdata(dev);
298 struct ir_scancode_table *rc_tab = &ir_dev->rc_tab;
299 struct ir_scancode *keymap = rc_tab->scan;
303 * Handle keycode table deletions
305 * If userspace is adding a KEY_UNKNOWN or KEY_RESERVED,
306 * deal as a trial to remove an existing scancode attribution
307 * if table become too big, reduce it to save space
309 if (keycode == KEY_UNKNOWN || keycode == KEY_RESERVED) {
310 rc = ir_seek_table(rc_tab, scancode);
314 IR_dprintk(1, "#%d: Deleting scan 0x%04x\n", rc, scancode);
315 clear_bit(keymap[rc].keycode, dev->keybit);
316 ir_delete_key(rc_tab, rc);
322 * Handle keycode replacements
324 * If the scancode exists, just replace by the new value
326 rc = ir_seek_table(rc_tab, scancode);
328 IR_dprintk(1, "#%d: Replacing scan 0x%04x with key 0x%04x\n",
329 rc, scancode, keycode);
331 clear_bit(keymap[rc].keycode, dev->keybit);
333 spin_lock_irqsave(&rc_tab->lock, flags);
334 keymap[rc].keycode = keycode;
335 spin_unlock_irqrestore(&rc_tab->lock, flags);
337 set_bit(keycode, dev->keybit);
343 * Handle new scancode inserts
345 * reallocate table if needed and insert a new keycode
348 /* Avoid growing the table indefinitely */
349 if (rc_tab->size + 1 > IR_TAB_MAX_SIZE)
352 rc = ir_insert_key(rc_tab, scancode, keycode);
355 set_bit(keycode, dev->keybit);
361 * ir_g_keycode_from_table() - gets the keycode that corresponds to a scancode
362 * @input_dev: the struct input_dev descriptor of the device
363 * @scancode: the scancode that we're seeking
365 * This routine is used by the input routines when a key is pressed at the
366 * IR. The scancode is received and needs to be converted into a keycode.
367 * If the key is not found, it returns KEY_UNKNOWN. Otherwise, returns the
368 * corresponding keycode from the table.
370 u32 ir_g_keycode_from_table(struct input_dev *dev, u32 scancode)
372 struct ir_input_dev *ir_dev = input_get_drvdata(dev);
373 struct ir_scancode_table *rc_tab = &ir_dev->rc_tab;
374 struct ir_scancode *keymap = rc_tab->scan;
377 elem = ir_seek_table(rc_tab, scancode);
379 IR_dprintk(1, "%s: scancode 0x%04x keycode 0x%02x\n",
380 dev->name, scancode, keymap[elem].keycode);
382 return rc_tab->scan[elem].keycode;
385 printk(KERN_INFO "%s: unknown key for scancode 0x%04x\n",
386 dev->name, scancode);
388 /* Reports userspace that an unknown keycode were got */
391 EXPORT_SYMBOL_GPL(ir_g_keycode_from_table);
394 * ir_input_register() - sets the IR keycode table and add the handlers
395 * for keymap table get/set
396 * @input_dev: the struct input_dev descriptor of the device
397 * @rc_tab: the struct ir_scancode_table table of scancode/keymap
399 * This routine is used to initialize the input infrastructure
400 * to work with an IR.
401 * It will register the input/evdev interface for the device and
402 * register the syfs code for IR class
404 int ir_input_register(struct input_dev *input_dev,
405 const struct ir_scancode_table *rc_tab,
406 const struct ir_dev_props *props)
408 struct ir_input_dev *ir_dev;
409 struct ir_scancode *keymap = rc_tab->scan;
412 if (rc_tab->scan == NULL || !rc_tab->size)
415 ir_dev = kzalloc(sizeof(*ir_dev), GFP_KERNEL);
419 spin_lock_init(&ir_dev->rc_tab.lock);
421 ir_dev->rc_tab.size = ir_roundup_tablesize(rc_tab->size);
422 ir_dev->rc_tab.scan = kzalloc(ir_dev->rc_tab.size *
423 sizeof(struct ir_scancode), GFP_KERNEL);
424 if (!ir_dev->rc_tab.scan) {
429 IR_dprintk(1, "Allocated space for %d keycode entries (%zd bytes)\n",
431 ir_dev->rc_tab.size * sizeof(ir_dev->rc_tab.scan));
433 ir_copy_table(&ir_dev->rc_tab, rc_tab);
434 ir_dev->props = props;
436 /* set the bits for the keys */
437 IR_dprintk(1, "key map size: %d\n", rc_tab->size);
438 for (i = 0; i < rc_tab->size; i++) {
439 IR_dprintk(1, "#%d: setting bit for keycode 0x%04x\n",
440 i, keymap[i].keycode);
441 set_bit(keymap[i].keycode, input_dev->keybit);
443 clear_bit(0, input_dev->keybit);
445 set_bit(EV_KEY, input_dev->evbit);
447 input_dev->getkeycode = ir_getkeycode;
448 input_dev->setkeycode = ir_setkeycode;
449 input_set_drvdata(input_dev, ir_dev);
451 rc = input_register_device(input_dev);
455 rc = ir_register_class(input_dev);
457 input_unregister_device(input_dev);
466 input_set_drvdata(input_dev, NULL);
469 EXPORT_SYMBOL_GPL(ir_input_register);
472 * ir_input_unregister() - unregisters IR and frees resources
473 * @input_dev: the struct input_dev descriptor of the device
475 * This routine is used to free memory and de-register interfaces.
477 void ir_input_unregister(struct input_dev *dev)
479 struct ir_input_dev *ir_dev = input_get_drvdata(dev);
480 struct ir_scancode_table *rc_tab;
485 IR_dprintk(1, "Freed keycode table\n");
487 rc_tab = &ir_dev->rc_tab;
492 ir_unregister_class(dev);
495 input_unregister_device(dev);
497 EXPORT_SYMBOL_GPL(ir_input_unregister);
499 int ir_core_debug; /* ir_debug level (0,1,2) */
500 EXPORT_SYMBOL_GPL(ir_core_debug);
501 module_param_named(debug, ir_core_debug, int, 0644);
503 MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
504 MODULE_LICENSE("GPL");