sfc: Don't use enums as a bitmask.
[zen-stable.git] / drivers / media / rc / ir-nec-decoder.c
blob63ee722dbd0204ea61860971b1c1d0a37d923dff
1 /* ir-nec-decoder.c - handle NEC IR Pulse/Space protocol
3 * Copyright (C) 2010 by Mauro Carvalho Chehab <mchehab@redhat.com>
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.
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.
15 #include <linux/bitrev.h>
16 #include "rc-core-priv.h"
18 #define NEC_NBITS 32
19 #define NEC_UNIT 562500 /* ns */
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 (4 * 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 */
29 #define NECX_REPEAT_BITS 1
31 enum nec_state {
32 STATE_INACTIVE,
33 STATE_HEADER_SPACE,
34 STATE_BIT_PULSE,
35 STATE_BIT_SPACE,
36 STATE_TRAILER_PULSE,
37 STATE_TRAILER_SPACE,
40 /**
41 * ir_nec_decode() - Decode one NEC pulse or space
42 * @dev: the struct rc_dev descriptor of the device
43 * @duration: the struct ir_raw_event descriptor of the pulse/space
45 * This function returns -EINVAL if the pulse violates the state machine
47 static int ir_nec_decode(struct rc_dev *dev, struct ir_raw_event ev)
49 struct nec_dec *data = &dev->raw->nec;
50 u32 scancode;
51 u8 address, not_address, command, not_command;
52 bool send_32bits = false;
54 if (!(dev->raw->enabled_protocols & RC_TYPE_NEC))
55 return 0;
57 if (!is_timing_event(ev)) {
58 if (ev.reset)
59 data->state = STATE_INACTIVE;
60 return 0;
63 IR_dprintk(2, "NEC decode started at state %d (%uus %s)\n",
64 data->state, TO_US(ev.duration), TO_STR(ev.pulse));
66 switch (data->state) {
68 case STATE_INACTIVE:
69 if (!ev.pulse)
70 break;
72 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
78 break;
80 data->count = 0;
81 data->state = STATE_HEADER_SPACE;
82 return 0;
84 case STATE_HEADER_SPACE:
85 if (ev.pulse)
86 break;
88 if (eq_margin(ev.duration, NEC_HEADER_SPACE, NEC_UNIT / 2)) {
89 data->state = STATE_BIT_PULSE;
90 return 0;
91 } else if (eq_margin(ev.duration, NEC_REPEAT_SPACE, NEC_UNIT / 2)) {
92 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;
99 return 0;
102 break;
104 case STATE_BIT_PULSE:
105 if (!ev.pulse)
106 break;
108 if (!eq_margin(ev.duration, NEC_BIT_PULSE, NEC_UNIT / 2))
109 break;
111 data->state = STATE_BIT_SPACE;
112 return 0;
114 case STATE_BIT_SPACE:
115 if (ev.pulse)
116 break;
118 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");
122 rc_repeat(dev);
123 data->state = STATE_INACTIVE;
124 return 0;
126 } else if (data->count > NECX_REPEAT_BITS)
127 data->necx_repeat = false;
129 data->bits <<= 1;
130 if (eq_margin(ev.duration, NEC_BIT_1_SPACE, NEC_UNIT / 2))
131 data->bits |= 1;
132 else if (!eq_margin(ev.duration, NEC_BIT_0_SPACE, NEC_UNIT / 2))
133 break;
134 data->count++;
136 if (data->count == NEC_NBITS)
137 data->state = STATE_TRAILER_PULSE;
138 else
139 data->state = STATE_BIT_PULSE;
141 return 0;
143 case STATE_TRAILER_PULSE:
144 if (!ev.pulse)
145 break;
147 if (!eq_margin(ev.duration, NEC_TRAILER_PULSE, NEC_UNIT / 2))
148 break;
150 data->state = STATE_TRAILER_SPACE;
151 return 0;
153 case STATE_TRAILER_SPACE:
154 if (ev.pulse)
155 break;
157 if (!geq_margin(ev.duration, NEC_TRAILER_SPACE, NEC_UNIT / 2))
158 break;
160 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);
165 if ((command ^ not_command) != 0xff) {
166 IR_dprintk(1, "NEC checksum error: received 0x%08x\n",
167 data->bits);
168 send_32bits = true;
171 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) {
177 /* 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 {
183 /* Normal NEC */
184 scancode = address << 8 | command;
185 IR_dprintk(1, "NEC scancode 0x%04x\n", scancode);
188 if (data->is_nec_x)
189 data->necx_repeat = true;
191 rc_keydown(dev, scancode, 0);
192 data->state = STATE_INACTIVE;
193 return 0;
196 IR_dprintk(1, "NEC decode failed at state %d (%uus %s)\n",
197 data->state, TO_US(ev.duration), TO_STR(ev.pulse));
198 data->state = STATE_INACTIVE;
199 return -EINVAL;
202 static struct ir_raw_handler nec_handler = {
203 .protocols = RC_TYPE_NEC,
204 .decode = ir_nec_decode,
207 static int __init ir_nec_decode_init(void)
209 ir_raw_handler_register(&nec_handler);
211 printk(KERN_INFO "IR NEC protocol handler initialized\n");
212 return 0;
215 static void __exit ir_nec_decode_exit(void)
217 ir_raw_handler_unregister(&nec_handler);
220 module_init(ir_nec_decode_init);
221 module_exit(ir_nec_decode_exit);
223 MODULE_LICENSE("GPL");
224 MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
225 MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)");
226 MODULE_DESCRIPTION("NEC IR protocol decoder");