aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/media
diff options
context:
space:
mode:
authorMauro Carvalho Chehab <mchehab@redhat.com>2009-11-29 09:08:02 -0500
committerMauro Carvalho Chehab <mchehab@redhat.com>2009-12-05 15:42:21 -0500
commitf6fc50494027e913ff0159e43c593cd75f35ec7a (patch)
treee9337bfa7c65fb9d35db726003b84907c9b9ac61 /drivers/media
parent055cd55601f948675006ca90362fc2bfaae90a86 (diff)
V4L/DVB (13538): ir-common: Use a dynamic keycode table
Signed-off-by: Mauro Carvalho Chehab <mchehab@redhat.com>
Diffstat (limited to 'drivers/media')
-rw-r--r--drivers/media/common/ir-functions.c19
-rw-r--r--drivers/media/common/ir-keytable.c75
2 files changed, 86 insertions, 8 deletions
diff --git a/drivers/media/common/ir-functions.c b/drivers/media/common/ir-functions.c
index b31bd27da374..e616f624ceaa 100644
--- a/drivers/media/common/ir-functions.c
+++ b/drivers/media/common/ir-functions.c
@@ -59,12 +59,20 @@ int ir_input_init(struct input_dev *dev, struct ir_input_state *ir,
59{ 59{
60 ir->ir_type = ir_type; 60 ir->ir_type = ir_type;
61 61
62 /* FIXME: Add the proper code to dynamically allocate IR table */ 62 ir->keytable.size = ir_roundup_tablesize(ir_codes->size);
63 ir->keytable.scan = kzalloc(ir->keytable.size *
64 sizeof(struct ir_scancode), GFP_KERNEL);
65 if (!ir->keytable.scan)
66 return -ENOMEM;
63 67
64 ir_set_keycode_table(dev, ir_codes); 68 IR_dprintk(1, "Allocated space for %d keycode entries (%zd bytes)\n",
69 ir->keytable.size,
70 ir->keytable.size * sizeof(ir->keytable.scan));
65 71
66 clear_bit(0, dev->keybit); 72 ir_copy_table(&ir->keytable, ir_codes);
73 ir_set_keycode_table(dev, &ir->keytable);
67 74
75 clear_bit(0, dev->keybit);
68 set_bit(EV_KEY, dev->evbit); 76 set_bit(EV_KEY, dev->evbit);
69 if (repeat) 77 if (repeat)
70 set_bit(EV_REP, dev->evbit); 78 set_bit(EV_REP, dev->evbit);
@@ -73,11 +81,6 @@ int ir_input_init(struct input_dev *dev, struct ir_input_state *ir,
73} 81}
74EXPORT_SYMBOL_GPL(ir_input_init); 82EXPORT_SYMBOL_GPL(ir_input_init);
75 83
76void ir_input_free(struct input_dev *input_dev)
77{
78 /* FIXME: Add the proper code to free allocated resources */
79}
80EXPORT_SYMBOL_GPL(ir_input_free);
81 84
82void ir_input_nokey(struct input_dev *dev, struct ir_input_state *ir) 85void ir_input_nokey(struct input_dev *dev, struct ir_input_state *ir)
83{ 86{
diff --git a/drivers/media/common/ir-keytable.c b/drivers/media/common/ir-keytable.c
index 65e352389b2d..6e3cc78c33b2 100644
--- a/drivers/media/common/ir-keytable.c
+++ b/drivers/media/common/ir-keytable.c
@@ -7,6 +7,68 @@
7 7
8#include <media/ir-common.h> 8#include <media/ir-common.h>
9 9
10#define IR_TAB_MIN_SIZE 32
11
12/**
13 * ir_roundup_tablesize() - gets an optimum value for the table size
14 * @n_elems: minimum number of entries to store keycodes
15 *
16 * This routine is used to choose the keycode table size.
17 *
18 * In order to have some empty space for new keycodes,
19 * and knowing in advance that kmalloc allocates only power of two
20 * segments, it optimizes the allocated space to have some spare space
21 * for those new keycodes by using the maximum number of entries that
22 * will be effectively be allocated by kmalloc.
23 * In order to reduce the quantity of table resizes, it has a minimum
24 * table size of IR_TAB_MIN_SIZE.
25 */
26int ir_roundup_tablesize(int n_elems)
27{
28 size_t size;
29
30 if (n_elems < IR_TAB_MIN_SIZE)
31 n_elems = IR_TAB_MIN_SIZE;
32
33 /*
34 * As kmalloc only allocates sizes of power of two, get as
35 * much entries as possible for the allocated memory segment
36 */
37 size = roundup_pow_of_two(n_elems * sizeof(struct ir_scancode));
38 n_elems = size / sizeof(struct ir_scancode);
39
40 return n_elems;
41}
42
43/**
44 * ir_copy_table() - copies a keytable, discarding the unused entries
45 * @destin: destin table
46 * @origin: origin table
47 *
48 * Copies all entries where the keycode is not KEY_UNKNOWN/KEY_RESERVED
49 */
50
51int ir_copy_table(struct ir_scancode_table *destin,
52 const struct ir_scancode_table *origin)
53{
54 int i, j = 0;
55
56 for (i = 0; i < origin->size; i++) {
57 if (origin->scan[i].keycode != KEY_UNKNOWN &&
58 origin->scan[i].keycode != KEY_RESERVED) {
59 memcpy(&destin->scan[j], &origin->scan[i],
60 sizeof(struct ir_scancode));
61 j++;
62 }
63 }
64 destin->size = j;
65
66 IR_dprintk(1, "Copied %d scancodes to the new keycode table\n", j);
67
68 return 0;
69}
70
71
10/** 72/**
11 * ir_getkeycode() - get a keycode at the evdev scancode ->keycode table 73 * ir_getkeycode() - get a keycode at the evdev scancode ->keycode table
12 * @dev: the struct input_dev device descriptor 74 * @dev: the struct input_dev device descriptor
@@ -152,3 +214,16 @@ int ir_set_keycode_table(struct input_dev *input_dev,
152 214
153 return 0; 215 return 0;
154} 216}
217
218void ir_input_free(struct input_dev *dev)
219{
220 struct ir_scancode_table *rc_tab = input_get_drvdata(dev);
221
222 IR_dprintk(1, "Freed keycode table\n");
223
224 rc_tab->size = 0;
225 kfree(rc_tab->scan);
226 rc_tab->scan = NULL;
227}
228EXPORT_SYMBOL_GPL(ir_input_free);
229