Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 7fee03e4 authored by Mauro Carvalho Chehab's avatar Mauro Carvalho Chehab
Browse files

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: default avatarMauro Carvalho Chehab <mchehab@redhat.com>
parent a53e2125
Loading
Loading
Loading
Loading
+75 −62
Original line number Diff line number Diff line
@@ -9,6 +9,38 @@

#define IR_TAB_MIN_SIZE	32

/**
 * ir_seek_table() - returns the element order on the table
 * @rc_tab:	the ir_scancode_table with the keymap to be used
 * @scancode:	the scancode that we're seeking
 *
 * This routine is used by the input routines when a key is pressed at the
 * IR. The scancode is received and needs to be converted into a keycode.
 * If the key is not found, it returns KEY_UNKNOWN. Otherwise, returns the
 * corresponding keycode from the table.
 */
static int ir_seek_table(struct ir_scancode_table *rc_tab, u32 scancode)
{
	int rc;
	unsigned long flags;
	struct ir_scancode *keymap = rc_tab->scan;

	spin_lock_irqsave(&rc_tab->lock, flags);

	/* FIXME: replace it by a binary search */

	for (rc = 0; rc < rc_tab->size; rc++)
		if (keymap[rc].scancode == scancode)
			goto exit;

	/* Not found */
	rc = -EINVAL;

exit:
	spin_unlock_irqrestore(&rc_tab->lock, flags);
	return rc;
}

