xref: /linux/drivers/media/rc/ir-nec-decoder.c (revision c0c914eca7f251c70facc37dfebeaf176601918d)
1 /* ir-nec-decoder.c - handle NEC IR Pulse/Space protocol
2  *
3  * Copyright (C) 2010 by Mauro Carvalho Chehab
4  *
5  * This program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation version 2 of the License.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  */
14 
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 #define NEC_HEADER_PULSE	(16 * NEC_UNIT)
22 #define NECX_HEADER_PULSE	(8  * NEC_UNIT) /* Less common NEC variant */
23 #define NEC_HEADER_SPACE	(8  * NEC_UNIT)
24 #define NEC_REPEAT_SPACE	(4  * NEC_UNIT)
25 #define NEC_BIT_PULSE		(1  * NEC_UNIT)
26 #define NEC_BIT_0_SPACE		(1  * NEC_UNIT)
27 #define NEC_BIT_1_SPACE		(3  * NEC_UNIT)
28 #define	NEC_TRAILER_PULSE	(1  * NEC_UNIT)
29 #define	NEC_TRAILER_SPACE	(10 * NEC_UNIT) /* even longer in reality */
30 #define NECX_REPEAT_BITS	1
31 
32 enum nec_state {
33 	STATE_INACTIVE,
34 	STATE_HEADER_SPACE,
35 	STATE_BIT_PULSE,
36 	STATE_BIT_SPACE,
37 	STATE_TRAILER_PULSE,
38 	STATE_TRAILER_SPACE,
39 };
40 
41 /**
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 	u32 scancode;
52 	u8 address, not_address, command, not_command;
53 	bool send_32bits = false;
54 
55 	if (!is_timing_event(ev)) {
56 		if (ev.reset)
57 			data->state = STATE_INACTIVE;
58 		return 0;
59 	}
60 
61 	IR_dprintk(2, "NEC decode started at state %d (%uus %s)\n",
62 		   data->state, TO_US(ev.duration), TO_STR(ev.pulse));
63 
64 	switch (data->state) {
65 
66 	case STATE_INACTIVE:
67 		if (!ev.pulse)
68 			break;
69 
70 		if (eq_margin(ev.duration, NEC_HEADER_PULSE, NEC_UNIT * 2)) {
71 			data->is_nec_x = false;
72 			data->necx_repeat = false;
73 		} else if (eq_margin(ev.duration, NECX_HEADER_PULSE, NEC_UNIT / 2))
74 			data->is_nec_x = true;
75 		else
76 			break;
77 
78 		data->count = 0;
79 		data->state = STATE_HEADER_SPACE;
80 		return 0;
81 
82 	case STATE_HEADER_SPACE:
83 		if (ev.pulse)
84 			break;
85 
86 		if (eq_margin(ev.duration, NEC_HEADER_SPACE, NEC_UNIT)) {
87 			data->state = STATE_BIT_PULSE;
88 			return 0;
89 		} else if (eq_margin(ev.duration, NEC_REPEAT_SPACE, NEC_UNIT / 2)) {
90 			if (!dev->keypressed) {
91 				IR_dprintk(1, "Discarding last key repeat: event after key up\n");
92 			} else {
93 				rc_repeat(dev);
94 				IR_dprintk(1, "Repeat last key\n");
95 				data->state = STATE_TRAILER_PULSE;
96 			}
97 			return 0;
98 		}
99 
100 		break;
101 
102 	case STATE_BIT_PULSE:
103 		if (!ev.pulse)
104 			break;
105 
106 		if (!eq_margin(ev.duration, NEC_BIT_PULSE, NEC_UNIT / 2))
107 			break;
108 
109 		data->state = STATE_BIT_SPACE;
110 		return 0;
111 
112 	case STATE_BIT_SPACE:
113 		if (ev.pulse)
114 			break;
115 
116 		if (data->necx_repeat && data->count == NECX_REPEAT_BITS &&
117 			geq_margin(ev.duration,
118 			NEC_TRAILER_SPACE, NEC_UNIT / 2)) {
119 				IR_dprintk(1, "Repeat last key\n");
120 				rc_repeat(dev);
121 				data->state = STATE_INACTIVE;
122 				return 0;
123 
124 		} else if (data->count > NECX_REPEAT_BITS)
125 			data->necx_repeat = false;
126 
127 		data->bits <<= 1;
128 		if (eq_margin(ev.duration, NEC_BIT_1_SPACE, NEC_UNIT / 2))
129 			data->bits |= 1;
130 		else if (!eq_margin(ev.duration, NEC_BIT_0_SPACE, NEC_UNIT / 2))
131 			break;
132 		data->count++;
133 
134 		if (data->count == NEC_NBITS)
135 			data->state = STATE_TRAILER_PULSE;
136 		else
137 			data->state = STATE_BIT_PULSE;
138 
139 		return 0;
140 
141 	case STATE_TRAILER_PULSE:
142 		if (!ev.pulse)
143 			break;
144 
145 		if (!eq_margin(ev.duration, NEC_TRAILER_PULSE, NEC_UNIT / 2))
146 			break;
147 
148 		data->state = STATE_TRAILER_SPACE;
149 		return 0;
150 
151 	case STATE_TRAILER_SPACE:
152 		if (ev.pulse)
153 			break;
154 
155 		if (!geq_margin(ev.duration, NEC_TRAILER_SPACE, NEC_UNIT / 2))
156 			break;
157 
158 		address     = bitrev8((data->bits >> 24) & 0xff);
159 		not_address = bitrev8((data->bits >> 16) & 0xff);
160 		command	    = bitrev8((data->bits >>  8) & 0xff);
161 		not_command = bitrev8((data->bits >>  0) & 0xff);
162 
163 		if ((command ^ not_command) != 0xff) {
164 			IR_dprintk(1, "NEC checksum error: received 0x%08x\n",
165 				   data->bits);
166 			send_32bits = true;
167 		}
168 
169 		if (send_32bits) {
170 			/* NEC transport, but modified protocol, used by at
171 			 * least Apple and TiVo remotes */
172 			scancode = data->bits;
173 			IR_dprintk(1, "NEC (modified) scancode 0x%08x\n", scancode);
174 		} else if ((address ^ not_address) != 0xff) {
175 			/* Extended NEC */
176 			scancode = address     << 16 |
177 				   not_address <<  8 |
178 				   command;
179 			IR_dprintk(1, "NEC (Ext) scancode 0x%06x\n", scancode);
180 		} else {
181 			/* Normal NEC */
182 			scancode = address << 8 | command;
183 			IR_dprintk(1, "NEC scancode 0x%04x\n", scancode);
184 		}
185 
186 		if (data->is_nec_x)
187 			data->necx_repeat = true;
188 
189 		rc_keydown(dev, RC_TYPE_NEC, scancode, 0);
190 		data->state = STATE_INACTIVE;
191 		return 0;
192 	}
193 
194 	IR_dprintk(1, "NEC decode failed at count %d state %d (%uus %s)\n",
195 		   data->count, data->state, TO_US(ev.duration), TO_STR(ev.pulse));
196 	data->state = STATE_INACTIVE;
197 	return -EINVAL;
198 }
199 
200 static struct ir_raw_handler nec_handler = {
201 	.protocols	= RC_BIT_NEC,
202 	.decode		= ir_nec_decode,
203 };
204 
205 static int __init ir_nec_decode_init(void)
206 {
207 	ir_raw_handler_register(&nec_handler);
208 
209 	printk(KERN_INFO "IR NEC protocol handler initialized\n");
210 	return 0;
211 }
212 
213 static void __exit ir_nec_decode_exit(void)
214 {
215 	ir_raw_handler_unregister(&nec_handler);
216 }
217 
218 module_init(ir_nec_decode_init);
219 module_exit(ir_nec_decode_exit);
220 
221 MODULE_LICENSE("GPL");
222 MODULE_AUTHOR("Mauro Carvalho Chehab");
223 MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)");
224 MODULE_DESCRIPTION("NEC IR protocol decoder");
225