1 /* ir-register.c - handle IR scancode->keycode tables
3 * Copyright (C) 2009 by Mauro Carvalho Chehab <mchehab@redhat.com>
6 #include <linux/usb/input.h>
8 #include <media/ir-common.h>
10 #define IR_TAB_MIN_SIZE 32
11 #define IR_TAB_MAX_SIZE 1024
14 * ir_seek_table() - returns the element order on the table
15 * @rc_tab: the ir_scancode_table with the keymap to be used
16 * @scancode: the scancode that we're seeking
18 * This routine is used by the input routines when a key is pressed at the
19 * IR. The scancode is received and needs to be converted into a keycode.
20 * If the key is not found, it returns KEY_UNKNOWN. Otherwise, returns the
21 * corresponding keycode from the table.
23 static int ir_seek_table(struct ir_scancode_table *rc_tab, u32 scancode)
27 struct ir_scancode *keymap = rc_tab->scan;
29 spin_lock_irqsave(&rc_tab->lock, flags);
31 /* FIXME: replace it by a binary search */
33 for (rc = 0; rc < rc_tab->size; rc++)
34 if (keymap[rc].scancode == scancode)
41 spin_unlock_irqrestore(&rc_tab->lock, flags);
46 * ir_roundup_tablesize() - gets an optimum value for the table size
47 * @n_elems: minimum number of entries to store keycodes
49 * This routine is used to choose the keycode table size.
51 * In order to have some empty space for new keycodes,
52 * and knowing in advance that kmalloc allocates only power of two
53 * segments, it optimizes the allocated space to have some spare space
54 * for those new keycodes by using the maximum number of entries that
55 * will be effectively be allocated by kmalloc.
56 * In order to reduce the quantity of table resizes, it has a minimum
57 * table size of IR_TAB_MIN_SIZE.
59 int ir_roundup_tablesize(int n_elems)
63 if (n_elems < IR_TAB_MIN_SIZE)
64 n_elems = IR_TAB_MIN_SIZE;
67 * As kmalloc only allocates sizes of power of two, get as
68 * much entries as possible for the allocated memory segment
70 size = roundup_pow_of_two(n_elems * sizeof(struct ir_scancode));
71 n_elems = size / sizeof(struct ir_scancode);
77 * ir_copy_table() - copies a keytable, discarding the unused entries
78 * @destin: destin table
79 * @origin: origin table
81 * Copies all entries where the keycode is not KEY_UNKNOWN/KEY_RESERVED
84 int ir_copy_table(struct ir_scancode_table *destin,
85 const struct ir_scancode_table *origin)
89 for (i = 0; i < origin->size; i++) {
90 if (origin->scan[i].keycode == KEY_UNKNOWN ||
91 origin->scan[i].keycode == KEY_RESERVED)
94 memcpy(&destin->scan[j], &origin->scan[i], sizeof(struct ir_scancode));
99 IR_dprintk(1, "Copied %d scancodes to the new keycode table\n", destin->size);
105 * ir_getkeycode() - get a keycode at the evdev scancode ->keycode table
106 * @dev: the struct input_dev device descriptor
107 * @scancode: the desired scancode
108 * @keycode: the keycode to be retorned.
110 * This routine is used to handle evdev EVIOCGKEY ioctl.
111 * If the key is not found, returns -EINVAL, otherwise, returns 0.
113 static int ir_getkeycode(struct input_dev *dev,
114 int scancode, int *keycode)
117 struct ir_scancode_table *rc_tab = input_get_drvdata(dev);
119 elem = ir_seek_table(rc_tab, scancode);
121 *keycode = rc_tab->scan[elem].keycode;
126 * Scancode not found and table can't be expanded
128 if (elem < 0 && rc_tab->size == IR_TAB_MAX_SIZE)
132 * If is there extra space, returns KEY_RESERVED,
133 * otherwise, input core won't let ir_setkeycode to work
135 *keycode = KEY_RESERVED;
141 * ir_is_resize_needed() - Check if the table needs rezise
142 * @table: keycode table that may need to resize
143 * @n_elems: minimum number of entries to store keycodes
145 * Considering that kmalloc uses power of two storage areas, this
146 * routine detects if the real alloced size will change. If not, it
147 * just returns without doing nothing. Otherwise, it will extend or
148 * reduce the table size to meet the new needs.
150 * It returns 0 if no resize is needed, 1 otherwise.
152 static int ir_is_resize_needed(struct ir_scancode_table *table, int n_elems)
154 int cur_size = ir_roundup_tablesize(table->size);
155 int new_size = ir_roundup_tablesize(n_elems);
157 if (cur_size == new_size)
160 /* Resize is needed */
165 * ir_delete_key() - remove a keycode from the table
166 * @rc_tab: keycode table
167 * @elem: element to be removed
170 static void ir_delete_key(struct ir_scancode_table *rc_tab, int elem)
172 unsigned long flags = 0;
173 int newsize = rc_tab->size - 1;
174 int resize = ir_is_resize_needed(rc_tab, newsize);
175 struct ir_scancode *oldkeymap = rc_tab->scan;
176 struct ir_scancode *newkeymap;
179 newkeymap = kzalloc(ir_roundup_tablesize(newsize) *
180 sizeof(*newkeymap), GFP_ATOMIC);
182 /* There's no memory for resize. Keep the old table */
188 newkeymap = oldkeymap;
190 /* We'll modify the live table. Lock it */
191 spin_lock_irqsave(&rc_tab->lock, flags);
195 * Copy the elements before the one that will be deleted
196 * if (!resize), both oldkeymap and newkeymap points
197 * to the same place, so, there's no need to copy
199 if (resize && elem > 0)
200 memcpy(newkeymap, oldkeymap,
201 elem * sizeof(*newkeymap));
204 * Copy the other elements overwriting the element to be removed
205 * This operation applies to both resize and non-resize case
208 memcpy(&newkeymap[elem], &oldkeymap[elem + 1],
209 (newsize - elem) * sizeof(*newkeymap));
213 * As the copy happened to a temporary table, only here
214 * it needs to lock while replacing the table pointers
215 * to use the new table
217 spin_lock_irqsave(&rc_tab->lock, flags);
218 rc_tab->size = newsize;
219 rc_tab->scan = newkeymap;
220 spin_unlock_irqrestore(&rc_tab->lock, flags);
222 /* Frees the old keytable */
225 rc_tab->size = newsize;
226 spin_unlock_irqrestore(&rc_tab->lock, flags);
231 * ir_insert_key() - insert a keycode at the table
232 * @rc_tab: keycode table
233 * @scancode: the desired scancode
234 * @keycode: the keycode to be retorned.
237 static int ir_insert_key(struct ir_scancode_table *rc_tab,
238 int scancode, int keycode)
241 int elem = rc_tab->size;
242 int newsize = rc_tab->size + 1;
243 int resize = ir_is_resize_needed(rc_tab, newsize);
244 struct ir_scancode *oldkeymap = rc_tab->scan;
245 struct ir_scancode *newkeymap;
248 newkeymap = kzalloc(ir_roundup_tablesize(newsize) *
249 sizeof(*newkeymap), GFP_ATOMIC);
253 memcpy(newkeymap, oldkeymap,
254 rc_tab->size * sizeof(*newkeymap));
256 newkeymap = oldkeymap;
258 /* Stores the new code at the table */
259 IR_dprintk(1, "#%d: New scan 0x%04x with key 0x%04x\n",
260 rc_tab->size, scancode, keycode);
262 spin_lock_irqsave(&rc_tab->lock, flags);
263 rc_tab->size = newsize;
265 rc_tab->scan = newkeymap;
268 newkeymap[elem].scancode = scancode;
269 newkeymap[elem].keycode = keycode;
270 spin_unlock_irqrestore(&rc_tab->lock, flags);
276 * ir_setkeycode() - set a keycode at the evdev scancode ->keycode table
277 * @dev: the struct input_dev device descriptor
278 * @scancode: the desired scancode
279 * @keycode: the keycode to be retorned.
281 * This routine is used to handle evdev EVIOCSKEY ioctl.
282 * There's one caveat here: how can we increase the size of the table?
283 * If the key is not found, returns -EINVAL, otherwise, returns 0.
285 static int ir_setkeycode(struct input_dev *dev,
286 int scancode, int keycode)
289 struct ir_scancode_table *rc_tab = input_get_drvdata(dev);
290 struct ir_scancode *keymap = rc_tab->scan;
294 * Handle keycode table deletions
296 * If userspace is adding a KEY_UNKNOWN or KEY_RESERVED,
297 * deal as a trial to remove an existing scancode attribution
298 * if table become too big, reduce it to save space
300 if (keycode == KEY_UNKNOWN || keycode == KEY_RESERVED) {
301 rc = ir_seek_table(rc_tab, scancode);
305 IR_dprintk(1, "#%d: Deleting scan 0x%04x\n", rc, scancode);
306 clear_bit(keymap[rc].keycode, dev->keybit);
307 ir_delete_key(rc_tab, rc);
313 * Handle keycode replacements
315 * If the scancode exists, just replace by the new value
317 rc = ir_seek_table(rc_tab, scancode);
319 IR_dprintk(1, "#%d: Replacing scan 0x%04x with key 0x%04x\n",
320 rc, scancode, keycode);
322 clear_bit(keymap[rc].keycode, dev->keybit);
324 spin_lock_irqsave(&rc_tab->lock, flags);
325 keymap[rc].keycode = keycode;
326 spin_unlock_irqrestore(&rc_tab->lock, flags);
328 set_bit(keycode, dev->keybit);
334 * Handle new scancode inserts
336 * reallocate table if needed and insert a new keycode
339 /* Avoid growing the table indefinitely */
340 if (rc_tab->size + 1 > IR_TAB_MAX_SIZE)
343 rc = ir_insert_key(rc_tab, scancode, keycode);
346 set_bit(keycode, dev->keybit);
352 * ir_g_keycode_from_table() - gets the keycode that corresponds to a scancode
353 * @input_dev: the struct input_dev descriptor of the device
354 * @scancode: the scancode that we're seeking
356 * This routine is used by the input routines when a key is pressed at the
357 * IR. The scancode is received and needs to be converted into a keycode.
358 * If the key is not found, it returns KEY_UNKNOWN. Otherwise, returns the
359 * corresponding keycode from the table.
361 u32 ir_g_keycode_from_table(struct input_dev *dev, u32 scancode)
363 struct ir_scancode_table *rc_tab = input_get_drvdata(dev);
364 struct ir_scancode *keymap = rc_tab->scan;
367 elem = ir_seek_table(rc_tab, scancode);
369 IR_dprintk(1, "%s: scancode 0x%04x keycode 0x%02x\n",
370 dev->name, scancode, keymap[elem].keycode);
372 return rc_tab->scan[elem].keycode;
375 printk(KERN_INFO "%s: unknown key for scancode 0x%04x\n",
376 dev->name, scancode);
378 /* Reports userspace that an unknown keycode were got */
383 * ir_set_keycode_table() - sets the IR keycode table and add the handlers
384 * for keymap table get/set
385 * @input_dev: the struct input_dev descriptor of the device
386 * @rc_tab: the struct ir_scancode_table table of scancode/keymap
388 * This routine is used to initialize the input infrastructure to work with
390 * It should be called before registering the IR device.
392 int ir_set_keycode_table(struct input_dev *input_dev,
393 struct ir_scancode_table *rc_tab)
395 struct ir_scancode *keymap = rc_tab->scan;
398 spin_lock_init(&rc_tab->lock);
400 if (rc_tab->scan == NULL || !rc_tab->size)
403 /* set the bits for the keys */
404 IR_dprintk(1, "key map size: %d\n", rc_tab->size);
405 for (i = 0; i < rc_tab->size; i++) {
406 IR_dprintk(1, "#%d: setting bit for keycode 0x%04x\n",
407 i, keymap[i].keycode);
408 set_bit(keymap[i].keycode, input_dev->keybit);
411 input_dev->getkeycode = ir_getkeycode;
412 input_dev->setkeycode = ir_setkeycode;
413 input_set_drvdata(input_dev, rc_tab);
418 void ir_input_free(struct input_dev *dev)
420 struct ir_scancode_table *rc_tab = input_get_drvdata(dev);
422 IR_dprintk(1, "Freed keycode table\n");
428 EXPORT_SYMBOL_GPL(ir_input_free);