V4L/DVB: ir-nec-decoder: Reimplement the entire decoder

Thanks to Andy Walls <awalls@md.metrocast.net> for pointing me his
code, that gave me some ideas to better implement it.

After some work with saa7134 bits, I found a way to catch both IRQ
edge pulses. By enabling it, the NEC decoder can now take both
pulse and spaces into account, making it more precise.

Instead of the old strategy of handling the events all at once,
this code implements a state machine. Due to that, it handles
individual pulse or space events, validating them against the
protocol, producing a much more reliable decoding.

With the new implementation, the protocol trailer bits are properly
handled, making possible for the repeat key to work.

Also, the code is now capable of handling both NEC and NEC extended
IR devices. With NEC, it produces a 16 bits code, while with NEC
extended, a 24 bits code is returned.

Signed-off-by: Mauro Carvalho Chehab <mchehab@redhat.com>
This commit is contained in:
Mauro Carvalho Chehab 2010-04-03 18:51:50 -03:00
parent 13c2449708
commit 2f16f6315f
2 changed files with 176 additions and 112 deletions

View file

@ -14,6 +14,14 @@
#include <media/ir-core.h> #include <media/ir-core.h>
#define NEC_UNIT 559979 /* ns */
#define NEC_HEADER_MARK (16 * NEC_UNIT)
#define NEC_HEADER_SPACE (8 * NEC_UNIT)
#define NEC_REPEAT_SPACE (4 * NEC_UNIT)
#define NEC_MARK (NEC_UNIT)
#define NEC_0_SYMBOL (NEC_UNIT)
#define NEC_1_SYMBOL (3 * NEC_UNIT)
/* Start time: 4.5 ms + 560 us of the next pulse */ /* Start time: 4.5 ms + 560 us of the next pulse */
#define MIN_START_TIME (3900000 + 560000) #define MIN_START_TIME (3900000 + 560000)
#define MAX_START_TIME (5100000 + 560000) #define MAX_START_TIME (5100000 + 560000)
@ -43,10 +51,32 @@
static LIST_HEAD(decoder_list); static LIST_HEAD(decoder_list);
static spinlock_t decoder_lock; static spinlock_t decoder_lock;
enum nec_state {
STATE_INACTIVE,
STATE_HEADER_MARK,
STATE_HEADER_SPACE,
STATE_MARK,
STATE_SPACE,
STATE_TRAILER_MARK,
STATE_TRAILER_SPACE,
};
struct nec_code {
u8 address;
u8 not_address;
u8 command;
u8 not_command;
};
struct decoder_data { struct decoder_data {
struct list_head list; struct list_head list;
struct ir_input_dev *ir_dev; struct ir_input_dev *ir_dev;
int enabled:1; int enabled:1;
/* State machine control */
enum nec_state state;
struct nec_code nec_code;
unsigned count;
}; };
@ -118,139 +148,173 @@ static struct attribute_group decoder_attribute_group = {
}; };
/** is_repeat - Check if it is a NEC repeat event
* @input_dev: the struct input_dev descriptor of the device
* @pos: the position of the first event
* @len: the length of the buffer
*/
static int is_repeat(struct ir_raw_event *evs, int len, int pos)
{
if ((evs[pos].delta.tv_nsec < MIN_REPEAT_START_TIME) ||
(evs[pos].delta.tv_nsec > MAX_REPEAT_START_TIME))
return 0;
if (++pos >= len)
return 0;
if ((evs[pos].delta.tv_nsec < MIN_REPEAT_TIME) ||
(evs[pos].delta.tv_nsec > MAX_REPEAT_TIME))
return 0;
return 1;
}
/** /**
* __ir_nec_decode() - Decode one NEC pulsecode * handle_event() - Decode one NEC pulse or space
* @input_dev: the struct input_dev descriptor of the device * @input_dev: the struct input_dev descriptor of the device
* @evs: event array with type/duration of pulse/space * @ev: event array with type/duration of pulse/space
* @len: length of the array *
* @pos: position to start seeking for a code * This function returns -EINVAL if the pulse violates the state machine
* This function returns -EINVAL if no pulse got decoded,
* 0 if buffer is empty and 1 if one keycode were handled.
*/ */
static int __ir_nec_decode(struct input_dev *input_dev, static int handle_event(struct input_dev *input_dev,
struct ir_raw_event *evs, struct ir_raw_event *ev)
int len, int *pos)
{ {
struct ir_input_dev *ir = input_get_drvdata(input_dev); struct decoder_data *data;
int count = -1; struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
int ircode = 0, not_code = 0; int bit, last_bit;
/* Be sure that the first event is an start one and is a pulse */ data = get_decoder_data(ir_dev);
for (; *pos < len; (*pos)++) { if (!data)
/* Very long delays are considered as start events */ return -EINVAL;
if (evs[*pos].delta.tv_nsec > MAX_NEC_TIME)
break;
if (evs[*pos].type & IR_START_EVENT)
break;
IR_dprintk(1, "%luus: Spurious NEC %s\n",
(evs[*pos].delta.tv_nsec + 500) / 1000,
(evs[*pos].type & IR_SPACE) ? "space" : "pulse");
} /* Except for the initial event, what matters is the previous bit */
if (*pos >= len) bit = (ev->type & IR_PULSE) ? 1 : 0;
last_bit = !bit;
/* Discards spurious space last_bits when inactive */
/* Very long delays are considered as start events */
if (ev->delta.tv_nsec > NEC_HEADER_MARK + NEC_HEADER_SPACE - NEC_UNIT / 2)
data->state = STATE_INACTIVE;
if (ev->type & IR_START_EVENT)
data->state = STATE_INACTIVE;
switch (data->state) {
case STATE_INACTIVE:
if (!bit) /* PULSE marks the start event */
return 0;
data->count = 0;
data->state = STATE_HEADER_MARK;
memset (&data->nec_code, 0, sizeof(data->nec_code));
return 0; return 0;
case STATE_HEADER_MARK:
(*pos)++; /* First event doesn't contain data */ if (!last_bit)
goto err;
if (evs[*pos].type != IR_PULSE) if (ev->delta.tv_nsec < NEC_HEADER_MARK - 6 * NEC_UNIT)
goto err; goto err;
data->state = STATE_HEADER_SPACE;
/* Check if it is a NEC repeat event */ return 0;
if (is_repeat(evs, len, *pos)) { case STATE_HEADER_SPACE:
*pos += 2; if (last_bit)
if (ir->keypressed) { goto err;
ir_repeat(input_dev); if (ev->delta.tv_nsec >= NEC_HEADER_SPACE - NEC_UNIT / 2) {
IR_dprintk(1, "NEC repeat event\n"); data->state = STATE_MARK;
return 1;
} else {
IR_dprintk(1, "missing NEC repeat event\n");
return 0; return 0;
} }
}
/* First space should have 4.5 ms otherwise is not NEC protocol */ if (ev->delta.tv_nsec >= NEC_REPEAT_SPACE - NEC_UNIT / 2) {
if ((evs[*pos].delta.tv_nsec < MIN_START_TIME) || ir_repeat(input_dev);
(evs[*pos].delta.tv_nsec > MAX_START_TIME)) IR_dprintk(1, "Repeat last key\n");
data->state = STATE_TRAILER_MARK;
return 0;
}
goto err; goto err;
case STATE_MARK:
count = 0; if (!last_bit)
for ((*pos)++; *pos < len; (*pos)++) { goto err;
int bit; if ((ev->delta.tv_nsec > NEC_MARK + NEC_UNIT / 2) ||
if ((evs[*pos].delta.tv_nsec > MIN_BIT1_TIME) && (ev->delta.tv_nsec < NEC_MARK - NEC_UNIT / 2))
(evs[*pos].delta.tv_nsec < MAX_BIT1_TIME)) goto err;
bit = 1; data->state = STATE_SPACE;
else if ((evs[*pos].delta.tv_nsec > MIN_BIT0_TIME) && return 0;
(evs[*pos].delta.tv_nsec < MAX_BIT0_TIME)) case STATE_SPACE:
bit = 0; if (last_bit)
else
goto err; goto err;
if ((ev->delta.tv_nsec >= NEC_0_SYMBOL - NEC_UNIT / 2) &&
(ev->delta.tv_nsec < NEC_0_SYMBOL + NEC_UNIT / 2))
bit = 0;
else if ((ev->delta.tv_nsec >= NEC_1_SYMBOL - NEC_UNIT / 2) &&
(ev->delta.tv_nsec < NEC_1_SYMBOL + NEC_UNIT / 2))
bit = 1;
else {
IR_dprintk(1, "Decode failed at %d-th bit (%s) @%luus\n",
data->count,
last_bit ? "pulse" : "space",
(ev->delta.tv_nsec + 500) / 1000);
goto err2;
}
/* Ok, we've got a valid bit. proccess it */
if (bit) { if (bit) {
int shift = count; int shift = data->count;
/* Address first, then command */
/*
* NEC transmit bytes on this temporal order:
* address | not address | command | not command
*/
if (shift < 8) { if (shift < 8) {
shift += 8; data->nec_code.address |= 1 << shift;
ircode |= 1 << shift;
} else if (shift < 16) { } else if (shift < 16) {
not_code |= 1 << shift; data->nec_code.not_address |= 1 << (shift - 8);
} else if (shift < 24) { } else if (shift < 24) {
shift -= 16; data->nec_code.command |= 1 << (shift - 16);
ircode |= 1 << shift;
} else { } else {
shift -= 24; data->nec_code.not_command |= 1 << (shift - 24);
not_code |= 1 << shift;
} }
} }
if (++count == 32) if (++data->count == 32) {
break; u32 scancode;
} /*
(*pos)++; * Fixme: may need to accept Extended NEC protocol?
*/
if ((data->nec_code.command ^ data->nec_code.not_command) != 0xff)
goto checksum_err;
/* if ((data->nec_code.address ^ data->nec_code.not_address) != 0xff) {
* Fixme: may need to accept Extended NEC protocol? /* Extended NEC */
*/ scancode = data->nec_code.address << 16 |
if ((ircode & ~not_code) != ircode) { data->nec_code.not_address << 8 |
IR_dprintk(1, "NEC checksum error: code 0x%04x, not-code 0x%04x\n", data->nec_code.command;
ircode, not_code); IR_dprintk(1, "NEC scancode 0x%06x\n", scancode);
return -EINVAL; } else {
/* normal NEC */
scancode = data->nec_code.address << 8 |
data->nec_code.command;
IR_dprintk(1, "NEC scancode 0x%04x\n", scancode);
}
ir_keydown(input_dev, scancode, 0);
data->state = STATE_TRAILER_MARK;
} else
data->state = STATE_MARK;
return 0;
case STATE_TRAILER_MARK:
if (!last_bit)
goto err;
data->state = STATE_TRAILER_SPACE;
return 0;
case STATE_TRAILER_SPACE:
if (last_bit)
goto err;
data->state = STATE_INACTIVE;
return 0;
} }
IR_dprintk(1, "NEC scancode 0x%04x\n", ircode);
ir_keydown(input_dev, ircode, 0);
return 1;
err: err:
IR_dprintk(1, "NEC decoded failed at bit %d (%s) while decoding %luus time\n", IR_dprintk(1, "NEC decoded failed at state %d (%s) @ %luus\n",
count, data->state,
(evs[*pos].type & IR_SPACE) ? "space" : "pulse", bit ? "pulse" : "space",
(evs[*pos].delta.tv_nsec + 500) / 1000); (ev->delta.tv_nsec + 500) / 1000);
err2:
data->state = STATE_INACTIVE;
return -EINVAL;
checksum_err:
data->state = STATE_INACTIVE;
IR_dprintk(1, "NEC checksum error: received 0x%02x%02x%02x%02x\n",
data->nec_code.address,
data->nec_code.not_address,
data->nec_code.command,
data->nec_code.not_command);
return -EINVAL; return -EINVAL;
} }
/** /**
* __ir_nec_decode() - Decodes all NEC pulsecodes on a given array * ir_nec_decode() - Decodes all NEC pulsecodes on a given array
* @input_dev: the struct input_dev descriptor of the device * @input_dev: the struct input_dev descriptor of the device
* @evs: event array with type/duration of pulse/space * @evs: event array with type/duration of pulse/space
* @len: length of the array * @len: length of the array
@ -269,10 +333,9 @@ static int ir_nec_decode(struct input_dev *input_dev,
if (!data || !data->enabled) if (!data || !data->enabled)
return 0; return 0;
while (pos < len) { for (pos = 0; pos < len; pos++)
if (__ir_nec_decode(input_dev, evs, len, &pos) > 0) handle_event(input_dev, &evs[pos]);
rc++;
}
return rc; return rc;
} }

View file

@ -658,7 +658,8 @@ int saa7134_input_init1(struct saa7134_dev *dev)
break; break;
case SAA7134_BOARD_AVERMEDIA_M135A: case SAA7134_BOARD_AVERMEDIA_M135A:
ir_codes = RC_MAP_AVERMEDIA_M135A_RM_JX; ir_codes = RC_MAP_AVERMEDIA_M135A_RM_JX;
mask_keydown = 0x0040000; mask_keydown = 0x0040000; /* Enable GPIO18 line on both edges */
mask_keyup = 0x0040000;
mask_keycode = 0xffff; mask_keycode = 0xffff;
raw_decode = 1; raw_decode = 1;
break; break;
@ -1014,13 +1015,13 @@ static int saa7134_raw_decode_irq(struct saa7134_dev *dev)
{ {
struct card_ir *ir = dev->remote; struct card_ir *ir = dev->remote;
unsigned long timeout; unsigned long timeout;
int pulse; int space;
/* Generate initial event */ /* Generate initial event */
saa_clearb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN); saa_clearb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
saa_setb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN); saa_setb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
pulse = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2) & ir->mask_keydown; space = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2) & ir->mask_keydown;
ir_raw_event_store(dev->remote->dev, pulse ? IR_PULSE : IR_SPACE); ir_raw_event_store(dev->remote->dev, space ? IR_SPACE : IR_PULSE);
/* /*