2009-11-27 18:01:23 -07:00
|
|
|
/* ir-register.c - handle IR scancode->keycode tables
|
|
|
|
*
|
|
|
|
* Copyright (C) 2009 by Mauro Carvalho Chehab <mchehab@redhat.com>
|
2009-12-11 04:34:07 -07:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation version 2 of the License.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2009-11-27 18:01:23 -07:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
2009-12-29 06:37:38 -07:00
|
|
|
#include <linux/input.h>
|
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit slab.h inclusion from percpu.h
percpu.h is included by sched.h and module.h and thus ends up being
included when building most .c files. percpu.h includes slab.h which
in turn includes gfp.h making everything defined by the two files
universally available and complicating inclusion dependencies.
percpu.h -> slab.h dependency is about to be removed. Prepare for
this change by updating users of gfp and slab facilities include those
headers directly instead of assuming availability. As this conversion
needs to touch large number of source files, the following script is
used as the basis of conversion.
http://userweb.kernel.org/~tj/misc/slabh-sweep.py
The script does the followings.
* Scan files for gfp and slab usages and update includes such that
only the necessary includes are there. ie. if only gfp is used,
gfp.h, if slab is used, slab.h.
* When the script inserts a new include, it looks at the include
blocks and try to put the new include such that its order conforms
to its surrounding. It's put in the include block which contains
core kernel includes, in the same order that the rest are ordered -
alphabetical, Christmas tree, rev-Xmas-tree or at the end if there
doesn't seem to be any matching order.
* If the script can't find a place to put a new include (mostly
because the file doesn't have fitting include block), it prints out
an error message indicating which .h file needs to be added to the
file.
The conversion was done in the following steps.
1. The initial automatic conversion of all .c files updated slightly
over 4000 files, deleting around 700 includes and adding ~480 gfp.h
and ~3000 slab.h inclusions. The script emitted errors for ~400
files.
2. Each error was manually checked. Some didn't need the inclusion,
some needed manual addition while adding it to implementation .h or
embedding .c file was more appropriate for others. This step added
inclusions to around 150 files.
3. The script was run again and the output was compared to the edits
from #2 to make sure no file was left behind.
4. Several build tests were done and a couple of problems were fixed.
e.g. lib/decompress_*.c used malloc/free() wrappers around slab
APIs requiring slab.h to be added manually.
5. The script was run on all .h files but without automatically
editing them as sprinkling gfp.h and slab.h inclusions around .h
files could easily lead to inclusion dependency hell. Most gfp.h
inclusion directives were ignored as stuff from gfp.h was usually
wildly available and often used in preprocessor macros. Each
slab.h inclusion directive was examined and added manually as
necessary.
6. percpu.h was updated not to include slab.h.
7. Build test were done on the following configurations and failures
were fixed. CONFIG_GCOV_KERNEL was turned off for all tests (as my
distributed build env didn't work with gcov compiles) and a few
more options had to be turned off depending on archs to make things
build (like ipr on powerpc/64 which failed due to missing writeq).
* x86 and x86_64 UP and SMP allmodconfig and a custom test config.
* powerpc and powerpc64 SMP allmodconfig
* sparc and sparc64 SMP allmodconfig
* ia64 SMP allmodconfig
* s390 SMP allmodconfig
* alpha SMP allmodconfig
* um on x86_64 SMP allmodconfig
8. percpu.h modifications were reverted so that it could be applied as
a separate patch and serve as bisection point.
Given the fact that I had only a couple of failures from tests on step
6, I'm fairly confident about the coverage of this conversion patch.
If there is a breakage, it's likely to be something in one of the arch
headers which should be easily discoverable easily on most builds of
the specific arch.
Signed-off-by: Tejun Heo <tj@kernel.org>
Guess-its-ok-by: Christoph Lameter <cl@linux-foundation.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Lee Schermerhorn <Lee.Schermerhorn@hp.com>
2010-03-24 01:04:11 -07:00
|
|
|
#include <linux/slab.h>
|
2009-11-27 18:01:23 -07:00
|
|
|
#include <media/ir-common.h>
|
|
|
|
|
2009-11-29 07:08:02 -07:00
|
|
|
#define IR_TAB_MIN_SIZE 32
|
2009-12-04 13:17:47 -07:00
|
|
|
#define IR_TAB_MAX_SIZE 1024
|
2009-11-29 07:08:02 -07:00
|
|
|
|
2009-12-02 11:56:47 -07:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
|
2009-11-29 07:08:02 -07:00
|
|
|
/**
|
|
|
|
* ir_roundup_tablesize() - gets an optimum value for the table size
|
|
|
|
* @n_elems: minimum number of entries to store keycodes
|
|
|
|
*
|
|
|
|
* This routine is used to choose the keycode table size.
|
|
|
|
*
|
|
|
|
* In order to have some empty space for new keycodes,
|
|
|
|
* and knowing in advance that kmalloc allocates only power of two
|
|
|
|
* segments, it optimizes the allocated space to have some spare space
|
|
|
|
* for those new keycodes by using the maximum number of entries that
|
|
|
|
* will be effectively be allocated by kmalloc.
|
|
|
|
* In order to reduce the quantity of table resizes, it has a minimum
|
|
|
|
* table size of IR_TAB_MIN_SIZE.
|
|
|
|
*/
|
2009-12-17 05:24:37 -07:00
|
|
|
static int ir_roundup_tablesize(int n_elems)
|
2009-11-29 07:08:02 -07:00
|
|
|
{
|
|
|
|
size_t size;
|
|
|
|
|
|
|
|
if (n_elems < IR_TAB_MIN_SIZE)
|
|
|
|
n_elems = IR_TAB_MIN_SIZE;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* As kmalloc only allocates sizes of power of two, get as
|
|
|
|
* much entries as possible for the allocated memory segment
|
|
|
|
*/
|
|
|
|
size = roundup_pow_of_two(n_elems * sizeof(struct ir_scancode));
|
|
|
|
n_elems = size / sizeof(struct ir_scancode);
|
|
|
|
|
|
|
|
return n_elems;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ir_copy_table() - copies a keytable, discarding the unused entries
|
|
|
|
* @destin: destin table
|
|
|
|
* @origin: origin table
|
|
|
|
*
|
|
|
|
* Copies all entries where the keycode is not KEY_UNKNOWN/KEY_RESERVED
|
2009-12-13 20:16:55 -07:00
|
|
|
* Also copies table size and table protocol.
|
|
|
|
* NOTE: It shouldn't copy the lock field
|
2009-11-29 07:08:02 -07:00
|
|
|
*/
|
|
|
|
|
2009-12-17 05:24:37 -07:00
|
|
|
static int ir_copy_table(struct ir_scancode_table *destin,
|
2009-11-29 07:08:02 -07:00
|
|
|
const struct ir_scancode_table *origin)
|
|
|
|
{
|
|
|
|
int i, j = 0;
|
|
|
|
|
|
|
|
for (i = 0; i < origin->size; i++) {
|
2009-12-02 11:56:47 -07:00
|
|
|
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++;
|
2009-11-29 07:08:02 -07:00
|
|
|
}
|
|
|
|
destin->size = j;
|
2009-12-13 20:16:55 -07:00
|
|
|
destin->ir_type = origin->ir_type;
|
2009-11-29 07:08:02 -07:00
|
|
|
|
2009-12-02 11:56:47 -07:00
|
|
|
IR_dprintk(1, "Copied %d scancodes to the new keycode table\n", destin->size);
|
2009-11-29 07:08:02 -07:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-11-27 18:01:23 -07:00
|
|
|
/**
|
|
|
|
* ir_getkeycode() - get a keycode at the evdev scancode ->keycode table
|
|
|
|
* @dev: the struct input_dev device descriptor
|
|
|
|
* @scancode: the desired scancode
|
|
|
|
* @keycode: the keycode to be retorned.
|
|
|
|
*
|
|
|
|
* This routine is used to handle evdev EVIOCGKEY ioctl.
|
|
|
|
* If the key is not found, returns -EINVAL, otherwise, returns 0.
|
|
|
|
*/
|
|
|
|
static int ir_getkeycode(struct input_dev *dev,
|
2010-03-08 23:37:10 -07:00
|
|
|
unsigned int scancode, unsigned int *keycode)
|
2009-11-27 18:01:23 -07:00
|
|
|
{
|
2009-12-02 11:56:47 -07:00
|
|
|
int elem;
|
2009-12-11 05:44:23 -07:00
|
|
|
struct ir_input_dev *ir_dev = input_get_drvdata(dev);
|
|
|
|
struct ir_scancode_table *rc_tab = &ir_dev->rc_tab;
|
2009-11-27 18:01:23 -07:00
|
|
|
|
2009-12-02 11:56:47 -07:00
|
|
|
elem = ir_seek_table(rc_tab, scancode);
|
|
|
|
if (elem >= 0) {
|
|
|
|
*keycode = rc_tab->scan[elem].keycode;
|
|
|
|
return 0;
|
|
|
|
}
|
2009-11-27 18:01:23 -07:00
|
|
|
|
2009-12-04 13:17:47 -07:00
|
|
|
/*
|
|
|
|
* Scancode not found and table can't be expanded
|
|
|
|
*/
|
|
|
|
if (elem < 0 && rc_tab->size == IR_TAB_MAX_SIZE)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If is there extra space, returns KEY_RESERVED,
|
|
|
|
* otherwise, input core won't let ir_setkeycode to work
|
|
|
|
*/
|
|
|
|
*keycode = KEY_RESERVED;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ir_is_resize_needed() - Check if the table needs rezise
|
|
|
|
* @table: keycode table that may need to resize
|
|
|
|
* @n_elems: minimum number of entries to store keycodes
|
|
|
|
*
|
|
|
|
* Considering that kmalloc uses power of two storage areas, this
|
|
|
|
* routine detects if the real alloced size will change. If not, it
|
|
|
|
* just returns without doing nothing. Otherwise, it will extend or
|
|
|
|
* reduce the table size to meet the new needs.
|
|
|
|
*
|
|
|
|
* It returns 0 if no resize is needed, 1 otherwise.
|
|
|
|
*/
|
|
|
|
static int ir_is_resize_needed(struct ir_scancode_table *table, int n_elems)
|
|
|
|
{
|
|
|
|
int cur_size = ir_roundup_tablesize(table->size);
|
|
|
|
int new_size = ir_roundup_tablesize(n_elems);
|
|
|
|
|
|
|
|
if (cur_size == new_size)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* Resize is needed */
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ir_delete_key() - remove a keycode from the table
|
|
|
|
* @rc_tab: keycode table
|
|
|
|
* @elem: element to be removed
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
static void ir_delete_key(struct ir_scancode_table *rc_tab, int elem)
|
|
|
|
{
|
|
|
|
unsigned long flags = 0;
|
|
|
|
int newsize = rc_tab->size - 1;
|
|
|
|
int resize = ir_is_resize_needed(rc_tab, newsize);
|
|
|
|
struct ir_scancode *oldkeymap = rc_tab->scan;
|
2009-12-29 04:25:13 -07:00
|
|
|
struct ir_scancode *newkeymap = NULL;
|
2009-12-04 13:17:47 -07:00
|
|
|
|
2009-12-29 04:25:13 -07:00
|
|
|
if (resize)
|
2009-12-04 13:17:47 -07:00
|
|
|
newkeymap = kzalloc(ir_roundup_tablesize(newsize) *
|
|
|
|
sizeof(*newkeymap), GFP_ATOMIC);
|
|
|
|
|
2009-12-29 04:25:13 -07:00
|
|
|
/* There's no memory for resize. Keep the old table */
|
|
|
|
if (!resize || !newkeymap) {
|
2009-12-04 13:17:47 -07:00
|
|
|
newkeymap = oldkeymap;
|
|
|
|
|
|
|
|
/* We'll modify the live table. Lock it */
|
|
|
|
spin_lock_irqsave(&rc_tab->lock, flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copy the elements before the one that will be deleted
|
|
|
|
* if (!resize), both oldkeymap and newkeymap points
|
|
|
|
* to the same place, so, there's no need to copy
|
|
|
|
*/
|
|
|
|
if (resize && elem > 0)
|
|
|
|
memcpy(newkeymap, oldkeymap,
|
|
|
|
elem * sizeof(*newkeymap));
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copy the other elements overwriting the element to be removed
|
|
|
|
* This operation applies to both resize and non-resize case
|
|
|
|
*/
|
|
|
|
if (elem < newsize)
|
|
|
|
memcpy(&newkeymap[elem], &oldkeymap[elem + 1],
|
|
|
|
(newsize - elem) * sizeof(*newkeymap));
|
|
|
|
|
|
|
|
if (resize) {
|
|
|
|
/*
|
|
|
|
* As the copy happened to a temporary table, only here
|
|
|
|
* it needs to lock while replacing the table pointers
|
|
|
|
* to use the new table
|
|
|
|
*/
|
|
|
|
spin_lock_irqsave(&rc_tab->lock, flags);
|
|
|
|
rc_tab->size = newsize;
|
|
|
|
rc_tab->scan = newkeymap;
|
|
|
|
spin_unlock_irqrestore(&rc_tab->lock, flags);
|
|
|
|
|
|
|
|
/* Frees the old keytable */
|
|
|
|
kfree(oldkeymap);
|
|
|
|
} else {
|
|
|
|
rc_tab->size = newsize;
|
|
|
|
spin_unlock_irqrestore(&rc_tab->lock, flags);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ir_insert_key() - insert a keycode at the table
|
|
|
|
* @rc_tab: keycode table
|
|
|
|
* @scancode: the desired scancode
|
|
|
|
* @keycode: the keycode to be retorned.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
static int ir_insert_key(struct ir_scancode_table *rc_tab,
|
|
|
|
int scancode, int keycode)
|
|
|
|
{
|
|
|
|
unsigned long flags;
|
|
|
|
int elem = rc_tab->size;
|
|
|
|
int newsize = rc_tab->size + 1;
|
|
|
|
int resize = ir_is_resize_needed(rc_tab, newsize);
|
|
|
|
struct ir_scancode *oldkeymap = rc_tab->scan;
|
|
|
|
struct ir_scancode *newkeymap;
|
|
|
|
|
|
|
|
if (resize) {
|
|
|
|
newkeymap = kzalloc(ir_roundup_tablesize(newsize) *
|
|
|
|
sizeof(*newkeymap), GFP_ATOMIC);
|
|
|
|
if (!newkeymap)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
memcpy(newkeymap, oldkeymap,
|
|
|
|
rc_tab->size * sizeof(*newkeymap));
|
|
|
|
} else
|
|
|
|
newkeymap = oldkeymap;
|
|
|
|
|
|
|
|
/* Stores the new code at the table */
|
|
|
|
IR_dprintk(1, "#%d: New scan 0x%04x with key 0x%04x\n",
|
|
|
|
rc_tab->size, scancode, keycode);
|
|
|
|
|
|
|
|
spin_lock_irqsave(&rc_tab->lock, flags);
|
|
|
|
rc_tab->size = newsize;
|
|
|
|
if (resize) {
|
|
|
|
rc_tab->scan = newkeymap;
|
|
|
|
kfree(oldkeymap);
|
|
|
|
}
|
|
|
|
newkeymap[elem].scancode = scancode;
|
|
|
|
newkeymap[elem].keycode = keycode;
|
|
|
|
spin_unlock_irqrestore(&rc_tab->lock, flags);
|
|
|
|
|
|
|
|
return 0;
|
2009-11-27 18:01:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ir_setkeycode() - set a keycode at the evdev scancode ->keycode table
|
|
|
|
* @dev: the struct input_dev device descriptor
|
|
|
|
* @scancode: the desired scancode
|
|
|
|
* @keycode: the keycode to be retorned.
|
|
|
|
*
|
|
|
|
* This routine is used to handle evdev EVIOCSKEY ioctl.
|
|
|
|
* There's one caveat here: how can we increase the size of the table?
|
|
|
|
* If the key is not found, returns -EINVAL, otherwise, returns 0.
|
|
|
|
*/
|
|
|
|
static int ir_setkeycode(struct input_dev *dev,
|
2010-03-08 23:37:10 -07:00
|
|
|
unsigned int scancode, unsigned int keycode)
|
2009-11-27 18:01:23 -07:00
|
|
|
{
|
2009-12-02 11:56:47 -07:00
|
|
|
int rc = 0;
|
2009-12-11 05:44:23 -07:00
|
|
|
struct ir_input_dev *ir_dev = input_get_drvdata(dev);
|
|
|
|
struct ir_scancode_table *rc_tab = &ir_dev->rc_tab;
|
2009-11-27 18:01:23 -07:00
|
|
|
struct ir_scancode *keymap = rc_tab->scan;
|
2009-12-02 11:56:47 -07:00
|
|
|
unsigned long flags;
|
2009-11-27 18:01:23 -07:00
|
|
|
|
2009-12-04 13:17:47 -07:00
|
|
|
/*
|
|
|
|
* Handle keycode table deletions
|
|
|
|
*
|
|
|
|
* If userspace is adding a KEY_UNKNOWN or KEY_RESERVED,
|
|
|
|
* deal as a trial to remove an existing scancode attribution
|
|
|
|
* if table become too big, reduce it to save space
|
|
|
|
*/
|
|
|
|
if (keycode == KEY_UNKNOWN || keycode == KEY_RESERVED) {
|
|
|
|
rc = ir_seek_table(rc_tab, scancode);
|
|
|
|
if (rc < 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
IR_dprintk(1, "#%d: Deleting scan 0x%04x\n", rc, scancode);
|
|
|
|
clear_bit(keymap[rc].keycode, dev->keybit);
|
|
|
|
ir_delete_key(rc_tab, rc);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Handle keycode replacements
|
|
|
|
*
|
|
|
|
* If the scancode exists, just replace by the new value
|
|
|
|
*/
|
2009-12-02 11:56:47 -07:00
|
|
|
rc = ir_seek_table(rc_tab, scancode);
|
2009-12-04 13:17:47 -07:00
|
|
|
if (rc >= 0) {
|
|
|
|
IR_dprintk(1, "#%d: Replacing scan 0x%04x with key 0x%04x\n",
|
|
|
|
rc, scancode, keycode);
|
2009-11-27 18:01:23 -07:00
|
|
|
|
2009-12-04 13:17:47 -07:00
|
|
|
clear_bit(keymap[rc].keycode, dev->keybit);
|
2009-11-27 18:01:23 -07:00
|
|
|
|
2009-12-04 13:17:47 -07:00
|
|
|
spin_lock_irqsave(&rc_tab->lock, flags);
|
|
|
|
keymap[rc].keycode = keycode;
|
|
|
|
spin_unlock_irqrestore(&rc_tab->lock, flags);
|
2009-12-02 11:56:47 -07:00
|
|
|
|
2009-12-04 13:17:47 -07:00
|
|
|
set_bit(keycode, dev->keybit);
|
2009-12-02 11:56:47 -07:00
|
|
|
|
2009-12-04 13:17:47 -07:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Handle new scancode inserts
|
|
|
|
*
|
|
|
|
* reallocate table if needed and insert a new keycode
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Avoid growing the table indefinitely */
|
|
|
|
if (rc_tab->size + 1 > IR_TAB_MAX_SIZE)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
rc = ir_insert_key(rc_tab, scancode, keycode);
|
|
|
|
if (rc < 0)
|
|
|
|
return rc;
|
2009-12-02 11:56:47 -07:00
|
|
|
set_bit(keycode, dev->keybit);
|
|
|
|
|
|
|
|
return 0;
|
2009-11-27 18:01:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ir_g_keycode_from_table() - gets the keycode that corresponds to a scancode
|
2009-12-02 11:56:47 -07:00
|
|
|
* @input_dev: the struct input_dev descriptor of the device
|
2009-11-27 18:01:23 -07:00
|
|
|
* @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.
|
2010-03-21 08:15:16 -07:00
|
|
|
* If the key is not found, it returns KEY_RESERVED. Otherwise, returns the
|
2009-11-27 18:01:23 -07:00
|
|
|
* corresponding keycode from the table.
|
|
|
|
*/
|
|
|
|
u32 ir_g_keycode_from_table(struct input_dev *dev, u32 scancode)
|
|
|
|
{
|
2009-12-11 05:44:23 -07:00
|
|
|
struct ir_input_dev *ir_dev = input_get_drvdata(dev);
|
|
|
|
struct ir_scancode_table *rc_tab = &ir_dev->rc_tab;
|
2009-11-27 18:01:23 -07:00
|
|
|
struct ir_scancode *keymap = rc_tab->scan;
|
2009-12-02 11:56:47 -07:00
|
|
|
int elem;
|
2009-11-27 18:01:23 -07:00
|
|
|
|
2009-12-02 11:56:47 -07:00
|
|
|
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[elem].keycode);
|
2009-11-27 18:01:23 -07:00
|
|
|
|
2009-12-02 11:56:47 -07:00
|
|
|
return rc_tab->scan[elem].keycode;
|
|
|
|
}
|
2009-11-27 18:01:23 -07:00
|
|
|
|
|
|
|
printk(KERN_INFO "%s: unknown key for scancode 0x%04x\n",
|
|
|
|
dev->name, scancode);
|
|
|
|
|
2009-12-02 11:56:47 -07:00
|
|
|
/* Reports userspace that an unknown keycode were got */
|
|
|
|
return KEY_RESERVED;
|
2009-11-27 18:01:23 -07:00
|
|
|
}
|
2009-12-11 04:34:07 -07:00
|
|
|
EXPORT_SYMBOL_GPL(ir_g_keycode_from_table);
|
2009-11-27 18:01:23 -07:00
|
|
|
|
2010-03-21 08:15:16 -07:00
|
|
|
/**
|
|
|
|
* ir_keyup() - generates input event to cleanup a key press
|
|
|
|
* @input_dev: the struct input_dev descriptor of the device
|
|
|
|
*
|
|
|
|
* This routine is used by the input routines when a key is pressed at the
|
|
|
|
* IR. It reports a keyup input event via input_report_key().
|
|
|
|
*/
|
|
|
|
void ir_keyup(struct input_dev *dev)
|
|
|
|
{
|
|
|
|
struct ir_input_dev *ir = input_get_drvdata(dev);
|
|
|
|
|
|
|
|
if (!ir->keypressed)
|
|
|
|
return;
|
|
|
|
|
2010-03-21 09:00:55 -07:00
|
|
|
IR_dprintk(1, "keyup key 0x%04x\n", ir->keycode);
|
2010-03-21 08:15:16 -07:00
|
|
|
input_report_key(dev, ir->keycode, 0);
|
|
|
|
input_sync(dev);
|
|
|
|
ir->keypressed = 0;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(ir_keyup);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ir_keydown() - generates input event for a key press
|
|
|
|
* @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
|
|
|
|
* IR. It gets the keycode for a scancode and reports an input event via
|
|
|
|
* input_report_key().
|
|
|
|
*/
|
|
|
|
void ir_keydown(struct input_dev *dev, int scancode)
|
|
|
|
{
|
|
|
|
struct ir_input_dev *ir = input_get_drvdata(dev);
|
|
|
|
|
|
|
|
u32 keycode = ir_g_keycode_from_table(dev, scancode);
|
|
|
|
|
|
|
|
/* If already sent a keydown, do a keyup */
|
|
|
|
if (ir->keypressed)
|
|
|
|
ir_keyup(dev);
|
|
|
|
|
|
|
|
if (KEY_RESERVED == keycode)
|
|
|
|
return;
|
|
|
|
|
|
|
|
ir->keycode = keycode;
|
|
|
|
ir->keypressed = 1;
|
|
|
|
|
|
|
|
IR_dprintk(1, "%s: key down event, key 0x%04x, scancode 0x%04x\n",
|
|
|
|
dev->name, keycode, scancode);
|
|
|
|
|
|
|
|
input_report_key(dev, ir->keycode, 1);
|
|
|
|
input_sync(dev);
|
|
|
|
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(ir_keydown);
|
|
|
|
|
2010-03-31 10:40:35 -07:00
|
|
|
static int ir_open(struct input_dev *input_dev)
|
|
|
|
{
|
|
|
|
struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
|
|
|
|
|
|
|
|
return ir_dev->props->open(ir_dev->props->priv);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ir_close(struct input_dev *input_dev)
|
|
|
|
{
|
|
|
|
struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
|
|
|
|
|
|
|
|
ir_dev->props->close(ir_dev->props->priv);
|
|
|
|
}
|
2010-03-21 08:15:16 -07:00
|
|
|
|
2009-11-27 18:01:23 -07:00
|
|
|
/**
|
2009-12-11 05:44:23 -07:00
|
|
|
* ir_input_register() - sets the IR keycode table and add the handlers
|
2009-11-27 18:01:23 -07:00
|
|
|
* for keymap table get/set
|
|
|
|
* @input_dev: the struct input_dev descriptor of the device
|
|
|
|
* @rc_tab: the struct ir_scancode_table table of scancode/keymap
|
|
|
|
*
|
2009-12-13 22:55:03 -07:00
|
|
|
* This routine is used to initialize the input infrastructure
|
|
|
|
* to work with an IR.
|
|
|
|
* It will register the input/evdev interface for the device and
|
|
|
|
* register the syfs code for IR class
|
2009-11-27 18:01:23 -07:00
|
|
|
*/
|
2009-12-11 05:44:23 -07:00
|
|
|
int ir_input_register(struct input_dev *input_dev,
|
2009-12-13 20:16:55 -07:00
|
|
|
const struct ir_scancode_table *rc_tab,
|
2010-03-12 17:18:14 -07:00
|
|
|
const struct ir_dev_props *props,
|
|
|
|
const char *driver_name)
|
2009-11-27 18:01:23 -07:00
|
|
|
{
|
2009-12-11 05:44:23 -07:00
|
|
|
struct ir_input_dev *ir_dev;
|
|
|
|
struct ir_scancode *keymap = rc_tab->scan;
|
2009-12-11 07:20:59 -07:00
|
|
|
int i, rc;
|
2009-11-27 18:01:23 -07:00
|
|
|
|
|
|
|
if (rc_tab->scan == NULL || !rc_tab->size)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2009-12-11 05:44:23 -07:00
|
|
|
ir_dev = kzalloc(sizeof(*ir_dev), GFP_KERNEL);
|
|
|
|
if (!ir_dev)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2009-12-13 20:16:55 -07:00
|
|
|
spin_lock_init(&ir_dev->rc_tab.lock);
|
2009-12-11 05:44:23 -07:00
|
|
|
|
2010-03-12 17:18:14 -07:00
|
|
|
ir_dev->driver_name = kmalloc(strlen(driver_name) + 1, GFP_KERNEL);
|
|
|
|
if (!ir_dev->driver_name)
|
|
|
|
return -ENOMEM;
|
|
|
|
strcpy(ir_dev->driver_name, driver_name);
|
2010-03-12 07:50:17 -07:00
|
|
|
ir_dev->rc_tab.name = rc_tab->name;
|
2009-12-11 05:44:23 -07:00
|
|
|
ir_dev->rc_tab.size = ir_roundup_tablesize(rc_tab->size);
|
|
|
|
ir_dev->rc_tab.scan = kzalloc(ir_dev->rc_tab.size *
|
|
|
|
sizeof(struct ir_scancode), GFP_KERNEL);
|
2010-01-09 09:51:14 -07:00
|
|
|
if (!ir_dev->rc_tab.scan) {
|
|
|
|
kfree(ir_dev);
|
2009-12-11 05:44:23 -07:00
|
|
|
return -ENOMEM;
|
2010-01-09 09:51:14 -07:00
|
|
|
}
|
2009-12-11 05:44:23 -07:00
|
|
|
|
|
|
|
IR_dprintk(1, "Allocated space for %d keycode entries (%zd bytes)\n",
|
|
|
|
ir_dev->rc_tab.size,
|
|
|
|
ir_dev->rc_tab.size * sizeof(ir_dev->rc_tab.scan));
|
|
|
|
|
|
|
|
ir_copy_table(&ir_dev->rc_tab, rc_tab);
|
2009-12-13 20:16:55 -07:00
|
|
|
ir_dev->props = props;
|
2010-03-31 10:40:35 -07:00
|
|
|
if (props && props->open)
|
|
|
|
input_dev->open = ir_open;
|
|
|
|
if (props && props->close)
|
|
|
|
input_dev->close = ir_close;
|
2009-12-11 05:44:23 -07:00
|
|
|
|
2009-11-27 18:01:23 -07:00
|
|
|
/* set the bits for the keys */
|
|
|
|
IR_dprintk(1, "key map size: %d\n", rc_tab->size);
|
|
|
|
for (i = 0; i < rc_tab->size; i++) {
|
|
|
|
IR_dprintk(1, "#%d: setting bit for keycode 0x%04x\n",
|
|
|
|
i, keymap[i].keycode);
|
|
|
|
set_bit(keymap[i].keycode, input_dev->keybit);
|
|
|
|
}
|
2009-12-11 05:44:23 -07:00
|
|
|
clear_bit(0, input_dev->keybit);
|
|
|
|
|
|
|
|
set_bit(EV_KEY, input_dev->evbit);
|
2009-11-27 18:01:23 -07:00
|
|
|
|
|
|
|
input_dev->getkeycode = ir_getkeycode;
|
|
|
|
input_dev->setkeycode = ir_setkeycode;
|
2009-12-11 05:44:23 -07:00
|
|
|
input_set_drvdata(input_dev, ir_dev);
|
2009-11-27 18:01:23 -07:00
|
|
|
|
2009-12-13 12:00:08 -07:00
|
|
|
rc = ir_register_class(input_dev);
|
2010-03-11 08:41:56 -07:00
|
|
|
if (rc < 0)
|
2009-12-13 12:00:08 -07:00
|
|
|
goto err;
|
2009-12-11 07:20:59 -07:00
|
|
|
|
2009-12-13 12:00:08 -07:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
err:
|
|
|
|
kfree(rc_tab->scan);
|
|
|
|
kfree(ir_dev);
|
2009-12-11 07:20:59 -07:00
|
|
|
return rc;
|
2009-11-27 18:01:23 -07:00
|
|
|
}
|
2009-12-11 05:44:23 -07:00
|
|
|
EXPORT_SYMBOL_GPL(ir_input_register);
|
2009-11-29 07:08:02 -07:00
|
|
|
|
2009-12-13 22:55:03 -07:00
|
|
|
/**
|
|
|
|
* ir_input_unregister() - unregisters IR and frees resources
|
|
|
|
* @input_dev: the struct input_dev descriptor of the device
|
|
|
|
|
|
|
|
* This routine is used to free memory and de-register interfaces.
|
|
|
|
*/
|
2009-12-11 05:47:42 -07:00
|
|
|
void ir_input_unregister(struct input_dev *dev)
|
2009-11-29 07:08:02 -07:00
|
|
|
{
|
2009-12-11 05:44:23 -07:00
|
|
|
struct ir_input_dev *ir_dev = input_get_drvdata(dev);
|
2009-12-11 07:20:59 -07:00
|
|
|
struct ir_scancode_table *rc_tab;
|
2009-11-29 07:08:02 -07:00
|
|
|
|
2009-12-11 07:20:59 -07:00
|
|
|
if (!ir_dev)
|
2009-12-06 04:32:49 -07:00
|
|
|
return;
|
|
|
|
|
2009-11-29 07:08:02 -07:00
|
|
|
IR_dprintk(1, "Freed keycode table\n");
|
|
|
|
|
2009-12-11 07:20:59 -07:00
|
|
|
rc_tab = &ir_dev->rc_tab;
|
2009-11-29 07:08:02 -07:00
|
|
|
rc_tab->size = 0;
|
|
|
|
kfree(rc_tab->scan);
|
|
|
|
rc_tab->scan = NULL;
|
2009-12-11 05:44:23 -07:00
|
|
|
|
2009-12-13 12:00:08 -07:00
|
|
|
ir_unregister_class(dev);
|
|
|
|
|
2009-12-11 05:44:23 -07:00
|
|
|
kfree(ir_dev);
|
2009-11-29 07:08:02 -07:00
|
|
|
}
|
2009-12-11 05:47:42 -07:00
|
|
|
EXPORT_SYMBOL_GPL(ir_input_unregister);
|
2009-11-29 07:08:02 -07:00
|
|
|
|
2009-12-11 04:34:07 -07:00
|
|
|
int ir_core_debug; /* ir_debug level (0,1,2) */
|
|
|
|
EXPORT_SYMBOL_GPL(ir_core_debug);
|
|
|
|
module_param_named(debug, ir_core_debug, int, 0644);
|
|
|
|
|
|
|
|
MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
|
|
|
|
MODULE_LICENSE("GPL");
|