aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/media
diff options
context:
space:
mode:
authorMauro Carvalho Chehab <mchehab@redhat.com>2009-12-02 13:56:47 -0500
committerMauro Carvalho Chehab <mchehab@redhat.com>2009-12-05 15:42:22 -0500
commit7fee03e487e87a196deb5602ee3c7676511995c9 (patch)
treeb46cfcd2d0cbe8640125cbd6548355872e36c193 /drivers/media
parenta53e21257171af42c9fa6aee417f7891744d6ebf (diff)
V4L/DVB (13540): ir-common: Cleanup get key evdev code
The same loop to seek for a key were used on different places. Also, no spinlock were protecting it to avoid the risk of replacing a keycode while seeking for a new code. This cleanup does: - create an unique function to seek for a code; - adds an spinlock to protect the table lookup; - remove some unused code; - simplifies to code to make it easier to understand. Basically no change in behavior should be noticed after this patch. Signed-off-by: Mauro Carvalho Chehab <mchehab@redhat.com>
Diffstat (limited to 'drivers/media')
-rw-r--r--drivers/media/common/ir-keytable.c137
1 files changed, 75 insertions, 62 deletions
diff --git a/drivers/media/common/ir-keytable.c b/drivers/media/common/ir-keytable.c
index 6e3cc78c33b2..b06f023ca760 100644
--- a/drivers/media/common/ir-keytable.c
+++ b/drivers/media/common/ir-keytable.c
@@ -10,6 +10,38 @@
10#define IR_TAB_MIN_SIZE 32 10#define IR_TAB_MIN_SIZE 32
11 11
12/** 12/**
13 * ir_seek_table() - returns the element order on the table
14 * @rc_tab: the ir_scancode_table with the keymap to be used
15 * @scancode: the scancode that we're seeking
16 *
17 * This routine is used by the input routines when a key is pressed at the
18 * IR. The scancode is received and needs to be converted into a keycode.
19 * If the key is not found, it returns KEY_UNKNOWN. Otherwise, returns the
20 * corresponding keycode from the table.
21 */
22static int ir_seek_table(struct ir_scancode_table *rc_tab, u32 scancode)
23{
24 int rc;
25 unsigned long flags;
26 struct ir_scancode *keymap = rc_tab->scan;
27
28 spin_lock_irqsave(&rc_tab->lock, flags);
29
30 /* FIXME: replace it by a binary search */
31
32 for (rc = 0; rc < rc_tab->size; rc++)
33 if (keymap[rc].scancode == scancode)
34 goto exit;
35
36 /* Not found */
37 rc = -EINVAL;
38
39exit:
40 spin_unlock_irqrestore(&rc_tab->lock, flags);
41 return rc;
42}
43
44/**
13 * ir_roundup_tablesize() - gets an optimum value for the table size 45 * ir_roundup_tablesize() - gets an optimum value for the table size
14 * @n_elems: minimum number of entries to store keycodes 46 * @n_elems: minimum number of entries to store keycodes
15 * 47 *
@@ -54,21 +86,20 @@ int ir_copy_table(struct ir_scancode_table *destin,
54 int i, j = 0; 86 int i, j = 0;
55 87
56 for (i = 0; i < origin->size; i++) { 88 for (i = 0; i < origin->size; i++) {
57 if (origin->scan[i].keycode != KEY_UNKNOWN && 89 if (origin->scan[i].keycode == KEY_UNKNOWN ||
58 origin->scan[i].keycode != KEY_RESERVED) { 90 origin->scan[i].keycode == KEY_RESERVED)
59 memcpy(&destin->scan[j], &origin->scan[i], 91 continue;
60 sizeof(struct ir_scancode)); 92
61 j++; 93 memcpy(&destin->scan[j], &origin->scan[i], sizeof(struct ir_scancode));
62 } 94 j++;
63 } 95 }
64 destin->size = j; 96 destin->size = j;
65 97
66 IR_dprintk(1, "Copied %d scancodes to the new keycode table\n", j); 98 IR_dprintk(1, "Copied %d scancodes to the new keycode table\n", destin->size);
67 99
68 return 0; 100 return 0;
69} 101}
70 102
71
72/** 103/**
73 * ir_getkeycode() - get a keycode at the evdev scancode ->keycode table 104 * ir_getkeycode() - get a keycode at the evdev scancode ->keycode table
74 * @dev: the struct input_dev device descriptor 105 * @dev: the struct input_dev device descriptor
@@ -81,28 +112,14 @@ int ir_copy_table(struct ir_scancode_table *destin,
81static int ir_getkeycode(struct input_dev *dev, 112static int ir_getkeycode(struct input_dev *dev,
82 int scancode, int *keycode) 113 int scancode, int *keycode)
83{ 114{
84 int i; 115 int elem;
85 struct ir_scancode_table *rc_tab = input_get_drvdata(dev); 116 struct ir_scancode_table *rc_tab = input_get_drvdata(dev);
86 struct ir_scancode *keymap = rc_tab->scan;
87
88 /* See if we can match the raw key code. */
89 for (i = 0; i < rc_tab->size; i++)
90 if (keymap[i].scancode == scancode) {
91 *keycode = keymap[i].keycode;
92 return 0;
93 }
94 117
95 /* 118 elem = ir_seek_table(rc_tab, scancode);
96 * If is there extra space, returns KEY_RESERVED, 119 if (elem >= 0) {
97 * otherwise, input core won't let ir_setkeycode 120 *keycode = rc_tab->scan[elem].keycode;
98 * to work 121 return 0;
99 */ 122 }
100 for (i = 0; i < rc_tab->size; i++)
101 if (keymap[i].keycode == KEY_RESERVED ||
102 keymap[i].keycode == KEY_UNKNOWN) {
103 *keycode = KEY_RESERVED;
104 return 0;
105 }
106 123
107 return -EINVAL; 124 return -EINVAL;
108} 125}
@@ -120,40 +137,33 @@ static int ir_getkeycode(struct input_dev *dev,
120static int ir_setkeycode(struct input_dev *dev, 137static int ir_setkeycode(struct input_dev *dev,
121 int scancode, int keycode) 138 int scancode, int keycode)
122{ 139{
123 int i; 140 int rc = 0;
124 struct ir_scancode_table *rc_tab = input_get_drvdata(dev); 141 struct ir_scancode_table *rc_tab = input_get_drvdata(dev);
125 struct ir_scancode *keymap = rc_tab->scan; 142 struct ir_scancode *keymap = rc_tab->scan;
143 unsigned long flags;
126 144
127 /* Search if it is replacing an existing keycode */ 145 /* Search if it is replacing an existing keycode */
128 for (i = 0; i < rc_tab->size; i++) 146 rc = ir_seek_table(rc_tab, scancode);
129 if (keymap[i].scancode == scancode) { 147 if (rc <0)
130 keymap[i].keycode = keycode; 148 return rc;
131 return 0;
132 }
133
134 /* Search if is there a clean entry. If so, use it */
135 for (i = 0; i < rc_tab->size; i++)
136 if (keymap[i].keycode == KEY_RESERVED ||
137 keymap[i].keycode == KEY_UNKNOWN) {
138 keymap[i].scancode = scancode;
139 keymap[i].keycode = keycode;
140 return 0;
141 }
142 149
143 /* 150 IR_dprintk(1, "#%d: Replacing scan 0x%04x with key 0x%04x\n",
144 * FIXME: Currently, it is not possible to increase the size of 151 rc, scancode, keycode);
145 * scancode table. For it to happen, one possibility
146 * would be to allocate a table with key_map_size + 1,
147 * copying data, appending the new key on it, and freeing
148 * the old one - or maybe just allocating some spare space
149 */
150 152
151 return -EINVAL; 153 clear_bit(keymap[rc].keycode, dev->keybit);
154
155 spin_lock_irqsave(&rc_tab->lock, flags);
156 keymap[rc].keycode = keycode;
157 spin_unlock_irqrestore(&rc_tab->lock, flags);
158
159 set_bit(keycode, dev->keybit);
160
161 return 0;
152} 162}
153 163
154/** 164/**
155 * ir_g_keycode_from_table() - gets the keycode that corresponds to a scancode 165 * ir_g_keycode_from_table() - gets the keycode that corresponds to a scancode
156 * @rc_tab: the ir_scancode_table with the keymap to be used 166 * @input_dev: the struct input_dev descriptor of the device
157 * @scancode: the scancode that we're seeking 167 * @scancode: the scancode that we're seeking
158 * 168 *
159 * This routine is used by the input routines when a key is pressed at the 169 * This routine is used by the input routines when a key is pressed at the
@@ -163,22 +173,23 @@ static int ir_setkeycode(struct input_dev *dev,
163 */ 173 */
164u32 ir_g_keycode_from_table(struct input_dev *dev, u32 scancode) 174u32 ir_g_keycode_from_table(struct input_dev *dev, u32 scancode)
165{ 175{
166 int i;
167 struct ir_scancode_table *rc_tab = input_get_drvdata(dev); 176 struct ir_scancode_table *rc_tab = input_get_drvdata(dev);
168 struct ir_scancode *keymap = rc_tab->scan; 177 struct ir_scancode *keymap = rc_tab->scan;
178 int elem;
169 179
170 for (i = 0; i < rc_tab->size; i++) 180 elem = ir_seek_table(rc_tab, scancode);
171 if (keymap[i].scancode == scancode) { 181 if (elem >= 0) {
172 IR_dprintk(1, "%s: scancode 0x%04x keycode 0x%02x\n", 182 IR_dprintk(1, "%s: scancode 0x%04x keycode 0x%02x\n",
173 dev->name, scancode, keymap[i].keycode); 183 dev->name, scancode, keymap[elem].keycode);
174 184
175 return keymap[i].keycode; 185 return rc_tab->scan[elem].keycode;
176 } 186 }
177 187
178 printk(KERN_INFO "%s: unknown key for scancode 0x%04x\n", 188 printk(KERN_INFO "%s: unknown key for scancode 0x%04x\n",
179 dev->name, scancode); 189 dev->name, scancode);
180 190
181 return KEY_UNKNOWN; 191 /* Reports userspace that an unknown keycode were got */
192 return KEY_RESERVED;
182} 193}
183 194
184/** 195/**
@@ -188,8 +199,8 @@ u32 ir_g_keycode_from_table(struct input_dev *dev, u32 scancode)
188 * @rc_tab: the struct ir_scancode_table table of scancode/keymap 199 * @rc_tab: the struct ir_scancode_table table of scancode/keymap
189 * 200 *
190 * This routine is used to initialize the input infrastructure to work with 201 * This routine is used to initialize the input infrastructure to work with
191 * an IR. It requires that the caller initializes the input_dev struct with 202 * an IR.
192 * some fields: name, 203 * It should be called before registering the IR device.
193 */ 204 */
194int ir_set_keycode_table(struct input_dev *input_dev, 205int ir_set_keycode_table(struct input_dev *input_dev,
195 struct ir_scancode_table *rc_tab) 206 struct ir_scancode_table *rc_tab)
@@ -197,6 +208,8 @@ int ir_set_keycode_table(struct input_dev *input_dev,
197 struct ir_scancode *keymap = rc_tab->scan; 208 struct ir_scancode *keymap = rc_tab->scan;
198 int i; 209 int i;
199 210
211 spin_lock_init(&rc_tab->lock);
212
200 if (rc_tab->scan == NULL || !rc_tab->size) 213 if (rc_tab->scan == NULL || !rc_tab->size)
201 return -EINVAL; 214 return -EINVAL;
202 215