V4L/DVB: ir-core: move decoding state to ir_raw_event_ctrl
[deliverable/linux.git] / drivers / media / IR / ir-nec-decoder.c
CommitLineData
995187be 1/* ir-nec-decoder.c - handle NEC IR Pulse/Space protocol
a3572c34
MCC
2 *
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
724e2495 15#include <linux/bitrev.h>
3f113e36 16#include "ir-core-priv.h"
a3572c34 17
67780d6a 18#define NEC_NBITS 32
724e2495 19#define NEC_UNIT 562500 /* ns */
e40b1127
DH
20#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)
23#define NEC_REPEAT_SPACE (8 * NEC_UNIT)
24#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 */
9f154782 29
2f16f631
MCC
30enum nec_state {
31 STATE_INACTIVE,
2f16f631 32 STATE_HEADER_SPACE,
724e2495
DH
33 STATE_BIT_PULSE,
34 STATE_BIT_SPACE,
35 STATE_TRAILER_PULSE,
2f16f631
MCC
36 STATE_TRAILER_SPACE,
37};
38
2f16f631 39/**
587835a4 40 * ir_nec_decode() - Decode one NEC pulse or space
9f154782 41 * @input_dev: the struct input_dev descriptor of the device
e40b1127 42 * @duration: the struct ir_raw_event descriptor of the pulse/space
2f16f631
MCC
43 *
44 * This function returns -EINVAL if the pulse violates the state machine
9f154782 45 */
e40b1127 46static int ir_nec_decode(struct input_dev *input_dev, struct ir_raw_event ev)
9f154782 47{
2f16f631 48 struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
c216369e 49 struct nec_dec *data = &ir_dev->raw->nec;
724e2495
DH
50 u32 scancode;
51 u8 address, not_address, command, not_command;
9f154782 52
667c9ebe 53 if (!(ir_dev->raw->enabled_protocols & IR_TYPE_NEC))
7f20d32d
MCC
54 return 0;
55
e40b1127 56 if (IS_RESET(ev)) {
2f16f631 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
70 if (!eq_margin(ev.duration, NEC_HEADER_PULSE, NEC_UNIT / 2) &&
71 !eq_margin(ev.duration, NECX_HEADER_PULSE, NEC_UNIT / 2))
72 break;
73
74 data->count = 0;
75 data->state = STATE_HEADER_SPACE;
2f16f631 76 return 0;
724e2495 77
2f16f631 78 case STATE_HEADER_SPACE:
e40b1127
DH
79 if (ev.pulse)
80 break;
81
82 if (eq_margin(ev.duration, NEC_HEADER_SPACE, NEC_UNIT / 2)) {
724e2495 83 data->state = STATE_BIT_PULSE;
9f154782 84 return 0;
e40b1127 85 } else if (eq_margin(ev.duration, NEC_REPEAT_SPACE, NEC_UNIT / 2)) {
2f16f631
MCC
86 ir_repeat(input_dev);
87 IR_dprintk(1, "Repeat last key\n");
724e2495 88 data->state = STATE_TRAILER_PULSE;
2f16f631
MCC
89 return 0;
90 }
e40b1127 91
724e2495
DH
92 break;
93
94 case STATE_BIT_PULSE:
e40b1127
DH
95 if (!ev.pulse)
96 break;
97
98 if (!eq_margin(ev.duration, NEC_BIT_PULSE, NEC_UNIT / 2))
99 break;
100
101 data->state = STATE_BIT_SPACE;
102 return 0;
724e2495
DH
103
104 case STATE_BIT_SPACE:
e40b1127 105 if (ev.pulse)
724e2495
DH
106 break;
107
c216369e 108 data->bits <<= 1;
e40b1127 109 if (eq_margin(ev.duration, NEC_BIT_1_SPACE, NEC_UNIT / 2))
c216369e 110 data->bits |= 1;
e40b1127
DH
111 else if (!eq_margin(ev.duration, NEC_BIT_0_SPACE, NEC_UNIT / 2))
112 break;
724e2495
DH
113 data->count++;
114
e40b1127
DH
115 if (data->count == NEC_NBITS)
116 data->state = STATE_TRAILER_PULSE;
117 else
724e2495 118 data->state = STATE_BIT_PULSE;
e40b1127
DH
119
120 return 0;
121
122 case STATE_TRAILER_PULSE:
123 if (!ev.pulse)
124 break;
125
126 if (!eq_margin(ev.duration, NEC_TRAILER_PULSE, NEC_UNIT / 2))
127 break;
128
129 data->state = STATE_TRAILER_SPACE;
130 return 0;
131
132 case STATE_TRAILER_SPACE:
133 if (ev.pulse)
134 break;
135
136 if (!geq_margin(ev.duration, NEC_TRAILER_SPACE, NEC_UNIT / 2))
137 break;
724e2495 138
c216369e
DH
139 address = bitrev8((data->bits >> 24) & 0xff);
140 not_address = bitrev8((data->bits >> 16) & 0xff);
141 command = bitrev8((data->bits >> 8) & 0xff);
142 not_command = bitrev8((data->bits >> 0) & 0xff);
724e2495
DH
143
144 if ((command ^ not_command) != 0xff) {
145 IR_dprintk(1, "NEC checksum error: received 0x%08x\n",
c216369e 146 data->bits);
724e2495 147 break;
2f16f631 148 }
a3572c34 149
724e2495
DH
150 if ((address ^ not_address) != 0xff) {
151 /* Extended NEC */
152 scancode = address << 16 |
153 not_address << 8 |
154 command;
155 IR_dprintk(1, "NEC (Ext) scancode 0x%06x\n", scancode);
156 } else {
e40b1127 157 /* Normal NEC */
724e2495
DH
158 scancode = address << 8 | command;
159 IR_dprintk(1, "NEC scancode 0x%04x\n", scancode);
a3572c34 160 }
724e2495
DH
161
162 ir_keydown(input_dev, scancode, 0);
e40b1127 163 data->state = STATE_INACTIVE;
2f16f631 164 return 0;
724e2495 165 }
a3572c34 166
e40b1127
DH
167 IR_dprintk(1, "NEC decode failed at state %d (%uus %s)\n",
168 data->state, TO_US(ev.duration), TO_STR(ev.pulse));
2f16f631 169 data->state = STATE_INACTIVE;
a3572c34
MCC
170 return -EINVAL;
171}
ada39630 172
995187be 173static struct ir_raw_handler nec_handler = {
667c9ebe 174 .protocols = IR_TYPE_NEC,
20d5f116 175 .decode = ir_nec_decode,
995187be
MCC
176};
177
178static int __init ir_nec_decode_init(void)
179{
180 ir_raw_handler_register(&nec_handler);
181
182 printk(KERN_INFO "IR NEC protocol handler initialized\n");
183 return 0;
184}
185
186static void __exit ir_nec_decode_exit(void)
187{
188 ir_raw_handler_unregister(&nec_handler);
189}
190
191module_init(ir_nec_decode_init);
192module_exit(ir_nec_decode_exit);
193
194MODULE_LICENSE("GPL");
195MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
196MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)");
197MODULE_DESCRIPTION("NEC IR protocol decoder");
This page took 0.056993 seconds and 5 git commands to generate.