ir-functions.c 8.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*
 *
 * some common structs and functions to handle infrared remotes via
 * input layer ...
 *
 * (c) 2003 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <linux/module.h>
#include <linux/string.h>
25
#include <linux/jiffies.h>
26 27 28 29 30 31 32 33 34 35 36
#include <media/ir-common.h>

/* -------------------------------------------------------------------------- */

MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
MODULE_LICENSE("GPL");

static int repeat = 1;
module_param(repeat, int, 0444);
MODULE_PARM_DESC(repeat,"auto-repeat for IR keys (default: on)");

37 38
int media_ir_debug;    /* media_ir_debug level (0,1,2) */
module_param_named(debug, media_ir_debug, int, 0644);
39 40 41 42 43 44

/* -------------------------------------------------------------------------- */

static void ir_input_key_event(struct input_dev *dev, struct ir_input_state *ir)
{
	if (KEY_RESERVED == ir->keycode) {
45 46
		printk(KERN_INFO "%s: unknown key: key=0x%02x down=%d\n",
		       dev->name, ir->ir_key, ir->keypressed);
47 48
		return;
	}
49
	IR_dprintk(1,"%s: key event code=%d down=%d\n",
50 51 52 53 54 55 56
		dev->name,ir->keycode,ir->keypressed);
	input_report_key(dev,ir->keycode,ir->keypressed);
	input_sync(dev);
}

/* -------------------------------------------------------------------------- */

57
int ir_input_init(struct input_dev *dev, struct ir_input_state *ir,
58
		   int ir_type, struct ir_scancode_table *ir_codes)
59 60
{
	ir->ir_type = ir_type;
61

62 63 64 65 66
	ir->keytable.size = ir_roundup_tablesize(ir_codes->size);
	ir->keytable.scan = kzalloc(ir->keytable.size *
				    sizeof(struct ir_scancode), GFP_KERNEL);
	if (!ir->keytable.scan)
		return -ENOMEM;
67

68 69 70
	IR_dprintk(1, "Allocated space for %d keycode entries (%zd bytes)\n",
		ir->keytable.size,
		ir->keytable.size * sizeof(ir->keytable.scan));
71

72 73
	ir_copy_table(&ir->keytable, ir_codes);
	ir_set_keycode_table(dev, &ir->keytable);
74

75
	clear_bit(0, dev->keybit);
76 77 78
	set_bit(EV_KEY, dev->evbit);
	if (repeat)
		set_bit(EV_REP, dev->evbit);
79 80

	return 0;
81
}
82
EXPORT_SYMBOL_GPL(ir_input_init);
83

84

85 86 87 88 89 90 91
void ir_input_nokey(struct input_dev *dev, struct ir_input_state *ir)
{
	if (ir->keypressed) {
		ir->keypressed = 0;
		ir_input_key_event(dev,ir);
	}
}
92
EXPORT_SYMBOL_GPL(ir_input_nokey);
93 94

void ir_input_keydown(struct input_dev *dev, struct ir_input_state *ir,
95
		      u32 ir_key)
96
{
97
	u32 keycode = ir_g_keycode_from_table(dev, ir_key);
98 99 100 101 102 103 104 105 106 107 108 109

	if (ir->keypressed && ir->keycode != keycode) {
		ir->keypressed = 0;
		ir_input_key_event(dev,ir);
	}
	if (!ir->keypressed) {
		ir->ir_key  = ir_key;
		ir->keycode = keycode;
		ir->keypressed = 1;
		ir_input_key_event(dev,ir);
	}
}
110
EXPORT_SYMBOL_GPL(ir_input_keydown);
111 112

/* -------------------------------------------------------------------------- */
113
/* extract mask bits out of data and pack them into the result */
114 115
u32 ir_extract_bits(u32 data, u32 mask)
{
116 117 118 119 120 121 122 123 124 125 126
	u32 vbit = 1, value = 0;

	do {
	    if (mask&1) {
		if (data&1)
			value |= vbit;
		vbit<<=1;
	    }
	    data>>=1;
	} while (mask>>=1);

127 128
	return value;
}
129
EXPORT_SYMBOL_GPL(ir_extract_bits);
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153

static int inline getbit(u32 *samples, int bit)
{
	return (samples[bit/32] & (1 << (31-(bit%32)))) ? 1 : 0;
}

/* sump raw samples for visual debugging ;) */
int ir_dump_samples(u32 *samples, int count)
{
	int i, bit, start;

	printk(KERN_DEBUG "ir samples: ");
	start = 0;
	for (i = 0; i < count * 32; i++) {
		bit = getbit(samples,i);
		if (bit)
			start = 1;
		if (0 == start)
			continue;
		printk("%s", bit ? "#" : "_");
	}
	printk("\n");
	return 0;
}
154
EXPORT_SYMBOL_GPL(ir_dump_samples);
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220

/* decode raw samples, pulse distance coding used by NEC remotes */
int ir_decode_pulsedistance(u32 *samples, int count, int low, int high)
{
	int i,last,bit,len;
	u32 curBit;
	u32 value;

	/* find start burst */
	for (i = len = 0; i < count * 32; i++) {
		bit = getbit(samples,i);
		if (bit) {
			len++;
		} else {
			if (len >= 29)
				break;
			len = 0;
		}
	}

	/* start burst to short */
	if (len < 29)
		return 0xffffffff;

	/* find start silence */
	for (len = 0; i < count * 32; i++) {
		bit = getbit(samples,i);
		if (bit) {
			break;
		} else {
			len++;
		}
	}

	/* silence to short */
	if (len < 7)
		return 0xffffffff;

	/* go decoding */
	len   = 0;
	last = 1;
	value = 0; curBit = 1;
	for (; i < count * 32; i++) {
		bit  = getbit(samples,i);
		if (last) {
			if(bit) {
				continue;
			} else {
				len = 1;
			}
		} else {
			if (bit) {
				if (len > (low + high) /2)
					value |= curBit;
				curBit <<= 1;
				if (curBit == 1)
					break;
			} else {
				len++;
			}
		}
		last = bit;
	}

	return value;
}
221
EXPORT_SYMBOL_GPL(ir_decode_pulsedistance);
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262

