diff options
author | David Härdeman <david@hardeman.nu> | 2010-04-02 14:58:28 -0400 |
---|---|---|
committer | Mauro Carvalho Chehab <mchehab@redhat.com> | 2010-05-19 11:56:53 -0400 |
commit | b3074c0a3f020766c05c1249a31212bff4a77bea (patch) | |
tree | 90fcff6ccd7746273810ebf7f22536855d31ff31 /drivers/media/IR/ir-keytable.c | |
parent | bdf1c98e4205832c07d6dedbfd0ec6eab83df358 (diff) |
V4L/DVB: drivers/media/IR - improve keytable code
The attached patch rewrites much of the keytable code in
drivers/media/IR/ir-keytable.c.
The scancodes are now inserted into the array in sorted
order which allows for a binary search on lookup.
The code has also been shrunk by about 150 lines.
In addition it fixes the following bugs:
Any use of ir_seek_table() was racy.
ir_dev->driver_name is leaked between ir_input_register() and
ir_input_unregister().
ir_setkeycode() unconditionally does clear_bit() on dev->keybit
when removing a mapping, but there might be another mapping with
a different scancode and the same keycode.
This version has been updated to incorporate patch feedback from
Mauro Carvalho Chehab.
[mchehab@redhat.com: Fix a conflict with RC keytable breakup patches and input changes]
Signed-off-by: David Härdeman <david@hardeman.nu>
Signed-off-by: Mauro Carvalho Chehab <mchehab@redhat.com>
Diffstat (limited to 'drivers/media/IR/ir-keytable.c')
-rw-r--r-- | drivers/media/IR/ir-keytable.c | 518 |
1 files changed, 188 insertions, 330 deletions
diff --git a/drivers/media/IR/ir-keytable.c b/drivers/media/IR/ir-keytable.c index 5d4ddc9f4cab..10e4be22dbfd 100644 --- a/drivers/media/IR/ir-keytable.c +++ b/drivers/media/IR/ir-keytable.c | |||
@@ -17,344 +17,214 @@ | |||
17 | #include <linux/slab.h> | 17 | #include <linux/slab.h> |
18 | #include <media/ir-common.h> | 18 | #include <media/ir-common.h> |
19 | 19 | ||
20 | #define IR_TAB_MIN_SIZE 32 | 20 | /* Sizes are in bytes, 256 bytes allows for 32 entries on x64 */ |
21 | #define IR_TAB_MAX_SIZE 1024 | 21 | #define IR_TAB_MIN_SIZE 256 |
22 | #define IR_TAB_MAX_SIZE 8192 | ||
22 | 23 | ||
23 | /** | 24 | /** |
24 | * ir_seek_table() - returns the element order on the table | 25 | * ir_resize_table() - resizes a scancode table if necessary |
25 | * @rc_tab: the ir_scancode_table with the keymap to be used | 26 | * @rc_tab: the ir_scancode_table to resize |
26 | * @scancode: the scancode that we're seeking | 27 | * @return: zero on success or a negative error code |
27 | * | 28 | * |
28 | * This routine is used by the input routines when a key is pressed at the | 29 | * This routine will shrink the ir_scancode_table if it has lots of |
29 | * IR. The scancode is received and needs to be converted into a keycode. | 30 | * unused entries and grow it if it is full. |
30 | * If the key is not found, it returns KEY_UNKNOWN. Otherwise, returns the | ||
31 | * corresponding keycode from the table. | ||
32 | */ | 31 | */ |
33 | static int ir_seek_table(struct ir_scancode_table *rc_tab, u32 scancode) | 32 | static int ir_resize_table(struct ir_scancode_table *rc_tab) |
34 | { | 33 | { |
35 | int rc; | 34 | unsigned int oldalloc = rc_tab->alloc; |
36 | unsigned long flags; | 35 | unsigned int newalloc = oldalloc; |
37 | struct ir_scancode *keymap = rc_tab->scan; | 36 | struct ir_scancode *oldscan = rc_tab->scan; |
37 | struct ir_scancode *newscan; | ||
38 | |||
39 | if (rc_tab->size == rc_tab->len) { | ||
40 | /* All entries in use -> grow keytable */ | ||
41 | if (rc_tab->alloc >= IR_TAB_MAX_SIZE) | ||
42 | return -ENOMEM; | ||
38 | 43 | ||
39 | spin_lock_irqsave(&rc_tab->lock, flags); | 44 | newalloc *= 2; |
45 | IR_dprintk(1, "Growing table to %u bytes\n", newalloc); | ||
46 | } | ||
40 | 47 | ||
41 | /* FIXME: replace it by a binary search */ | 48 | if ((rc_tab->len * 3 < rc_tab->size) && (oldalloc > IR_TAB_MIN_SIZE)) { |
49 | /* Less than 1/3 of entries in use -> shrink keytable */ | ||
50 | newalloc /= 2; | ||
51 | IR_dprintk(1, "Shrinking table to %u bytes\n", newalloc); | ||
52 | } | ||
42 | 53 | ||
43 | for (rc = 0; rc < rc_tab->size; rc++) | 54 | if (newalloc == oldalloc) |
44 | if (keymap[rc].scancode == scancode) | 55 | return 0; |
45 | goto exit; | ||
46 | 56 | ||
47 | /* Not found */ | 57 | newscan = kmalloc(newalloc, GFP_ATOMIC); |
48 | rc = -EINVAL; | 58 | if (!newscan) { |
59 | IR_dprintk(1, "Failed to kmalloc %u bytes\n", newalloc); | ||
60 | return -ENOMEM; | ||
61 | } | ||
49 | 62 | ||
50 | exit: | 63 | memcpy(newscan, rc_tab->scan, rc_tab->len * sizeof(struct ir_scancode)); |
51 | spin_unlock_irqrestore(&rc_tab->lock, flags); | 64 | rc_tab->scan = newscan; |
52 | return rc; | 65 | rc_tab->alloc = newalloc; |
66 | rc_tab->size = rc_tab->alloc / sizeof(struct ir_scancode); | ||
67 | kfree(oldscan); | ||
68 | return 0; | ||
53 | } | 69 | } |
54 | 70 | ||
55 | /** | 71 | /** |
56 | * ir_roundup_tablesize() - gets an optimum value for the table size | 72 | * ir_do_setkeycode() - internal function to set a keycode in the |
57 | * @n_elems: minimum number of entries to store keycodes | 73 | * scancode->keycode table |
58 | * | 74 | * @dev: the struct input_dev device descriptor |
59 | * This routine is used to choose the keycode table size. | 75 | * @rc_tab: the struct ir_scancode_table to set the keycode in |
76 | * @scancode: the scancode for the ir command | ||
77 | * @keycode: the keycode for the ir command | ||
78 | * @return: -EINVAL if the keycode could not be inserted, otherwise zero. | ||
60 | * | 79 | * |
61 | * In order to have some empty space for new keycodes, | 80 | * This routine is used internally to manipulate the scancode->keycode table. |
62 | * and knowing in advance that kmalloc allocates only power of two | 81 | * The caller has to hold @rc_tab->lock. |
63 | * segments, it optimizes the allocated space to have some spare space | ||
64 | * for those new keycodes by using the maximum number of entries that | ||
65 | * will be effectively be allocated by kmalloc. | ||
66 | * In order to reduce the quantity of table resizes, it has a minimum | ||
67 | * table size of IR_TAB_MIN_SIZE. | ||
68 | */ | 82 | */ |
69 | static int ir_roundup_tablesize(int n_elems) | 83 | static int ir_do_setkeycode(struct input_dev *dev, |
84 | struct ir_scancode_table *rc_tab, | ||
85 | unsigned scancode, unsigned keycode) | ||
70 | { | 86 | { |
71 | size_t size; | 87 | unsigned int i; |
88 | int old_keycode = KEY_RESERVED; | ||
89 | |||
90 | /* First check if we already have a mapping for this ir command */ | ||
91 | for (i = 0; i < rc_tab->len; i++) { | ||
92 | /* Keytable is sorted from lowest to highest scancode */ | ||
93 | if (rc_tab->scan[i].scancode > scancode) | ||
94 | break; | ||
95 | else if (rc_tab->scan[i].scancode < scancode) | ||
96 | continue; | ||
72 | 97 | ||
73 | if (n_elems < IR_TAB_MIN_SIZE) | 98 | old_keycode = rc_tab->scan[i].keycode; |
74 | n_elems = IR_TAB_MIN_SIZE; | 99 | rc_tab->scan[i].keycode = keycode; |
75 | 100 | ||
76 | /* | 101 | /* Did the user wish to remove the mapping? */ |
77 | * As kmalloc only allocates sizes of power of two, get as | 102 | if (keycode == KEY_RESERVED || keycode == KEY_UNKNOWN) { |
78 | * much entries as possible for the allocated memory segment | 103 | rc_tab->len--; |
79 | */ | 104 | memmove(&rc_tab->scan[i], &rc_tab->scan[i + 1], |
80 | size = roundup_pow_of_two(n_elems * sizeof(struct ir_scancode)); | 105 | (rc_tab->len - i) * sizeof(struct ir_scancode)); |
81 | n_elems = size / sizeof(struct ir_scancode); | 106 | } |
82 | 107 | ||
83 | return n_elems; | 108 | /* Possibly shrink the keytable, failure is not a problem */ |
84 | } | 109 | ir_resize_table(rc_tab); |
85 | 110 | break; | |
86 | /** | 111 | } |
87 | * ir_copy_table() - copies a keytable, discarding the unused entries | ||
88 | * @destin: destin table | ||
89 | * @origin: origin table | ||
90 | * | ||
91 | * Copies all entries where the keycode is not KEY_UNKNOWN/KEY_RESERVED | ||
92 | * Also copies table size and table protocol. | ||
93 | * NOTE: It shouldn't copy the lock field | ||
94 | */ | ||
95 | |||
96 | static int ir_copy_table(struct ir_scancode_table *destin, | ||
97 | const struct ir_scancode_table *origin) | ||
98 | { | ||
99 | int i, j = 0; | ||
100 | 112 | ||
101 | for (i = 0; i < origin->size; i++) { | 113 | if (old_keycode == KEY_RESERVED) { |
102 | if (origin->scan[i].keycode == KEY_UNKNOWN || | 114 | /* No previous mapping found, we might need to grow the table */ |
103 | origin->scan[i].keycode == KEY_RESERVED) | 115 | if (ir_resize_table(rc_tab)) |
104 | continue; | 116 | return -ENOMEM; |
105 | 117 | ||
106 | memcpy(&destin->scan[j], &origin->scan[i], sizeof(struct ir_scancode)); | 118 | /* i is the proper index to insert our new keycode */ |
107 | j++; | 119 | memmove(&rc_tab->scan[i + 1], &rc_tab->scan[i], |
120 | (rc_tab->len - i) * sizeof(struct ir_scancode)); | ||
121 | rc_tab->scan[i].scancode = scancode; | ||
122 | rc_tab->scan[i].keycode = keycode; | ||
123 | rc_tab->len++; | ||
124 | set_bit(keycode, dev->keybit); | ||
125 | } else { | ||
126 | /* A previous mapping was updated... */ | ||
127 | clear_bit(old_keycode, dev->keybit); | ||
128 | /* ...but another scancode might use the same keycode */ | ||
129 | for (i = 0; i < rc_tab->len; i++) { | ||
130 | if (rc_tab->scan[i].keycode == old_keycode) { | ||
131 | set_bit(old_keycode, dev->keybit); | ||
132 | break; | ||
133 | } | ||
134 | } | ||
108 | } | 135 | } |
109 | destin->size = j; | ||
110 | destin->ir_type = origin->ir_type; | ||
111 | |||
112 | IR_dprintk(1, "Copied %d scancodes to the new keycode table\n", destin->size); | ||
113 | 136 | ||
114 | return 0; | 137 | return 0; |
115 | } | 138 | } |
116 | 139 | ||
117 | /** | 140 | /** |
118 | * ir_getkeycode() - get a keycode at the evdev scancode ->keycode table | 141 | * ir_setkeycode() - set a keycode in the scancode->keycode table |
119 | * @dev: the struct input_dev device descriptor | 142 | * @dev: the struct input_dev device descriptor |
120 | * @scancode: the desired scancode | 143 | * @scancode: the desired scancode |
121 | * @keycode: the keycode to be retorned. | 144 | * @keycode: result |
145 | * @return: -EINVAL if the keycode could not be inserted, otherwise zero. | ||
122 | * | 146 | * |
123 | * This routine is used to handle evdev EVIOCGKEY ioctl. | 147 | * This routine is used to handle evdev EVIOCSKEY ioctl. |
124 | * If the key is not found, returns -EINVAL, otherwise, returns 0. | ||
125 | */ | 148 | */ |
126 | static int ir_getkeycode(struct input_dev *dev, | 149 | static int ir_setkeycode(struct input_dev *dev, |
127 | unsigned int scancode, unsigned int *keycode) | 150 | unsigned int scancode, unsigned int keycode) |
128 | { | 151 | { |
129 | int elem; | 152 | int rc; |
153 | unsigned long flags; | ||
130 | struct ir_input_dev *ir_dev = input_get_drvdata(dev); | 154 | struct ir_input_dev *ir_dev = input_get_drvdata(dev); |
131 | struct ir_scancode_table *rc_tab = &ir_dev->rc_tab; | 155 | struct ir_scancode_table *rc_tab = &ir_dev->rc_tab; |
132 | 156 | ||
133 | elem = ir_seek_table(rc_tab, scancode); | 157 | spin_lock_irqsave(&rc_tab->lock, flags); |
134 | if (elem >= 0) { | 158 | rc = ir_do_setkeycode(dev, rc_tab, scancode, keycode); |
135 | *keycode = rc_tab->scan[elem].keycode; | 159 | spin_unlock_irqrestore(&rc_tab->lock, flags); |
136 | return 0; | 160 | return rc; |
137 | } | ||
138 | |||
139 | /* | ||
140 | * Scancode not found and table can't be expanded | ||
141 | */ | ||
142 | if (elem < 0 && rc_tab->size == IR_TAB_MAX_SIZE) | ||
143 | return -EINVAL; | ||
144 | |||
145 | /* | ||
146 | * If is there extra space, returns KEY_RESERVED, | ||
147 | * otherwise, input core won't let ir_setkeycode to work | ||
148 | */ | ||
149 | *keycode = KEY_RESERVED; | ||
150 | return 0; | ||
151 | } | ||
152 | |||
153 | /** | ||
154 | * ir_is_resize_needed() - Check if the table needs rezise | ||
155 | * @table: keycode table that may need to resize | ||
156 | * @n_elems: minimum number of entries to store keycodes | ||
157 | * | ||
158 | * Considering that kmalloc uses power of two storage areas, this | ||
159 | * routine detects if the real alloced size will change. If not, it | ||
160 | * just returns without doing nothing. Otherwise, it will extend or | ||
161 | * reduce the table size to meet the new needs. | ||
162 | * | ||
163 | * It returns 0 if no resize is needed, 1 otherwise. | ||
164 | */ | ||
165 | static int ir_is_resize_needed(struct ir_scancode_table *table, int n_elems) | ||
166 | { | ||
167 | int cur_size = ir_roundup_tablesize(table->size); | ||
168 | int new_size = ir_roundup_tablesize(n_elems); | ||
169 | |||
170 | if (cur_size == new_size) | ||
171 | return 0; | ||
172 | |||
173 | /* Resize is needed */ | ||
174 | return 1; | ||
175 | } | ||
176 | |||
177 | /** | ||
178 | * ir_delete_key() - remove a keycode from the table | ||
179 | * @rc_tab: keycode table | ||
180 | * @elem: element to be removed | ||
181 | * | ||
182 | */ | ||
183 | static void ir_delete_key(struct ir_scancode_table *rc_tab, int elem) | ||
184 | { | ||
185 | unsigned long flags = 0; | ||
186 | int newsize = rc_tab->size - 1; | ||
187 | int resize = ir_is_resize_needed(rc_tab, newsize); | ||
188 | struct ir_scancode *oldkeymap = rc_tab->scan; | ||
189 | struct ir_scancode *newkeymap = NULL; | ||
190 | |||
191 | if (resize) | ||
192 | newkeymap = kzalloc(ir_roundup_tablesize(newsize) * | ||
193 | sizeof(*newkeymap), GFP_ATOMIC); | ||
194 | |||
195 | /* There's no memory for resize. Keep the old table */ | ||
196 | if (!resize || !newkeymap) { | ||
197 | newkeymap = oldkeymap; | ||
198 | |||
199 | /* We'll modify the live table. Lock it */ | ||
200 | spin_lock_irqsave(&rc_tab->lock, flags); | ||
201 | } | ||
202 | |||
203 | /* | ||
204 | * Copy the elements before the one that will be deleted | ||
205 | * if (!resize), both oldkeymap and newkeymap points | ||
206 | * to the same place, so, there's no need to copy | ||
207 | */ | ||
208 | if (resize && elem > 0) | ||
209 | memcpy(newkeymap, oldkeymap, | ||
210 | elem * sizeof(*newkeymap)); | ||
211 | |||
212 | /* | ||
213 | * Copy the other elements overwriting the element to be removed | ||
214 | * This operation applies to both resize and non-resize case | ||
215 | */ | ||
216 | if (elem < newsize) | ||
217 | memcpy(&newkeymap[elem], &oldkeymap[elem + 1], | ||
218 | (newsize - elem) * sizeof(*newkeymap)); | ||
219 | |||
220 | if (resize) { | ||
221 | /* | ||
222 | * As the copy happened to a temporary table, only here | ||
223 | * it needs to lock while replacing the table pointers | ||
224 | * to use the new table | ||
225 | */ | ||
226 | spin_lock_irqsave(&rc_tab->lock, flags); | ||
227 | rc_tab->size = newsize; | ||
228 | rc_tab->scan = newkeymap; | ||
229 | spin_unlock_irqrestore(&rc_tab->lock, flags); | ||
230 | |||
231 | /* Frees the old keytable */ | ||
232 | kfree(oldkeymap); | ||
233 | } else { | ||
234 | rc_tab->size = newsize; | ||
235 | spin_unlock_irqrestore(&rc_tab->lock, flags); | ||
236 | } | ||
237 | } | 161 | } |
238 | 162 | ||
239 | /** | 163 | /** |
240 | * ir_insert_key() - insert a keycode at the table | 164 | * ir_setkeytable() - sets several entries in the scancode->keycode table |
241 | * @rc_tab: keycode table | 165 | * @dev: the struct input_dev device descriptor |
242 | * @scancode: the desired scancode | 166 | * @to: the struct ir_scancode_table to copy entries to |
243 | * @keycode: the keycode to be retorned. | 167 | * @from: the struct ir_scancode_table to copy entries from |
168 | * @return: -EINVAL if all keycodes could not be inserted, otherwise zero. | ||
244 | * | 169 | * |
170 | * This routine is used to handle table initialization. | ||
245 | */ | 171 | */ |
246 | static int ir_insert_key(struct ir_scancode_table *rc_tab, | 172 | static int ir_setkeytable(struct input_dev *dev, |
247 | int scancode, int keycode) | 173 | struct ir_scancode_table *to, |
174 | const struct ir_scancode_table *from) | ||
248 | { | 175 | { |
176 | struct ir_input_dev *ir_dev = input_get_drvdata(dev); | ||
177 | struct ir_scancode_table *rc_tab = &ir_dev->rc_tab; | ||
249 | unsigned long flags; | 178 | unsigned long flags; |
250 | int elem = rc_tab->size; | 179 | unsigned int i; |
251 | int newsize = rc_tab->size + 1; | 180 | int rc = 0; |
252 | int resize = ir_is_resize_needed(rc_tab, newsize); | ||
253 | struct ir_scancode *oldkeymap = rc_tab->scan; | ||
254 | struct ir_scancode *newkeymap; | ||
255 | |||
256 | if (resize) { | ||
257 | newkeymap = kzalloc(ir_roundup_tablesize(newsize) * | ||
258 | sizeof(*newkeymap), GFP_ATOMIC); | ||
259 | if (!newkeymap) | ||
260 | return -ENOMEM; | ||
261 | |||
262 | memcpy(newkeymap, oldkeymap, | ||
263 | rc_tab->size * sizeof(*newkeymap)); | ||
264 | } else | ||
265 | newkeymap = oldkeymap; | ||
266 | |||
267 | /* Stores the new code at the table */ | ||
268 | IR_dprintk(1, "#%d: New scan 0x%04x with key 0x%04x\n", | ||
269 | rc_tab->size, scancode, keycode); | ||
270 | 181 | ||
271 | spin_lock_irqsave(&rc_tab->lock, flags); | 182 | spin_lock_irqsave(&rc_tab->lock, flags); |
272 | rc_tab->size = newsize; | 183 | for (i = 0; i < from->size; i++) { |
273 | if (resize) { | 184 | rc = ir_do_setkeycode(dev, to, from->scan[i].scancode, |
274 | rc_tab->scan = newkeymap; | 185 | from->scan[i].keycode); |
275 | kfree(oldkeymap); | 186 | if (rc) |
187 | break; | ||
276 | } | 188 | } |
277 | newkeymap[elem].scancode = scancode; | ||
278 | newkeymap[elem].keycode = keycode; | ||
279 | spin_unlock_irqrestore(&rc_tab->lock, flags); | 189 | spin_unlock_irqrestore(&rc_tab->lock, flags); |
280 | 190 | return rc; | |
281 | return 0; | ||
282 | } | 191 | } |
283 | 192 | ||
284 | /** | 193 | /** |
285 | * ir_setkeycode() - set a keycode at the evdev scancode ->keycode table | 194 | * ir_getkeycode() - get a keycode from the scancode->keycode table |
286 | * @dev: the struct input_dev device descriptor | 195 | * @dev: the struct input_dev device descriptor |
287 | * @scancode: the desired scancode | 196 | * @scancode: the desired scancode |
288 | * @keycode: the keycode to be retorned. | 197 | * @keycode: used to return the keycode, if found, or KEY_RESERVED |
198 | * @return: always returns zero. | ||
289 | * | 199 | * |
290 | * This routine is used to handle evdev EVIOCSKEY ioctl. | 200 | * This routine is used to handle evdev EVIOCGKEY ioctl. |
291 | * There's one caveat here: how can we increase the size of the table? | ||
292 | * If the key is not found, returns -EINVAL, otherwise, returns 0. | ||
293 | */ | 201 | */ |
294 | static int ir_setkeycode(struct input_dev *dev, | 202 | static int ir_getkeycode(struct input_dev *dev, |
295 | unsigned int scancode, unsigned int keycode) | 203 | unsigned int scancode, unsigned int *keycode) |
296 | { | 204 | { |
297 | int rc = 0; | 205 | int start, end, mid; |
206 | unsigned long flags; | ||
207 | int key = KEY_RESERVED; | ||
298 | struct ir_input_dev *ir_dev = input_get_drvdata(dev); | 208 | struct ir_input_dev *ir_dev = input_get_drvdata(dev); |
299 | struct ir_scancode_table *rc_tab = &ir_dev->rc_tab; | 209 | struct ir_scancode_table *rc_tab = &ir_dev->rc_tab; |
300 | struct ir_scancode *keymap = rc_tab->scan; | ||
301 | unsigned long flags; | ||
302 | |||
303 | /* | ||
304 | * Handle keycode table deletions | ||
305 | * | ||
306 | * If userspace is adding a KEY_UNKNOWN or KEY_RESERVED, | ||
307 | * deal as a trial to remove an existing scancode attribution | ||
308 | * if table become too big, reduce it to save space | ||
309 | */ | ||
310 | if (keycode == KEY_UNKNOWN || keycode == KEY_RESERVED) { | ||
311 | rc = ir_seek_table(rc_tab, scancode); | ||
312 | if (rc < 0) | ||
313 | return 0; | ||
314 | |||
315 | IR_dprintk(1, "#%d: Deleting scan 0x%04x\n", rc, scancode); | ||
316 | clear_bit(keymap[rc].keycode, dev->keybit); | ||
317 | ir_delete_key(rc_tab, rc); | ||
318 | |||
319 | return 0; | ||
320 | } | ||
321 | |||
322 | /* | ||
323 | * Handle keycode replacements | ||
324 | * | ||
325 | * If the scancode exists, just replace by the new value | ||
326 | */ | ||
327 | rc = ir_seek_table(rc_tab, scancode); | ||
328 | if (rc >= 0) { | ||
329 | IR_dprintk(1, "#%d: Replacing scan 0x%04x with key 0x%04x\n", | ||
330 | rc, scancode, keycode); | ||
331 | 210 | ||
332 | clear_bit(keymap[rc].keycode, dev->keybit); | 211 | spin_lock_irqsave(&rc_tab->lock, flags); |
333 | 212 | start = 0; | |
334 | spin_lock_irqsave(&rc_tab->lock, flags); | 213 | end = rc_tab->len - 1; |
335 | keymap[rc].keycode = keycode; | 214 | while (start <= end) { |
336 | spin_unlock_irqrestore(&rc_tab->lock, flags); | 215 | mid = (start + end) / 2; |
337 | 216 | if (rc_tab->scan[mid].scancode < scancode) | |
338 | set_bit(keycode, dev->keybit); | 217 | start = mid + 1; |
339 | 218 | else if (rc_tab->scan[mid].scancode > scancode) | |
340 | return 0; | 219 | end = mid - 1; |
220 | else { | ||
221 | key = rc_tab->scan[mid].keycode; | ||
222 | break; | ||
223 | } | ||
341 | } | 224 | } |
225 | spin_unlock_irqrestore(&rc_tab->lock, flags); | ||
342 | 226 | ||
343 | /* | 227 | *keycode = key; |
344 | * Handle new scancode inserts | ||
345 | * | ||
346 | * reallocate table if needed and insert a new keycode | ||
347 | */ | ||
348 | |||
349 | /* Avoid growing the table indefinitely */ | ||
350 | if (rc_tab->size + 1 > IR_TAB_MAX_SIZE) | ||
351 | return -EINVAL; | ||
352 | |||
353 | rc = ir_insert_key(rc_tab, scancode, keycode); | ||
354 | if (rc < 0) | ||
355 | return rc; | ||
356 | set_bit(keycode, dev->keybit); | ||
357 | |||
358 | return 0; | 228 | return 0; |
359 | } | 229 | } |
360 | 230 | ||
@@ -370,24 +240,12 @@ static int ir_setkeycode(struct input_dev *dev, | |||
370 | */ | 240 | */ |
371 | u32 ir_g_keycode_from_table(struct input_dev *dev, u32 scancode) | 241 | u32 ir_g_keycode_from_table(struct input_dev *dev, u32 scancode) |
372 | { | 242 | { |
373 | struct ir_input_dev *ir_dev = input_get_drvdata(dev); | 243 | int keycode; |
374 | struct ir_scancode_table *rc_tab = &ir_dev->rc_tab; | ||
375 | struct ir_scancode *keymap = rc_tab->scan; | ||
376 | int elem; | ||
377 | |||
378 | elem = ir_seek_table(rc_tab, scancode); | ||
379 | if (elem >= 0) { | ||
380 | IR_dprintk(1, "%s: scancode 0x%04x keycode 0x%02x\n", | ||
381 | dev->name, scancode, keymap[elem].keycode); | ||
382 | |||
383 | return rc_tab->scan[elem].keycode; | ||
384 | } | ||
385 | 244 | ||
386 | printk(KERN_INFO "%s: unknown key for scancode 0x%04x\n", | 245 | ir_getkeycode(dev, scancode, &keycode); |
387 | dev->name, scancode); | 246 | IR_dprintk(1, "%s: scancode 0x%04x keycode 0x%02x\n", |
388 | 247 | dev->name, scancode, keycode); | |
389 | /* Reports userspace that an unknown keycode were got */ | 248 | return keycode; |
390 | return KEY_RESERVED; | ||
391 | } | 249 | } |
392 | EXPORT_SYMBOL_GPL(ir_g_keycode_from_table); | 250 | EXPORT_SYMBOL_GPL(ir_g_keycode_from_table); |
393 | 251 | ||
@@ -477,8 +335,7 @@ int __ir_input_register(struct input_dev *input_dev, | |||
477 | const char *driver_name) | 335 | const char *driver_name) |
478 | { | 336 | { |
479 | struct ir_input_dev *ir_dev; | 337 | struct ir_input_dev *ir_dev; |
480 | struct ir_scancode *keymap = rc_tab->scan; | 338 | int rc; |
481 | int i, rc; | ||
482 | 339 | ||
483 | if (rc_tab->scan == NULL || !rc_tab->size) | 340 | if (rc_tab->scan == NULL || !rc_tab->size) |
484 | return -EINVAL; | 341 | return -EINVAL; |
@@ -487,55 +344,55 @@ int __ir_input_register(struct input_dev *input_dev, | |||
487 | if (!ir_dev) | 344 | if (!ir_dev) |
488 | return -ENOMEM; | 345 | return -ENOMEM; |
489 | 346 | ||
490 | spin_lock_init(&ir_dev->rc_tab.lock); | 347 | ir_dev->driver_name = kasprintf(GFP_KERNEL, "%s", driver_name); |
348 | if (!ir_dev->driver_name) { | ||
349 | rc = -ENOMEM; | ||
350 | goto out_dev; | ||
351 | } | ||
491 | 352 | ||
492 | ir_dev->driver_name = kmalloc(strlen(driver_name) + 1, GFP_KERNEL); | 353 | input_dev->getkeycode = ir_getkeycode; |
493 | if (!ir_dev->driver_name) | 354 | input_dev->setkeycode = ir_setkeycode; |
494 | return -ENOMEM; | 355 | input_set_drvdata(input_dev, ir_dev); |
495 | strcpy(ir_dev->driver_name, driver_name); | 356 | |
357 | spin_lock_init(&ir_dev->rc_tab.lock); | ||
496 | ir_dev->rc_tab.name = rc_tab->name; | 358 | ir_dev->rc_tab.name = rc_tab->name; |
497 | ir_dev->rc_tab.size = ir_roundup_tablesize(rc_tab->size); | 359 | ir_dev->rc_tab.ir_type = rc_tab->ir_type; |
498 | ir_dev->rc_tab.scan = kzalloc(ir_dev->rc_tab.size * | 360 | ir_dev->rc_tab.alloc = roundup_pow_of_two(rc_tab->size * |
499 | sizeof(struct ir_scancode), GFP_KERNEL); | 361 | sizeof(struct ir_scancode)); |
362 | ir_dev->rc_tab.scan = kmalloc(ir_dev->rc_tab.alloc, GFP_KERNEL); | ||
363 | ir_dev->rc_tab.size = ir_dev->rc_tab.alloc / sizeof(struct ir_scancode); | ||
364 | |||
500 | if (!ir_dev->rc_tab.scan) { | 365 | if (!ir_dev->rc_tab.scan) { |
501 | kfree(ir_dev); | 366 | rc = -ENOMEM; |
502 | return -ENOMEM; | 367 | goto out_name; |
503 | } | 368 | } |
504 | 369 | ||
505 | IR_dprintk(1, "Allocated space for %d keycode entries (%zd bytes)\n", | 370 | IR_dprintk(1, "Allocated space for %u keycode entries (%u bytes)\n", |
506 | ir_dev->rc_tab.size, | 371 | ir_dev->rc_tab.size, ir_dev->rc_tab.alloc); |
507 | ir_dev->rc_tab.size * sizeof(ir_dev->rc_tab.scan)); | 372 | |
373 | set_bit(EV_KEY, input_dev->evbit); | ||
374 | if (ir_setkeytable(input_dev, &ir_dev->rc_tab, rc_tab)) { | ||
375 | rc = -ENOMEM; | ||
376 | goto out_table; | ||
377 | } | ||
508 | 378 | ||
509 | ir_copy_table(&ir_dev->rc_tab, rc_tab); | ||
510 | ir_dev->props = props; | 379 | ir_dev->props = props; |
511 | if (props && props->open) | 380 | if (props && props->open) |
512 | input_dev->open = ir_open; | 381 | input_dev->open = ir_open; |
513 | if (props && props->close) | 382 | if (props && props->close) |
514 | input_dev->close = ir_close; | 383 | input_dev->close = ir_close; |
515 | 384 | ||
516 | /* set the bits for the keys */ | ||
517 | IR_dprintk(1, "key map size: %d\n", rc_tab->size); | ||
518 | for (i = 0; i < rc_tab->size; i++) { | ||
519 | IR_dprintk(1, "#%d: setting bit for keycode 0x%04x\n", | ||
520 | i, keymap[i].keycode); | ||
521 | set_bit(keymap[i].keycode, input_dev->keybit); | ||
522 | } | ||
523 | clear_bit(0, input_dev->keybit); | ||
524 | |||
525 | set_bit(EV_KEY, input_dev->evbit); | ||
526 | |||
527 | input_dev->getkeycode = ir_getkeycode; | ||
528 | input_dev->setkeycode = ir_setkeycode; | ||
529 | input_set_drvdata(input_dev, ir_dev); | ||
530 | |||
531 | rc = ir_register_class(input_dev); | 385 | rc = ir_register_class(input_dev); |
532 | if (rc < 0) | 386 | if (rc < 0) |
533 | goto err; | 387 | goto out_table; |
534 | 388 | ||
535 | return 0; | 389 | return 0; |
536 | 390 | ||
537 | err: | 391 | out_table: |
538 | kfree(rc_tab->scan); | 392 | kfree(ir_dev->rc_tab.scan); |
393 | out_name: | ||
394 | kfree(ir_dev->driver_name); | ||
395 | out_dev: | ||
539 | kfree(ir_dev); | 396 | kfree(ir_dev); |
540 | return rc; | 397 | return rc; |
541 | } | 398 | } |
@@ -564,6 +421,7 @@ void ir_input_unregister(struct input_dev *dev) | |||
564 | 421 | ||
565 | ir_unregister_class(dev); | 422 | ir_unregister_class(dev); |
566 | 423 | ||
424 | kfree(ir_dev->driver_name); | ||
567 | kfree(ir_dev); | 425 | kfree(ir_dev); |
568 | } | 426 | } |
569 | EXPORT_SYMBOL_GPL(ir_input_unregister); | 427 | EXPORT_SYMBOL_GPL(ir_input_unregister); |