1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19#include <linux/bitrev.h>
20#include <linux/module.h>
21#include "rc-core-priv.h"
22
23#define XMP_UNIT 136000
24#define XMP_LEADER 210000
25#define XMP_NIBBLE_PREFIX 760000
26#define XMP_HALFFRAME_SPACE 13800000
27#define XMP_TRAILER_SPACE 20000000
28
29enum xmp_state {
30 STATE_INACTIVE,
31 STATE_LEADER_PULSE,
32 STATE_NIBBLE_SPACE,
33};
34
35
36
37
38
39
40
41
42static int ir_xmp_decode(struct rc_dev *dev, struct ir_raw_event ev)
43{
44 struct xmp_dec *data = &dev->raw->xmp;
45
46 if (!is_timing_event(ev)) {
47 if (ev.reset)
48 data->state = STATE_INACTIVE;
49 return 0;
50 }
51
52 dev_dbg(&dev->dev, "XMP decode started at state %d %d (%uus %s)\n",
53 data->state, data->count, TO_US(ev.duration), TO_STR(ev.pulse));
54
55 switch (data->state) {
56
57 case STATE_INACTIVE:
58 if (!ev.pulse)
59 break;
60
61 if (eq_margin(ev.duration, XMP_LEADER, XMP_UNIT / 2)) {
62 data->count = 0;
63 data->state = STATE_NIBBLE_SPACE;
64 }
65
66 return 0;
67
68 case STATE_LEADER_PULSE:
69 if (!ev.pulse)
70 break;
71
72 if (eq_margin(ev.duration, XMP_LEADER, XMP_UNIT / 2))
73 data->state = STATE_NIBBLE_SPACE;
74
75 return 0;
76
77 case STATE_NIBBLE_SPACE:
78 if (ev.pulse)
79 break;
80
81 if (geq_margin(ev.duration, XMP_TRAILER_SPACE, XMP_NIBBLE_PREFIX)) {
82 int divider, i;
83 u8 addr, subaddr, subaddr2, toggle, oem, obc1, obc2, sum1, sum2;
84 u32 *n;
85 u32 scancode;
86
87 if (data->count != 16) {
88 dev_dbg(&dev->dev, "received TRAILER period at index %d: %u\n",
89 data->count, ev.duration);
90 data->state = STATE_INACTIVE;
91 return -EINVAL;
92 }
93
94 n = data->durations;
95
96
97
98
99
100 divider = (n[3] - XMP_NIBBLE_PREFIX) / 15 - 2000;
101 if (divider < 50) {
102 dev_dbg(&dev->dev, "divider to small %d.\n",
103 divider);
104 data->state = STATE_INACTIVE;
105 return -EINVAL;
106 }
107
108
109 for (i = 0; i < 16; i++)
110 n[i] = (n[i] - XMP_NIBBLE_PREFIX) / divider;
111 sum1 = (15 + n[0] + n[1] + n[2] + n[3] +
112 n[4] + n[5] + n[6] + n[7]) % 16;
113 sum2 = (15 + n[8] + n[9] + n[10] + n[11] +
114 n[12] + n[13] + n[14] + n[15]) % 16;
115
116 if (sum1 != 15 || sum2 != 15) {
117 dev_dbg(&dev->dev, "checksum errors sum1=0x%X sum2=0x%X\n",
118 sum1, sum2);
119 data->state = STATE_INACTIVE;
120 return -EINVAL;
121 }
122
123 subaddr = n[0] << 4 | n[2];
124 subaddr2 = n[8] << 4 | n[11];
125 oem = n[4] << 4 | n[5];
126 addr = n[6] << 4 | n[7];
127 toggle = n[10];
128 obc1 = n[12] << 4 | n[13];
129 obc2 = n[14] << 4 | n[15];
130 if (subaddr != subaddr2) {
131 dev_dbg(&dev->dev, "subaddress nibbles mismatch 0x%02X != 0x%02X\n",
132 subaddr, subaddr2);
133 data->state = STATE_INACTIVE;
134 return -EINVAL;
135 }
136 if (oem != 0x44)
137 dev_dbg(&dev->dev, "Warning: OEM nibbles 0x%02X. Expected 0x44\n",
138 oem);
139
140 scancode = addr << 24 | subaddr << 16 |
141 obc1 << 8 | obc2;
142 dev_dbg(&dev->dev, "XMP scancode 0x%06x\n", scancode);
143
144 if (toggle == 0) {
145 rc_keydown(dev, RC_PROTO_XMP, scancode, 0);
146 } else {
147 rc_repeat(dev);
148 dev_dbg(&dev->dev, "Repeat last key\n");
149 }
150 data->state = STATE_INACTIVE;
151
152 return 0;
153
154 } else if (geq_margin(ev.duration, XMP_HALFFRAME_SPACE, XMP_NIBBLE_PREFIX)) {
155
156 if (data->count == 16) {
157 dev_dbg(&dev->dev, "received half frame pulse at index %d. Probably a final frame key-up event: %u\n",
158 data->count, ev.duration);
159
160
161
162
163
164 data->count = 8;
165 }
166
167 else if (data->count != 8)
168 dev_dbg(&dev->dev, "received half frame pulse at index %d: %u\n",
169 data->count, ev.duration);
170 data->state = STATE_LEADER_PULSE;
171
172 return 0;
173
174 } else if (geq_margin(ev.duration, XMP_NIBBLE_PREFIX, XMP_UNIT)) {
175
176 if (data->count == 16) {
177 dev_dbg(&dev->dev, "to many pulses (%d) ignoring: %u\n",
178 data->count, ev.duration);
179 data->state = STATE_INACTIVE;
180 return -EINVAL;
181 }
182 data->durations[data->count] = ev.duration;
183 data->count++;
184 data->state = STATE_LEADER_PULSE;
185
186 return 0;
187
188 }
189
190 break;
191 }
192
193 dev_dbg(&dev->dev, "XMP decode failed at count %d state %d (%uus %s)\n",
194 data->count, data->state, TO_US(ev.duration), TO_STR(ev.pulse));
195 data->state = STATE_INACTIVE;
196 return -EINVAL;
197}
198
199static struct ir_raw_handler xmp_handler = {
200 .protocols = RC_PROTO_BIT_XMP,
201 .decode = ir_xmp_decode,
202 .min_timeout = XMP_TRAILER_SPACE,
203};
204
205static int __init ir_xmp_decode_init(void)
206{
207 ir_raw_handler_register(&xmp_handler);
208
209 printk(KERN_INFO "IR XMP protocol handler initialized\n");
210 return 0;
211}
212
213static void __exit ir_xmp_decode_exit(void)
214{
215 ir_raw_handler_unregister(&xmp_handler);
216}
217
218module_init(ir_xmp_decode_init);
219module_exit(ir_xmp_decode_exit);
220
221MODULE_LICENSE("GPL");
222MODULE_AUTHOR("Marcel Mol <marcel@mesa.nl>");
223MODULE_AUTHOR("MESA Consulting (http://www.mesa.nl)");
224MODULE_DESCRIPTION("XMP IR protocol decoder");
225