/* decode raw samples, biphase coding, used by rc5 for example */
int ir_decode_biphase(u32 *samples, int count, int low, int high)
{
	int i,last,bit,len,flips;
	u32 value;

	/* find start bit (1) */
	for (i = 0; i < 32; i++) {
		bit = getbit(samples,i);
		if (bit)
			break;
	}

	/* go decoding */
	len   = 0;
	flips = 0;
	value = 1;
	for (; i < count * 32; i++) {
		if (len > high)
			break;
		if (flips > 1)
			break;
		last = bit;
		bit  = getbit(samples,i);
		if (last == bit) {
			len++;
			continue;
		}
		if (len < low) {
			len++;
			flips++;
			continue;
		}
		value <<= 1;
		value |= bit;
		flips = 0;
		len   = 1;
	}
	return value;
}
263
EXPORT_SYMBOL_GPL(ir_decode_biphase);
264

265 266 267 268
/* RC5 decoding stuff, moved from bttv-input.c to share it with
 * saa7134 */

/* decode raw bit pattern to RC5 code */
269
u32 ir_rc5_decode(unsigned int code)
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
{
	unsigned int org_code = code;
	unsigned int pair;
	unsigned int rc5 = 0;
	int i;

	for (i = 0; i < 14; ++i) {
		pair = code & 0x3;
		code >>= 2;

		rc5 <<= 1;
		switch (pair) {
		case 0:
		case 2:
			break;
		case 1:
			rc5 |= 1;
			break;
		case 3:
289
			IR_dprintk(1, "ir-common: ir_rc5_decode(%x) bad code\n", org_code);
290 291 292
			return 0;
		}
	}
293
	IR_dprintk(1, "ir-common: code=%x, rc5=%x, start=%x, toggle=%x, address=%x, "
294 295 296 297
		"instr=%x\n", rc5, org_code, RC5_START(rc5),
		RC5_TOGGLE(rc5), RC5_ADDR(rc5), RC5_INSTR(rc5));
	return rc5;
}
298
EXPORT_SYMBOL_GPL(ir_rc5_decode);
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319

void ir_rc5_timer_end(unsigned long data)
{
	struct card_ir *ir = (struct card_ir *)data;
	struct timeval tv;
	unsigned long current_jiffies, timeout;
	u32 gap;
	u32 rc5 = 0;

	/* get time */
	current_jiffies = jiffies;
	do_gettimeofday(&tv);

	/* avoid overflow with gap >1s */
	if (tv.tv_sec - ir->base_time.tv_sec > 1) {
		gap = 200000;
	} else {
		gap = 1000000 * (tv.tv_sec - ir->base_time.tv_sec) +
		    tv.tv_usec - ir->base_time.tv_usec;
	}

320 321 322 323
	/* signal we're ready to start a new code */
	ir->active = 0;

	/* Allow some timer jitter (RC5 is ~24ms anyway so this is ok) */
324
	if (gap < 28000) {
325
		IR_dprintk(1, "ir-common: spurious timer_end\n");
326 327 328 329 330
		return;
	}

	if (ir->last_bit < 20) {
		/* ignore spurious codes (caused by light/other remotes) */
331
		IR_dprintk(1, "ir-common: short code: %x\n", ir->code);
332 333 334 335 336 337
	} else {
		ir->code = (ir->code << ir->shift_by) | 1;
		rc5 = ir_rc5_decode(ir->code);

		/* two start bits? */
		if (RC5_START(rc5) != ir->start) {
338
			IR_dprintk(1, "ir-common: rc5 start bits invalid: %u\n", RC5_START(rc5));
339 340 341 342 343 344 345 346 347

			/* right address? */
		} else if (RC5_ADDR(rc5) == ir->addr) {
			u32 toggle = RC5_TOGGLE(rc5);
			u32 instr = RC5_INSTR(rc5);

			/* Good code, decide if repeat/repress */
			if (toggle != RC5_TOGGLE(ir->last_rc5) ||
			    instr != RC5_INSTR(ir->last_rc5)) {
348
				IR_dprintk(1, "ir-common: instruction %x, toggle %x\n", instr,
349 350
					toggle);
				ir_input_nokey(ir->dev, &ir->ir);
351
				ir_input_keydown(ir->dev, &ir->ir, instr);
352 353 354
			}

			/* Set/reset key-up timer */
355 356
			timeout = current_jiffies +
				  msecs_to_jiffies(ir->rc5_key_timeout);
357 358 359 360 361 362 363
			mod_timer(&ir->timer_keyup, timeout);

			/* Save code for repeat test */
			ir->last_rc5 = rc5;
		}
	}
}
364
EXPORT_SYMBOL_GPL(ir_rc5_timer_end);
365 366 367 368 369

void ir_rc5_timer_keyup(unsigned long data)
{
	struct card_ir *ir = (struct card_ir *)data;

370
	IR_dprintk(1, "ir-common: key released\n");
371 372 373
	ir_input_nokey(ir->dev, &ir->ir);
}
EXPORT_SYMBOL_GPL(ir_rc5_timer_keyup);