blob: 2ca509e6e16b6c73cc09b0bc84616b8b1b1beafe [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 *
3 * Copyright (C) 2010 by Mauro Carvalho Chehab <mchehab@redhat.com>
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
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;
52 u8 address, not_address, command, not_command;
Jarod Wilson4be22b62011-03-22 17:23:15 -030053 bool send_32bits = false;
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -030054
Mauro Carvalho Chehab52b66142010-11-17 14:20:52 -030055 if (!(dev->raw->enabled_protocols & RC_TYPE_NEC))
Mauro Carvalho Chehab7f20d322010-04-04 14:45:04 -030056 return 0;
57
Maxim Levitsky46519182010-10-16 19:56:28 -030058 if (!is_timing_event(ev)) {
59 if (ev.reset)
60 data->state = STATE_INACTIVE;
Mauro Carvalho Chehab2f16f632010-04-03 18:51:50 -030061 return 0;
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -030062 }
63
David Härdemane40b1122010-04-15 18:46:00 -030064 IR_dprintk(2, "NEC decode started at state %d (%uus %s)\n",
65 data->state, TO_US(ev.duration), TO_STR(ev.pulse));
David Härdeman724e2492010-04-08 13:10:00 -030066
67 switch (data->state) {
68
69 case STATE_INACTIVE:
David Härdemane40b1122010-04-15 18:46:00 -030070 if (!ev.pulse)
71 break;
72
Sean Young743135e2012-07-31 06:37:29 -030073 if (eq_margin(ev.duration, NEC_HEADER_PULSE, NEC_UNIT * 2)) {
Maxim Levitsky86ff0712010-07-31 11:59:20 -030074 data->is_nec_x = false;
75 data->necx_repeat = false;
76 } else if (eq_margin(ev.duration, NECX_HEADER_PULSE, NEC_UNIT / 2))
77 data->is_nec_x = true;
78 else
David Härdemane40b1122010-04-15 18:46:00 -030079 break;
80
81 data->count = 0;
82 data->state = STATE_HEADER_SPACE;
David Härdeman724e2492010-04-08 13:10:00 -030083 return 0;
84
85 case STATE_HEADER_SPACE:
David Härdemane40b1122010-04-15 18:46:00 -030086 if (ev.pulse)
87 break;
88
Sean Young743135e2012-07-31 06:37:29 -030089 if (eq_margin(ev.duration, NEC_HEADER_SPACE, NEC_UNIT)) {
David Härdeman724e2492010-04-08 13:10:00 -030090 data->state = STATE_BIT_PULSE;
91 return 0;
David Härdemane40b1122010-04-15 18:46:00 -030092 } else if (eq_margin(ev.duration, NEC_REPEAT_SPACE, NEC_UNIT / 2)) {
Mariusz Białończyk21d33012010-12-29 19:48:43 -030093 if (!dev->keypressed) {
94 IR_dprintk(1, "Discarding last key repeat: event after key up\n");
95 } else {
96 rc_repeat(dev);
97 IR_dprintk(1, "Repeat last key\n");
98 data->state = STATE_TRAILER_PULSE;
99 }
David Härdeman724e2492010-04-08 13:10:00 -0300100 return 0;
101 }
David Härdemane40b1122010-04-15 18:46:00 -0300102
David Härdeman724e2492010-04-08 13:10:00 -0300103 break;
104
105 case STATE_BIT_PULSE:
David Härdemane40b1122010-04-15 18:46:00 -0300106 if (!ev.pulse)
107 break;
108
109 if (!eq_margin(ev.duration, NEC_BIT_PULSE, NEC_UNIT / 2))
110 break;
111
112 data->state = STATE_BIT_SPACE;
113 return 0;
David Härdeman724e2492010-04-08 13:10:00 -0300114
115 case STATE_BIT_SPACE:
David Härdemane40b1122010-04-15 18:46:00 -0300116 if (ev.pulse)
David Härdeman724e2492010-04-08 13:10:00 -0300117 break;
118
Maxim Levitsky86ff0712010-07-31 11:59:20 -0300119 if (data->necx_repeat && data->count == NECX_REPEAT_BITS &&
120 geq_margin(ev.duration,
121 NEC_TRAILER_SPACE, NEC_UNIT / 2)) {
122 IR_dprintk(1, "Repeat last key\n");
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300123 rc_repeat(dev);
Maxim Levitsky86ff0712010-07-31 11:59:20 -0300124 data->state = STATE_INACTIVE;
125 return 0;
126
127 } else if (data->count > NECX_REPEAT_BITS)
128 data->necx_repeat = false;
129
David Härdemanc2163692010-06-13 17:29:36 -0300130 data->bits <<= 1;
David Härdemane40b1122010-04-15 18:46:00 -0300131 if (eq_margin(ev.duration, NEC_BIT_1_SPACE, NEC_UNIT / 2))
David Härdemanc2163692010-06-13 17:29:36 -0300132 data->bits |= 1;
David Härdemane40b1122010-04-15 18:46:00 -0300133 else if (!eq_margin(ev.duration, NEC_BIT_0_SPACE, NEC_UNIT / 2))
134 break;
David Härdeman724e2492010-04-08 13:10:00 -0300135 data->count++;
136
David Härdemane40b1122010-04-15 18:46:00 -0300137 if (data->count == NEC_NBITS)
138 data->state = STATE_TRAILER_PULSE;
139 else
David Härdeman724e2492010-04-08 13:10:00 -0300140 data->state = STATE_BIT_PULSE;
David Härdemane40b1122010-04-15 18:46:00 -0300141
142 return 0;
143
144 case STATE_TRAILER_PULSE:
145 if (!ev.pulse)
146 break;
147
148 if (!eq_margin(ev.duration, NEC_TRAILER_PULSE, NEC_UNIT / 2))
149 break;
150
151 data->state = STATE_TRAILER_SPACE;
152 return 0;
153
154 case STATE_TRAILER_SPACE:
155 if (ev.pulse)
156 break;
157
158 if (!geq_margin(ev.duration, NEC_TRAILER_SPACE, NEC_UNIT / 2))
159 break;
David Härdeman724e2492010-04-08 13:10:00 -0300160
David Härdemanc2163692010-06-13 17:29:36 -0300161 address = bitrev8((data->bits >> 24) & 0xff);
162 not_address = bitrev8((data->bits >> 16) & 0xff);
163 command = bitrev8((data->bits >> 8) & 0xff);
164 not_command = bitrev8((data->bits >> 0) & 0xff);
David Härdeman724e2492010-04-08 13:10:00 -0300165
166 if ((command ^ not_command) != 0xff) {
167 IR_dprintk(1, "NEC checksum error: received 0x%08x\n",
David Härdemanc2163692010-06-13 17:29:36 -0300168 data->bits);
Jarod Wilson4be22b62011-03-22 17:23:15 -0300169 send_32bits = true;
David Härdeman724e2492010-04-08 13:10:00 -0300170 }
171
Jarod Wilson4be22b62011-03-22 17:23:15 -0300172 if (send_32bits) {
173 /* NEC transport, but modified protocol, used by at
174 * least Apple and TiVo remotes */
175 scancode = data->bits;
176 IR_dprintk(1, "NEC (modified) scancode 0x%08x\n", scancode);
177 } else if ((address ^ not_address) != 0xff) {
David Härdeman724e2492010-04-08 13:10:00 -0300178 /* Extended NEC */
179 scancode = address << 16 |
180 not_address << 8 |
181 command;
182 IR_dprintk(1, "NEC (Ext) scancode 0x%06x\n", scancode);
183 } else {
David Härdemane40b1122010-04-15 18:46:00 -0300184 /* Normal NEC */
David Härdeman724e2492010-04-08 13:10:00 -0300185 scancode = address << 8 | command;
186 IR_dprintk(1, "NEC scancode 0x%04x\n", scancode);
187 }
188
Maxim Levitsky86ff0712010-07-31 11:59:20 -0300189 if (data->is_nec_x)
190 data->necx_repeat = true;
191
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300192 rc_keydown(dev, scancode, 0);
David Härdemane40b1122010-04-15 18:46:00 -0300193 data->state = STATE_INACTIVE;
David Härdeman724e2492010-04-08 13:10:00 -0300194 return 0;
David Härdeman724e2492010-04-08 13:10:00 -0300195 }
196
Mauro Carvalho Chehab39cac372011-11-23 12:03:32 -0300197 IR_dprintk(1, "NEC decode failed at count %d state %d (%uus %s)\n",
198 data->count, data->state, TO_US(ev.duration), TO_STR(ev.pulse));
Mauro Carvalho Chehab2f16f632010-04-03 18:51:50 -0300199 data->state = STATE_INACTIVE;
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -0300200 return -EINVAL;
201}
Mauro Carvalho Chehabada39632010-03-21 12:24:24 -0300202
Mauro Carvalho Chehab995187b2010-03-24 20:47:53 -0300203static struct ir_raw_handler nec_handler = {
Mauro Carvalho Chehab52b66142010-11-17 14:20:52 -0300204 .protocols = RC_TYPE_NEC,
Mauro Carvalho Chehab20d5f112010-03-25 23:49:46 -0300205 .decode = ir_nec_decode,
Mauro Carvalho Chehab995187b2010-03-24 20:47:53 -0300206};
207
208static int __init ir_nec_decode_init(void)
209{
210 ir_raw_handler_register(&nec_handler);
211
212 printk(KERN_INFO "IR NEC protocol handler initialized\n");
213 return 0;
214}
215
216static void __exit ir_nec_decode_exit(void)
217{
218 ir_raw_handler_unregister(&nec_handler);
219}
220
221module_init(ir_nec_decode_init);
222module_exit(ir_nec_decode_exit);
223
224MODULE_LICENSE("GPL");
225MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
226MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)");
227MODULE_DESCRIPTION("NEC IR protocol decoder");