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/moduleparam.h>
25#include <linux/string.h>
26#include <linux/jiffies.h>
27#include <media/ir-common.h>
28
29/* -------------------------------------------------------------------------- */
30
31MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
32MODULE_LICENSE("GPL");
33
34static int repeat = 1;
35module_param(repeat, int, 0444);
36MODULE_PARM_DESC(repeat,"auto-repeat for IR keys (default: on)");
37
38static int debug = 0;    /* debug level (0,1,2) */
39module_param(debug, int, 0644);
40
41#define dprintk(level, fmt, arg...)	if (debug >= level) \
42	printk(KERN_DEBUG fmt , ## arg)
43
44/* -------------------------------------------------------------------------- */
45
46static void ir_input_key_event(struct input_dev *dev, struct ir_input_state *ir)
47{
48	if (KEY_RESERVED == ir->keycode) {
49		printk(KERN_INFO "%s: unknown key: key=0x%02x raw=0x%02x down=%d\n",
50		       dev->name,ir->ir_key,ir->ir_raw,ir->keypressed);
51		return;
52	}
53	dprintk(1,"%s: key event code=%d down=%d\n",
54		dev->name,ir->keycode,ir->keypressed);
55	input_report_key(dev,ir->keycode,ir->keypressed);
56	input_sync(dev);
57}
58
59/* -------------------------------------------------------------------------- */
60
61void ir_input_init(struct input_dev *dev, struct ir_input_state *ir,
62		   int ir_type, IR_KEYTAB_TYPE *ir_codes)
63{
64	int i;
65
66	ir->ir_type = ir_type;
67	if (ir_codes)
68		memcpy(ir->ir_codes, ir_codes, sizeof(ir->ir_codes));
69
70
71	dev->keycode     = ir->ir_codes;
72	dev->keycodesize = sizeof(IR_KEYTAB_TYPE);
73	dev->keycodemax  = IR_KEYTAB_SIZE;
74	for (i = 0; i < IR_KEYTAB_SIZE; i++)
75		set_bit(ir->ir_codes[i], dev->keybit);
76	clear_bit(0, dev->keybit);
77
78	set_bit(EV_KEY, dev->evbit);
79	if (repeat)
80		set_bit(EV_REP, dev->evbit);
81}
82
83void ir_input_nokey(struct input_dev *dev, struct ir_input_state *ir)
84{
85	if (ir->keypressed) {
86		ir->keypressed = 0;
87		ir_input_key_event(dev,ir);
88	}
89}
90
91void ir_input_keydown(struct input_dev *dev, struct ir_input_state *ir,
92		      u32 ir_key, u32 ir_raw)
93{
94	u32 keycode = IR_KEYCODE(ir->ir_codes, 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->ir_raw  = ir_raw;
103		ir->keycode = keycode;
104		ir->keypressed = 1;
105		ir_input_key_event(dev,ir);
106	}
107}
108
109/* -------------------------------------------------------------------------- */
110
111u32 ir_extract_bits(u32 data, u32 mask)
112{
113	int mbit, vbit;
114	u32 value;
115
116	value = 0;
117	vbit  = 0;
118	for (mbit = 0; mbit < 32; mbit++) {
119		if (!(mask & ((u32)1 << mbit)))
120			continue;
121		if (data & ((u32)1 << mbit))
122			value |= (1 << vbit);
123		vbit++;
124	}
125	return value;
126}
127
128static 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 ;) */
134int 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
152/* decode raw samples, pulse distance coding used by NEC remotes */
153int ir_decode_pulsedistance(u32 *samples, int count, int low, int high)
154{
155	int i,last,bit,len;
156	u32 curBit;
157	u32 value;
158
159	/* find start burst */
160	for (i = len = 0; i < count * 32; i++) {
161		bit = getbit(samples,i);
162		if (bit) {
163			len++;
164		} else {
165			if (len >= 29)
166				break;
167			len = 0;
168		}
169	}
170
171	/* start burst to short */
172	if (len < 29)
173		return 0xffffffff;
174
175	/* find start silence */
176	for (len = 0; i < count * 32; i++) {
177		bit = getbit(samples,i);
178		if (bit) {
179			break;
180		} else {
181			len++;
182		}
183	}
184
185	/* silence to short */
186	if (len < 7)
187		return 0xffffffff;
188
189	/* go decoding */
190	len   = 0;
191	last = 1;
192	value = 0; curBit = 1;
193	for (; i < count * 32; i++) {
194		bit  = getbit(samples,i);
195		if (last) {
196			if(bit) {
197				continue;
198			} else {
199				len = 1;
200			}
201		} else {
202			if (bit) {
203				if (len > (low + high) /2)
204					value |= curBit;
205				curBit <<= 1;
206				if (curBit == 1)
207					break;
208			} else {
209				len++;
210			}
211		}
212		last = bit;
213	}
214
215	return value;
216}
217
218/* decode raw samples, biphase coding, used by rc5 for example */
219int ir_decode_biphase(u32 *samples, int count, int low, int high)
220{
221	int i,last,bit,len,flips;
222	u32 value;
223
224	/* find start bit (1) */
225	for (i = 0; i < 32; i++) {
226		bit = getbit(samples,i);
227		if (bit)
228			break;
229	}
230
231	/* go decoding */
232	len   = 0;
233	flips = 0;
234	value = 1;
235	for (; i < count * 32; i++) {
236		if (len > high)
237			break;
238		if (flips > 1)
239			break;
240		last = bit;
241		bit  = getbit(samples,i);
242		if (last == bit) {
243			len++;
244			continue;
245		}
246		if (len < low) {
247			len++;
248			flips++;
249			continue;
250		}
251		value <<= 1;
252		value |= bit;
253		flips = 0;
254		len   = 1;
255	}
256	return value;
257}
258
259/* RC5 decoding stuff, moved from bttv-input.c to share it with
260 * saa7134 */
261
262/* decode raw bit pattern to RC5 code */
263u32 ir_rc5_decode(unsigned int code)
264{
265	unsigned int org_code = code;
266	unsigned int pair;
267	unsigned int rc5 = 0;
268	int i;
269
270	for (i = 0; i < 14; ++i) {
271		pair = code & 0x3;
272		code >>= 2;
273
274		rc5 <<= 1;
275		switch (pair) {
276		case 0:
277		case 2:
278			break;
279		case 1:
280			rc5 |= 1;
281			break;
282		case 3:
283			dprintk(1, "ir-common: ir_rc5_decode(%x) bad code\n", org_code);
284			return 0;
285		}
286	}
287	dprintk(1, "ir-common: code=%x, rc5=%x, start=%x, toggle=%x, address=%x, "
288		"instr=%x\n", rc5, org_code, RC5_START(rc5),
289		RC5_TOGGLE(rc5), RC5_ADDR(rc5), RC5_INSTR(rc5));
290	return rc5;
291}
292
293void ir_rc5_timer_end(unsigned long data)
294{
295	struct card_ir *ir = (struct card_ir *)data;
296	struct timeval tv;
297	unsigned long current_jiffies, timeout;
298	u32 gap;
299	u32 rc5 = 0;
300
301	/* get time */
302	current_jiffies = jiffies;
303	do_gettimeofday(&tv);
304
305	/* avoid overflow with gap >1s */
306	if (tv.tv_sec - ir->base_time.tv_sec > 1) {
307		gap = 200000;
308	} else {
309		gap = 1000000 * (tv.tv_sec - ir->base_time.tv_sec) +
310		    tv.tv_usec - ir->base_time.tv_usec;
311	}
312
313	/* signal we're ready to start a new code */
314	ir->active = 0;
315
316	/* Allow some timer jitter (RC5 is ~24ms anyway so this is ok) */
317	if (gap < 28000) {
318		dprintk(1, "ir-common: spurious timer_end\n");
319		return;
320	}
321
322	if (ir->last_bit < 20) {
323		/* ignore spurious codes (caused by light/other remotes) */
324		dprintk(1, "ir-common: short code: %x\n", ir->code);
325	} else {
326		ir->code = (ir->code << ir->shift_by) | 1;
327		rc5 = ir_rc5_decode(ir->code);
328
329		/* two start bits? */
330		if (RC5_START(rc5) != ir->start) {
331			dprintk(1, "ir-common: rc5 start bits invalid: %u\n", RC5_START(rc5));
332
333			/* right address? */
334		} else if (RC5_ADDR(rc5) == ir->addr) {
335			u32 toggle = RC5_TOGGLE(rc5);
336			u32 instr = RC5_INSTR(rc5);
337
338			/* Good code, decide if repeat/repress */
339			if (toggle != RC5_TOGGLE(ir->last_rc5) ||
340			    instr != RC5_INSTR(ir->last_rc5)) {
341				dprintk(1, "ir-common: instruction %x, toggle %x\n", instr,
342					toggle);
343				ir_input_nokey(ir->dev, &ir->ir);
344				ir_input_keydown(ir->dev, &ir->ir, instr,
345						 instr);
346			}
347
348			/* Set/reset key-up timer */
349			timeout = current_jiffies + (500 + ir->rc5_key_timeout
350						     * HZ) / 1000;
351			mod_timer(&ir->timer_keyup, timeout);
352
353			/* Save code for repeat test */
354			ir->last_rc5 = rc5;
355		}
356	}
357}
358
359void ir_rc5_timer_keyup(unsigned long data)
360{
361	struct card_ir *ir = (struct card_ir *)data;
362
363	dprintk(1, "ir-common: key released\n");
364	ir_input_nokey(ir->dev, &ir->ir);
365}
366
367EXPORT_SYMBOL_GPL(ir_input_init);
368EXPORT_SYMBOL_GPL(ir_input_nokey);
369EXPORT_SYMBOL_GPL(ir_input_keydown);
370
371EXPORT_SYMBOL_GPL(ir_extract_bits);
372EXPORT_SYMBOL_GPL(ir_dump_samples);
373EXPORT_SYMBOL_GPL(ir_decode_biphase);
374EXPORT_SYMBOL_GPL(ir_decode_pulsedistance);
375
376EXPORT_SYMBOL_GPL(ir_rc5_decode);
377EXPORT_SYMBOL_GPL(ir_rc5_timer_end);
378EXPORT_SYMBOL_GPL(ir_rc5_timer_keyup);
379
380/*
381 * Local variables:
382 * c-basic-offset: 8
383 * End:
384 */
385