blob: 63ee722dbd0204ea61860971b1c1d0a37d923dff [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>
Mauro Carvalho Chehabf62de672010-11-09 23:09:57 -030016#include "rc-core-priv.h"
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -030017
Mauro Carvalho Chehab67780d62010-04-03 20:33:00 -030018#define NEC_NBITS 32
David Härdeman724e2492010-04-08 13:10:00 -030019#define NEC_UNIT 562500 /* ns */
David Härdemane40b1122010-04-15 18:46:00 -030020#define NEC_HEADER_PULSE (16 * NEC_UNIT)
21#define NECX_HEADER_PULSE (8 * NEC_UNIT) /* Less common NEC variant */
22#define NEC_HEADER_SPACE (8 * NEC_UNIT)
Maxim Levitskye31f4122010-07-31 11:59:19 -030023#define NEC_REPEAT_SPACE (4 * NEC_UNIT)
David Härdemane40b1122010-04-15 18:46:00 -030024#define NEC_BIT_PULSE (1 * NEC_UNIT)
25#define NEC_BIT_0_SPACE (1 * NEC_UNIT)
26#define NEC_BIT_1_SPACE (3 * NEC_UNIT)
27#define NEC_TRAILER_PULSE (1 * NEC_UNIT)
28#define NEC_TRAILER_SPACE (10 * NEC_UNIT) /* even longer in reality */
Maxim Levitsky86ff0712010-07-31 11:59:20 -030029#define NECX_REPEAT_BITS 1
Mauro Carvalho Chehab9f154782010-03-21 13:00:55 -030030
Mauro Carvalho Chehab2f16f632010-04-03 18:51:50 -030031enum nec_state {
32 STATE_INACTIVE,
Mauro Carvalho Chehab2f16f632010-04-03 18:51:50 -030033 STATE_HEADER_SPACE,
David Härdeman724e2492010-04-08 13:10:00 -030034 STATE_BIT_PULSE,
35 STATE_BIT_SPACE,
36 STATE_TRAILER_PULSE,
Mauro Carvalho Chehab2f16f632010-04-03 18:51:50 -030037 STATE_TRAILER_SPACE,
38};
39
Mauro Carvalho Chehabada39632010-03-21 12:24:24 -030040/**
Mauro Carvalho Chehab587835a2010-04-04 10:44:51 -030041 * ir_nec_decode() - Decode one NEC pulse or space
David Härdemand8b4b582010-10-29 16:08:23 -030042 * @dev: the struct rc_dev descriptor of the device
David Härdemane40b1122010-04-15 18:46:00 -030043 * @duration: the struct ir_raw_event descriptor of the pulse/space
Mauro Carvalho Chehab2f16f632010-04-03 18:51:50 -030044 *
45 * This function returns -EINVAL if the pulse violates the state machine
Mauro Carvalho Chehabada39632010-03-21 12:24:24 -030046 */
David Härdemand8b4b582010-10-29 16:08:23 -030047static int ir_nec_decode(struct rc_dev *dev, struct ir_raw_event ev)
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -030048{
David Härdemand8b4b582010-10-29 16:08:23 -030049 struct nec_dec *data = &dev->raw->nec;
David Härdeman724e2492010-04-08 13:10:00 -030050 u32 scancode;
51 u8 address, not_address, command, not_command;
Jarod Wilson4be22b62011-03-22 17:23:15 -030052 bool send_32bits = false;
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -030053
Mauro Carvalho Chehab52b66142010-11-17 14:20:52 -030054 if (!(dev->raw->enabled_protocols & RC_TYPE_NEC))
Mauro Carvalho Chehab7f20d322010-04-04 14:45:04 -030055 return 0;
56
Maxim Levitsky46519182010-10-16 19:56:28 -030057 if (!is_timing_event(ev)) {
58 if (ev.reset)
59 data->state = STATE_INACTIVE;
Mauro Carvalho Chehab2f16f632010-04-03 18:51:50 -030060 return 0;
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -030061 }
62
David Härdemane40b1122010-04-15 18:46:00 -030063 IR_dprintk(2, "NEC decode started at state %d (%uus %s)\n",
64 data->state, TO_US(ev.duration), TO_STR(ev.pulse));
David Härdeman724e2492010-04-08 13:10:00 -030065
66 switch (data->state) {
67
68 case STATE_INACTIVE:
David Härdemane40b1122010-04-15 18:46:00 -030069 if (!ev.pulse)
70 break;
71
Maxim Levitsky86ff0712010-07-31 11:59:20 -030072 if (eq_margin(ev.duration, NEC_HEADER_PULSE, NEC_UNIT / 2)) {
73 data->is_nec_x = false;
74 data->necx_repeat = false;
75 } else if (eq_margin(ev.duration, NECX_HEADER_PULSE, NEC_UNIT / 2))
76 data->is_nec_x = true;
77 else
David Härdemane40b1122010-04-15 18:46:00 -030078 break;
79
80 data->count = 0;
81 data->state = STATE_HEADER_SPACE;
David Härdeman724e2492010-04-08 13:10:00 -030082 return 0;
83
84 case STATE_HEADER_SPACE:
David Härdemane40b1122010-04-15 18:46:00 -030085 if (ev.pulse)
86 break;
87
88 if (eq_margin(ev.duration, NEC_HEADER_SPACE, NEC_UNIT / 2)) {
David Härdeman724e2492010-04-08 13:10:00 -030089 data->state = STATE_BIT_PULSE;
90 return 0;
David Härdemane40b1122010-04-15 18:46:00 -030091 } else if (eq_margin(ev.duration, NEC_REPEAT_SPACE, NEC_UNIT / 2)) {
Mariusz Białończyk21d33012010-12-29 19:48:43 -030092 if (!dev->keypressed) {
93 IR_dprintk(1, "Discarding last key repeat: event after key up\n");
94 } else {
95 rc_repeat(dev);
96 IR_dprintk(1, "Repeat last key\n");
97 data->state = STATE_TRAILER_PULSE;
98 }
David Härdeman724e2492010-04-08 13:10:00 -030099 return 0;
100 }
David Härdemane40b1122010-04-15 18:46:00 -0300101
David Härdeman724e2492010-04-08 13:10:00 -0300102 break;
103
104 case STATE_BIT_PULSE:
David Härdemane40b1122010-04-15 18:46:00 -0300105 if (!ev.pulse)
106 break;
107
108 if (!eq_margin(ev.duration, NEC_BIT_PULSE, NEC_UNIT / 2))
109 break;
110
111 data->state = STATE_BIT_SPACE;
112 return 0;
David Härdeman724e2492010-04-08 13:10:00 -0300113
114 case STATE_BIT_SPACE:
David Härdemane40b1122010-04-15 18:46:00 -0300115 if (ev.pulse)
David Härdeman724e2492010-04-08 13:10:00 -0300116 break;
117
Maxim Levitsky86ff0712010-07-31 11:59:20 -0300118 if (data->necx_repeat && data->count == NECX_REPEAT_BITS &&
119 geq_margin(ev.duration,
120 NEC_TRAILER_SPACE, NEC_UNIT / 2)) {
121 IR_dprintk(1, "Repeat last key\n");
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300122 rc_repeat(dev);
Maxim Levitsky86ff0712010-07-31 11:59:20 -0300123 data->state = STATE_INACTIVE;
124 return 0;
125
126 } else if (data->count > NECX_REPEAT_BITS)
127 data->necx_repeat = false;
128
David Härdemanc2163692010-06-13 17:29:36 -0300129 data->bits <<= 1;
David Härdemane40b1122010-04-15 18:46:00 -0300130 if (eq_margin(ev.duration, NEC_BIT_1_SPACE, NEC_UNIT / 2))
David Härdemanc2163692010-06-13 17:29:36 -0300131 data->bits |= 1;
David Härdemane40b1122010-04-15 18:46:00 -0300132 else if (!eq_margin(ev.duration, NEC_BIT_0_SPACE, NEC_UNIT / 2))
133 break;
David Härdeman724e2492010-04-08 13:10:00 -0300134 data->count++;
135
David Härdemane40b1122010-04-15 18:46:00 -0300136 if (data->count == NEC_NBITS)
137 data->state = STATE_TRAILER_PULSE;
138 else
David Härdeman724e2492010-04-08 13:10:00 -0300139 data->state = STATE_BIT_PULSE;
David Härdemane40b1122010-04-15 18:46:00 -0300140
141 return 0;
142
143 case STATE_TRAILER_PULSE:
144 if (!ev.pulse)
145 break;
146
147 if (!eq_margin(ev.duration, NEC_TRAILER_PULSE, NEC_UNIT / 2))
148 break;
149
150 data->state = STATE_TRAILER_SPACE;
151 return 0;
152
153 case STATE_TRAILER_SPACE:
154 if (ev.pulse)
155 break;
156
157 if (!geq_margin(ev.duration, NEC_TRAILER_SPACE, NEC_UNIT / 2))
158 break;
David Härdeman724e2492010-04-08 13:10:00 -0300159
David Härdemanc2163692010-06-13 17:29:36 -0300160 address = bitrev8((data->bits >> 24) & 0xff);
161 not_address = bitrev8((data->bits >> 16) & 0xff);
162 command = bitrev8((data->bits >> 8) & 0xff);
163 not_command = bitrev8((data->bits >> 0) & 0xff);
David Härdeman724e2492010-04-08 13:10:00 -0300164
165 if ((command ^ not_command) != 0xff) {
166 IR_dprintk(1, "NEC checksum error: received 0x%08x\n",
David Härdemanc2163692010-06-13 17:29:36 -0300167 data->bits);
Jarod Wilson4be22b62011-03-22 17:23:15 -0300168 send_32bits = true;
David Härdeman724e2492010-04-08 13:10:00 -0300169 }
170
Jarod Wilson4be22b62011-03-22 17:23:15 -0300171 if (send_32bits) {
172 /* NEC transport, but modified protocol, used by at
173 * least Apple and TiVo remotes */
174 scancode = data->bits;
175 IR_dprintk(1, "NEC (modified) scancode 0x%08x\n", scancode);
176 } else if ((address ^ not_address) != 0xff) {
David Härdeman724e2492010-04-08 13:10:00 -0300177 /* Extended NEC */
178 scancode = address << 16 |
179 not_address << 8 |
180 command;
181 IR_dprintk(1, "NEC (Ext) scancode 0x%06x\n", scancode);
182 } else {
David Härdemane40b1122010-04-15 18:46:00 -0300183 /* Normal NEC */
David Härdeman724e2492010-04-08 13:10:00 -0300184 scancode = address << 8 | command;
185 IR_dprintk(1, "NEC scancode 0x%04x\n", scancode);
186 }
187
Maxim Levitsky86ff0712010-07-31 11:59:20 -0300188 if (data->is_nec_x)
189 data->necx_repeat = true;
190
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300191 rc_keydown(dev, scancode, 0);
David Härdemane40b1122010-04-15 18:46:00 -0300192 data->state = STATE_INACTIVE;
David Härdeman724e2492010-04-08 13:10:00 -0300193 return 0;
David Härdeman724e2492010-04-08 13:10:00 -0300194 }
195
David Härdemane40b1122010-04-15 18:46:00 -0300196 IR_dprintk(1, "NEC decode failed at state %d (%uus %s)\n",
197 data->state, TO_US(ev.duration), TO_STR(ev.pulse));
Mauro Carvalho Chehab2f16f632010-04-03 18:51:50 -0300198 data->state = STATE_INACTIVE;
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -0300199 return -EINVAL;
200}
Mauro Carvalho Chehabada39632010-03-21 12:24:24 -0300201
Mauro Carvalho Chehab995187b2010-03-24 20:47:53 -0300202static struct ir_raw_handler nec_handler = {
Mauro Carvalho Chehab52b66142010-11-17 14:20:52 -0300203 .protocols = RC_TYPE_NEC,
Mauro Carvalho Chehab20d5f112010-03-25 23:49:46 -0300204 .decode = ir_nec_decode,
Mauro Carvalho Chehab995187b2010-03-24 20:47:53 -0300205};
206
207static int __init ir_nec_decode_init(void)
208{
209 ir_raw_handler_register(&nec_handler);
210
211 printk(KERN_INFO "IR NEC protocol handler initialized\n");
212 return 0;
213}
214
215static void __exit ir_nec_decode_exit(void)
216{
217 ir_raw_handler_unregister(&nec_handler);
218}
219
220module_init(ir_nec_decode_init);
221module_exit(ir_nec_decode_exit);
222
223MODULE_LICENSE("GPL");
224MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
225MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)");
226MODULE_DESCRIPTION("NEC IR protocol decoder");