Commit | Line | Data |
---|---|---|
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> |
f62de675 | 16 | #include "rc-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) | |
e31f4127 | 23 | #define NEC_REPEAT_SPACE (4 * NEC_UNIT) |
e40b1127 DH |
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 */ | |
86ff071c | 29 | #define NECX_REPEAT_BITS 1 |
9f154782 | 30 | |
2f16f631 MCC |
31 | enum nec_state { |
32 | STATE_INACTIVE, | |
2f16f631 | 33 | STATE_HEADER_SPACE, |
724e2495 DH |
34 | STATE_BIT_PULSE, |
35 | STATE_BIT_SPACE, | |
36 | STATE_TRAILER_PULSE, | |
2f16f631 MCC |
37 | STATE_TRAILER_SPACE, |
38 | }; | |
39 | ||
2f16f631 | 40 | /** |
587835a4 | 41 | * ir_nec_decode() - Decode one NEC pulse or space |
d8b4b582 | 42 | * @dev: the struct rc_dev descriptor of the device |
e40b1127 | 43 | * @duration: the struct ir_raw_event descriptor of the pulse/space |
2f16f631 MCC |
44 | * |
45 | * This function returns -EINVAL if the pulse violates the state machine | |
9f154782 | 46 | */ |
d8b4b582 | 47 | static int ir_nec_decode(struct rc_dev *dev, struct ir_raw_event ev) |
9f154782 | 48 | { |
d8b4b582 | 49 | struct nec_dec *data = &dev->raw->nec; |
724e2495 DH |
50 | u32 scancode; |
51 | u8 address, not_address, command, not_command; | |
9f154782 | 52 | |
52b66144 | 53 | if (!(dev->raw->enabled_protocols & RC_TYPE_NEC)) |
7f20d32d MCC |
54 | return 0; |
55 | ||
4651918a ML |
56 | if (!is_timing_event(ev)) { |
57 | if (ev.reset) | |
58 | data->state = STATE_INACTIVE; | |
724e2495 DH |
59 | return 0; |
60 | } | |
a3572c34 | 61 | |
e40b1127 DH |
62 | IR_dprintk(2, "NEC decode started at state %d (%uus %s)\n", |
63 | data->state, TO_US(ev.duration), TO_STR(ev.pulse)); | |
9f154782 | 64 | |
2f16f631 | 65 | switch (data->state) { |
a3572c34 | 66 | |
724e2495 | 67 | case STATE_INACTIVE: |
e40b1127 DH |
68 | if (!ev.pulse) |
69 | break; | |
70 | ||
86ff071c ML |
71 | if (eq_margin(ev.duration, NEC_HEADER_PULSE, NEC_UNIT / 2)) { |
72 | data->is_nec_x = false; | |
73 | data->necx_repeat = false; | |
74 | } else if (eq_margin(ev.duration, NECX_HEADER_PULSE, NEC_UNIT / 2)) | |
75 | data->is_nec_x = true; | |
76 | else | |
e40b1127 DH |
77 | break; |
78 | ||
79 | data->count = 0; | |
80 | data->state = STATE_HEADER_SPACE; | |
2f16f631 | 81 | return 0; |
724e2495 | 82 | |
2f16f631 | 83 | case STATE_HEADER_SPACE: |
e40b1127 DH |
84 | if (ev.pulse) |
85 | break; | |
86 | ||
87 | if (eq_margin(ev.duration, NEC_HEADER_SPACE, NEC_UNIT / 2)) { | |
724e2495 | 88 | data->state = STATE_BIT_PULSE; |
9f154782 | 89 | return 0; |
e40b1127 | 90 | } else if (eq_margin(ev.duration, NEC_REPEAT_SPACE, NEC_UNIT / 2)) { |
21d33014 MB |
91 | if (!dev->keypressed) { |
92 | IR_dprintk(1, "Discarding last key repeat: event after key up\n"); | |
93 | } else { | |
94 | rc_repeat(dev); | |
95 | IR_dprintk(1, "Repeat last key\n"); | |
96 | data->state = STATE_TRAILER_PULSE; | |
97 | } | |
2f16f631 MCC |
98 | return 0; |
99 | } | |
e40b1127 | 100 | |
724e2495 DH |
101 | break; |
102 | ||
103 | case STATE_BIT_PULSE: | |
e40b1127 DH |
104 | if (!ev.pulse) |
105 | break; | |
106 | ||
107 | if (!eq_margin(ev.duration, NEC_BIT_PULSE, NEC_UNIT / 2)) | |
108 | break; | |
109 | ||
110 | data->state = STATE_BIT_SPACE; | |
111 | return 0; | |
724e2495 DH |
112 | |
113 | case STATE_BIT_SPACE: | |
e40b1127 | 114 | if (ev.pulse) |
724e2495 DH |
115 | break; |
116 | ||
86ff071c ML |
117 | if (data->necx_repeat && data->count == NECX_REPEAT_BITS && |
118 | geq_margin(ev.duration, | |
119 | NEC_TRAILER_SPACE, NEC_UNIT / 2)) { | |
120 | IR_dprintk(1, "Repeat last key\n"); | |
ca86674b | 121 | rc_repeat(dev); |
86ff071c ML |
122 | data->state = STATE_INACTIVE; |
123 | return 0; | |
124 | ||
125 | } else if (data->count > NECX_REPEAT_BITS) | |
126 | data->necx_repeat = false; | |
127 | ||
c216369e | 128 | data->bits <<= 1; |
e40b1127 | 129 | if (eq_margin(ev.duration, NEC_BIT_1_SPACE, NEC_UNIT / 2)) |
c216369e | 130 | data->bits |= 1; |
e40b1127 DH |
131 | else if (!eq_margin(ev.duration, NEC_BIT_0_SPACE, NEC_UNIT / 2)) |
132 | break; | |
724e2495 DH |
133 | data->count++; |
134 | ||
e40b1127 DH |
135 | if (data->count == NEC_NBITS) |
136 | data->state = STATE_TRAILER_PULSE; | |
137 | else | |
724e2495 | 138 | data->state = STATE_BIT_PULSE; |
e40b1127 DH |
139 | |
140 | return 0; | |
141 | ||
142 | case STATE_TRAILER_PULSE: | |
143 | if (!ev.pulse) | |
144 | break; | |
145 | ||
146 | if (!eq_margin(ev.duration, NEC_TRAILER_PULSE, NEC_UNIT / 2)) | |
147 | break; | |
148 | ||
149 | data->state = STATE_TRAILER_SPACE; | |
150 | return 0; | |
151 | ||
152 | case STATE_TRAILER_SPACE: | |
153 | if (ev.pulse) | |
154 | break; | |
155 | ||
156 | if (!geq_margin(ev.duration, NEC_TRAILER_SPACE, NEC_UNIT / 2)) | |
157 | break; | |
724e2495 | 158 | |
c216369e DH |
159 | address = bitrev8((data->bits >> 24) & 0xff); |
160 | not_address = bitrev8((data->bits >> 16) & 0xff); | |
161 | command = bitrev8((data->bits >> 8) & 0xff); | |
162 | not_command = bitrev8((data->bits >> 0) & 0xff); | |
724e2495 DH |
163 | |
164 | if ((command ^ not_command) != 0xff) { | |
165 | IR_dprintk(1, "NEC checksum error: received 0x%08x\n", | |
c216369e | 166 | data->bits); |
724e2495 | 167 | break; |
2f16f631 | 168 | } |
a3572c34 | 169 | |
724e2495 DH |
170 | if ((address ^ not_address) != 0xff) { |
171 | /* Extended NEC */ | |
172 | scancode = address << 16 | | |
173 | not_address << 8 | | |
174 | command; | |
175 | IR_dprintk(1, "NEC (Ext) scancode 0x%06x\n", scancode); | |
176 | } else { | |
e40b1127 | 177 | /* Normal NEC */ |
724e2495 DH |
178 | scancode = address << 8 | command; |
179 | IR_dprintk(1, "NEC scancode 0x%04x\n", scancode); | |
a3572c34 | 180 | } |
724e2495 | 181 | |
86ff071c ML |
182 | if (data->is_nec_x) |
183 | data->necx_repeat = true; | |
184 | ||
ca86674b | 185 | rc_keydown(dev, scancode, 0); |
e40b1127 | 186 | data->state = STATE_INACTIVE; |
2f16f631 | 187 | return 0; |
724e2495 | 188 | } |
a3572c34 | 189 | |
e40b1127 DH |
190 | IR_dprintk(1, "NEC decode failed at state %d (%uus %s)\n", |
191 | data->state, TO_US(ev.duration), TO_STR(ev.pulse)); | |
2f16f631 | 192 | data->state = STATE_INACTIVE; |
a3572c34 MCC |
193 | return -EINVAL; |
194 | } | |
ada39630 | 195 | |
995187be | 196 | static struct ir_raw_handler nec_handler = { |
52b66144 | 197 | .protocols = RC_TYPE_NEC, |
20d5f116 | 198 | .decode = ir_nec_decode, |
995187be MCC |
199 | }; |
200 | ||
201 | static int __init ir_nec_decode_init(void) | |
202 | { | |
203 | ir_raw_handler_register(&nec_handler); | |
204 | ||
205 | printk(KERN_INFO "IR NEC protocol handler initialized\n"); | |
206 | return 0; | |
207 | } | |
208 | ||
209 | static void __exit ir_nec_decode_exit(void) | |
210 | { | |
211 | ir_raw_handler_unregister(&nec_handler); | |
212 | } | |
213 | ||
214 | module_init(ir_nec_decode_init); | |
215 | module_exit(ir_nec_decode_exit); | |
216 | ||
217 | MODULE_LICENSE("GPL"); | |
218 | MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>"); | |
219 | MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)"); | |
220 | MODULE_DESCRIPTION("NEC IR protocol decoder"); |