1 /* ir-keytable.c - handle IR scancode->keycode tables
3 * Copyright (C) 2009 by Mauro Carvalho Chehab <mchehab@redhat.com>
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.
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.
16 #include <linux/input.h>
17 #include <linux/slab.h>
18 #include "ir-core-priv.h"
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
24 /* FIXME: IR_KEYPRESS_TIMEOUT should be protocol specific */
25 #define IR_KEYPRESS_TIMEOUT 250
28 * ir_resize_table() - resizes a scancode table if necessary
29 * @rc_tab: the ir_scancode_table to resize
30 * @return: zero on success or a negative error code
32 * This routine will shrink the ir_scancode_table if it has lots of
33 * unused entries and grow it if it is full.
35 static int ir_resize_table(struct ir_scancode_table
*rc_tab
)
37 unsigned int oldalloc
= rc_tab
->alloc
;
38 unsigned int newalloc
= oldalloc
;
39 struct ir_scancode
*oldscan
= rc_tab
->scan
;
40 struct ir_scancode
*newscan
;
42 if (rc_tab
->size
== rc_tab
->len
) {
43 /* All entries in use -> grow keytable */
44 if (rc_tab
->alloc
>= IR_TAB_MAX_SIZE
)
48 IR_dprintk(1, "Growing table to %u bytes\n", newalloc
);
51 if ((rc_tab
->len
* 3 < rc_tab
->size
) && (oldalloc
> IR_TAB_MIN_SIZE
)) {
52 /* Less than 1/3 of entries in use -> shrink keytable */
54 IR_dprintk(1, "Shrinking table to %u bytes\n", newalloc
);
57 if (newalloc
== oldalloc
)
60 newscan
= kmalloc(newalloc
, GFP_ATOMIC
);
62 IR_dprintk(1, "Failed to kmalloc %u bytes\n", newalloc
);
66 memcpy(newscan
, rc_tab
->scan
, rc_tab
->len
* sizeof(struct ir_scancode
));
67 rc_tab
->scan
= newscan
;
68 rc_tab
->alloc
= newalloc
;
69 rc_tab
->size
= rc_tab
->alloc
/ sizeof(struct ir_scancode
);
75 * ir_do_setkeycode() - internal function to set a keycode in the
76 * scancode->keycode table
77 * @dev: the struct input_dev device descriptor
78 * @rc_tab: the struct ir_scancode_table to set the keycode in
79 * @scancode: the scancode for the ir command
80 * @keycode: the keycode for the ir command
81 * @resize: whether the keytable may be shrunk
82 * @return: -EINVAL if the keycode could not be inserted, otherwise zero.
84 * This routine is used internally to manipulate the scancode->keycode table.
85 * The caller has to hold @rc_tab->lock.
87 static int ir_do_setkeycode(struct input_dev
*dev
,
88 struct ir_scancode_table
*rc_tab
,
89 unsigned scancode
, unsigned keycode
,
93 int old_keycode
= KEY_RESERVED
;
94 struct ir_input_dev
*ir_dev
= input_get_drvdata(dev
);
97 * Unfortunately, some hardware-based IR decoders don't provide
98 * all bits for the complete IR code. In general, they provide only
99 * the command part of the IR code. Yet, as it is possible to replace
100 * the provided IR with another one, it is needed to allow loading
101 * IR tables from other remotes. So,
103 if (ir_dev
->props
&& ir_dev
->props
->scanmask
) {
104 scancode
&= ir_dev
->props
->scanmask
;
107 /* First check if we already have a mapping for this ir command */
108 for (i
= 0; i
< rc_tab
->len
; i
++) {
109 /* Keytable is sorted from lowest to highest scancode */
110 if (rc_tab
->scan
[i
].scancode
> scancode
)
112 else if (rc_tab
->scan
[i
].scancode
< scancode
)
115 old_keycode
= rc_tab
->scan
[i
].keycode
;
116 rc_tab
->scan
[i
].keycode
= keycode
;
118 /* Did the user wish to remove the mapping? */
119 if (keycode
== KEY_RESERVED
|| keycode
== KEY_UNKNOWN
) {
120 IR_dprintk(1, "#%d: Deleting scan 0x%04x\n",
123 memmove(&rc_tab
->scan
[i
], &rc_tab
->scan
[i
+ 1],
124 (rc_tab
->len
- i
) * sizeof(struct ir_scancode
));
127 /* Possibly shrink the keytable, failure is not a problem */
128 ir_resize_table(rc_tab
);
132 if (old_keycode
== KEY_RESERVED
&& keycode
!= KEY_RESERVED
) {
133 /* No previous mapping found, we might need to grow the table */
134 if (resize
&& ir_resize_table(rc_tab
))
137 IR_dprintk(1, "#%d: New scan 0x%04x with key 0x%04x\n",
138 i
, scancode
, keycode
);
140 /* i is the proper index to insert our new keycode */
141 memmove(&rc_tab
->scan
[i
+ 1], &rc_tab
->scan
[i
],
142 (rc_tab
->len
- i
) * sizeof(struct ir_scancode
));
143 rc_tab
->scan
[i
].scancode
= scancode
;
144 rc_tab
->scan
[i
].keycode
= keycode
;
146 set_bit(keycode
, dev
->keybit
);
148 IR_dprintk(1, "#%d: Replacing scan 0x%04x with key 0x%04x\n",
149 i
, scancode
, keycode
);
150 /* A previous mapping was updated... */
151 clear_bit(old_keycode
, dev
->keybit
);
152 /* ...but another scancode might use the same keycode */
153 for (i
= 0; i
< rc_tab
->len
; i
++) {
154 if (rc_tab
->scan
[i
].keycode
== old_keycode
) {
155 set_bit(old_keycode
, dev
->keybit
);
165 * ir_setkeycode() - set a keycode in the scancode->keycode table
166 * @dev: the struct input_dev device descriptor
167 * @scancode: the desired scancode
169 * @return: -EINVAL if the keycode could not be inserted, otherwise zero.
171 * This routine is used to handle evdev EVIOCSKEY ioctl.
173 static int ir_setkeycode(struct input_dev
*dev
,
174 unsigned int scancode
, unsigned int keycode
)
178 struct ir_input_dev
*ir_dev
= input_get_drvdata(dev
);
179 struct ir_scancode_table
*rc_tab
= &ir_dev
->rc_tab
;
181 spin_lock_irqsave(&rc_tab
->lock
, flags
);
182 rc
= ir_do_setkeycode(dev
, rc_tab
, scancode
, keycode
, true);
183 spin_unlock_irqrestore(&rc_tab
->lock
, flags
);
188 * ir_setkeytable() - sets several entries in the scancode->keycode table
189 * @dev: the struct input_dev device descriptor
190 * @to: the struct ir_scancode_table to copy entries to
191 * @from: the struct ir_scancode_table to copy entries from
192 * @return: -EINVAL if all keycodes could not be inserted, otherwise zero.
194 * This routine is used to handle table initialization.
196 static int ir_setkeytable(struct input_dev
*dev
,
197 struct ir_scancode_table
*to
,
198 const struct ir_scancode_table
*from
)
200 struct ir_input_dev
*ir_dev
= input_get_drvdata(dev
);
201 struct ir_scancode_table
*rc_tab
= &ir_dev
->rc_tab
;
206 spin_lock_irqsave(&rc_tab
->lock
, flags
);
207 for (i
= 0; i
< from
->size
; i
++) {
208 rc
= ir_do_setkeycode(dev
, to
, from
->scan
[i
].scancode
,
209 from
->scan
[i
].keycode
, false);
213 spin_unlock_irqrestore(&rc_tab
->lock
, flags
);
218 * ir_getkeycode() - get a keycode from the scancode->keycode table
219 * @dev: the struct input_dev device descriptor
220 * @scancode: the desired scancode
221 * @keycode: used to return the keycode, if found, or KEY_RESERVED
222 * @return: always returns zero.
224 * This routine is used to handle evdev EVIOCGKEY ioctl.
226 static int ir_getkeycode(struct input_dev
*dev
,
227 unsigned int scancode
, unsigned int *keycode
)
231 int key
= KEY_RESERVED
;
232 struct ir_input_dev
*ir_dev
= input_get_drvdata(dev
);
233 struct ir_scancode_table
*rc_tab
= &ir_dev
->rc_tab
;
235 spin_lock_irqsave(&rc_tab
->lock
, flags
);
237 end
= rc_tab
->len
- 1;
238 while (start
<= end
) {
239 mid
= (start
+ end
) / 2;
240 if (rc_tab
->scan
[mid
].scancode
< scancode
)
242 else if (rc_tab
->scan
[mid
].scancode
> scancode
)
245 key
= rc_tab
->scan
[mid
].keycode
;
249 spin_unlock_irqrestore(&rc_tab
->lock
, flags
);
251 if (key
== KEY_RESERVED
)
252 IR_dprintk(1, "unknown key for scancode 0x%04x\n",
260 * ir_g_keycode_from_table() - gets the keycode that corresponds to a scancode
261 * @input_dev: the struct input_dev descriptor of the device
262 * @scancode: the scancode that we're seeking
264 * This routine is used by the input routines when a key is pressed at the
265 * IR. The scancode is received and needs to be converted into a keycode.
266 * If the key is not found, it returns KEY_RESERVED. Otherwise, returns the
267 * corresponding keycode from the table.
269 u32
ir_g_keycode_from_table(struct input_dev
*dev
, u32 scancode
)
273 ir_getkeycode(dev
, scancode
, &keycode
);
274 if (keycode
!= KEY_RESERVED
)
275 IR_dprintk(1, "%s: scancode 0x%04x keycode 0x%02x\n",
276 dev
->name
, scancode
, keycode
);
279 EXPORT_SYMBOL_GPL(ir_g_keycode_from_table
);
282 * ir_keyup() - generates input event to cleanup a key press
283 * @ir: the struct ir_input_dev descriptor of the device
285 * This routine is used to signal that a key has been released on the
286 * remote control. It reports a keyup input event via input_report_key().
288 static void ir_keyup(struct ir_input_dev
*ir
)
293 IR_dprintk(1, "keyup key 0x%04x\n", ir
->last_keycode
);
294 input_report_key(ir
->input_dev
, ir
->last_keycode
, 0);
295 input_sync(ir
->input_dev
);
296 ir
->keypressed
= false;
300 * ir_timer_keyup() - generates a keyup event after a timeout
301 * @cookie: a pointer to struct ir_input_dev passed to setup_timer()
303 * This routine will generate a keyup event some time after a keydown event
304 * is generated when no further activity has been detected.
306 static void ir_timer_keyup(unsigned long cookie
)
308 struct ir_input_dev
*ir
= (struct ir_input_dev
*)cookie
;
312 * ir->keyup_jiffies is used to prevent a race condition if a
313 * hardware interrupt occurs at this point and the keyup timer
314 * event is moved further into the future as a result.
316 * The timer will then be reactivated and this function called
317 * again in the future. We need to exit gracefully in that case
318 * to allow the input subsystem to do its auto-repeat magic or
319 * a keyup event might follow immediately after the keydown.
321 spin_lock_irqsave(&ir
->keylock
, flags
);
322 if (time_is_before_eq_jiffies(ir
->keyup_jiffies
))
324 spin_unlock_irqrestore(&ir
->keylock
, flags
);
328 * ir_repeat() - notifies the IR core that a key is still pressed
329 * @dev: the struct input_dev descriptor of the device
331 * This routine is used by IR decoders when a repeat message which does
332 * not include the necessary bits to reproduce the scancode has been
335 void ir_repeat(struct input_dev
*dev
)
338 struct ir_input_dev
*ir
= input_get_drvdata(dev
);
340 spin_lock_irqsave(&ir
->keylock
, flags
);
342 input_event(dev
, EV_MSC
, MSC_SCAN
, ir
->last_scancode
);
347 ir
->keyup_jiffies
= jiffies
+ msecs_to_jiffies(IR_KEYPRESS_TIMEOUT
);
348 mod_timer(&ir
->timer_keyup
, ir
->keyup_jiffies
);
351 spin_unlock_irqrestore(&ir
->keylock
, flags
);
353 EXPORT_SYMBOL_GPL(ir_repeat
);
356 * ir_keydown() - generates input event for a key press
357 * @dev: the struct input_dev descriptor of the device
358 * @scancode: the scancode that we're seeking
359 * @toggle: the toggle value (protocol dependent, if the protocol doesn't
360 * support toggle values, this should be set to zero)
362 * This routine is used by the input routines when a key is pressed at the
363 * IR. It gets the keycode for a scancode and reports an input event via
364 * input_report_key().
366 void ir_keydown(struct input_dev
*dev
, int scancode
, u8 toggle
)
369 struct ir_input_dev
*ir
= input_get_drvdata(dev
);
371 u32 keycode
= ir_g_keycode_from_table(dev
, scancode
);
373 spin_lock_irqsave(&ir
->keylock
, flags
);
375 input_event(dev
, EV_MSC
, MSC_SCAN
, scancode
);
378 if (ir
->keypressed
&&
379 ir
->last_scancode
== scancode
&&
380 ir
->last_toggle
== toggle
)
383 /* Release old keypress */
386 ir
->last_scancode
= scancode
;
387 ir
->last_toggle
= toggle
;
388 ir
->last_keycode
= keycode
;
391 if (keycode
== KEY_RESERVED
)
395 /* Register a keypress */
396 ir
->keypressed
= true;
397 IR_dprintk(1, "%s: key down event, key 0x%04x, scancode 0x%04x\n",
398 dev
->name
, keycode
, scancode
);
399 input_report_key(dev
, ir
->last_keycode
, 1);
403 ir
->keyup_jiffies
= jiffies
+ msecs_to_jiffies(IR_KEYPRESS_TIMEOUT
);
404 mod_timer(&ir
->timer_keyup
, ir
->keyup_jiffies
);
406 spin_unlock_irqrestore(&ir
->keylock
, flags
);
408 EXPORT_SYMBOL_GPL(ir_keydown
);
410 static int ir_open(struct input_dev
*input_dev
)
412 struct ir_input_dev
*ir_dev
= input_get_drvdata(input_dev
);
414 return ir_dev
->props
->open(ir_dev
->props
->priv
);
417 static void ir_close(struct input_dev
*input_dev
)
419 struct ir_input_dev
*ir_dev
= input_get_drvdata(input_dev
);
421 ir_dev
->props
->close(ir_dev
->props
->priv
);
425 * __ir_input_register() - sets the IR keycode table and add the handlers
426 * for keymap table get/set
427 * @input_dev: the struct input_dev descriptor of the device
428 * @rc_tab: the struct ir_scancode_table table of scancode/keymap
430 * This routine is used to initialize the input infrastructure
431 * to work with an IR.
432 * It will register the input/evdev interface for the device and
433 * register the syfs code for IR class
435 int __ir_input_register(struct input_dev
*input_dev
,
436 const struct ir_scancode_table
*rc_tab
,
437 struct ir_dev_props
*props
,
438 const char *driver_name
)
440 struct ir_input_dev
*ir_dev
;
443 if (rc_tab
->scan
== NULL
|| !rc_tab
->size
)
446 ir_dev
= kzalloc(sizeof(*ir_dev
), GFP_KERNEL
);
450 ir_dev
->driver_name
= kasprintf(GFP_KERNEL
, "%s", driver_name
);
451 if (!ir_dev
->driver_name
) {
456 input_dev
->getkeycode
= ir_getkeycode
;
457 input_dev
->setkeycode
= ir_setkeycode
;
458 input_set_drvdata(input_dev
, ir_dev
);
459 ir_dev
->input_dev
= input_dev
;
461 spin_lock_init(&ir_dev
->rc_tab
.lock
);
462 spin_lock_init(&ir_dev
->keylock
);
463 setup_timer(&ir_dev
->timer_keyup
, ir_timer_keyup
, (unsigned long)ir_dev
);
465 ir_dev
->rc_tab
.name
= rc_tab
->name
;
466 ir_dev
->rc_tab
.ir_type
= rc_tab
->ir_type
;
467 ir_dev
->rc_tab
.alloc
= roundup_pow_of_two(rc_tab
->size
*
468 sizeof(struct ir_scancode
));
469 ir_dev
->rc_tab
.scan
= kmalloc(ir_dev
->rc_tab
.alloc
, GFP_KERNEL
);
470 ir_dev
->rc_tab
.size
= ir_dev
->rc_tab
.alloc
/ sizeof(struct ir_scancode
);
472 ir_dev
->props
= props
;
474 input_dev
->open
= ir_open
;
476 input_dev
->close
= ir_close
;
479 if (!ir_dev
->rc_tab
.scan
) {
484 IR_dprintk(1, "Allocated space for %u keycode entries (%u bytes)\n",
485 ir_dev
->rc_tab
.size
, ir_dev
->rc_tab
.alloc
);
487 set_bit(EV_KEY
, input_dev
->evbit
);
488 set_bit(EV_REP
, input_dev
->evbit
);
489 set_bit(EV_MSC
, input_dev
->evbit
);
490 set_bit(MSC_SCAN
, input_dev
->mscbit
);
492 if (ir_setkeytable(input_dev
, &ir_dev
->rc_tab
, rc_tab
)) {
497 rc
= ir_register_class(input_dev
);
502 if (ir_dev
->props
->driver_type
== RC_DRIVER_IR_RAW
) {
503 rc
= ir_raw_event_register(input_dev
);
508 IR_dprintk(1, "Registered input device on %s for %s remote%s.\n",
509 driver_name
, rc_tab
->name
,
510 (ir_dev
->props
&& ir_dev
->props
->driver_type
== RC_DRIVER_IR_RAW
) ?
511 " in raw mode" : "");
514 * Default delay of 250ms is too short for some protocols, expecially
515 * since the timeout is currently set to 250ms. Increase it to 500ms,
516 * to avoid wrong repetition of the keycodes.
518 input_dev
->rep
[REP_DELAY
] = 500;
523 ir_unregister_class(input_dev
);
525 kfree(ir_dev
->rc_tab
.scan
);
527 kfree(ir_dev
->driver_name
);
532 EXPORT_SYMBOL_GPL(__ir_input_register
);
535 * ir_input_unregister() - unregisters IR and frees resources
536 * @input_dev: the struct input_dev descriptor of the device
538 * This routine is used to free memory and de-register interfaces.
540 void ir_input_unregister(struct input_dev
*input_dev
)
542 struct ir_input_dev
*ir_dev
= input_get_drvdata(input_dev
);
543 struct ir_scancode_table
*rc_tab
;
548 IR_dprintk(1, "Freed keycode table\n");
550 del_timer_sync(&ir_dev
->timer_keyup
);
552 if (ir_dev
->props
->driver_type
== RC_DRIVER_IR_RAW
)
553 ir_raw_event_unregister(input_dev
);
555 rc_tab
= &ir_dev
->rc_tab
;
560 ir_unregister_class(input_dev
);
562 kfree(ir_dev
->driver_name
);
565 EXPORT_SYMBOL_GPL(ir_input_unregister
);
567 int ir_core_debug
; /* ir_debug level (0,1,2) */
568 EXPORT_SYMBOL_GPL(ir_core_debug
);
569 module_param_named(debug
, ir_core_debug
, int, 0644);
571 MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
572 MODULE_LICENSE("GPL");
This page took 0.049689 seconds and 6 git commands to generate.