/**
 * ir_roundup_tablesize() - gets an optimum value for the table size
 * @n_elems:		minimum number of entries to store keycodes
@@ -54,21 +86,20 @@ int ir_copy_table(struct ir_scancode_table *destin,
	int i, j = 0;

	for (i = 0; i < origin->size; i++) {
		if (origin->scan[i].keycode != KEY_UNKNOWN &&
		   origin->scan[i].keycode != KEY_RESERVED) {
			memcpy(&destin->scan[j], &origin->scan[i],
			       sizeof(struct ir_scancode));
		if (origin->scan[i].keycode == KEY_UNKNOWN ||
		   origin->scan[i].keycode == KEY_RESERVED)
			continue;

		memcpy(&destin->scan[j], &origin->scan[i], sizeof(struct ir_scancode));
		j++;
	}
	}
	destin->size = j;

	IR_dprintk(1, "Copied %d scancodes to the new keycode table\n", j);
	IR_dprintk(1, "Copied %d scancodes to the new keycode table\n", destin->size);

	return 0;
}


/**
 * ir_getkeycode() - get a keycode at the evdev scancode ->keycode table
 * @dev:	the struct input_dev device descriptor
@@ -81,26 +112,12 @@ int ir_copy_table(struct ir_scancode_table *destin,
static int ir_getkeycode(struct input_dev *dev,
			 int scancode, int *keycode)
{
	int i;
	int elem;
	struct ir_scancode_table *rc_tab = input_get_drvdata(dev);
	struct ir_scancode *keymap = rc_tab->scan;

	/* See if we can match the raw key code. */
	for (i = 0; i < rc_tab->size; i++)
		if (keymap[i].scancode == scancode) {
			*keycode = keymap[i].keycode;
			return 0;
		}

	/*
	 * If is there extra space, returns KEY_RESERVED,
	 * otherwise, input core won't let ir_setkeycode
	 * to work
	 */
	for (i = 0; i < rc_tab->size; i++)
		if (keymap[i].keycode == KEY_RESERVED ||
		    keymap[i].keycode == KEY_UNKNOWN) {
			*keycode = KEY_RESERVED;
	elem = ir_seek_table(rc_tab, scancode);
	if (elem >= 0) {
		*keycode = rc_tab->scan[elem].keycode;
		return 0;
	}

@@ -120,40 +137,33 @@ static int ir_getkeycode(struct input_dev *dev,
static int ir_setkeycode(struct input_dev *dev,
			 int scancode, int keycode)
{
	int i;
	int rc = 0;
	struct ir_scancode_table *rc_tab = input_get_drvdata(dev);
	struct ir_scancode *keymap = rc_tab->scan;
	unsigned long flags;

	/* Search if it is replacing an existing keycode */
	for (i = 0; i < rc_tab->size; i++)
		if (keymap[i].scancode == scancode) {
			keymap[i].keycode = keycode;
			return 0;
		}
	rc = ir_seek_table(rc_tab, scancode);
	if (rc <0)
		return rc;

	/* Search if is there a clean entry. If so, use it */
	for (i = 0; i < rc_tab->size; i++)
		if (keymap[i].keycode == KEY_RESERVED ||
		    keymap[i].keycode == KEY_UNKNOWN) {
			keymap[i].scancode = scancode;
			keymap[i].keycode = keycode;
			return 0;
		}
	IR_dprintk(1, "#%d: Replacing scan 0x%04x with key 0x%04x\n",
		rc, scancode, keycode);

	/*
	 * FIXME: Currently, it is not possible to increase the size of
	 * scancode table. For it to happen, one possibility
	 * would be to allocate a table with key_map_size + 1,
	 * copying data, appending the new key on it, and freeing
	 * the old one - or maybe just allocating some spare space
	 */
	clear_bit(keymap[rc].keycode, dev->keybit);

	return -EINVAL;
	spin_lock_irqsave(&rc_tab->lock, flags);
	keymap[rc].keycode = keycode;
	spin_unlock_irqrestore(&rc_tab->lock, flags);

	set_bit(keycode, dev->keybit);

	return 0;
}

/**
 * ir_g_keycode_from_table() - gets the keycode that corresponds to a scancode
 * @rc_tab:	the ir_scancode_table with the keymap to be used
 * @input_dev:	the struct input_dev descriptor of the device
 * @scancode:	the scancode that we're seeking
 *
 * 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,
 */
u32 ir_g_keycode_from_table(struct input_dev *dev, u32 scancode)
{
	int i;
	struct ir_scancode_table *rc_tab = input_get_drvdata(dev);
	struct ir_scancode *keymap = rc_tab->scan;
	int elem;

	for (i = 0; i < rc_tab->size; i++)
		if (keymap[i].scancode == scancode) {
	elem = ir_seek_table(rc_tab, scancode);
	if (elem >= 0) {
		IR_dprintk(1, "%s: scancode 0x%04x keycode 0x%02x\n",
				dev->name, scancode, keymap[i].keycode);
			   dev->name, scancode, keymap[elem].keycode);

			return keymap[i].keycode;
		return rc_tab->scan[elem].keycode;
	}

	printk(KERN_INFO "%s: unknown key for scancode 0x%04x\n",
	       dev->name, scancode);

	return KEY_UNKNOWN;
	/* Reports userspace that an unknown keycode were got */
	return KEY_RESERVED;
}

/**
@@ -188,8 +199,8 @@ u32 ir_g_keycode_from_table(struct input_dev *dev, u32 scancode)
 * @rc_tab:	the struct ir_scancode_table table of scancode/keymap
 *
 * This routine is used to initialize the input infrastructure to work with
 * an IR. It requires that the caller initializes the input_dev struct with
 * some fields: name,
 * an IR.
 * It should be called before registering the IR device.
 */
int ir_set_keycode_table(struct input_dev *input_dev,
			 struct ir_scancode_table *rc_tab)
@@ -197,6 +208,8 @@ int ir_set_keycode_table(struct input_dev *input_dev,
	struct ir_scancode *keymap = rc_tab->scan;
	int i;

	spin_lock_init(&rc_tab->lock);

	if (rc_tab->scan == NULL || !rc_tab->size)
		return -EINVAL;

+2 −0
Original line number Diff line number Diff line
@@ -26,6 +26,7 @@
#include <linux/input.h>
#include <linux/workqueue.h>
#include <linux/interrupt.h>
#include <linux/spinlock.h>

extern int media_ir_debug;    /* media_ir_debug level (0,1,2) */
#define IR_dprintk(level, fmt, arg...)	if (media_ir_debug >= level) \
@@ -43,6 +44,7 @@ struct ir_scancode {
struct ir_scancode_table {
	struct ir_scancode *scan;
	int size;
	spinlock_t lock;
};

#define RC5_START(x)	(((x)>>12)&3)