blob: a95d09acc22a56a81f63260a8fecb7ecb216fe6c [file] [log] [blame]
Mauro Carvalho Chehab995187b2010-03-24 20:47:53 -03001/* ir-nec-decoder.c - handle NEC IR Pulse/Space protocol
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -03002 *
Mauro Carvalho Chehab37e59f82014-02-07 08:03:07 -02003 * Copyright (C) 2010 by Mauro Carvalho Chehab
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -03004 *
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
David Härdeman724e2492010-04-08 13:10:00 -030015#include <linux/bitrev.h>
Paul Gortmaker7a707b82011-07-03 14:03:12 -040016#include <linux/module.h>
Mauro Carvalho Chehabf62de672010-11-09 23:09:57 -030017#include "rc-core-priv.h"
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -030018
Mauro Carvalho Chehab67780d62010-04-03 20:33:00 -030019#define NEC_NBITS 32
David Härdeman724e2492010-04-08 13:10:00 -030020#define NEC_UNIT 562500 /* ns */
David Härdemane40b1122010-04-15 18:46:00 -030021#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)
Maxim Levitskye31f4122010-07-31 11:59:19 -030024#define NEC_REPEAT_SPACE (4 * NEC_UNIT)
David Härdemane40b1122010-04-15 18:46:00 -030025#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 */
Maxim Levitsky86ff0712010-07-31 11:59:20 -030030#define NECX_REPEAT_BITS 1
Mauro Carvalho Chehab9f154782010-03-21 13:00:55 -030031
Mauro Carvalho Chehab2f16f632010-04-03 18:51:50 -030032enum nec_state {
33 STATE_INACTIVE,
Mauro Carvalho Chehab2f16f632010-04-03 18:51:50 -030034 STATE_HEADER_SPACE,
David Härdeman724e2492010-04-08 13:10:00 -030035 STATE_BIT_PULSE,
36 STATE_BIT_SPACE,
37 STATE_TRAILER_PULSE,
Mauro Carvalho Chehab2f16f632010-04-03 18:51:50 -030038 STATE_TRAILER_SPACE,
39};
40
Mauro Carvalho Chehabada39632010-03-21 12:24:24 -030041/**
Mauro Carvalho Chehab587835a2010-04-04 10:44:51 -030042 * ir_nec_decode() - Decode one NEC pulse or space
David Härdemand8b4b582010-10-29 16:08:23 -030043 * @dev: the struct rc_dev descriptor of the device
David Härdemane40b1122010-04-15 18:46:00 -030044 * @duration: the struct ir_raw_event descriptor of the pulse/space
Mauro Carvalho Chehab2f16f632010-04-03 18:51:50 -030045 *
46 * This function returns -EINVAL if the pulse violates the state machine
Mauro Carvalho Chehabada39632010-03-21 12:24:24 -030047 */
David Härdemand8b4b582010-10-29 16:08:23 -030048static int ir_nec_decode(struct rc_dev *dev, struct ir_raw_event ev)
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -030049{
David Härdemand8b4b582010-10-29 16:08:23 -030050 struct nec_dec *data = &dev->raw->nec;
David Härdeman724e2492010-04-08 13:10:00 -030051 u32 scancode;
Sean Young6d741bf2017-08-07 16:20:58 -040052 enum rc_proto rc_proto;
David Härdeman724e2492010-04-08 13:10:00 -030053 u8 address, not_address, command, not_command;
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -030054
Maxim Levitsky46519182010-10-16 19:56:28 -030055 if (!is_timing_event(ev)) {
56 if (ev.reset)
57 data->state = STATE_INACTIVE;
Mauro Carvalho Chehab2f16f632010-04-03 18:51:50 -030058 return 0;
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -030059 }
60
David Härdemane40b1122010-04-15 18:46:00 -030061 IR_dprintk(2, "NEC decode started at state %d (%uus %s)\n",
62 data->state, TO_US(ev.duration), TO_STR(ev.pulse));
David Härdeman724e2492010-04-08 13:10:00 -030063
64 switch (data->state) {
65
66 case STATE_INACTIVE:
David Härdemane40b1122010-04-15 18:46:00 -030067 if (!ev.pulse)
68 break;
69
Sean Young743135e2012-07-31 06:37:29 -030070 if (eq_margin(ev.duration, NEC_HEADER_PULSE, NEC_UNIT * 2)) {
Maxim Levitsky86ff0712010-07-31 11:59:20 -030071 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
David Härdemane40b1122010-04-15 18:46:00 -030076 break;
77
78 data->count = 0;
79 data->state = STATE_HEADER_SPACE;
David Härdeman724e2492010-04-08 13:10:00 -030080 return 0;
81
82 case STATE_HEADER_SPACE:
David Härdemane40b1122010-04-15 18:46:00 -030083 if (ev.pulse)
84 break;
85
Sean Young743135e2012-07-31 06:37:29 -030086 if (eq_margin(ev.duration, NEC_HEADER_SPACE, NEC_UNIT)) {
David Härdeman724e2492010-04-08 13:10:00 -030087 data->state = STATE_BIT_PULSE;
88 return 0;
David Härdemane40b1122010-04-15 18:46:00 -030089 } else if (eq_margin(ev.duration, NEC_REPEAT_SPACE, NEC_UNIT / 2)) {
David Härdeman265a2982017-06-22 15:23:54 -040090 data->state = STATE_TRAILER_PULSE;
David Härdeman724e2492010-04-08 13:10:00 -030091 return 0;
92 }
David Härdemane40b1122010-04-15 18:46:00 -030093
David Härdeman724e2492010-04-08 13:10:00 -030094 break;
95
96 case STATE_BIT_PULSE:
David Härdemane40b1122010-04-15 18:46:00 -030097 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;
David Härdeman724e2492010-04-08 13:10:00 -0300105
106 case STATE_BIT_SPACE:
David Härdemane40b1122010-04-15 18:46:00 -0300107 if (ev.pulse)
David Härdeman724e2492010-04-08 13:10:00 -0300108 break;
109
Maxim Levitsky86ff0712010-07-31 11:59:20 -0300110 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");
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300114 rc_repeat(dev);
Maxim Levitsky86ff0712010-07-31 11:59:20 -0300115 data->state = STATE_INACTIVE;
116 return 0;
117
118 } else if (data->count > NECX_REPEAT_BITS)
119 data->necx_repeat = false;
120
David Härdemanc2163692010-06-13 17:29:36 -0300121 data->bits <<= 1;
David Härdemane40b1122010-04-15 18:46:00 -0300122 if (eq_margin(ev.duration, NEC_BIT_1_SPACE, NEC_UNIT / 2))
David Härdemanc2163692010-06-13 17:29:36 -0300123 data->bits |= 1;
David Härdemane40b1122010-04-15 18:46:00 -0300124 else if (!eq_margin(ev.duration, NEC_BIT_0_SPACE, NEC_UNIT / 2))
125 break;
David Härdeman724e2492010-04-08 13:10:00 -0300126 data->count++;
127
David Härdemane40b1122010-04-15 18:46:00 -0300128 if (data->count == NEC_NBITS)
129 data->state = STATE_TRAILER_PULSE;
130 else
David Härdeman724e2492010-04-08 13:10:00 -0300131 data->state = STATE_BIT_PULSE;
David Härdemane40b1122010-04-15 18:46:00 -0300132
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;
David Härdeman724e2492010-04-08 13:10:00 -0300151
Sean Young829bbf22017-10-01 16:38:29 -0400152 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);
David Härdeman724e2492010-04-08 13:10:00 -0300157
Sean Young829bbf22017-10-01 16:38:29 -0400158 scancode = ir_nec_bytes_to_scancode(address,
159 not_address,
160 command,
161 not_command,
162 &rc_proto);
David Härdeman724e2492010-04-08 13:10:00 -0300163
Sean Young829bbf22017-10-01 16:38:29 -0400164 if (data->is_nec_x)
165 data->necx_repeat = true;
Maxim Levitsky86ff0712010-07-31 11:59:20 -0300166
Sean Young829bbf22017-10-01 16:38:29 -0400167 rc_keydown(dev, rc_proto, scancode, 0);
168 } else {
169 rc_repeat(dev);
170 }
171
David Härdemane40b1122010-04-15 18:46:00 -0300172 data->state = STATE_INACTIVE;
David Härdeman724e2492010-04-08 13:10:00 -0300173 return 0;
David Härdeman724e2492010-04-08 13:10:00 -0300174 }
175
Mauro Carvalho Chehab39cac372011-11-23 12:03:32 -0300176 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));
Mauro Carvalho Chehab2f16f632010-04-03 18:51:50 -0300178 data->state = STATE_INACTIVE;
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -0300179 return -EINVAL;
180}
Mauro Carvalho Chehabada39632010-03-21 12:24:24 -0300181
James Hogan141cfb12014-03-14 20:04:14 -0300182/**
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 * @raw: raw data to be modulated.
187 */
Sean Young6d741bf2017-08-07 16:20:58 -0400188static u32 ir_nec_scancode_to_raw(enum rc_proto protocol, u32 scancode)
James Hogan141cfb12014-03-14 20:04:14 -0300189{
190 unsigned int addr, addr_inv, data, data_inv;
191
192 data = scancode & 0xff;
193
Sean Young6d741bf2017-08-07 16:20:58 -0400194 if (protocol == RC_PROTO_NEC32) {
James Hogan141cfb12014-03-14 20:04:14 -0300195 /* 32-bit NEC (used by Apple and TiVo remotes) */
196 /* scan encoding: aaAAddDD */
197 addr_inv = (scancode >> 24) & 0xff;
198 addr = (scancode >> 16) & 0xff;
199 data_inv = (scancode >> 8) & 0xff;
Sean Young6d741bf2017-08-07 16:20:58 -0400200 } else if (protocol == RC_PROTO_NECX) {
James Hogan141cfb12014-03-14 20:04:14 -0300201 /* Extended NEC */
202 /* scan encoding AAaaDD */
203 addr = (scancode >> 16) & 0xff;
204 addr_inv = (scancode >> 8) & 0xff;
205 data_inv = data ^ 0xff;
206 } else {
207 /* Normal NEC */
208 /* scan encoding: AADD */
209 addr = (scancode >> 8) & 0xff;
210 addr_inv = addr ^ 0xff;
211 data_inv = data ^ 0xff;
212 }
213
214 /* raw encoding: ddDDaaAA */
215 return data_inv << 24 |
216 data << 16 |
217 addr_inv << 8 |
218 addr;
219}
220
221static const struct ir_raw_timings_pd ir_nec_timings = {
222 .header_pulse = NEC_HEADER_PULSE,
223 .header_space = NEC_HEADER_SPACE,
224 .bit_pulse = NEC_BIT_PULSE,
225 .bit_space[0] = NEC_BIT_0_SPACE,
226 .bit_space[1] = NEC_BIT_1_SPACE,
227 .trailer_pulse = NEC_TRAILER_PULSE,
228 .trailer_space = NEC_TRAILER_SPACE,
229 .msb_first = 0,
230};
231
232/**
233 * ir_nec_encode() - Encode a scancode as a stream of raw events
234 *
235 * @protocol: protocol to encode
236 * @scancode: scancode to encode
237 * @events: array of raw ir events to write into
238 * @max: maximum size of @events
239 *
240 * Returns: The number of events written.
241 * -ENOBUFS if there isn't enough space in the array to fit the
242 * encoding. In this case all @max events will have been written.
243 */
Sean Young6d741bf2017-08-07 16:20:58 -0400244static int ir_nec_encode(enum rc_proto protocol, u32 scancode,
James Hogan141cfb12014-03-14 20:04:14 -0300245 struct ir_raw_event *events, unsigned int max)
246{
247 struct ir_raw_event *e = events;
248 int ret;
249 u32 raw;
250
251 /* Convert a NEC scancode to raw NEC data */
252 raw = ir_nec_scancode_to_raw(protocol, scancode);
253
254 /* Modulate the raw data using a pulse distance modulation */
255 ret = ir_raw_gen_pd(&e, max, &ir_nec_timings, NEC_NBITS, raw);
256 if (ret < 0)
257 return ret;
258
259 return e - events;
260}
261
Mauro Carvalho Chehab995187b2010-03-24 20:47:53 -0300262static struct ir_raw_handler nec_handler = {
Sean Young6d741bf2017-08-07 16:20:58 -0400263 .protocols = RC_PROTO_BIT_NEC | RC_PROTO_BIT_NECX |
264 RC_PROTO_BIT_NEC32,
Mauro Carvalho Chehab20d5f112010-03-25 23:49:46 -0300265 .decode = ir_nec_decode,
James Hogan141cfb12014-03-14 20:04:14 -0300266 .encode = ir_nec_encode,
Mauro Carvalho Chehab995187b2010-03-24 20:47:53 -0300267};
268
269static int __init ir_nec_decode_init(void)
270{
271 ir_raw_handler_register(&nec_handler);
272
273 printk(KERN_INFO "IR NEC protocol handler initialized\n");
274 return 0;
275}
276
277static void __exit ir_nec_decode_exit(void)
278{
279 ir_raw_handler_unregister(&nec_handler);
280}
281
282module_init(ir_nec_decode_init);
283module_exit(ir_nec_decode_exit);
284
285MODULE_LICENSE("GPL");
Mauro Carvalho Chehab37e59f82014-02-07 08:03:07 -0200286MODULE_AUTHOR("Mauro Carvalho Chehab");
Mauro Carvalho Chehab995187b2010-03-24 20:47:53 -0300287MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)");
288MODULE_DESCRIPTION("NEC IR protocol decoder");