ir-nec-decoder.c 5.8 KB
Newer Older
1
/* ir-nec-decoder.c - handle NEC IR Pulse/Space protocol
2
 *
3
 * Copyright (C) 2010 by Mauro Carvalho Chehab
4 5 6 7 8 9 10 11 12 13 14
 *
 * 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.
 */

15
#include <linux/bitrev.h>
16
#include <linux/module.h>
17
#include "rc-core-priv.h"
18

19
#define NEC_NBITS		32
20
#define NEC_UNIT		562500  /* ns */
21 22 23
#define NEC_HEADER_PULSE	(16 * NEC_UNIT)
#define NECX_HEADER_PULSE	(8  * NEC_UNIT) /* Less common NEC variant */
#define NEC_HEADER_SPACE	(8  * NEC_UNIT)
24
#define NEC_REPEAT_SPACE	(4  * NEC_UNIT)
25 26 27 28 29
#define NEC_BIT_PULSE		(1  * NEC_UNIT)
#define NEC_BIT_0_SPACE		(1  * NEC_UNIT)
#define NEC_BIT_1_SPACE		(3  * NEC_UNIT)
#define	NEC_TRAILER_PULSE	(1  * NEC_UNIT)
#define	NEC_TRAILER_SPACE	(10 * NEC_UNIT) /* even longer in reality */
30
#define NECX_REPEAT_BITS	1
31

32 33 34
enum nec_state {
	STATE_INACTIVE,
	STATE_HEADER_SPACE,
35 36 37
	STATE_BIT_PULSE,
	STATE_BIT_SPACE,
	STATE_TRAILER_PULSE,
38 39 40 41
	STATE_TRAILER_SPACE,
};

/**
42
 * ir_nec_decode() - Decode one NEC pulse or space
43
 * @dev:	the struct rc_dev descriptor of the device
44
 * @duration:	the struct ir_raw_event descriptor of the pulse/space
45 46
 *
 * This function returns -EINVAL if the pulse violates the state machine
47
 */
