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

Signed-off-by: Mauro Carvalho Chehab <mchehab@redhat.com>
diff --git a/drivers/media/common/ir-functions.c b/drivers/media/common/ir-functions.c
index b31bd27..e616f62 100644
--- a/drivers/media/common/ir-functions.c
+++ b/drivers/media/common/ir-functions.c
@@ -59,12 +59,20 @@
 {
 	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 @@
 }
 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)
 {
diff --git a/drivers/media/common/ir-keytable.c b/drivers/media/common/ir-keytable.c
index 65e3523..6e3cc78 100644
--- a/drivers/media/common/ir-keytable.c
+++ b/drivers/media/common/ir-keytable.c
@@ -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 @@
 
 	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);
+
diff --git a/include/media/ir-common.h b/include/media/ir-common.h
index 16b8f17..72df046 100644
--- a/include/media/ir-common.h
+++ b/include/media/ir-common.h
@@ -54,6 +54,8 @@
 	/* 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 @@
 
 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 */