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.
15 #include <linux/usb/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 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
);
84 EXPORT_SYMBOL_GPL(ir_roundup_tablesize
);
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
94 int ir_copy_table(struct ir_scancode_table
*destin
,
95 const struct ir_scancode_table
*origin
)
99 for (i
= 0; i
< origin
->size
; i
++) {
100 if (origin
->scan
[i
].keycode
== KEY_UNKNOWN
||
101 origin
->scan
[i
].keycode
== KEY_RESERVED
)
104 memcpy(&destin
->scan
[j
], &origin
->scan
[i
], sizeof(struct ir_scancode
));
109 IR_dprintk(1, "Copied %d scancodes to the new keycode table\n", destin
->size
);
113 EXPORT_SYMBOL_GPL(ir_copy_table
);
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.
121 * This routine is used to handle evdev EVIOCGKEY ioctl.
122 * If the key is not found, returns -EINVAL, otherwise, returns 0.
124 static int ir_getkeycode(struct input_dev
*dev
,
125 int scancode
, int *keycode
)
128 struct ir_input_dev
*ir_dev
= input_get_drvdata(dev
);
129 struct ir_scancode_table
*rc_tab
= &ir_dev
->rc_tab
;
131 elem
= ir_seek_table(rc_tab
, scancode
);
133 *keycode
= rc_tab
->scan
[elem
].keycode
;
138 * Scancode not found and table can't be expanded
140 if (elem
< 0 && rc_tab
->size
== IR_TAB_MAX_SIZE
)
144 * If is there extra space, returns KEY_RESERVED,
145 * otherwise, input core won't let ir_setkeycode to work
147 *keycode
= KEY_RESERVED
;
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
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.
161 * It returns 0 if no resize is needed, 1 otherwise.
163 static int ir_is_resize_needed(struct ir_scancode_table
*table
, int n_elems
)
165 int cur_size
= ir_roundup_tablesize(table
->size
);
166 int new_size
= ir_roundup_tablesize(n_elems
);
168 if (cur_size
== new_size
)
171 /* Resize is needed */
176 * ir_delete_key() - remove a keycode from the table
177 * @rc_tab: keycode table
178 * @elem: element to be removed
181 static void ir_delete_key(struct ir_scancode_table
*rc_tab
, int elem
)
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
;
190 newkeymap
= kzalloc(ir_roundup_tablesize(newsize
) *
191 sizeof(*newkeymap
), GFP_ATOMIC
);
193 /* There's no memory for resize. Keep the old table */
199 newkeymap
= oldkeymap
;
201 /* We'll modify the live table. Lock it */
202 spin_lock_irqsave(&rc_tab
->lock
, flags
);
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
210 if (resize
&& elem
> 0)
211 memcpy(newkeymap
, oldkeymap
,
212 elem
* sizeof(*newkeymap
));
215 * Copy the other elements overwriting the element to be removed
216 * This operation applies to both resize and non-resize case
219 memcpy(&newkeymap
[elem
], &oldkeymap
[elem
+ 1],
220 (newsize
- elem
) * sizeof(*newkeymap
));
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
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
);
233 /* Frees the old keytable */
236 rc_tab
->size
= newsize
;
237 spin_unlock_irqrestore(&rc_tab
->lock
, flags
);
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.
248 static int ir_insert_key(struct ir_scancode_table
*rc_tab
,
249 int scancode
, int keycode
)
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
;
259 newkeymap
= kzalloc(ir_roundup_tablesize(newsize
) *
260 sizeof(*newkeymap
), GFP_ATOMIC
);
264 memcpy(newkeymap
, oldkeymap
,
265 rc_tab
->size
* sizeof(*newkeymap
));
267 newkeymap
= oldkeymap
;
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
);
273 spin_lock_irqsave(&rc_tab
->lock
, flags
);
274 rc_tab
->size
= newsize
;
276 rc_tab
->scan
= newkeymap
;
279 newkeymap
[elem
].scancode
= scancode
;
280 newkeymap
[elem
].keycode
= keycode
;
281 spin_unlock_irqrestore(&rc_tab
->lock
, flags
);
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.
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.
296 static int ir_setkeycode(struct input_dev
*dev
,
297 int scancode
, int keycode
)
300 struct ir_input_dev
*ir_dev
= input_get_drvdata(dev
);
301 struct ir_scancode_table
*rc_tab
= &ir_dev
->rc_tab
;
302 struct ir_scancode
*keymap
= rc_tab
->scan
;
306 * Handle keycode table deletions
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
312 if (keycode
== KEY_UNKNOWN
|| keycode
== KEY_RESERVED
) {
313 rc
= ir_seek_table(rc_tab
, scancode
);
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
);
325 * Handle keycode replacements
327 * If the scancode exists, just replace by the new value
329 rc
= ir_seek_table(rc_tab
, scancode
);
331 IR_dprintk(1, "#%d: Replacing scan 0x%04x with key 0x%04x\n",
332 rc
, scancode
, keycode
);
334 clear_bit(keymap
[rc
].keycode
, dev
->keybit
);
336 spin_lock_irqsave(&rc_tab
->lock
, flags
);
337 keymap
[rc
].keycode
= keycode
;
338 spin_unlock_irqrestore(&rc_tab
->lock
, flags
);
340 set_bit(keycode
, dev
->keybit
);
346 * Handle new scancode inserts
348 * reallocate table if needed and insert a new keycode
351 /* Avoid growing the table indefinitely */
352 if (rc_tab
->size
+ 1 > IR_TAB_MAX_SIZE
)
355 rc
= ir_insert_key(rc_tab
, scancode
, keycode
);
358 set_bit(keycode
, dev
->keybit
);
364 * ir_g_keycode_from_table() - gets the keycode that corresponds to a scancode
365 * @input_dev: the struct input_dev descriptor of the device
366 * @scancode: the scancode that we're seeking
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.
373 u32
ir_g_keycode_from_table(struct input_dev
*dev
, u32 scancode
)
375 struct ir_input_dev
*ir_dev
= input_get_drvdata(dev
);
376 struct ir_scancode_table
*rc_tab
= &ir_dev
->rc_tab
;
377 struct ir_scancode
*keymap
= rc_tab
->scan
;
380 elem
= ir_seek_table(rc_tab
, scancode
);
382 IR_dprintk(1, "%s: scancode 0x%04x keycode 0x%02x\n",
383 dev
->name
, scancode
, keymap
[elem
].keycode
);
385 return rc_tab
->scan
[elem
].keycode
;
388 printk(KERN_INFO
"%s: unknown key for scancode 0x%04x\n",
389 dev
->name
, scancode
);
391 /* Reports userspace that an unknown keycode were got */
394 EXPORT_SYMBOL_GPL(ir_g_keycode_from_table
);
397 * ir_input_register() - sets the IR keycode table and add the handlers
398 * 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
402 * This routine is used to initialize the input infrastructure to work with
404 * It should be called before registering the IR device.
406 int ir_input_register(struct input_dev
*input_dev
,
407 struct ir_scancode_table
*rc_tab
)
409 struct ir_input_dev
*ir_dev
;
410 struct ir_scancode
*keymap
= rc_tab
->scan
;
413 if (rc_tab
->scan
== NULL
|| !rc_tab
->size
)
416 ir_dev
= kzalloc(sizeof(*ir_dev
), GFP_KERNEL
);
420 spin_lock_init(&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
)
428 IR_dprintk(1, "Allocated space for %d keycode entries (%zd bytes)\n",
430 ir_dev
->rc_tab
.size
* sizeof(ir_dev
->rc_tab
.scan
));
432 ir_copy_table(&ir_dev
->rc_tab
, rc_tab
);
434 /* 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
);
441 clear_bit(0, input_dev
->keybit
);
443 set_bit(EV_KEY
, input_dev
->evbit
);
445 input_dev
->getkeycode
= ir_getkeycode
;
446 input_dev
->setkeycode
= ir_setkeycode
;
447 input_set_drvdata(input_dev
, ir_dev
);
451 EXPORT_SYMBOL_GPL(ir_input_register
);
453 void ir_input_free(struct input_dev
*dev
)
455 struct ir_input_dev
*ir_dev
= input_get_drvdata(dev
);
456 struct ir_scancode_table
*rc_tab
= &ir_dev
->rc_tab
;
461 IR_dprintk(1, "Freed keycode table\n");
468 input_set_drvdata(dev
, NULL
);
470 EXPORT_SYMBOL_GPL(ir_input_free
);
472 int ir_core_debug
; /* ir_debug level (0,1,2) */
473 EXPORT_SYMBOL_GPL(ir_core_debug
);
474 module_param_named(debug
, ir_core_debug
, int, 0644);
476 MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
477 MODULE_LICENSE("GPL");