48
static int ir_nec_decode(struct rc_dev *dev, struct ir_raw_event ev)
49
{
50
	struct nec_dec *data = &dev->raw->nec;
51 52
	u32 scancode;
	u8 address, not_address, command, not_command;
53
	bool send_32bits = false;
54

55
	if (!(dev->enabled_protocols & RC_BIT_NEC))
56 57
		return 0;

58 59 60
	if (!is_timing_event(ev)) {
		if (ev.reset)
			data->state = STATE_INACTIVE;
61 62
		return 0;
	}
63

64 65
	IR_dprintk(2, "NEC decode started at state %d (%uus %s)\n",
		   data->state, TO_US(ev.duration), TO_STR(ev.pulse));
66

67
	switch (data->state) {
68

69
	case STATE_INACTIVE:
70 71 72
		if (!ev.pulse)
			break;

73
		if (eq_margin(ev.duration, NEC_HEADER_PULSE, NEC_UNIT * 2)) {
74 75 76 77 78
			data->is_nec_x = false;
			data->necx_repeat = false;
		} else if (eq_margin(ev.duration, NECX_HEADER_PULSE, NEC_UNIT / 2))
			data->is_nec_x = true;
		else
79 80 81 82
			break;

		data->count = 0;
		data->state = STATE_HEADER_SPACE;
83
		return 0;
84

85
	case STATE_HEADER_SPACE:
86 87 88
		if (ev.pulse)
			break;

89
		if (eq_margin(ev.duration, NEC_HEADER_SPACE, NEC_UNIT)) {
90
			data->state = STATE_BIT_PULSE;
91
			return 0;
92
		} else if (eq_margin(ev.duration, NEC_REPEAT_SPACE, NEC_UNIT / 2)) {
93 94 95 96 97 98 99
			if (!dev->keypressed) {
				IR_dprintk(1, "Discarding last key repeat: event after key up\n");
			} else {
				rc_repeat(dev);
				IR_dprintk(1, "Repeat last key\n");
				data->state = STATE_TRAILER_PULSE;
			}
100 101
			return 0;
		}
102

103 104 105
		break;

	case STATE_BIT_PULSE:
106 107 108 109 110 111 112 113
		if (!ev.pulse)
			break;

		if (!eq_margin(ev.duration, NEC_BIT_PULSE, NEC_UNIT / 2))
			break;

		data->state = STATE_BIT_SPACE;
		return 0;
114 115

	case STATE_BIT_SPACE:
116
		if (ev.pulse)
117 118
			break;

119 120 121 122
		if (data->necx_repeat && data->count == NECX_REPEAT_BITS &&
			geq_margin(ev.duration,
			NEC_TRAILER_SPACE, NEC_UNIT / 2)) {
				IR_dprintk(1, "Repeat last key\n");
123
				rc_repeat(dev);
124 125 126 127 128 129
				data->state = STATE_INACTIVE;
				return 0;

		} else if (data->count > NECX_REPEAT_BITS)
			data->necx_repeat = false;

130
		data->bits <<= 1;
131
		if (eq_margin(ev.duration, NEC_BIT_1_SPACE, NEC_UNIT / 2))
132
			data->bits |= 1;
133 134
		else if (!eq_margin(ev.duration, NEC_BIT_0_SPACE, NEC_UNIT / 2))
			break;
135 136
		data->count++;

137 138 139
		if (data->count == NEC_NBITS)
			data->state = STATE_TRAILER_PULSE;
		else
140
			data->state = STATE_BIT_PULSE;
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159

		return 0;

	case STATE_TRAILER_PULSE:
		if (!ev.pulse)
			break;

		if (!eq_margin(ev.duration, NEC_TRAILER_PULSE, NEC_UNIT / 2))
			break;

		data->state = STATE_TRAILER_SPACE;
		return 0;

	case STATE_TRAILER_SPACE:
		if (ev.pulse)
			break;

		if (!geq_margin(ev.duration, NEC_TRAILER_SPACE, NEC_UNIT / 2))
			break;
160

161 162 163 164
		address     = bitrev8((data->bits >> 24) & 0xff);
		not_address = bitrev8((data->bits >> 16) & 0xff);
		command	    = bitrev8((data->bits >>  8) & 0xff);
		not_command = bitrev8((data->bits >>  0) & 0xff);
165 166 167

		if ((command ^ not_command) != 0xff) {
			IR_dprintk(1, "NEC checksum error: received 0x%08x\n",
168
				   data->bits);
169
			send_32bits = true;
170
		}
171

172 173 174
		if (send_32bits) {
			/* NEC transport, but modified protocol, used by at
			 * least Apple and TiVo remotes */
175 176 177 178
			scancode = not_address << 24 |
				   address     << 16 |
				   not_command <<  8 |
				   command;
179 180
			IR_dprintk(1, "NEC (modified) scancode 0x%08x\n", scancode);
		} else if ((address ^ not_address) != 0xff) {
181 182 183 184 185 186
			/* Extended NEC */
			scancode = address     << 16 |
				   not_address <<  8 |
				   command;
			IR_dprintk(1, "NEC (Ext) scancode 0x%06x\n", scancode);
		} else {
187
			/* Normal NEC */
188 189
			scancode = address << 8 | command;
			IR_dprintk(1, "NEC scancode 0x%04x\n", scancode);
190
		}
191

192 193 194
		if (data->is_nec_x)
			data->necx_repeat = true;

195
		rc_keydown(dev, scancode, 0);
196
		data->state = STATE_INACTIVE;
197
		return 0;
198
	}
199

200 201
	IR_dprintk(1, "NEC decode failed at count %d state %d (%uus %s)\n",
		   data->count, data->state, TO_US(ev.duration), TO_STR(ev.pulse));
202
	data->state = STATE_INACTIVE;
203 204
	return -EINVAL;
}
205

206
static struct ir_raw_handler nec_handler = {
207
	.protocols	= RC_BIT_NEC,
208
	.decode		= ir_nec_decode,
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
};

static int __init ir_nec_decode_init(void)
{
	ir_raw_handler_register(&nec_handler);

	printk(KERN_INFO "IR NEC protocol handler initialized\n");
	return 0;
}

static void __exit ir_nec_decode_exit(void)
{
	ir_raw_handler_unregister(&nec_handler);
}

module_init(ir_nec_decode_init);
module_exit(ir_nec_decode_exit);

MODULE_LICENSE("GPL");
228
MODULE_AUTHOR("Mauro Carvalho Chehab");
229 230
MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)");
MODULE_DESCRIPTION("NEC IR protocol decoder");