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_create_table() - initializes a scancode table
29 * @rc_tab: the ir_scancode_table to initialize
30 * @name: name to assign to the table
31 * @ir_type: ir type to assign to the new table
32 * @size: initial size of the table
33 * @return: zero on success or a negative error code
35 * This routine will initialize the ir_scancode_table and will allocate
36 * memory to hold at least the specified number elements.
38 static int ir_create_table(struct ir_scancode_table *rc_tab,
39 const char *name, u64 ir_type, size_t size)
42 rc_tab->ir_type = ir_type;
43 rc_tab->alloc = roundup_pow_of_two(size * sizeof(struct ir_scancode));
44 rc_tab->size = rc_tab->alloc / sizeof(struct ir_scancode);
45 rc_tab->scan = kmalloc(rc_tab->alloc, GFP_KERNEL);
49 IR_dprintk(1, "Allocated space for %u keycode entries (%u bytes)\n",
50 rc_tab->size, rc_tab->alloc);
55 * ir_free_table() - frees memory allocated by a scancode table
56 * @rc_tab: the table whose mappings need to be freed
58 * This routine will free memory alloctaed for key mappings used by given
61 static void ir_free_table(struct ir_scancode_table *rc_tab)
69 * ir_resize_table() - resizes a scancode table if necessary
70 * @rc_tab: the ir_scancode_table to resize
71 * @gfp_flags: gfp flags to use when allocating memory
72 * @return: zero on success or a negative error code
74 * This routine will shrink the ir_scancode_table if it has lots of
75 * unused entries and grow it if it is full.
77 static int ir_resize_table(struct ir_scancode_table *rc_tab, gfp_t gfp_flags)
79 unsigned int oldalloc = rc_tab->alloc;
80 unsigned int newalloc = oldalloc;
81 struct ir_scancode *oldscan = rc_tab->scan;
82 struct ir_scancode *newscan;
84 if (rc_tab->size == rc_tab->len) {
85 /* All entries in use -> grow keytable */
86 if (rc_tab->alloc >= IR_TAB_MAX_SIZE)
90 IR_dprintk(1, "Growing table to %u bytes\n", newalloc);
93 if ((rc_tab->len * 3 < rc_tab->size) && (oldalloc > IR_TAB_MIN_SIZE)) {
94 /* Less than 1/3 of entries in use -> shrink keytable */
96 IR_dprintk(1, "Shrinking table to %u bytes\n", newalloc);
99 if (newalloc == oldalloc)
102 newscan = kmalloc(newalloc, gfp_flags);
104 IR_dprintk(1, "Failed to kmalloc %u bytes\n", newalloc);
108 memcpy(newscan, rc_tab->scan, rc_tab->len * sizeof(struct ir_scancode));
109 rc_tab->scan = newscan;
110 rc_tab->alloc = newalloc;
111 rc_tab->size = rc_tab->alloc / sizeof(struct ir_scancode);
117 * ir_update_mapping() - set a keycode in the scancode->keycode table
118 * @dev: the struct input_dev device descriptor
119 * @rc_tab: scancode table to be adjusted
120 * @index: index of the mapping that needs to be updated
121 * @keycode: the desired keycode
122 * @return: previous keycode assigned to the mapping
124 * This routine is used to update scancode->keycopde mapping at given
127 static unsigned int ir_update_mapping(struct input_dev *dev,
128 struct ir_scancode_table *rc_tab,
130 unsigned int new_keycode)
132 int old_keycode = rc_tab->scan[index].keycode;
135 /* Did the user wish to remove the mapping? */
136 if (new_keycode == KEY_RESERVED || new_keycode == KEY_UNKNOWN) {
137 IR_dprintk(1, "#%d: Deleting scan 0x%04x\n",
138 index, rc_tab->scan[index].scancode);
140 memmove(&rc_tab->scan[index], &rc_tab->scan[index+ 1],
141 (rc_tab->len - index) * sizeof(struct ir_scancode));
143 IR_dprintk(1, "#%d: %s scan 0x%04x with key 0x%04x\n",
145 old_keycode == KEY_RESERVED ? "New" : "Replacing",
146 rc_tab->scan[index].scancode, new_keycode);
147 rc_tab->scan[index].keycode = new_keycode;
148 __set_bit(new_keycode, dev->keybit);
151 if (old_keycode != KEY_RESERVED) {
152 /* A previous mapping was updated... */
153 __clear_bit(old_keycode, dev->keybit);
154 /* ... but another scancode might use the same keycode */
155 for (i = 0; i < rc_tab->len; i++) {
156 if (rc_tab->scan[i].keycode == old_keycode) {
157 __set_bit(old_keycode, dev->keybit);
162 /* Possibly shrink the keytable, failure is not a problem */
163 ir_resize_table(rc_tab, GFP_ATOMIC);
170 * ir_locate_scancode() - set a keycode in the scancode->keycode table
171 * @ir_dev: the struct ir_input_dev device descriptor
172 * @rc_tab: scancode table to be searched
173 * @scancode: the desired scancode
174 * @resize: controls whether we allowed to resize the table to
175 * accomodate not yet present scancodes
176 * @return: index of the mapping containing scancode in question
177 * or -1U in case of failure.
179 * This routine is used to locate given scancode in ir_scancode_table.
180 * If scancode is not yet present the routine will allocate a new slot
183 static unsigned int ir_establish_scancode(struct ir_input_dev *ir_dev,
184 struct ir_scancode_table *rc_tab,
185 unsigned int scancode,
191 * Unfortunately, some hardware-based IR decoders don't provide
192 * all bits for the complete IR code. In general, they provide only
193 * the command part of the IR code. Yet, as it is possible to replace
194 * the provided IR with another one, it is needed to allow loading
195 * IR tables from other remotes. So,
197 if (ir_dev->props && ir_dev->props->scanmask)
198 scancode &= ir_dev->props->scanmask;
200 /* First check if we already have a mapping for this ir command */
201 for (i = 0; i < rc_tab->len; i++) {
202 if (rc_tab->scan[i].scancode == scancode)
205 /* Keytable is sorted from lowest to highest scancode */
206 if (rc_tab->scan[i].scancode >= scancode)
210 /* No previous mapping found, we might need to grow the table */
211 if (rc_tab->size == rc_tab->len) {
212 if (!resize || ir_resize_table(rc_tab, GFP_ATOMIC))
216 /* i is the proper index to insert our new keycode */
218 memmove(&rc_tab->scan[i + 1], &rc_tab->scan[i],
219 (rc_tab->len - i) * sizeof(struct ir_scancode));
220 rc_tab->scan[i].scancode = scancode;
221 rc_tab->scan[i].keycode = KEY_RESERVED;
228 * ir_setkeycode() - set a keycode in the scancode->keycode table
229 * @dev: the struct input_dev device descriptor
230 * @scancode: the desired scancode
232 * @return: -EINVAL if the keycode could not be inserted, otherwise zero.
234 * This routine is used to handle evdev EVIOCSKEY ioctl.
236 static int ir_setkeycode(struct input_dev *dev,
237 const struct input_keymap_entry *ke,
238 unsigned int *old_keycode)
240 struct ir_input_dev *ir_dev = input_get_drvdata(dev);
241 struct ir_scancode_table *rc_tab = &ir_dev->rc_tab;
243 unsigned int scancode;
247 spin_lock_irqsave(&rc_tab->lock, flags);
249 if (ke->flags & INPUT_KEYMAP_BY_INDEX) {
251 if (index >= rc_tab->len) {
256 retval = input_scancode_to_scalar(ke, &scancode);
260 index = ir_establish_scancode(ir_dev, rc_tab, scancode, true);
261 if (index >= rc_tab->len) {
267 *old_keycode = ir_update_mapping(dev, rc_tab, index, ke->keycode);
270 spin_unlock_irqrestore(&rc_tab->lock, flags);
275 * ir_setkeytable() - sets several entries in the scancode->keycode table
276 * @dev: the struct input_dev device descriptor
277 * @to: the struct ir_scancode_table to copy entries to
278 * @from: the struct ir_scancode_table to copy entries from
279 * @return: -ENOMEM if all keycodes could not be inserted, otherwise zero.
281 * This routine is used to handle table initialization.
283 static int ir_setkeytable(struct ir_input_dev *ir_dev,
284 const struct ir_scancode_table *from)
286 struct ir_scancode_table *rc_tab = &ir_dev->rc_tab;
287 unsigned int i, index;
290 rc = ir_create_table(&ir_dev->rc_tab,
291 from->name, from->ir_type, from->size);
295 IR_dprintk(1, "Allocated space for %u keycode entries (%u bytes)\n",
296 rc_tab->size, rc_tab->alloc);
298 for (i = 0; i < from->size; i++) {
299 index = ir_establish_scancode(ir_dev, rc_tab,
300 from->scan[i].scancode, false);
301 if (index >= rc_tab->len) {
306 ir_update_mapping(ir_dev->input_dev, rc_tab, index,
307 from->scan[i].keycode);
311 ir_free_table(rc_tab);
317 * ir_lookup_by_scancode() - locate mapping by scancode
318 * @rc_tab: the &struct ir_scancode_table to search
319 * @scancode: scancode to look for in the table
320 * @return: index in the table, -1U if not found
322 * This routine performs binary search in RC keykeymap table for
325 static unsigned int ir_lookup_by_scancode(const struct ir_scancode_table *rc_tab,
326 unsigned int scancode)
329 int end = rc_tab->len - 1;
332 while (start <= end) {
333 mid = (start + end) / 2;
334 if (rc_tab->scan[mid].scancode < scancode)
336 else if (rc_tab->scan[mid].scancode > scancode)
346 * ir_getkeycode() - get a keycode from the scancode->keycode table
347 * @dev: the struct input_dev device descriptor
348 * @scancode: the desired scancode
349 * @keycode: used to return the keycode, if found, or KEY_RESERVED
350 * @return: always returns zero.
352 * This routine is used to handle evdev EVIOCGKEY ioctl.
354 static int ir_getkeycode(struct input_dev *dev,
355 struct input_keymap_entry *ke)
357 struct ir_input_dev *ir_dev = input_get_drvdata(dev);
358 struct ir_scancode_table *rc_tab = &ir_dev->rc_tab;
359 struct ir_scancode *entry;
362 unsigned int scancode;
365 spin_lock_irqsave(&rc_tab->lock, flags);
367 if (ke->flags & INPUT_KEYMAP_BY_INDEX) {
370 retval = input_scancode_to_scalar(ke, &scancode);
374 index = ir_lookup_by_scancode(rc_tab, scancode);
377 if (index >= rc_tab->len) {
378 if (!(ke->flags & INPUT_KEYMAP_BY_INDEX))
379 IR_dprintk(1, "unknown key for scancode 0x%04x\n",
385 entry = &rc_tab->scan[index];
388 ke->keycode = entry->keycode;
389 ke->len = sizeof(entry->scancode);
390 memcpy(ke->scancode, &entry->scancode, sizeof(entry->scancode));
395 spin_unlock_irqrestore(&rc_tab->lock, flags);
400 * ir_g_keycode_from_table() - gets the keycode that corresponds to a scancode
401 * @input_dev: the struct input_dev descriptor of the device
402 * @scancode: the scancode that we're seeking
404 * This routine is used by the input routines when a key is pressed at the
405 * IR. The scancode is received and needs to be converted into a keycode.
406 * If the key is not found, it returns KEY_RESERVED. Otherwise, returns the
407 * corresponding keycode from the table.
409 u32 ir_g_keycode_from_table(struct input_dev *dev, u32 scancode)
411 struct ir_input_dev *ir_dev = input_get_drvdata(dev);
412 struct ir_scancode_table *rc_tab = &ir_dev->rc_tab;
413 unsigned int keycode;
417 spin_lock_irqsave(&rc_tab->lock, flags);
419 index = ir_lookup_by_scancode(rc_tab, scancode);
420 keycode = index < rc_tab->len ?
421 rc_tab->scan[index].keycode : KEY_RESERVED;
423 spin_unlock_irqrestore(&rc_tab->lock, flags);
425 if (keycode != KEY_RESERVED)
426 IR_dprintk(1, "%s: scancode 0x%04x keycode 0x%02x\n",
427 dev->name, scancode, keycode);
431 EXPORT_SYMBOL_GPL(ir_g_keycode_from_table);
434 * ir_do_keyup() - internal function to signal the release of a keypress
435 * @ir: the struct ir_input_dev descriptor of the device
437 * This function is used internally to release a keypress, it must be
438 * called with keylock held.
440 static void ir_do_keyup(struct ir_input_dev *ir)
445 IR_dprintk(1, "keyup key 0x%04x\n", ir->last_keycode);
446 input_report_key(ir->input_dev, ir->last_keycode, 0);
447 input_sync(ir->input_dev);
448 ir->keypressed = false;
452 * ir_keyup() - generates input event to signal the release of a keypress
453 * @dev: the struct input_dev descriptor of the device
455 * This routine is used to signal that a key has been released on the
458 void ir_keyup(struct input_dev *dev)
461 struct ir_input_dev *ir = input_get_drvdata(dev);
463 spin_lock_irqsave(&ir->keylock, flags);
465 spin_unlock_irqrestore(&ir->keylock, flags);
467 EXPORT_SYMBOL_GPL(ir_keyup);
470 * ir_timer_keyup() - generates a keyup event after a timeout
471 * @cookie: a pointer to struct ir_input_dev passed to setup_timer()
473 * This routine will generate a keyup event some time after a keydown event
474 * is generated when no further activity has been detected.
476 static void ir_timer_keyup(unsigned long cookie)
478 struct ir_input_dev *ir = (struct ir_input_dev *)cookie;
482 * ir->keyup_jiffies is used to prevent a race condition if a
483 * hardware interrupt occurs at this point and the keyup timer
484 * event is moved further into the future as a result.
486 * The timer will then be reactivated and this function called
487 * again in the future. We need to exit gracefully in that case
488 * to allow the input subsystem to do its auto-repeat magic or
489 * a keyup event might follow immediately after the keydown.
491 spin_lock_irqsave(&ir->keylock, flags);
492 if (time_is_before_eq_jiffies(ir->keyup_jiffies))
494 spin_unlock_irqrestore(&ir->keylock, flags);
498 * ir_repeat() - notifies the IR core that a key is still pressed
499 * @dev: the struct input_dev descriptor of the device
501 * This routine is used by IR decoders when a repeat message which does
502 * not include the necessary bits to reproduce the scancode has been
505 void ir_repeat(struct input_dev *dev)
508 struct ir_input_dev *ir = input_get_drvdata(dev);
510 spin_lock_irqsave(&ir->keylock, flags);
512 input_event(dev, EV_MSC, MSC_SCAN, ir->last_scancode);
517 ir->keyup_jiffies = jiffies + msecs_to_jiffies(IR_KEYPRESS_TIMEOUT);
518 mod_timer(&ir->timer_keyup, ir->keyup_jiffies);
521 spin_unlock_irqrestore(&ir->keylock, flags);
523 EXPORT_SYMBOL_GPL(ir_repeat);
526 * ir_do_keydown() - internal function to process a keypress
527 * @dev: the struct input_dev descriptor of the device
528 * @scancode: the scancode of the keypress
529 * @keycode: the keycode of the keypress
530 * @toggle: the toggle value of the keypress
532 * This function is used internally to register a keypress, it must be
533 * called with keylock held.
535 static void ir_do_keydown(struct input_dev *dev, int scancode,
536 u32 keycode, u8 toggle)
538 struct ir_input_dev *ir = input_get_drvdata(dev);
540 input_event(dev, EV_MSC, MSC_SCAN, scancode);
543 if (ir->keypressed &&
544 ir->last_scancode == scancode &&
545 ir->last_toggle == toggle)
548 /* Release old keypress */
551 ir->last_scancode = scancode;
552 ir->last_toggle = toggle;
553 ir->last_keycode = keycode;
555 if (keycode == KEY_RESERVED)
558 /* Register a keypress */
559 ir->keypressed = true;
560 IR_dprintk(1, "%s: key down event, key 0x%04x, scancode 0x%04x\n",
561 dev->name, keycode, scancode);
562 input_report_key(dev, ir->last_keycode, 1);
567 * ir_keydown() - generates input event for a key press
568 * @dev: the struct input_dev descriptor of the device
569 * @scancode: the scancode that we're seeking
570 * @toggle: the toggle value (protocol dependent, if the protocol doesn't
571 * support toggle values, this should be set to zero)
573 * This routine is used by the input routines when a key is pressed at the
574 * IR. It gets the keycode for a scancode and reports an input event via
575 * input_report_key().
577 void ir_keydown(struct input_dev *dev, int scancode, u8 toggle)
580 struct ir_input_dev *ir = input_get_drvdata(dev);
581 u32 keycode = ir_g_keycode_from_table(dev, scancode);
583 spin_lock_irqsave(&ir->keylock, flags);
584 ir_do_keydown(dev, scancode, keycode, toggle);
586 if (ir->keypressed) {
587 ir->keyup_jiffies = jiffies + msecs_to_jiffies(IR_KEYPRESS_TIMEOUT);
588 mod_timer(&ir->timer_keyup, ir->keyup_jiffies);
590 spin_unlock_irqrestore(&ir->keylock, flags);
592 EXPORT_SYMBOL_GPL(ir_keydown);
595 * ir_keydown_notimeout() - generates input event for a key press without
596 * an automatic keyup event at a later time
597 * @dev: the struct input_dev descriptor of the device
598 * @scancode: the scancode that we're seeking
599 * @toggle: the toggle value (protocol dependent, if the protocol doesn't
600 * support toggle values, this should be set to zero)
602 * This routine is used by the input routines when a key is pressed at the
603 * IR. It gets the keycode for a scancode and reports an input event via
604 * input_report_key(). The driver must manually call ir_keyup() at a later
607 void ir_keydown_notimeout(struct input_dev *dev, int scancode, u8 toggle)
610 struct ir_input_dev *ir = input_get_drvdata(dev);
611 u32 keycode = ir_g_keycode_from_table(dev, scancode);
613 spin_lock_irqsave(&ir->keylock, flags);
614 ir_do_keydown(dev, scancode, keycode, toggle);
615 spin_unlock_irqrestore(&ir->keylock, flags);
617 EXPORT_SYMBOL_GPL(ir_keydown_notimeout);
619 static int ir_open(struct input_dev *input_dev)
621 struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
623 return ir_dev->props->open(ir_dev->props->priv);
626 static void ir_close(struct input_dev *input_dev)
628 struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
630 ir_dev->props->close(ir_dev->props->priv);
634 * __ir_input_register() - sets the IR keycode table and add the handlers
635 * for keymap table get/set
636 * @input_dev: the struct input_dev descriptor of the device
637 * @rc_tab: the struct ir_scancode_table table of scancode/keymap
639 * This routine is used to initialize the input infrastructure
640 * to work with an IR.
641 * It will register the input/evdev interface for the device and
642 * register the syfs code for IR class
644 int __ir_input_register(struct input_dev *input_dev,
645 const struct ir_scancode_table *rc_tab,
646 struct ir_dev_props *props,
647 const char *driver_name)
649 struct ir_input_dev *ir_dev;
652 if (rc_tab->scan == NULL || !rc_tab->size)
655 ir_dev = kzalloc(sizeof(*ir_dev), GFP_KERNEL);
659 ir_dev->driver_name = kasprintf(GFP_KERNEL, "%s", driver_name);
660 if (!ir_dev->driver_name) {
665 input_dev->getkeycode_new = ir_getkeycode;
666 input_dev->setkeycode_new = ir_setkeycode;
667 input_set_drvdata(input_dev, ir_dev);
668 ir_dev->input_dev = input_dev;
670 spin_lock_init(&ir_dev->rc_tab.lock);
671 spin_lock_init(&ir_dev->keylock);
672 setup_timer(&ir_dev->timer_keyup, ir_timer_keyup, (unsigned long)ir_dev);
675 ir_dev->props = props;
677 input_dev->open = ir_open;
679 input_dev->close = ir_close;
682 set_bit(EV_KEY, input_dev->evbit);
683 set_bit(EV_REP, input_dev->evbit);
684 set_bit(EV_MSC, input_dev->evbit);
685 set_bit(MSC_SCAN, input_dev->mscbit);
687 rc = ir_setkeytable(ir_dev, rc_tab);
691 rc = ir_register_class(input_dev);
696 if (ir_dev->props->driver_type == RC_DRIVER_IR_RAW) {
697 rc = ir_raw_event_register(input_dev);
702 rc = ir_register_input(input_dev);
706 IR_dprintk(1, "Registered input device on %s for %s remote%s.\n",
707 driver_name, rc_tab->name,
708 (ir_dev->props && ir_dev->props->driver_type == RC_DRIVER_IR_RAW) ?
709 " in raw mode" : "");
712 * Default delay of 250ms is too short for some protocols, expecially
713 * since the timeout is currently set to 250ms. Increase it to 500ms,
714 * to avoid wrong repetition of the keycodes.
716 input_dev->rep[REP_DELAY] = 500;
721 ir_unregister_class(input_dev);
723 ir_free_table(&ir_dev->rc_tab);
725 kfree(ir_dev->driver_name);
730 EXPORT_SYMBOL_GPL(__ir_input_register);
733 * ir_input_unregister() - unregisters IR and frees resources
734 * @input_dev: the struct input_dev descriptor of the device
736 * This routine is used to free memory and de-register interfaces.
738 void ir_input_unregister(struct input_dev *input_dev)
740 struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
745 IR_dprintk(1, "Freed keycode table\n");
747 del_timer_sync(&ir_dev->timer_keyup);
749 if (ir_dev->props->driver_type == RC_DRIVER_IR_RAW)
750 ir_raw_event_unregister(input_dev);
752 ir_free_table(&ir_dev->rc_tab);
754 ir_unregister_class(input_dev);
756 kfree(ir_dev->driver_name);
759 EXPORT_SYMBOL_GPL(ir_input_unregister);
761 int ir_core_debug; /* ir_debug level (0,1,2) */
762 EXPORT_SYMBOL_GPL(ir_core_debug);
763 module_param_named(debug, ir_core_debug, int, 0644);
765 MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
766 MODULE_LICENSE("GPL");