V4L/DVB (13538): ir-common: Use a dynamic keycode table

Signed-off-by: Mauro Carvalho Chehab <mchehab@redhat.com>
This commit is contained in:
Mauro Carvalho Chehab 2009-11-29 11:08:02 -03:00
parent 055cd55601
commit f6fc504940
3 changed files with 92 additions and 8 deletions

View file

@ -59,12 +59,20 @@ int ir_input_init(struct input_dev *dev, struct ir_input_state *ir,
{
ir->ir_type = ir_type;
/* FIXME: Add the proper code to dynamically allocate IR table */
ir->keytable.size = ir_roundup_tablesize(ir_codes->size);
ir->keytable.scan = kzalloc(ir->keytable.size *
sizeof(struct ir_scancode), GFP_KERNEL);
if (!ir->keytable.scan)
return -ENOMEM;
ir_set_keycode_table(dev, ir_codes);
IR_dprintk(1, "Allocated space for %d keycode entries (%zd bytes)\n",
ir->keytable.size,
ir->keytable.size * sizeof(ir->keytable.scan));
ir_copy_table(&ir->keytable, ir_codes);
ir_set_keycode_table(dev, &ir->keytable);
clear_bit(0, dev->keybit);
set_bit(EV_KEY, dev->evbit);
if (repeat)
set_bit(EV_REP, dev->evbit);
@ -73,11 +81,6 @@ int ir_input_init(struct input_dev *dev, struct ir_input_state *ir,
}
EXPORT_SYMBOL_GPL(ir_input_init);
void ir_input_free(struct input_dev *input_dev)
{
/* FIXME: Add the proper code to free allocated resources */
}
EXPORT_SYMBOL_GPL(ir_input_free);
void ir_input_nokey(struct input_dev *dev, struct ir_input_state *ir)
{

View file

@ -7,6 +7,68 @@
#include <media/ir-common.h>
#define IR_TAB_MIN_SIZE 32
/**
* ir_roundup_tablesize() - gets an optimum value for the table size
* @n_elems: minimum number of entries to store keycodes
*
* This routine is used to choose the keycode table size.
*
* In order to have some empty space for new keycodes,
* and knowing in advance that kmalloc allocates only power of two
* segments, it optimizes the allocated space to have some spare space
* for those new keycodes by using the maximum number of entries that
* will be effectively be allocated by kmalloc.
* In order to reduce the quantity of table resizes, it has a minimum
* table size of IR_TAB_MIN_SIZE.
*/
int ir_roundup_tablesize(int n_elems)
{
size_t size;
if (n_elems < IR_TAB_MIN_SIZE)
n_elems = IR_TAB_MIN_SIZE;
/*
* As kmalloc only allocates sizes of power of two, get as
* much entries as possible for the allocated memory segment
*/
size = roundup_pow_of_two(n_elems * sizeof(struct ir_scancode));
n_elems = size / sizeof(struct ir_scancode);
return n_elems;
}
/**
* ir_copy_table() - copies a keytable, discarding the unused entries
* @destin: destin table
* @origin: origin table
*
* Copies all entries where the keycode is not KEY_UNKNOWN/KEY_RESERVED
*/
int ir_copy_table(struct ir_scancode_table *destin,
const struct ir_scancode_table *origin)
{
int i, j = 0;
for (i = 0; i < origin->size; i++) {
if (origin->scan[i].keycode != KEY_UNKNOWN &&
origin->scan[i].keycode != KEY_RESERVED) {
memcpy(&destin->scan[j], &origin->scan[i],
sizeof(struct ir_scancode));
j++;
}
}
destin->size = j;
IR_dprintk(1, "Copied %d scancodes to the new keycode table\n", j);
return 0;
}
/**
* ir_getkeycode() - get a keycode at the evdev scancode ->keycode table
* @dev: the struct input_dev device descriptor
@ -152,3 +214,16 @@ int ir_set_keycode_table(struct input_dev *input_dev,
return 0;
}
void ir_input_free(struct input_dev *dev)
{
struct ir_scancode_table *rc_tab = input_get_drvdata(dev);
IR_dprintk(1, "Freed keycode table\n");
rc_tab->size = 0;
kfree(rc_tab->scan);
rc_tab->scan = NULL;
}
EXPORT_SYMBOL_GPL(ir_input_free);

View file

@ -54,6 +54,8 @@ struct ir_input_state {
/* configuration */
int ir_type;
struct ir_scancode_table keytable;
/* key info */
u32 ir_key; /* ir scancode */
u32 keycode; /* linux key code */
@ -121,6 +123,10 @@ u32 ir_g_keycode_from_table(struct input_dev *input_dev,
int ir_set_keycode_table(struct input_dev *input_dev,
struct ir_scancode_table *rc_tab);
int ir_roundup_tablesize(int n_elems);
int ir_copy_table(struct ir_scancode_table *destin,
const struct ir_scancode_table *origin);
void ir_input_free(struct input_dev *input_dev);
/* scancode->keycode map tables from ir-keymaps.c */