2db22948a310719c9fe31e957ac6bd1a28880de1
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / media / IR / ir-functions.c
1 /*
2 *
3 * some common structs and functions to handle infrared remotes via
4 * input layer ...
5 *
6 * (c) 2003 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22
23 #include <linux/module.h>
24 #include <linux/string.h>
25 #include <linux/jiffies.h>
26 #include <media/ir-common.h>
27
28 /* -------------------------------------------------------------------------- */
29
30 MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
31 MODULE_LICENSE("GPL");
32
33 static int repeat = 1;
34 module_param(repeat, int, 0444);
35 MODULE_PARM_DESC(repeat,"auto-repeat for IR keys (default: on)");
36
37 /* -------------------------------------------------------------------------- */
38
39 static void ir_input_key_event(struct input_dev *dev, struct ir_input_state *ir)
40 {
41 if (KEY_RESERVED == ir->keycode) {
42 printk(KERN_INFO "%s: unknown key: key=0x%02x down=%d\n",
43 dev->name, ir->ir_key, ir->keypressed);
44 return;
45 }
46 IR_dprintk(1,"%s: key event code=%d down=%d\n",
47 dev->name,ir->keycode,ir->keypressed);
48 input_report_key(dev,ir->keycode,ir->keypressed);
49 input_sync(dev);
50 }
51
52 /* -------------------------------------------------------------------------- */
53
54 int ir_input_init(struct input_dev *dev, struct ir_input_state *ir,
55 int ir_type, struct ir_scancode_table *ir_codes)
56 {
57 ir->ir_type = ir_type;
58
59 ir->keytable.size = ir_roundup_tablesize(ir_codes->size);
60 ir->keytable.scan = kzalloc(ir->keytable.size *
61 sizeof(struct ir_scancode), GFP_KERNEL);
62 if (!ir->keytable.scan)
63 return -ENOMEM;
64
65 IR_dprintk(1, "Allocated space for %d keycode entries (%zd bytes)\n",
66 ir->keytable.size,
67 ir->keytable.size * sizeof(ir->keytable.scan));
68
69 ir_copy_table(&ir->keytable, ir_codes);
70 ir_set_keycode_table(dev, &ir->keytable);
71
72 clear_bit(0, dev->keybit);
73 set_bit(EV_KEY, dev->evbit);
74 if (repeat)
75 set_bit(EV_REP, dev->evbit);
76
77 return 0;
78 }
79 EXPORT_SYMBOL_GPL(ir_input_init);
80
81
82 void ir_input_nokey(struct input_dev *dev, struct ir_input_state *ir)
83 {
84 if (ir->keypressed) {
85 ir->keypressed = 0;
86 ir_input_key_event(dev,ir);
87 }
88 }
89 EXPORT_SYMBOL_GPL(ir_input_nokey);
90
91 void ir_input_keydown(struct input_dev *dev, struct ir_input_state *ir,
92 u32 ir_key)
93 {
94 u32 keycode = ir_g_keycode_from_table(dev, ir_key);
95
96 if (ir->keypressed && ir->keycode != keycode) {
97 ir->keypressed = 0;
98 ir_input_key_event(dev,ir);
99 }
100 if (!ir->keypressed) {
101 ir->ir_key = ir_key;
102 ir->keycode = keycode;
103 ir->keypressed = 1;
104 ir_input_key_event(dev,ir);
105 }
106 }
107 EXPORT_SYMBOL_GPL(ir_input_keydown);
108
109 /* -------------------------------------------------------------------------- */
110 /* extract mask bits out of data and pack them into the result */
111 u32 ir_extract_bits(u32 data, u32 mask)
112 {
113 u32 vbit = 1, value = 0;
114
115 do {
116 if (mask&1) {
117 if (data&1)
118 value |= vbit;
119 vbit<<=1;
120 }
121 data>>=1;
122 } while (mask>>=1);
123
124 return value;
125 }
126 EXPORT_SYMBOL_GPL(ir_extract_bits);
127
128 static int inline getbit(u32 *samples, int bit)
129 {
130 return (samples[bit/32] & (1 << (31-(bit%32)))) ? 1 : 0;
131 }
132
133 /* sump raw samples for visual debugging ;) */
134 int ir_dump_samples(u32 *samples, int count)
135 {
136 int i, bit, start;
137
138 printk(KERN_DEBUG "ir samples: ");
139 start = 0;
140 for (i = 0; i < count * 32; i++) {
141 bit = getbit(samples,i);
142 if (bit)
143 start = 1;
144 if (0 == start)
145 continue;
146 printk("%s", bit ? "#" : "_");
147 }
148 printk("\n");
149 return 0;
150 }
151 EXPORT_SYMBOL_GPL(ir_dump_samples);
152
153 /* decode raw samples, pulse distance coding used by NEC remotes */
154 int ir_decode_pulsedistance(u32 *samples, int count, int low, int high)
155 {
156 int i,last,bit,len;
157 u32 curBit;
158 u32 value;
159
160 /* find start burst */
161 for (i = len = 0; i < count * 32; i++) {
162 bit = getbit(samples,i);
163 if (bit) {
164 len++;
165 } else {
166 if (len >= 29)
167 break;
168 len = 0;
169 }
170 }
171
172 /* start burst to short */
173 if (len < 29)
174 return 0xffffffff;
175
176 /* find start silence */
177 for (len = 0; i < count * 32; i++) {
178 bit = getbit(samples,i);
179 if (bit) {
180 break;
181 } else {
182 len++;
183 }
184 }
185
186 /* silence to short */
187 if (len < 7)
188 return 0xffffffff;
189
190 /* go decoding */
191 len = 0;
192 last = 1;
193 value = 0; curBit = 1;
194 for (; i < count * 32; i++) {
195 bit = getbit(samples,i);
196 if (last) {
197 if(bit) {
198 continue;
199 } else {
200 len = 1;
201 }
202 } else {
203 if (bit) {
204 if (len > (low + high) /2)
205 value |= curBit;
206 curBit <<= 1;
207 if (curBit == 1)
208 break;
209 } else {
210 len++;
211 }
212 }
213 last = bit;
214 }
215
216 return value;
217 }
218 EXPORT_SYMBOL_GPL(ir_decode_pulsedistance);
219
220 /* decode raw samples, biphase coding, used by rc5 for example */
221 int ir_decode_biphase(u32 *samples, int count, int low, int high)
222 {
223 int i,last,bit,len,flips;
224 u32 value;
225
226 /* find start bit (1) */
227 for (i = 0; i < 32; i++) {
228 bit = getbit(samples,i);
229 if (bit)
230 break;
231 }
232
233 /* go decoding */
234 len = 0;
235 flips = 0;
236 value = 1;
237 for (; i < count * 32; i++) {
238 if (len > high)
239 break;
240 if (flips > 1)
241 break;
242 last = bit;
243 bit = getbit(samples,i);
244 if (last == bit) {
245 len++;
246 continue;
247 }
248 if (len < low) {
249 len++;
250 flips++;
251 continue;
252 }
253 value <<= 1;
254 value |= bit;
255 flips = 0;
256 len = 1;
257 }
258 return value;
259 }
260 EXPORT_SYMBOL_GPL(ir_decode_biphase);
261
262 /* RC5 decoding stuff, moved from bttv-input.c to share it with
263 * saa7134 */
264
265 /* decode raw bit pattern to RC5 code */
266 u32 ir_rc5_decode(unsigned int code)
267 {
268 unsigned int org_code = code;
269 unsigned int pair;
270 unsigned int rc5 = 0;
271 int i;
272
273 for (i = 0; i < 14; ++i) {
274 pair = code & 0x3;
275 code >>= 2;
276
277 rc5 <<= 1;
278 switch (pair) {
279 case 0:
280 case 2:
281 break;
282 case 1:
283 rc5 |= 1;
284 break;
285 case 3:
286 IR_dprintk(1, "ir-common: ir_rc5_decode(%x) bad code\n", org_code);
287 return 0;
288 }
289 }
290 IR_dprintk(1, "ir-common: code=%x, rc5=%x, start=%x, toggle=%x, address=%x, "
291 "instr=%x\n", rc5, org_code, RC5_START(rc5),
292 RC5_TOGGLE(rc5), RC5_ADDR(rc5), RC5_INSTR(rc5));
293 return rc5;
294 }
295 EXPORT_SYMBOL_GPL(ir_rc5_decode);
296
297 void ir_rc5_timer_end(unsigned long data)
298 {
299 struct card_ir *ir = (struct card_ir *)data;
300 struct timeval tv;
301 unsigned long current_jiffies, timeout;
302 u32 gap;
303 u32 rc5 = 0;
304
305 /* get time */
306 current_jiffies = jiffies;
307 do_gettimeofday(&tv);
308
309 /* avoid overflow with gap >1s */
310 if (tv.tv_sec - ir->base_time.tv_sec > 1) {
311 gap = 200000;
312 } else {
313 gap = 1000000 * (tv.tv_sec - ir->base_time.tv_sec) +
314 tv.tv_usec - ir->base_time.tv_usec;
315 }
316
317 /* signal we're ready to start a new code */
318 ir->active = 0;
319
320 /* Allow some timer jitter (RC5 is ~24ms anyway so this is ok) */
321 if (gap < 28000) {
322 IR_dprintk(1, "ir-common: spurious timer_end\n");
323 return;
324 }
325
326 if (ir->last_bit < 20) {
327 /* ignore spurious codes (caused by light/other remotes) */
328 IR_dprintk(1, "ir-common: short code: %x\n", ir->code);
329 } else {
330 ir->code = (ir->code << ir->shift_by) | 1;
331 rc5 = ir_rc5_decode(ir->code);
332
333 /* two start bits? */
334 if (RC5_START(rc5) != ir->start) {
335 IR_dprintk(1, "ir-common: rc5 start bits invalid: %u\n", RC5_START(rc5));
336
337 /* right address? */
338 } else if (RC5_ADDR(rc5) == ir->addr) {
339 u32 toggle = RC5_TOGGLE(rc5);
340 u32 instr = RC5_INSTR(rc5);
341
342 /* Good code, decide if repeat/repress */
343 if (toggle != RC5_TOGGLE(ir->last_rc5) ||
344 instr != RC5_INSTR(ir->last_rc5)) {
345 IR_dprintk(1, "ir-common: instruction %x, toggle %x\n", instr,
346 toggle);
347 ir_input_nokey(ir->dev, &ir->ir);
348 ir_input_keydown(ir->dev, &ir->ir, instr);
349 }
350
351 /* Set/reset key-up timer */
352 timeout = current_jiffies +
353 msecs_to_jiffies(ir->rc5_key_timeout);
354 mod_timer(&ir->timer_keyup, timeout);
355
356 /* Save code for repeat test */
357 ir->last_rc5 = rc5;
358 }
359 }
360 }
361 EXPORT_SYMBOL_GPL(ir_rc5_timer_end);
362
363 void ir_rc5_timer_keyup(unsigned long data)
364 {
365 struct card_ir *ir = (struct card_ir *)data;
366
367 IR_dprintk(1, "ir-common: key released\n");
368 ir_input_nokey(ir->dev, &ir->ir);
369 }
370 EXPORT_SYMBOL_GPL(ir_rc5_timer_keyup);