[media] rc-core: document the protocol type
[linux-2.6-block.git] / drivers / media / rc / ir-nec-decoder.c
CommitLineData
995187be 1/* ir-nec-decoder.c - handle NEC IR Pulse/Space protocol
a3572c34 2 *
37e59f87 3 * Copyright (C) 2010 by Mauro Carvalho Chehab
a3572c34
MCC
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
724e2495 15#include <linux/bitrev.h>
7a707b89 16#include <linux/module.h>
f62de675 17#include "rc-core-priv.h"
a3572c34 18
67780d6a 19#define NEC_NBITS 32
724e2495 20#define NEC_UNIT 562500 /* ns */
e40b1127
DH
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)
e31f4127 24#define NEC_REPEAT_SPACE (4 * NEC_UNIT)
e40b1127
DH
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 */
86ff071c 30#define NECX_REPEAT_BITS 1
9f154782 31
2f16f631
MCC
32enum nec_state {
33 STATE_INACTIVE,
2f16f631 34 STATE_HEADER_SPACE,
724e2495
DH
35 STATE_BIT_PULSE,
36 STATE_BIT_SPACE,
37 STATE_TRAILER_PULSE,
2f16f631
MCC
38 STATE_TRAILER_SPACE,
39};
40
2f16f631 41/**
587835a4 42 * ir_nec_decode() - Decode one NEC pulse or space
d8b4b582 43 * @dev: the struct rc_dev descriptor of the device
e40b1127 44 * @duration: the struct ir_raw_event descriptor of the pulse/space
2f16f631
MCC
45 *
46 * This function returns -EINVAL if the pulse violates the state machine
9f154782 47 */
d8b4b582 48static int ir_nec_decode(struct rc_dev *dev, struct ir_raw_event ev)
9f154782 49{
d8b4b582 50 struct nec_dec *data = &dev->raw->nec;
724e2495
DH
51 u32 scancode;
52 u8 address, not_address, command, not_command;
4be22b6a 53 bool send_32bits = false;
9f154782 54
1a1934fa 55 if (!rc_protocols_enabled(dev, RC_BIT_NEC))
7f20d32d
MCC
56 return 0;
57
4651918a
ML
58 if (!is_timing_event(ev)) {
59 if (ev.reset)
60 data->state = STATE_INACTIVE;
724e2495
DH
61 return 0;
62 }
a3572c34 63
e40b1127
DH
64 IR_dprintk(2, "NEC decode started at state %d (%uus %s)\n",
65 data->state, TO_US(ev.duration), TO_STR(ev.pulse));
9f154782 66
2f16f631 67 switch (data->state) {
a3572c34 68
724e2495 69 case STATE_INACTIVE:
e40b1127
DH
70 if (!ev.pulse)
71 break;
72
743135e7 73 if (eq_margin(ev.duration, NEC_HEADER_PULSE, NEC_UNIT * 2)) {
86ff071c
ML
74 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
e40b1127
DH
79 break;
80
81 data->count = 0;
82 data->state = STATE_HEADER_SPACE;
2f16f631 83 return 0;
724e2495 84
2f16f631 85 case STATE_HEADER_SPACE:
e40b1127
DH
86 if (ev.pulse)
87 break;
88
743135e7 89 if (eq_margin(ev.duration, NEC_HEADER_SPACE, NEC_UNIT)) {
724e2495 90 data->state = STATE_BIT_PULSE;
9f154782 91 return 0;
e40b1127 92 } else if (eq_margin(ev.duration, NEC_REPEAT_SPACE, NEC_UNIT / 2)) {
21d33014
MB
93 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 }
2f16f631
MCC
100 return 0;
101 }
e40b1127 102
724e2495
DH
103 break;
104
105 case STATE_BIT_PULSE:
e40b1127
DH
106 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;
724e2495
DH
114
115 case STATE_BIT_SPACE:
e40b1127 116 if (ev.pulse)
724e2495
DH
117 break;
118
86ff071c
ML
119 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");
ca86674b 123 rc_repeat(dev);
86ff071c
ML
124 data->state = STATE_INACTIVE;
125 return 0;
126
127 } else if (data->count > NECX_REPEAT_BITS)
128 data->necx_repeat = false;
129
c216369e 130 data->bits <<= 1;
e40b1127 131 if (eq_margin(ev.duration, NEC_BIT_1_SPACE, NEC_UNIT / 2))
c216369e 132 data->bits |= 1;
e40b1127
DH
133 else if (!eq_margin(ev.duration, NEC_BIT_0_SPACE, NEC_UNIT / 2))
134 break;
724e2495
DH
135 data->count++;
136
e40b1127
DH
137 if (data->count == NEC_NBITS)
138 data->state = STATE_TRAILER_PULSE;
139 else
724e2495 140 data->state = STATE_BIT_PULSE;
e40b1127
DH
141
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;
724e2495 160
c216369e
DH
161 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);
724e2495
DH
165
166 if ((command ^ not_command) != 0xff) {
167 IR_dprintk(1, "NEC checksum error: received 0x%08x\n",
c216369e 168 data->bits);
4be22b6a 169 send_32bits = true;
2f16f631 170 }
a3572c34 171
4be22b6a
JW
172 if (send_32bits) {
173 /* NEC transport, but modified protocol, used by at
174 * least Apple and TiVo remotes */
42f5e630 175 scancode = data->bits;
4be22b6a
JW
176 IR_dprintk(1, "NEC (modified) scancode 0x%08x\n", scancode);
177 } else if ((address ^ not_address) != 0xff) {
724e2495
DH
178 /* 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 {
e40b1127 184 /* Normal NEC */
724e2495
DH
185 scancode = address << 8 | command;
186 IR_dprintk(1, "NEC scancode 0x%04x\n", scancode);
a3572c34 187 }
724e2495 188
86ff071c
ML
189 if (data->is_nec_x)
190 data->necx_repeat = true;
191
120703f9 192 rc_keydown(dev, RC_TYPE_NEC, scancode, 0);
e40b1127 193 data->state = STATE_INACTIVE;
2f16f631 194 return 0;
724e2495 195 }
a3572c34 196
39cac375
MCC
197 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));
2f16f631 199 data->state = STATE_INACTIVE;
a3572c34
MCC
200 return -EINVAL;
201}
ada39630 202
995187be 203static struct ir_raw_handler nec_handler = {
c003ab1b 204 .protocols = RC_BIT_NEC,
20d5f116 205 .decode = ir_nec_decode,
995187be
MCC
206};
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");
37e59f87 225MODULE_AUTHOR("Mauro Carvalho Chehab");
995187be
MCC
226MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)");
227MODULE_DESCRIPTION("NEC IR protocol decoder");