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  * @ev:		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 	enum rc_proto rc_proto;
53 	u8 address, not_address, command, not_command;
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 			data->state = STATE_TRAILER_PULSE;
91 			return 0;
92 		}
93 
94 		break;
95 
96 	case STATE_BIT_PULSE:
97 		if (!ev.pulse)
98 			break;
99 
100 		if (!eq_margin(ev.duration, NEC_BIT_PULSE, NEC_UNIT / 2))
101 			break;
102 
103 		data->state = STATE_BIT_SPACE;
104 		return 0;
105 
106 	case STATE_BIT_SPACE:
107 		if (ev.pulse)
108 			break;
109 
110 		if (data->necx_repeat && data->count == NECX_REPEAT_BITS &&
111 			geq_margin(ev.duration,
112 			NEC_TRAILER_SPACE, NEC_UNIT / 2)) {
113 				IR_dprintk(1, "Repeat last key\n");
114 				rc_repeat(dev);
115 				data->state = STATE_INACTIVE;
116 				return 0;
117 
118 		} else if (data->count > NECX_REPEAT_BITS)
119 			data->necx_repeat = false;
120 
121 		data->bits <<= 1;
122 		if (eq_margin(ev.duration, NEC_BIT_1_SPACE, NEC_UNIT / 2))
123 			data->bits |= 1;
124 		else if (!eq_margin(ev.duration, NEC_BIT_0_SPACE, NEC_UNIT / 2))
125 			break;
126 		data->count++;
127 
128 		if (data->count == NEC_NBITS)
129 			data->state = STATE_TRAILER_PULSE;
130 		else
131 			data->state = STATE_BIT_PULSE;
132 
133 		return 0;
134 
135 	case STATE_TRAILER_PULSE:
136 		if (!ev.pulse)
137 			break;
138 
139 		if (!eq_margin(ev.duration, NEC_TRAILER_PULSE, NEC_UNIT / 2))
140 			break;
141 
142 		data->state = STATE_TRAILER_SPACE;
143 		return 0;
144 
145 	case STATE_TRAILER_SPACE:
146 		if (ev.pulse)
147 			break;
148 
149 		if (!geq_margin(ev.duration, NEC_TRAILER_SPACE, NEC_UNIT / 2))
150 			break;
151 
152 		if (data->count == NEC_NBITS) {
153 			address     = bitrev8((data->bits >> 24) & 0xff);
154 			not_address = bitrev8((data->bits >> 16) & 0xff);
155 			command	    = bitrev8((data->bits >>  8) & 0xff);
156 			not_command = bitrev8((data->bits >>  0) & 0xff);
157 
158 			scancode = ir_nec_bytes_to_scancode(address,
159 							    not_address,
160 							    command,
161 							    not_command,
162 							    &rc_proto);
163 
164 			if (data->is_nec_x)
165 				data->necx_repeat = true;
166 
167 			rc_keydown(dev, rc_proto, scancode, 0);
168 		} else {
169 			rc_repeat(dev);
170 		}
171 
172 		data->state = STATE_INACTIVE;
173 		return 0;
174 	}
175 
176 	IR_dprintk(1, "NEC decode failed at count %d state %d (%uus %s)\n",
177 		   data->count, data->state, TO_US(ev.duration), TO_STR(ev.pulse));
178 	data->state = STATE_INACTIVE;
179 	return -EINVAL;
180 }
181 
182 /**
183  * ir_nec_scancode_to_raw() - encode an NEC scancode ready for modulation.
184  * @protocol:	specific protocol to use
185  * @scancode:	a single NEC scancode.
186  */
187 static u32 ir_nec_scancode_to_raw(enum rc_proto protocol, u32 scancode)
188 {
189 	unsigned int addr, addr_inv, data, data_inv;
190 
191 	data = scancode & 0xff;
192 
193 	if (protocol == RC_PROTO_NEC32) {
194 		/* 32-bit NEC (used by Apple and TiVo remotes) */
195 		/* scan encoding: aaAAddDD */
196 		addr_inv   = (scancode >> 24) & 0xff;
197 		addr       = (scancode >> 16) & 0xff;
198 		data_inv   = (scancode >>  8) & 0xff;
199 	} else if (protocol == RC_PROTO_NECX) {
200 		/* Extended NEC */
201 		/* scan encoding AAaaDD */
202 		addr       = (scancode >> 16) & 0xff;
203 		addr_inv   = (scancode >>  8) & 0xff;
204 		data_inv   = data ^ 0xff;
205 	} else {
206 		/* Normal NEC */
207 		/* scan encoding: AADD */
208 		addr       = (scancode >>  8) & 0xff;
209 		addr_inv   = addr ^ 0xff;
210 		data_inv   = data ^ 0xff;
211 	}
212 
213 	/* raw encoding: ddDDaaAA */
214 	return data_inv << 24 |
215 	       data     << 16 |
216 	       addr_inv <<  8 |
217 	       addr;
218 }
219 
220 static const struct ir_raw_timings_pd ir_nec_timings = {
221 	.header_pulse	= NEC_HEADER_PULSE,
222 	.header_space	= NEC_HEADER_SPACE,
223 	.bit_pulse	= NEC_BIT_PULSE,
224 	.bit_space[0]	= NEC_BIT_0_SPACE,
225 	.bit_space[1]	= NEC_BIT_1_SPACE,
226 	.trailer_pulse	= NEC_TRAILER_PULSE,
227 	.trailer_space	= NEC_TRAILER_SPACE,
228 	.msb_first	= 0,
229 };
230 
231 /**
232  * ir_nec_encode() - Encode a scancode as a stream of raw events
233  *
234  * @protocol:	protocol to encode
235  * @scancode:	scancode to encode
236  * @events:	array of raw ir events to write into
237  * @max:	maximum size of @events
238  *
239  * Returns:	The number of events written.
240  *		-ENOBUFS if there isn't enough space in the array to fit the
241  *		encoding. In this case all @max events will have been written.
242  */
243 static int ir_nec_encode(enum rc_proto protocol, u32 scancode,
244 			 struct ir_raw_event *events, unsigned int max)
245 {
246 	struct ir_raw_event *e = events;
247 	int ret;
248 	u32 raw;
249 
250 	/* Convert a NEC scancode to raw NEC data */
251 	raw = ir_nec_scancode_to_raw(protocol, scancode);
252 
253 	/* Modulate the raw data using a pulse distance modulation */
254 	ret = ir_raw_gen_pd(&e, max, &ir_nec_timings, NEC_NBITS, raw);
255 	if (ret < 0)
256 		return ret;
257 
258 	return e - events;
259 }
260 
261 static struct ir_raw_handler nec_handler = {
262 	.protocols	= RC_PROTO_BIT_NEC | RC_PROTO_BIT_NECX |
263 							RC_PROTO_BIT_NEC32,
264 	.decode		= ir_nec_decode,
265 	.encode		= ir_nec_encode,
266 };
267 
268 static int __init ir_nec_decode_init(void)
269 {
270 	ir_raw_handler_register(&nec_handler);
271 
272 	printk(KERN_INFO "IR NEC protocol handler initialized\n");
273 	return 0;
274 }
275 
276 static void __exit ir_nec_decode_exit(void)
277 {
278 	ir_raw_handler_unregister(&nec_handler);
279 }
280 
281 module_init(ir_nec_decode_init);
282 module_exit(ir_nec_decode_exit);
283 
284 MODULE_LICENSE("GPL");
285 MODULE_AUTHOR("Mauro Carvalho Chehab");
286 MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)");
287 MODULE_DESCRIPTION("NEC IR protocol decoder");
288