Commit | Line | Data |
---|---|---|
ef53a115 MCC |
1 | /* ir-register.c - handle IR scancode->keycode tables |
2 | * | |
3 | * Copyright (C) 2009 by Mauro Carvalho Chehab <mchehab@redhat.com> | |
446e4a64 MCC |
4 | * |
5 | * This program is free software; you can redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License as published by | |
7 | * the Free Software Foundation version 2 of the License. | |
8 | * | |
9 | * This program is distributed in the hope that it will be useful, | |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | * GNU General Public License for more details. | |
ef53a115 MCC |
13 | */ |
14 | ||
ef53a115 | 15 | |
882ead32 | 16 | #include <linux/input.h> |
5a0e3ad6 | 17 | #include <linux/slab.h> |
ef53a115 MCC |
18 | #include <media/ir-common.h> |
19 | ||
b3074c0a DH |
20 | /* Sizes are in bytes, 256 bytes allows for 32 entries on x64 */ |
21 | #define IR_TAB_MIN_SIZE 256 | |
22 | #define IR_TAB_MAX_SIZE 8192 | |
f6fc5049 | 23 | |
7fee03e4 | 24 | /** |
b3074c0a DH |
25 | * ir_resize_table() - resizes a scancode table if necessary |
26 | * @rc_tab: the ir_scancode_table to resize | |
27 | * @return: zero on success or a negative error code | |
7fee03e4 | 28 | * |
b3074c0a DH |
29 | * This routine will shrink the ir_scancode_table if it has lots of |
30 | * unused entries and grow it if it is full. | |
7fee03e4 | 31 | */ |
b3074c0a | 32 | static int ir_resize_table(struct ir_scancode_table *rc_tab) |
7fee03e4 | 33 | { |
b3074c0a DH |
34 | unsigned int oldalloc = rc_tab->alloc; |
35 | unsigned int newalloc = oldalloc; | |
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; | |
7fee03e4 | 43 | |
b3074c0a DH |
44 | newalloc *= 2; |
45 | IR_dprintk(1, "Growing table to %u bytes\n", newalloc); | |
46 | } | |
7fee03e4 | 47 | |
b3074c0a DH |
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 | } | |
7fee03e4 | 53 | |
b3074c0a DH |
54 | if (newalloc == oldalloc) |
55 | return 0; | |
7fee03e4 | 56 | |
b3074c0a DH |
57 | newscan = kmalloc(newalloc, GFP_ATOMIC); |
58 | if (!newscan) { | |
59 | IR_dprintk(1, "Failed to kmalloc %u bytes\n", newalloc); | |
60 | return -ENOMEM; | |
61 | } | |
7fee03e4 | 62 | |
b3074c0a DH |
63 | memcpy(newscan, rc_tab->scan, rc_tab->len * sizeof(struct ir_scancode)); |
64 | rc_tab->scan = newscan; | |
65 | rc_tab->alloc = newalloc; | |
66 | rc_tab->size = rc_tab->alloc / sizeof(struct ir_scancode); | |
67 | kfree(oldscan); | |
68 | return 0; | |
7fee03e4 MCC |
69 | } |
70 | ||
f6fc5049 | 71 | /** |
b3074c0a DH |
72 | * ir_do_setkeycode() - internal function to set a keycode in the |
73 | * scancode->keycode table | |
74 | * @dev: the struct input_dev device descriptor | |
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. | |
f6fc5049 | 79 | * |
b3074c0a DH |
80 | * This routine is used internally to manipulate the scancode->keycode table. |
81 | * The caller has to hold @rc_tab->lock. | |
f6fc5049 | 82 | */ |
b3074c0a DH |
83 | static int ir_do_setkeycode(struct input_dev *dev, |
84 | struct ir_scancode_table *rc_tab, | |
85 | unsigned scancode, unsigned keycode) | |
f6fc5049 | 86 | { |
b3074c0a DH |
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; | |
f6fc5049 | 97 | |
b3074c0a DH |
98 | old_keycode = rc_tab->scan[i].keycode; |
99 | rc_tab->scan[i].keycode = keycode; | |
f6fc5049 | 100 | |
b3074c0a DH |
101 | /* Did the user wish to remove the mapping? */ |
102 | if (keycode == KEY_RESERVED || keycode == KEY_UNKNOWN) { | |
35438946 MCC |
103 | IR_dprintk(1, "#%d: Deleting scan 0x%04x\n", |
104 | i, scancode); | |
b3074c0a DH |
105 | rc_tab->len--; |
106 | memmove(&rc_tab->scan[i], &rc_tab->scan[i + 1], | |
107 | (rc_tab->len - i) * sizeof(struct ir_scancode)); | |
108 | } | |
f6fc5049 | 109 | |
b3074c0a DH |
110 | /* Possibly shrink the keytable, failure is not a problem */ |
111 | ir_resize_table(rc_tab); | |
112 | break; | |
113 | } | |
f6fc5049 | 114 | |
b3074c0a DH |
115 | if (old_keycode == KEY_RESERVED) { |
116 | /* No previous mapping found, we might need to grow the table */ | |
117 | if (ir_resize_table(rc_tab)) | |
118 | return -ENOMEM; | |
7fee03e4 | 119 | |
35438946 MCC |
120 | IR_dprintk(1, "#%d: New scan 0x%04x with key 0x%04x\n", |
121 | i, scancode, keycode); | |
122 | ||
b3074c0a DH |
123 | /* i is the proper index to insert our new keycode */ |
124 | memmove(&rc_tab->scan[i + 1], &rc_tab->scan[i], | |
125 | (rc_tab->len - i) * sizeof(struct ir_scancode)); | |
126 | rc_tab->scan[i].scancode = scancode; | |
127 | rc_tab->scan[i].keycode = keycode; | |
128 | rc_tab->len++; | |
129 | set_bit(keycode, dev->keybit); | |
130 | } else { | |
35438946 MCC |
131 | IR_dprintk(1, "#%d: Replacing scan 0x%04x with key 0x%04x\n", |
132 | i, scancode, keycode); | |
b3074c0a DH |
133 | /* A previous mapping was updated... */ |
134 | clear_bit(old_keycode, dev->keybit); | |
135 | /* ...but another scancode might use the same keycode */ | |
136 | for (i = 0; i < rc_tab->len; i++) { | |
137 | if (rc_tab->scan[i].keycode == old_keycode) { | |
138 | set_bit(old_keycode, dev->keybit); | |
139 | break; | |
140 | } | |
141 | } | |
f6fc5049 | 142 | } |
f6fc5049 MCC |
143 | |
144 | return 0; | |
145 | } | |
146 | ||
ef53a115 | 147 | /** |
b3074c0a | 148 | * ir_setkeycode() - set a keycode in the scancode->keycode table |
ef53a115 MCC |
149 | * @dev: the struct input_dev device descriptor |
150 | * @scancode: the desired scancode | |
b3074c0a DH |
151 | * @keycode: result |
152 | * @return: -EINVAL if the keycode could not be inserted, otherwise zero. | |
ef53a115 | 153 | * |
b3074c0a | 154 | * This routine is used to handle evdev EVIOCSKEY ioctl. |
ef53a115 | 155 | */ |
b3074c0a DH |
156 | static int ir_setkeycode(struct input_dev *dev, |
157 | unsigned int scancode, unsigned int keycode) | |
ef53a115 | 158 | { |
b3074c0a DH |
159 | int rc; |
160 | unsigned long flags; | |
75543cce MCC |
161 | struct ir_input_dev *ir_dev = input_get_drvdata(dev); |
162 | struct ir_scancode_table *rc_tab = &ir_dev->rc_tab; | |
ef53a115 | 163 | |
b3074c0a DH |
164 | spin_lock_irqsave(&rc_tab->lock, flags); |
165 | rc = ir_do_setkeycode(dev, rc_tab, scancode, keycode); | |
166 | spin_unlock_irqrestore(&rc_tab->lock, flags); | |
167 | return rc; | |
e97f4677 MCC |
168 | } |
169 | ||
170 | /** | |
b3074c0a DH |
171 | * ir_setkeytable() - sets several entries in the scancode->keycode table |
172 | * @dev: the struct input_dev device descriptor | |
173 | * @to: the struct ir_scancode_table to copy entries to | |
174 | * @from: the struct ir_scancode_table to copy entries from | |
175 | * @return: -EINVAL if all keycodes could not be inserted, otherwise zero. | |
e97f4677 | 176 | * |
b3074c0a | 177 | * This routine is used to handle table initialization. |
e97f4677 | 178 | */ |
b3074c0a DH |
179 | static int ir_setkeytable(struct input_dev *dev, |
180 | struct ir_scancode_table *to, | |
181 | const struct ir_scancode_table *from) | |
e97f4677 | 182 | { |
b3074c0a DH |
183 | struct ir_input_dev *ir_dev = input_get_drvdata(dev); |
184 | struct ir_scancode_table *rc_tab = &ir_dev->rc_tab; | |
e97f4677 | 185 | unsigned long flags; |
b3074c0a DH |
186 | unsigned int i; |
187 | int rc = 0; | |
e97f4677 MCC |
188 | |
189 | spin_lock_irqsave(&rc_tab->lock, flags); | |
b3074c0a DH |
190 | for (i = 0; i < from->size; i++) { |
191 | rc = ir_do_setkeycode(dev, to, from->scan[i].scancode, | |
192 | from->scan[i].keycode); | |
193 | if (rc) | |
194 | break; | |
e97f4677 | 195 | } |
e97f4677 | 196 | spin_unlock_irqrestore(&rc_tab->lock, flags); |
b3074c0a | 197 | return rc; |
ef53a115 MCC |
198 | } |
199 | ||
200 | /** | |
b3074c0a | 201 | * ir_getkeycode() - get a keycode from the scancode->keycode table |
ef53a115 MCC |
202 | * @dev: the struct input_dev device descriptor |
203 | * @scancode: the desired scancode | |
b3074c0a DH |
204 | * @keycode: used to return the keycode, if found, or KEY_RESERVED |
205 | * @return: always returns zero. | |
ef53a115 | 206 | * |
b3074c0a | 207 | * This routine is used to handle evdev EVIOCGKEY ioctl. |
ef53a115 | 208 | */ |
b3074c0a DH |
209 | static int ir_getkeycode(struct input_dev *dev, |
210 | unsigned int scancode, unsigned int *keycode) | |
ef53a115 | 211 | { |
b3074c0a DH |
212 | int start, end, mid; |
213 | unsigned long flags; | |
214 | int key = KEY_RESERVED; | |
75543cce MCC |
215 | struct ir_input_dev *ir_dev = input_get_drvdata(dev); |
216 | struct ir_scancode_table *rc_tab = &ir_dev->rc_tab; | |
ef53a115 | 217 | |
b3074c0a DH |
218 | spin_lock_irqsave(&rc_tab->lock, flags); |
219 | start = 0; | |
220 | end = rc_tab->len - 1; | |
221 | while (start <= end) { | |
222 | mid = (start + end) / 2; | |
223 | if (rc_tab->scan[mid].scancode < scancode) | |
224 | start = mid + 1; | |
225 | else if (rc_tab->scan[mid].scancode > scancode) | |
226 | end = mid - 1; | |
227 | else { | |
228 | key = rc_tab->scan[mid].keycode; | |
229 | break; | |
230 | } | |
e97f4677 | 231 | } |
b3074c0a | 232 | spin_unlock_irqrestore(&rc_tab->lock, flags); |
e97f4677 | 233 | |
35438946 MCC |
234 | if (key == KEY_RESERVED) |
235 | IR_dprintk(1, "unknown key for scancode 0x%04x\n", | |
236 | scancode); | |
237 | ||
b3074c0a | 238 | *keycode = key; |
7fee03e4 | 239 | return 0; |
ef53a115 MCC |
240 | } |
241 | ||
242 | /** | |
243 | * ir_g_keycode_from_table() - gets the keycode that corresponds to a scancode | |
7fee03e4 | 244 | * @input_dev: the struct input_dev descriptor of the device |
ef53a115 MCC |
245 | * @scancode: the scancode that we're seeking |
246 | * | |
247 | * This routine is used by the input routines when a key is pressed at the | |
248 | * IR. The scancode is received and needs to be converted into a keycode. | |
6660de56 | 249 | * If the key is not found, it returns KEY_RESERVED. Otherwise, returns the |
ef53a115 MCC |
250 | * corresponding keycode from the table. |
251 | */ | |
252 | u32 ir_g_keycode_from_table(struct input_dev *dev, u32 scancode) | |
253 | { | |
b3074c0a | 254 | int keycode; |
ef53a115 | 255 | |
b3074c0a | 256 | ir_getkeycode(dev, scancode, &keycode); |
35438946 MCC |
257 | if (keycode != KEY_RESERVED) |
258 | IR_dprintk(1, "%s: scancode 0x%04x keycode 0x%02x\n", | |
259 | dev->name, scancode, keycode); | |
b3074c0a | 260 | return keycode; |
ef53a115 | 261 | } |
446e4a64 | 262 | EXPORT_SYMBOL_GPL(ir_g_keycode_from_table); |
ef53a115 | 263 | |
6660de56 MCC |
264 | /** |
265 | * ir_keyup() - generates input event to cleanup a key press | |
266 | * @input_dev: the struct input_dev descriptor of the device | |
267 | * | |
268 | * This routine is used by the input routines when a key is pressed at the | |
269 | * IR. It reports a keyup input event via input_report_key(). | |
270 | */ | |
271 | void ir_keyup(struct input_dev *dev) | |
272 | { | |
273 | struct ir_input_dev *ir = input_get_drvdata(dev); | |
274 | ||
275 | if (!ir->keypressed) | |
276 | return; | |
277 | ||
9f154782 | 278 | IR_dprintk(1, "keyup key 0x%04x\n", ir->keycode); |
6660de56 MCC |
279 | input_report_key(dev, ir->keycode, 0); |
280 | input_sync(dev); | |
281 | ir->keypressed = 0; | |
282 | } | |
283 | EXPORT_SYMBOL_GPL(ir_keyup); | |
284 | ||
285 | /** | |
286 | * ir_keydown() - generates input event for a key press | |
287 | * @input_dev: the struct input_dev descriptor of the device | |
288 | * @scancode: the scancode that we're seeking | |
289 | * | |
290 | * This routine is used by the input routines when a key is pressed at the | |
291 | * IR. It gets the keycode for a scancode and reports an input event via | |
292 | * input_report_key(). | |
293 | */ | |
294 | void ir_keydown(struct input_dev *dev, int scancode) | |
295 | { | |
296 | struct ir_input_dev *ir = input_get_drvdata(dev); | |
297 | ||
298 | u32 keycode = ir_g_keycode_from_table(dev, scancode); | |
299 | ||
300 | /* If already sent a keydown, do a keyup */ | |
301 | if (ir->keypressed) | |
302 | ir_keyup(dev); | |
303 | ||
304 | if (KEY_RESERVED == keycode) | |
305 | return; | |
306 | ||
307 | ir->keycode = keycode; | |
308 | ir->keypressed = 1; | |
309 | ||
310 | IR_dprintk(1, "%s: key down event, key 0x%04x, scancode 0x%04x\n", | |
311 | dev->name, keycode, scancode); | |
312 | ||
313 | input_report_key(dev, ir->keycode, 1); | |
314 | input_sync(dev); | |
315 | ||
316 | } | |
317 | EXPORT_SYMBOL_GPL(ir_keydown); | |
318 | ||
716aab44 MCC |
319 | static int ir_open(struct input_dev *input_dev) |
320 | { | |
321 | struct ir_input_dev *ir_dev = input_get_drvdata(input_dev); | |
322 | ||
323 | return ir_dev->props->open(ir_dev->props->priv); | |
324 | } | |
325 | ||
326 | static void ir_close(struct input_dev *input_dev) | |
327 | { | |
328 | struct ir_input_dev *ir_dev = input_get_drvdata(input_dev); | |
329 | ||
330 | ir_dev->props->close(ir_dev->props->priv); | |
331 | } | |
6660de56 | 332 | |
ef53a115 | 333 | /** |
b2245ba1 | 334 | * __ir_input_register() - sets the IR keycode table and add the handlers |
ef53a115 MCC |
335 | * for keymap table get/set |
336 | * @input_dev: the struct input_dev descriptor of the device | |
337 | * @rc_tab: the struct ir_scancode_table table of scancode/keymap | |
338 | * | |
d4b778d3 MCC |
339 | * This routine is used to initialize the input infrastructure |
340 | * to work with an IR. | |
341 | * It will register the input/evdev interface for the device and | |
342 | * register the syfs code for IR class | |
ef53a115 | 343 | */ |
b2245ba1 | 344 | int __ir_input_register(struct input_dev *input_dev, |
e93854da | 345 | const struct ir_scancode_table *rc_tab, |
727e625c MCC |
346 | const struct ir_dev_props *props, |
347 | const char *driver_name) | |
ef53a115 | 348 | { |
75543cce | 349 | struct ir_input_dev *ir_dev; |
b3074c0a | 350 | int rc; |
ef53a115 MCC |
351 | |
352 | if (rc_tab->scan == NULL || !rc_tab->size) | |
353 | return -EINVAL; | |
354 | ||
75543cce MCC |
355 | ir_dev = kzalloc(sizeof(*ir_dev), GFP_KERNEL); |
356 | if (!ir_dev) | |
357 | return -ENOMEM; | |
358 | ||
b3074c0a DH |
359 | ir_dev->driver_name = kasprintf(GFP_KERNEL, "%s", driver_name); |
360 | if (!ir_dev->driver_name) { | |
361 | rc = -ENOMEM; | |
362 | goto out_dev; | |
363 | } | |
75543cce | 364 | |
b3074c0a DH |
365 | input_dev->getkeycode = ir_getkeycode; |
366 | input_dev->setkeycode = ir_setkeycode; | |
367 | input_set_drvdata(input_dev, ir_dev); | |
368 | ||
369 | spin_lock_init(&ir_dev->rc_tab.lock); | |
9c89a181 | 370 | ir_dev->rc_tab.name = rc_tab->name; |
b3074c0a DH |
371 | ir_dev->rc_tab.ir_type = rc_tab->ir_type; |
372 | ir_dev->rc_tab.alloc = roundup_pow_of_two(rc_tab->size * | |
373 | sizeof(struct ir_scancode)); | |
374 | ir_dev->rc_tab.scan = kmalloc(ir_dev->rc_tab.alloc, GFP_KERNEL); | |
375 | ir_dev->rc_tab.size = ir_dev->rc_tab.alloc / sizeof(struct ir_scancode); | |
376 | ||
8231152f | 377 | if (!ir_dev->rc_tab.scan) { |
b3074c0a DH |
378 | rc = -ENOMEM; |
379 | goto out_name; | |
8231152f | 380 | } |
75543cce | 381 | |
b3074c0a DH |
382 | IR_dprintk(1, "Allocated space for %u keycode entries (%u bytes)\n", |
383 | ir_dev->rc_tab.size, ir_dev->rc_tab.alloc); | |
384 | ||
385 | set_bit(EV_KEY, input_dev->evbit); | |
386 | if (ir_setkeytable(input_dev, &ir_dev->rc_tab, rc_tab)) { | |
387 | rc = -ENOMEM; | |
388 | goto out_table; | |
389 | } | |
75543cce | 390 | |
e93854da | 391 | ir_dev->props = props; |
716aab44 MCC |
392 | if (props && props->open) |
393 | input_dev->open = ir_open; | |
394 | if (props && props->close) | |
395 | input_dev->close = ir_close; | |
75543cce | 396 | |
4714eda8 | 397 | rc = ir_register_class(input_dev); |
945cdfa2 | 398 | if (rc < 0) |
b3074c0a | 399 | goto out_table; |
579e7d60 | 400 | |
35438946 MCC |
401 | IR_dprintk(1, "Registered input device on %s for %s remote.\n", |
402 | driver_name, rc_tab->name); | |
403 | ||
4714eda8 MCC |
404 | return 0; |
405 | ||
b3074c0a DH |
406 | out_table: |
407 | kfree(ir_dev->rc_tab.scan); | |
408 | out_name: | |
409 | kfree(ir_dev->driver_name); | |
410 | out_dev: | |
4714eda8 | 411 | kfree(ir_dev); |
579e7d60 | 412 | return rc; |
ef53a115 | 413 | } |
b2245ba1 | 414 | EXPORT_SYMBOL_GPL(__ir_input_register); |
f6fc5049 | 415 | |
d4b778d3 MCC |
416 | /** |
417 | * ir_input_unregister() - unregisters IR and frees resources | |
418 | * @input_dev: the struct input_dev descriptor of the device | |
419 | ||
420 | * This routine is used to free memory and de-register interfaces. | |
421 | */ | |
38ef6aa8 | 422 | void ir_input_unregister(struct input_dev *dev) |
f6fc5049 | 423 | { |
75543cce | 424 | struct ir_input_dev *ir_dev = input_get_drvdata(dev); |
579e7d60 | 425 | struct ir_scancode_table *rc_tab; |
f6fc5049 | 426 | |
579e7d60 | 427 | if (!ir_dev) |
05395a3d MCC |
428 | return; |
429 | ||
f6fc5049 MCC |
430 | IR_dprintk(1, "Freed keycode table\n"); |
431 | ||
579e7d60 | 432 | rc_tab = &ir_dev->rc_tab; |
f6fc5049 MCC |
433 | rc_tab->size = 0; |
434 | kfree(rc_tab->scan); | |
435 | rc_tab->scan = NULL; | |
75543cce | 436 | |
4714eda8 MCC |
437 | ir_unregister_class(dev); |
438 | ||
b3074c0a | 439 | kfree(ir_dev->driver_name); |
75543cce | 440 | kfree(ir_dev); |
f6fc5049 | 441 | } |
38ef6aa8 | 442 | EXPORT_SYMBOL_GPL(ir_input_unregister); |
f6fc5049 | 443 | |
446e4a64 MCC |
444 | int ir_core_debug; /* ir_debug level (0,1,2) */ |
445 | EXPORT_SYMBOL_GPL(ir_core_debug); | |
446 | module_param_named(debug, ir_core_debug, int, 0644); | |
447 | ||
448 | MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>"); | |
449 | MODULE_LICENSE("GPL"); |