2006-03-03 22:13:42 +07:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* some common structs and functions to handle infrared remotes via
|
|
|
|
* input layer ...
|
|
|
|
*
|
|
|
|
* (c) 2003 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
|
|
|
|
*
|
|
|
|
* 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; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/string.h>
|
2006-12-27 23:04:09 +07:00
|
|
|
#include <linux/jiffies.h>
|
2006-03-03 22:13:42 +07:00
|
|
|
#include <media/ir-common.h>
|
2010-04-09 01:10:27 +07:00
|
|
|
#include "ir-core-priv.h"
|
2006-03-03 22:13:42 +07:00
|
|
|
|
|
|
|
/* -------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
|
|
|
|
MODULE_LICENSE("GPL");
|
|
|
|
|
|
|
|
static int repeat = 1;
|
|
|
|
module_param(repeat, int, 0444);
|
|
|
|
MODULE_PARM_DESC(repeat,"auto-repeat for IR keys (default: on)");
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
static void ir_input_key_event(struct input_dev *dev, struct ir_input_state *ir)
|
|
|
|
{
|
|
|
|
if (KEY_RESERVED == ir->keycode) {
|
2009-11-28 08:40:22 +07:00
|
|
|
printk(KERN_INFO "%s: unknown key: key=0x%02x down=%d\n",
|
|
|
|
dev->name, ir->ir_key, ir->keypressed);
|
2006-03-03 22:13:42 +07:00
|
|
|
return;
|
|
|
|
}
|
2009-11-28 07:54:41 +07:00
|
|
|
IR_dprintk(1,"%s: key event code=%d down=%d\n",
|
2006-03-03 22:13:42 +07:00
|
|
|
dev->name,ir->keycode,ir->keypressed);
|
|
|
|
input_report_key(dev,ir->keycode,ir->keypressed);
|
|
|
|
input_sync(dev);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------------- */
|
|
|
|
|
2009-11-29 18:19:59 +07:00
|
|
|
int ir_input_init(struct input_dev *dev, struct ir_input_state *ir,
|
2009-12-14 23:53:37 +07:00
|
|
|
const u64 ir_type)
|
2006-03-03 22:13:42 +07:00
|
|
|
{
|
|
|
|
ir->ir_type = ir_type;
|
V4L/DVB (12595): common/ir: use a struct for keycode tables
Currently, V4L uses a scancode table whose index is the scancode and
the value is the keycode. While this works, it has some drawbacks:
1) It requires that the scancode to be at the range 00-7f;
2) keycodes should be masked on 7 bits in order for it to work;
3) due to the 7 bits approach, sometimes it is not possible to replace
the default keyboard to another one with a different encoding rule;
4) it is different than what is done with dvb-usb approach;
5) it requires a typedef for it to work. This is not a recommended
Linux CodingStyle.
This patch is part of a larger series of IR changes. It basically
replaces the IR_KEYTAB_TYPE tables by a structured table:
struct ir_scancode {
u16 scancode;
u32 keycode;
};
This is very close to what dvb does. So, a further integration with DVB
code will be easy.
While we've changed the tables, for now, the IR keycode handling is still
based on the old approach.
The only notable effect is the redution of about 35% of the ir-common
module size:
text data bss dec hex filename
6721 29208 4 35933 8c5d old/ir-common.ko
5756 18040 4 23800 5cf8 new/ir-common.ko
In thesis, we could be using above u8 for scancode, reducing even more the size
of the module, but defining it as u16 is more convenient, since, on dvb, each
scancode has up to 16 bits, and we currently have a few troubles with rc5, as their
scancodes are defined with more than 8 bits.
This patch itself shouldn't be doing any functional changes.
Signed-off-by: Mauro Carvalho Chehab <mchehab@redhat.com>
2009-08-30 00:15:55 +07:00
|
|
|
|
2006-03-03 22:13:42 +07:00
|
|
|
if (repeat)
|
|
|
|
set_bit(EV_REP, dev->evbit);
|
2009-11-29 18:19:59 +07:00
|
|
|
|
|
|
|
return 0;
|
2006-03-03 22:13:42 +07:00
|
|
|
}
|
2008-07-18 08:28:56 +07:00
|
|
|
EXPORT_SYMBOL_GPL(ir_input_init);
|
2006-03-03 22:13:42 +07:00
|
|
|
|
2009-11-29 18:19:59 +07:00
|
|
|
|
2006-03-03 22:13:42 +07:00
|
|
|
void ir_input_nokey(struct input_dev *dev, struct ir_input_state *ir)
|
|
|
|
{
|
|
|
|
if (ir->keypressed) {
|
|
|
|
ir->keypressed = 0;
|
|
|
|
ir_input_key_event(dev,ir);
|
|
|
|
}
|
|
|
|
}
|
2008-07-18 08:28:56 +07:00
|
|
|
EXPORT_SYMBOL_GPL(ir_input_nokey);
|
2006-03-03 22:13:42 +07:00
|
|
|
|
|
|
|
void ir_input_keydown(struct input_dev *dev, struct ir_input_state *ir,
|
2009-11-28 08:40:22 +07:00
|
|
|
u32 ir_key)
|
2006-03-03 22:13:42 +07:00
|
|
|
{
|
2009-11-28 08:40:22 +07:00
|
|
|
u32 keycode = ir_g_keycode_from_table(dev, ir_key);
|
2006-03-03 22:13:42 +07:00
|
|
|
|
|
|
|
if (ir->keypressed && ir->keycode != keycode) {
|
|
|
|
ir->keypressed = 0;
|
|
|
|
ir_input_key_event(dev,ir);
|
|
|
|
}
|
|
|
|
if (!ir->keypressed) {
|
|
|
|
ir->ir_key = ir_key;
|
|
|
|
ir->keycode = keycode;
|
|
|
|
ir->keypressed = 1;
|
|
|
|
ir_input_key_event(dev,ir);
|
|
|
|
}
|
|
|
|
}
|
2008-07-18 08:28:56 +07:00
|
|
|
EXPORT_SYMBOL_GPL(ir_input_keydown);
|
2006-03-03 22:13:42 +07:00
|
|
|
|
|
|
|
/* -------------------------------------------------------------------------- */
|
2007-07-12 06:28:44 +07:00
|
|
|
/* extract mask bits out of data and pack them into the result */
|
2006-03-03 22:13:42 +07:00
|
|
|
u32 ir_extract_bits(u32 data, u32 mask)
|
|
|
|
{
|
2007-07-12 06:28:44 +07:00
|
|
|
u32 vbit = 1, value = 0;
|
|
|
|
|
|
|
|
do {
|
|
|
|
if (mask&1) {
|
|
|
|
if (data&1)
|
|
|
|
value |= vbit;
|
|
|
|
vbit<<=1;
|
|
|
|
}
|
|
|
|
data>>=1;
|
|
|
|
} while (mask>>=1);
|
|
|
|
|
2006-03-03 22:13:42 +07:00
|
|
|
return value;
|
|
|
|
}
|
2008-07-18 08:28:56 +07:00
|
|
|
EXPORT_SYMBOL_GPL(ir_extract_bits);
|
2006-03-03 22:13:42 +07:00
|
|
|
|
|
|
|
static int inline getbit(u32 *samples, int bit)
|
|
|
|
{
|
|
|
|
return (samples[bit/32] & (1 << (31-(bit%32)))) ? 1 : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* sump raw samples for visual debugging ;) */
|
|
|
|
int ir_dump_samples(u32 *samples, int count)
|
|
|
|
{
|
|
|
|
int i, bit, start;
|
|
|
|
|
|
|
|
printk(KERN_DEBUG "ir samples: ");
|
|
|
|
start = 0;
|
|
|
|
for (i = 0; i < count * 32; i++) {
|
|
|
|
bit = getbit(samples,i);
|
|
|
|
if (bit)
|
|
|
|
start = 1;
|
|
|
|
if (0 == start)
|
|
|
|
continue;
|
|
|
|
printk("%s", bit ? "#" : "_");
|
|
|
|
}
|
|
|
|
printk("\n");
|
|
|
|
return 0;
|
|
|
|
}
|
2008-07-18 08:28:56 +07:00
|
|
|
EXPORT_SYMBOL_GPL(ir_dump_samples);
|
2006-03-03 22:13:42 +07:00
|
|
|
|
|
|
|
/* decode raw samples, pulse distance coding used by NEC remotes */
|
|
|
|
int ir_decode_pulsedistance(u32 *samples, int count, int low, int high)
|
|
|
|
{
|
|
|
|
int i,last,bit,len;
|
|
|
|
u32 curBit;
|
|
|
|
u32 value;
|
|
|
|
|
|
|
|
/* find start burst */
|
|
|
|
for (i = len = 0; i < count * 32; i++) {
|
|
|
|
bit = getbit(samples,i);
|
|
|
|
if (bit) {
|
|
|
|
len++;
|
|
|
|
} else {
|
|
|
|
if (len >= 29)
|
|
|
|
break;
|
|
|
|
len = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* start burst to short */
|
|
|
|
if (len < 29)
|
|
|
|
return 0xffffffff;
|
|
|
|
|
|
|
|
/* find start silence */
|
|
|
|
for (len = 0; i < count * 32; i++) {
|
|
|
|
bit = getbit(samples,i);
|
|
|
|
if (bit) {
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
len++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* silence to short */
|
|
|
|
if (len < 7)
|
|
|
|
return 0xffffffff;
|
|
|
|
|
|
|
|
/* go decoding */
|
|
|
|
len = 0;
|
|
|
|
last = 1;
|
|
|
|
value = 0; curBit = 1;
|
|
|
|
for (; i < count * 32; i++) {
|
|
|
|
bit = getbit(samples,i);
|
|
|
|
if (last) {
|
|
|
|
if(bit) {
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
len = 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (bit) {
|
|
|
|
if (len > (low + high) /2)
|
|
|
|
value |= curBit;
|
|
|
|
curBit <<= 1;
|
|
|
|
if (curBit == 1)
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
len++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
last = bit;
|
|
|
|
}
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
2008-07-18 08:28:56 +07:00
|
|
|
EXPORT_SYMBOL_GPL(ir_decode_pulsedistance);
|
2006-03-03 22:13:42 +07:00
|
|
|
|
|
|
|
/* decode raw samples, biphase coding, used by rc5 for example */
|
|
|
|
int ir_decode_biphase(u32 *samples, int count, int low, int high)
|
|
|
|
{
|
|
|
|
int i,last,bit,len,flips;
|
|
|
|
u32 value;
|
|
|
|
|
|
|
|
/* find start bit (1) */
|
|
|
|
for (i = 0; i < 32; i++) {
|
|
|
|
bit = getbit(samples,i);
|
|
|
|
if (bit)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* go decoding */
|
|
|
|
len = 0;
|
|
|
|
flips = 0;
|
|
|
|
value = 1;
|
|
|
|
for (; i < count * 32; i++) {
|
|
|
|
if (len > high)
|
|
|
|
break;
|
|
|
|
if (flips > 1)
|
|
|
|
break;
|
|
|
|
last = bit;
|
|
|
|
bit = getbit(samples,i);
|
|
|
|
if (last == bit) {
|
|
|
|
len++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (len < low) {
|
|
|
|
len++;
|
|
|
|
flips++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
value <<= 1;
|
|
|
|
value |= bit;
|
|
|
|
flips = 0;
|
|
|
|
len = 1;
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
2008-07-18 08:28:56 +07:00
|
|
|
EXPORT_SYMBOL_GPL(ir_decode_biphase);
|
2006-03-03 22:13:42 +07:00
|
|
|
|
2006-12-08 07:45:28 +07:00
|
|
|
/* RC5 decoding stuff, moved from bttv-input.c to share it with
|
|
|
|
* saa7134 */
|
|
|
|
|
|
|
|
/* decode raw bit pattern to RC5 code */
|
2009-09-28 06:05:23 +07:00
|
|
|
u32 ir_rc5_decode(unsigned int code)
|
2006-12-08 07:45:28 +07:00
|
|
|
{
|
|
|
|
unsigned int org_code = code;
|
|
|
|
unsigned int pair;
|
|
|
|
unsigned int rc5 = 0;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < 14; ++i) {
|
|
|
|
pair = code & 0x3;
|
|
|
|
code >>= 2;
|
|
|
|
|
|
|
|
rc5 <<= 1;
|
|
|
|
switch (pair) {
|
|
|
|
case 0:
|
|
|
|
case 2:
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
rc5 |= 1;
|
|
|
|
break;
|
|
|
|
case 3:
|
2009-11-28 07:54:41 +07:00
|
|
|
IR_dprintk(1, "ir-common: ir_rc5_decode(%x) bad code\n", org_code);
|
2006-12-08 07:45:28 +07:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2009-11-28 07:54:41 +07:00
|
|
|
IR_dprintk(1, "ir-common: code=%x, rc5=%x, start=%x, toggle=%x, address=%x, "
|
2006-12-08 07:45:28 +07:00
|
|
|
"instr=%x\n", rc5, org_code, RC5_START(rc5),
|
|
|
|
RC5_TOGGLE(rc5), RC5_ADDR(rc5), RC5_INSTR(rc5));
|
|
|
|
return rc5;
|
|
|
|
}
|
2009-09-28 06:05:23 +07:00
|
|
|
EXPORT_SYMBOL_GPL(ir_rc5_decode);
|
2006-12-08 07:45:28 +07:00
|
|
|
|
|
|
|
void ir_rc5_timer_end(unsigned long data)
|
|
|
|
{
|
|
|
|
struct card_ir *ir = (struct card_ir *)data;
|
|
|
|
struct timeval tv;
|
|
|
|
unsigned long current_jiffies, timeout;
|
|
|
|
u32 gap;
|
|
|
|
u32 rc5 = 0;
|
|
|
|
|
|
|
|
/* get time */
|
|
|
|
current_jiffies = jiffies;
|
|
|
|
do_gettimeofday(&tv);
|
|
|
|
|
|
|
|
/* avoid overflow with gap >1s */
|
|
|
|
if (tv.tv_sec - ir->base_time.tv_sec > 1) {
|
|
|
|
gap = 200000;
|
|
|
|
} else {
|
|
|
|
gap = 1000000 * (tv.tv_sec - ir->base_time.tv_sec) +
|
|
|
|
tv.tv_usec - ir->base_time.tv_usec;
|
|
|
|
}
|
|
|
|
|
2007-03-25 21:58:23 +07:00
|
|
|
/* signal we're ready to start a new code */
|
|
|
|
ir->active = 0;
|
|
|
|
|
|
|
|
/* Allow some timer jitter (RC5 is ~24ms anyway so this is ok) */
|
2006-12-08 07:45:28 +07:00
|
|
|
if (gap < 28000) {
|
2009-11-28 07:54:41 +07:00
|
|
|
IR_dprintk(1, "ir-common: spurious timer_end\n");
|
2006-12-08 07:45:28 +07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ir->last_bit < 20) {
|
|
|
|
/* ignore spurious codes (caused by light/other remotes) */
|
2009-11-28 07:54:41 +07:00
|
|
|
IR_dprintk(1, "ir-common: short code: %x\n", ir->code);
|
2006-12-08 07:45:28 +07:00
|
|
|
} else {
|
|
|
|
ir->code = (ir->code << ir->shift_by) | 1;
|
|
|
|
rc5 = ir_rc5_decode(ir->code);
|
|
|
|
|
|
|
|
/* two start bits? */
|
|
|
|
if (RC5_START(rc5) != ir->start) {
|
2009-11-28 07:54:41 +07:00
|
|
|
IR_dprintk(1, "ir-common: rc5 start bits invalid: %u\n", RC5_START(rc5));
|
2006-12-08 07:45:28 +07:00
|
|
|
|
|
|
|
/* right address? */
|
|
|
|
} else if (RC5_ADDR(rc5) == ir->addr) {
|
|
|
|
u32 toggle = RC5_TOGGLE(rc5);
|
|
|
|
u32 instr = RC5_INSTR(rc5);
|
|
|
|
|
|
|
|
/* Good code, decide if repeat/repress */
|
|
|
|
if (toggle != RC5_TOGGLE(ir->last_rc5) ||
|
|
|
|
instr != RC5_INSTR(ir->last_rc5)) {
|
2009-11-28 07:54:41 +07:00
|
|
|
IR_dprintk(1, "ir-common: instruction %x, toggle %x\n", instr,
|
2006-12-08 07:45:28 +07:00
|
|
|
toggle);
|
|
|
|
ir_input_nokey(ir->dev, &ir->ir);
|
2009-11-28 08:40:22 +07:00
|
|
|
ir_input_keydown(ir->dev, &ir->ir, instr);
|
2006-12-08 07:45:28 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Set/reset key-up timer */
|
2007-07-18 02:27:30 +07:00
|
|
|
timeout = current_jiffies +
|
|
|
|
msecs_to_jiffies(ir->rc5_key_timeout);
|
2006-12-08 07:45:28 +07:00
|
|
|
mod_timer(&ir->timer_keyup, timeout);
|
|
|
|
|
|
|
|
/* Save code for repeat test */
|
|
|
|
ir->last_rc5 = rc5;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-07-18 08:28:56 +07:00
|
|
|
EXPORT_SYMBOL_GPL(ir_rc5_timer_end);
|
2006-12-08 07:45:28 +07:00
|
|
|
|
|
|
|
void ir_rc5_timer_keyup(unsigned long data)
|
|
|
|
{
|
|
|
|
struct card_ir *ir = (struct card_ir *)data;
|
|
|
|
|
2009-11-28 07:54:41 +07:00
|
|
|
IR_dprintk(1, "ir-common: key released\n");
|
2006-12-08 07:45:28 +07:00
|
|
|
ir_input_nokey(ir->dev, &ir->ir);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(ir_rc5_timer_keyup);
|