[media] media: rc: move check whether a protocol is enabled to the core
[deliverable/linux.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
4651918a
ML
55 if (!is_timing_event(ev)) {
56 if (ev.reset)
57 data->state = STATE_INACTIVE;
724e2495
DH
58 return 0;
59 }
a3572c34 60
e40b1127
DH
61 IR_dprintk(2, "NEC decode started at state %d (%uus %s)\n",
62 data->state, TO_US(ev.duration), TO_STR(ev.pulse));
9f154782 63
2f16f631 64 switch (data->state) {
a3572c34 65
724e2495 66 case STATE_INACTIVE:
e40b1127
DH
67 if (!ev.pulse)
68 break;
69
743135e7 70 if (eq_margin(ev.duration, NEC_HEADER_PULSE, NEC_UNIT * 2)) {
86ff071c
ML
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
e40b1127
DH
76 break;
77
78 data->count = 0;
79 data->state = STATE_HEADER_SPACE;
2f16f631 80 return 0;
724e2495 81
2f16f631 82 case STATE_HEADER_SPACE:
e40b1127
DH
83 if (ev.pulse)
84 break;
85
743135e7 86 if (eq_margin(ev.duration, NEC_HEADER_SPACE, NEC_UNIT)) {
724e2495 87 data->state = STATE_BIT_PULSE;
9f154782 88 return 0;
e40b1127 89 } else if (eq_margin(ev.duration, NEC_REPEAT_SPACE, NEC_UNIT / 2)) {
21d33014
MB
90 if (!dev->keypressed) {
91 IR_dprintk(1, "Discarding last key repeat: event after key up\n");
92 } else {
93 rc_repeat(dev);
94 IR_dprintk(1, "Repeat last key\n");
95 data->state = STATE_TRAILER_PULSE;
96 }
2f16f631
MCC
97 return 0;
98 }
e40b1127 99
724e2495
DH
100 break;
101
102 case STATE_BIT_PULSE:
e40b1127
DH
103 if (!ev.pulse)
104 break;
105
106 if (!eq_margin(ev.duration, NEC_BIT_PULSE, NEC_UNIT / 2))
107 break;
108
109 data->state = STATE_BIT_SPACE;
110 return 0;
724e2495
DH
111
112 case STATE_BIT_SPACE:
e40b1127 113 if (ev.pulse)
724e2495
DH
114 break;
115
86ff071c
ML
116 if (data->necx_repeat && data->count == NECX_REPEAT_BITS &&
117 geq_margin(ev.duration,
118 NEC_TRAILER_SPACE, NEC_UNIT / 2)) {
119 IR_dprintk(1, "Repeat last key\n");
ca86674b 120 rc_repeat(dev);
86ff071c
ML
121 data->state = STATE_INACTIVE;
122 return 0;
123
124 } else if (data->count > NECX_REPEAT_BITS)
125 data->necx_repeat = false;
126
c216369e 127 data->bits <<= 1;
e40b1127 128 if (eq_margin(ev.duration, NEC_BIT_1_SPACE, NEC_UNIT / 2))
c216369e 129 data->bits |= 1;
e40b1127
DH
130 else if (!eq_margin(ev.duration, NEC_BIT_0_SPACE, NEC_UNIT / 2))
131 break;
724e2495
DH
132 data->count++;
133
e40b1127
DH
134 if (data->count == NEC_NBITS)
135 data->state = STATE_TRAILER_PULSE;
136 else
724e2495 137 data->state = STATE_BIT_PULSE;
e40b1127
DH
138
139 return 0;
140
141 case STATE_TRAILER_PULSE:
142 if (!ev.pulse)
143 break;
144
145 if (!eq_margin(ev.duration, NEC_TRAILER_PULSE, NEC_UNIT / 2))
146 break;
147
148 data->state = STATE_TRAILER_SPACE;
149 return 0;
150
151 case STATE_TRAILER_SPACE:
152 if (ev.pulse)
153 break;
154
155 if (!geq_margin(ev.duration, NEC_TRAILER_SPACE, NEC_UNIT / 2))
156 break;
724e2495 157
c216369e
DH
158 address = bitrev8((data->bits >> 24) & 0xff);
159 not_address = bitrev8((data->bits >> 16) & 0xff);
160 command = bitrev8((data->bits >> 8) & 0xff);
161 not_command = bitrev8((data->bits >> 0) & 0xff);
724e2495
DH
162
163 if ((command ^ not_command) != 0xff) {
164 IR_dprintk(1, "NEC checksum error: received 0x%08x\n",
c216369e 165 data->bits);
4be22b6a 166 send_32bits = true;
2f16f631 167 }
a3572c34 168
4be22b6a
JW
169 if (send_32bits) {
170 /* NEC transport, but modified protocol, used by at
171 * least Apple and TiVo remotes */
42f5e630 172 scancode = data->bits;
4be22b6a
JW
173 IR_dprintk(1, "NEC (modified) scancode 0x%08x\n", scancode);
174 } else if ((address ^ not_address) != 0xff) {
724e2495
DH
175 /* Extended NEC */
176 scancode = address << 16 |
177 not_address << 8 |
178 command;
179 IR_dprintk(1, "NEC (Ext) scancode 0x%06x\n", scancode);
180 } else {
e40b1127 181 /* Normal NEC */
724e2495
DH
182 scancode = address << 8 | command;
183 IR_dprintk(1, "NEC scancode 0x%04x\n", scancode);
a3572c34 184 }
724e2495 185
86ff071c
ML
186 if (data->is_nec_x)
187 data->necx_repeat = true;
188
120703f9 189 rc_keydown(dev, RC_TYPE_NEC, scancode, 0);
e40b1127 190 data->state = STATE_INACTIVE;
2f16f631 191 return 0;
724e2495 192 }
a3572c34 193
39cac375
MCC
194 IR_dprintk(1, "NEC decode failed at count %d state %d (%uus %s)\n",
195 data->count, data->state, TO_US(ev.duration), TO_STR(ev.pulse));
2f16f631 196 data->state = STATE_INACTIVE;
a3572c34
MCC
197 return -EINVAL;
198}
ada39630 199
995187be 200static struct ir_raw_handler nec_handler = {
c003ab1b 201 .protocols = RC_BIT_NEC,
20d5f116 202 .decode = ir_nec_decode,
995187be
MCC
203};
204
205static int __init ir_nec_decode_init(void)
206{
207 ir_raw_handler_register(&nec_handler);
208
209 printk(KERN_INFO "IR NEC protocol handler initialized\n");
210 return 0;
211}
212
213static void __exit ir_nec_decode_exit(void)
214{
215 ir_raw_handler_unregister(&nec_handler);
216}
217
218module_init(ir_nec_decode_init);
219module_exit(ir_nec_decode_exit);
220
221MODULE_LICENSE("GPL");
37e59f87 222MODULE_AUTHOR("Mauro Carvalho Chehab");
995187be
MCC
223MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)");
224MODULE_DESCRIPTION("NEC IR protocol decoder");
This page took 0.396002 seconds and 5 git commands to generate.